~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/chat.py

  • Committer: David Coles
  • Date: 2010-02-24 12:23:57 UTC
  • Revision ID: coles.david@gmail.com-20100224122357-ejlonohh1l4dg0ng
chat: Factor out the encoding and decoding functions so we can test them and possibly reuse elsewhere

Show diffs side-by-side

added added

removed removed

Lines of Context:
89
89
        (conn, addr) = s.accept()
90
90
        conn.settimeout(SOCKETTIMEOUT)
91
91
        try:
92
 
            # Grab the input
 
92
            # Grab the input and try to decode
93
93
            inp = recv_netstring(conn)
94
 
            env = cjson.decode(inp)
95
 
 
96
 
            # Check that the message is 
97
 
            digest = hashlib.md5(env['content'] + magic).hexdigest()
98
 
            if env['digest'] != digest:
 
94
            try:
 
95
                content = decode(inp)
 
96
            except ProtocolError:
99
97
                conn.close()
100
98
                continue
101
99
 
102
 
            content = cjson.decode(env['content'])
103
 
 
104
100
            response = handler(content)
105
101
 
106
102
            send_netstring(conn, cjson.encode(response))
131
127
    sok = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
132
128
    sok.connect((host, port))
133
129
    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)
 
130
 
 
131
    json = encode(msg, magic)
138
132
 
139
133
    send_netstring(sok, json)
140
134
    inp = recv_netstring(sok)
146
140
    else:
147
141
        return inp
148
142
 
 
143
def encode(message, magic):
 
144
    """Converts a message into a JSON serialisation and uses a magic
 
145
    string to attach a HMAC digest.
 
146
    """
 
147
    # XXX: Any reason that we double encode?
 
148
    content = cjson.encode(message)
 
149
 
 
150
    digest = hashlib.md5(content + magic).hexdigest()
 
151
    env = {'digest':digest,'content':content}
 
152
    json = cjson.encode(env)
 
153
 
 
154
    return json
 
155
 
 
156
 
 
157
def decode(message, magic):
 
158
    """Takes a message with an attached HMAC digest and validates the message.
 
159
    """
 
160
    msg = cjson.decode(message)
 
161
 
 
162
    # Check that the message is valid
 
163
    digest = hashlib.md5(msg['content'] + magic).hexdigest()
 
164
    if msg['digest'] != digest:
 
165
        raise ProtocolError("HMAC digest is invalid")
 
166
    content = cjson.decode(msg['content'])
 
167
 
 
168
    return content
 
169
 
149
170
 
150
171
def send_netstring(sok, data):
 
172
    """ Sends a netstring to a socket
 
173
    """
151
174
    netstring = "%d:%s,"%(len(data),data)
152
175
    sok.sendall(netstring)
153
176
 
154
177
 
155
178
def recv_netstring(sok):
 
179
    """ Attempts to recieve a Netstring from a socket.
 
180
    Throws a ProtocolError if the received data violates the Netstring 
 
181
    protocol.
 
182
    """
156
183
    # Decode netstring
157
184
    size_buffer = []
158
185
    c = sok.recv(1)