30
37
class Terminate(Exception):
31
"""Exception thrown when server is to be shut down. It will attempt to sned
32
the final_response to the client and then exits"""
38
"""Exception thrown when server is to be shut down. It will attempt to
39
send the final_response to the client and then exits"""
33
40
def __init__(self, final_response=None):
34
41
self.final_response = final_response
37
44
return repr(self.final_response)
46
class ProtocolError(Exception):
47
"""Exception thrown when client violates the the chat protocol"""
40
50
def start_server(port, magic, daemon_mode, handler, initializer = None):
41
51
# Attempt to open the socket.
83
93
(conn, addr) = s.accept()
94
conn.settimeout(SOCKETTIMEOUT)
86
buf = cStringIO.StringIO()
91
blk = conn.recv(1024, socket.MSG_DONTWAIT)
93
# Exception thrown if it WOULD block (but we
94
# told it not to wait) - ie. we are done
97
env = cjson.decode(inp)
99
# Check that the message is
100
digest = hashlib.md5(env['content'] + magic).hexdigest()
101
if env['digest'] != digest:
96
# Grab the input and try to decode
97
inp = recv_netstring(conn)
99
content = decode(inp, magic)
100
except ProtocolError:
105
content = cjson.decode(env['content'])
107
104
response = handler(content)
109
conn.sendall(cjson.encode(response))
106
send_netstring(conn, json.dumps(response))
113
110
except Terminate, t:
114
111
# Try and send final response and then terminate
115
112
if t.final_response:
116
conn.sendall(cjson.encode(t.final_response))
113
send_netstring(conn, json.dumps(t.final_response))
119
116
except Exception:
126
123
"value": str(e_val),
127
124
"traceback": tb_dump.getvalue()
129
conn.sendall(cjson.encode(json_exc))
126
send_netstring(conn, json.dumps(json_exc))
133
130
def chat(host, port, msg, magic, decode = True):
134
131
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
135
132
sok.connect((host, port))
136
content = cjson.encode(msg)
137
digest = hashlib.md5(content + magic).hexdigest()
138
env = {'digest':digest,'content':content}
139
sok.send(cjson.encode(env))
141
buf = cStringIO.StringIO()
146
blk = sok.recv(1024, socket.MSG_DONTWAIT)
147
except socket.error, e:
150
# Exception thrown if it WOULD block (but we
151
# told it not to wait) - ie. we are done
133
sok.settimeout(SOCKETTIMEOUT)
135
out = encode(msg, magic)
137
send_netstring(sok, out)
138
inp = recv_netstring(sok)
157
return cjson.decode(inp)
143
return json.loads(inp)
147
def encode(message, magic):
148
"""Converts a message into a JSON serialisation and uses a magic
149
string to attach a HMAC digest.
151
# XXX: Any reason that we double encode?
152
content = json.dumps(message)
154
digest = hashlib.md5(content + magic).hexdigest()
155
env = {'digest':digest,'content':content}
156
return json.dumps(env)
159
def decode(message, magic):
160
"""Takes a message with an attached HMAC digest and validates the message.
162
msg = json.loads(message)
164
# Check that the message is valid
165
digest = hashlib.md5(msg['content'] + magic).hexdigest()
166
if msg['digest'] != digest:
167
raise ProtocolError("HMAC digest is invalid")
168
content = json.loads(msg['content'])
173
def send_netstring(sok, data):
174
""" Sends a netstring to a socket
176
netstring = "%d:%s,"%(len(data),data)
177
sok.sendall(netstring)
180
def recv_netstring(sok):
181
""" Attempts to recieve a Netstring from a socket.
182
Throws a ProtocolError if the received data violates the Netstring
189
# Limit the Netstring to less than 10^10 bytes (~1GB).
190
if len(size_buffer) >= 10:
192
"Could not read Netstring size in first 9 bytes: '%s'"%(
193
''.join(size_buffer)))
194
size_buffer.append(c)
197
# Message should be length plus ',' terminator
198
recv_expected = int(''.join(size_buffer)) + 1
199
except ValueError, e:
200
raise ProtocolError("Could not decode Netstring size as int: '%s'"%(
201
''.join(size_buffer)))
205
recv_data = sok.recv(min(recv_expected, BLOCKSIZE))
206
recv_size = len(recv_data)
207
while recv_size < recv_expected:
208
buf.append(recv_data)
209
recv_data = sok.recv(min(recv_expected-recv_size, 1024))
210
recv_size = recv_size + len(recv_data)
211
assert(recv_size == recv_expected)
213
# Did we receive the correct amount?
214
if recv_data[-1] != ',':
215
raise ProtocolError("Netstring did not end with ','")
216
buf.append(recv_data[:-1])