89
93
(conn, addr) = s.accept()
90
94
conn.settimeout(SOCKETTIMEOUT)
96
# Grab the input and try to decode
93
97
inp = recv_netstring(conn)
94
env = cjson.decode(inp)
96
# Check that the message is
97
digest = hashlib.md5(env['content'] + magic).hexdigest()
98
if env['digest'] != digest:
99
content = decode(inp, magic)
100
except ProtocolError:
102
content = cjson.decode(env['content'])
104
104
response = handler(content)
106
send_netstring(conn, cjson.encode(response))
106
send_netstring(conn, json.dumps(response))
110
110
except Terminate, t:
111
111
# Try and send final response and then terminate
112
112
if t.final_response:
113
send_netstring(conn, cjson.encode(t.final_response))
113
send_netstring(conn, json.dumps(t.final_response))
116
116
except Exception:
131
131
sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
132
132
sok.connect((host, port))
133
133
sok.settimeout(SOCKETTIMEOUT)
134
content = cjson.encode(msg)
135
digest = hashlib.md5(content + magic).hexdigest()
136
env = {'digest':digest,'content':content}
137
json = cjson.encode(env)
139
send_netstring(sok, json)
135
out = encode(msg, magic)
137
send_netstring(sok, out)
140
138
inp = recv_netstring(sok)
145
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'])
150
173
def send_netstring(sok, data):
174
""" Sends a netstring to a socket
151
176
netstring = "%d:%s,"%(len(data),data)
152
177
sok.sendall(netstring)
155
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
156
185
# Decode netstring