Sat, 11 Apr 2015 21:02:54 +0300
- fixed a derp in commitsdb
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | import ssl |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | import socket |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | import errno |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | import select |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | import asyncore |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | import base64 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
7 | import re |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | import json |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | import urllib |
94 | 10 | import irc as Irc |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | from datetime import datetime, timedelta |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | from configfile import Config |
97 | 13 | import hgpoll as HgPoll |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | g_credentials = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | g_portnumber = None |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | g_throttle = [] |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
120
9880bb697149
- added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
19 | valid_repos = ['torr_samaho/zandronum', 'torr_samaho/zandronum-stable', |
9880bb697149
- added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
20 | 'crimsondusk/zandronum-sandbox', 'crimsondusk/zandronum-sandbox-stable', 'crimsondusk/zfc9000', 'blzut3/doomseeker'] |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | def is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | i = 0 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
25 | while i < len (g_throttle): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
26 | if g_throttle[i][1] <= datetime.utcnow(): |
94 | 27 | print 'Throttle of %s expired' % g_throttle[i][0] |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | item = g_throttle.pop (i) # expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | |
94 | 30 | if item[0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | return False # this address expired |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | |
94 | 35 | if g_throttle[i][0] == address: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | return True # is throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | i += 1 |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
40 | return False # not throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
41 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
42 | def throttle (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
43 | tt = datetime.utcnow() + timedelta (0, 30) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
44 | |
121
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
45 | if address in g_throttle: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | g_throttle[g_throttle.index (address)][1] = tt |
121
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
47 | else: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | g_throttle.append ([address, tt]) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | |
96 | 50 | Irc.broadcast ('Throttling %s for 30 seconds' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | def handle_rest_http (data, address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | global g_credentials |
94 | 54 | displayaddress = address |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | authrex = re.compile (r'^authorization: Basic (.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | payloadrex = re.compile (r'^payload=(.+)$') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | authenticated = False |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | payload = '' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | if not g_credentials: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | g_credentials = base64.b64encode (Config.get_node ('rest').get_value ('credentials')) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | # Authenticate and find the payload |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | for line in data: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | match = authrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | |
94 | 67 | if match and match.group (1) == g_credentials: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | authenticated = True |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | continue |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | match = payloadrex.match (line) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | if match: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | payload = match.group (1) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | if not authenticated: |
96 | 76 | Irc.broadcast ('%s failed to authenticate' % address) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | jsonstring = urllib.unquote_plus (payload).decode ('utf-8') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
81 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | jsondata = json.loads (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | repodata = jsondata['repository'] |
99 | 85 | repo_fullname = '%s/%s' % (repodata['owner'], repodata['name']) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | |
120
9880bb697149
- added doomseeker support, fixed up mod_bridge, now shortens new commit links with bit.ly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
87 | if repo_fullname.lower() not in valid_repos: |
99 | 88 | raise ValueError ('unknown repository %s' % repo_fullname) |
97 | 89 | |
90 | commit_data = [] | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | if 'commits' in jsondata: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | for commit in jsondata['commits']: |
97 | 94 | commit_data.append ([commit['node'], commit['message']]) |
95 | ||
96 | if len (commit_data) > 0: | |
99 | 97 | HgPoll.process_new_commits (repodata['name'], commit_data) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
98 | except Exception as e: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
99 | Irc.broadcast ('%s provided bad JSON: %s' % (address, e)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
100 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
101 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
102 | with open ('rejected_json.txt', 'w') as fp: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
103 | fp.write (jsonstring) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
104 | Irc.broadcast ('bad json written to rejected_json.txt') |
121
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
105 | except Exception as e: |
ac07779f788d
- reworked mercurial repository handling, removed hardcoded values
Teemu Piippo <crimsondusk64@gmail.com>
parents:
120
diff
changeset
|
106 | Irc.broadcast ('failed to log json because %s' % e) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
107 | pass |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
108 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
109 | throttle (address) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
110 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
111 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
112 | class RESTConnection (asyncore.dispatcher): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
113 | httpdata = '' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
114 | writebuffer = '' |
94 | 115 | address = None |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
116 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
117 | def __init__ (self, conn, address): |
96 | 118 | asyncore.dispatcher.__init__ (self, conn) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
119 | self.socket = ssl.wrap_socket (conn, server_side=True, keyfile='key.pem', |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
120 | certfile='cert.pem', do_handshake_on_connect=False) |
96 | 121 | self.socket.settimeout (3) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
122 | self.address = address |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
123 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
124 | while True: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
125 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
126 | self.socket.do_handshake() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
127 | break |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
128 | except ssl.SSLError, err: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
129 | if err.args[0] == ssl.SSL_ERROR_WANT_READ: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
130 | select.select([self.socket], [], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
131 | elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
132 | select.select([], [self.socket], []) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
133 | else: |
94 | 134 | Irc.broadcast ('%s:%s: SSL error: %s' % (self.address[0], err)) |
135 | throttle (self.address[0]) | |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
136 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
137 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
138 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
139 | def write (self, msg): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
140 | self.writebuffer += msg |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
141 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
142 | def readable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
143 | return True |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
144 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
145 | def writable (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
146 | return self.writebuffer |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
147 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
148 | def handle_close (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
149 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
150 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
151 | def handle_read (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
152 | while 1: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
153 | try: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
154 | data = self.recv (4096) |
94 | 155 | except: |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
156 | # EOF |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
157 | self.finish() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
158 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
159 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
160 | self.httpdata += data.replace ('\r', '') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
161 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
162 | def finish (self): |
94 | 163 | handle_rest_http (self.httpdata.split ('\n'), self.address[0]) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
164 | self.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
165 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
166 | def handle_write (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
167 | self.send (self.writebuffer) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
168 | self.writebuffer='' |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
169 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
170 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
171 | raise |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
172 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
173 | class RESTServer (asyncore.dispatcher): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
174 | def __init__ (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
175 | global g_portnumber |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
176 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
177 | if g_portnumber == None: |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
178 | g_portnumber = Config.get_node ('rest').get_value ('port') |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
179 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
180 | asyncore.dispatcher.__init__ (self) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
181 | self.create_socket (socket.AF_INET, socket.SOCK_STREAM) |
94 | 182 | self.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
183 | self.bind (('', g_portnumber)) |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
184 | self.listen (5) |
93 | 185 | print 'REST server initialized' |
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
186 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
187 | def handle_accept (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
188 | sock, address = self.accept() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
189 | |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
190 | if is_throttled (address): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
191 | # throttled |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
192 | sock.close() |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
193 | return |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
194 | |
94 | 195 | conn = RESTConnection (sock, address) |
196 | ||
92
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
197 | def handle_error (self): |
da291d9426ea
- first implementation of REST
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
198 | return |