forked from libmapper/webmapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmapper_http_server.py
executable file
·454 lines (400 loc) · 15.8 KB
/
webmapper_http_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
import SocketServer
import SimpleHTTPServer
import socket, errno
import urllib
import urlparse
import cgi
import threading, Queue
import time
import json
from select import select
import sys
import struct
import hashlib
from cStringIO import StringIO
import pdb
message_pipe = Queue.Queue()
tracing = False
done = False
class RequestCounter(object):
def __init__(self):
self.count = 1
self.started = False
def start(self):
if not self.started:
self.started = True
self.count -= 1
def inc(self):
if (not self.started):
self.start()
self.count += 1
def dec(self):
if (self.started):
self.count -= 1
ref = RequestCounter()
class ReuseTCPServer(SocketServer.ThreadingTCPServer):
allow_reuse_address = True
class MapperHTTPServer(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_POST(self):
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query = cgi.parse_multipart(self.rfile, pdict)
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.end_headers()
fn = query['filename'][0].split('\\')[-1]
er = 0
try:
er = 1
if cmd_handlers.has_key('load'):
er = 2
sources = query['sources'][0].split(',')
destinations = query['destinations'][0].split(',')
devices = {'sources': sources, 'destinations': destinations}
cmd_handlers['load'](query['mapping_json'][0], devices)
print >>self.wfile, "Success: %s loaded successfully."%fn
except Exception, e:
print >>self.wfile, "Error: loading %s (%d)."%(fn,er)
raise e
def do_GET(self):
command = self.path
args = []
try:
parsed = urlparse.urlparse(self.path)
command = parsed.path
args = dict(urlparse.parse_qsl(parsed.query))
except Exception, e:
print e
contenttype = { 'html': 'Content-Type: text/html; charset=UTF-8',
'js': 'Content-Type: text/javascript',
'css': 'Content-Type: text/css',
'json': 'Content-Type: text/javascript',
'png': 'Content-Type: image/png',
'dl': None}
def found(type=''):
if (type=='socket'):
if tracing: print 'websocket requested'
return self.do_websocket()
print >>self.wfile, "HTTP/1.0 200 OK"
if type=='dl': return
try:
print >>self.wfile, contenttype[type]
except KeyError:
pass
finally:
print >>self.wfile
def notfound(type=''):
print >>self.wfile, "HTTP/1.0 404 Not Found"
try:
print >>self.wfile, contenttype[type]
finally:
print >>self.wfile
try:
found(handlers[command][1])
if command=='/send_cmd':
if tracing: print 'hxr_recv:',args['msg']
handlers[command][0](self.wfile, args)
except KeyError:
try:
f = open(self.path[1:], 'rb')
found(self.path.rsplit('.',1)[-1])
self.copyfile(f, self.wfile)
except IOError:
notfound('html')
print >>self.wfile, "404 Not Found:", self.path
def do_websocket(self):
ref.inc()
ws_version = self.websocket_handshake()
try:
if ws_version < 8:
self.do_websocket_0()
else:
self.do_websocket_8()
except socket.error, e:
if e.errno == errno.EPIPE or e.errno == errno.ECONNRESET:
# Avoid reporting a huge stack trace for broken pipe
# exception, it just means that the websocket closed
# because the browser window was closed for example.
print '[ws]',e
else:
raise e
finally:
ref.dec()
def do_websocket_0(self):
msg = ""
while not done:
time.sleep(0.1)
if not message_pipe.empty():
sendmsg = message_pipe.get()
if tracing: print 'ws_send:',sendmsg
self.wfile.write(chr(0)+json.dumps({"cmd": sendmsg[0],
"args": sendmsg[1]})
+ chr(0xFF));
self.wfile.flush()
while len(select([self.rfile._sock],[],[],0)[0])>0:
msg += self.rfile.read(1)
if len(msg)==0:
break
if ord(msg[-1])==0x00:
msg = "";
elif ord(msg[-1])==0xFF:
break;
if len(msg)>0 and ord(msg[-1])==0xFF:
out = StringIO()
if tracing: print 'ws_recv:',msg[:-1]
handler_send_command(out, {'msg':msg[:-1]})
msg = ""
r = out.getvalue()
if len(r) > 0:
if tracing: print 'ws_send2:',r
self.wfile.write(chr(0)+r.encode('utf-8')+chr(0xFF))
self.wfile.flush()
def do_websocket_8(self):
def send_string(s):
opcode = chr((1<<7)|1) # FIN + text
if len(s)<126:
L = chr(len(s))
elif len(s)>=126 and len(s)<65536:
L = chr(126)+chr((len(s)>>8)&0xFF)+chr(len(s)&0xFF)
else:
print '[ws] message too long! (TODO extended-length msgs)'
self.wfile.write(opcode+L)
self.wfile.write(s)
self.wfile.flush()
msg = ""
length = -1
offset = -1
while not done:
to_read = len(select([self.rfile._sock],[],[],0.1)[0]) > 0
n = 0
while not message_pipe.empty() and n < 30:
sendmsg = message_pipe.get()
if tracing: print 'ws_send:',sendmsg
s = json.dumps({"cmd": sendmsg[0],
"args": sendmsg[1]})
send_string(s)
n += 1
while to_read:
prevlen = len(msg)
msg += self.rfile.read(1)
if len(msg)==prevlen:
return
if len(msg)==1:
opcode=ord(msg[0]) # TODO check FIN
# opcode&0x7F should be 1 for text, 2 for binary
if (opcode&0x7F) == 8:
# Connection close
return
if (opcode&0x7F)!=1 and (opcode&0x7F)!=2:
print '[ws] unknown opcode %#x'%(opcode&0x7F)
if len(msg)==2:
mask = ord(msg[1]) & 0x80
length = ord(msg[1]) & 0x7F
offset = 2 + 4*(mask!=0)
if len(msg)==4:
if length == 126:
length = (ord(msg[2])<<8) | ord(msg[3])
offset = 4 + 4*(mask!=0)
elif length == 127:
print 'TODO extended message length'
if len(msg)==6 and length<126:
key = map(ord,msg[2:6])
elif len(msg)==8 and mask!=0 and length >= 126:
key = map(ord,msg[4:8])
if len(msg)==length+offset:
break
to_read = len(select([self.rfile._sock],[],[],0)[0]) > 0
if len(msg)==length+offset:
m = ''.join([chr(ord(c)^key[n%4])
for n,c in enumerate(msg[offset:])])
out = StringIO()
if tracing: print 'ws_recv:',m
handler_send_command(out, {'msg':m})
msg = ""
length = offset = -1
r = out.getvalue()
if len(r) > 0:
if tracing: print 'ws_send:',r
send_string(r.encode('utf-8'))
def websocket_handshake(self):
print >>self.wfile, ("HTTP/1.1 101 Web Socket Protocol Handshake\r")
print >>self.wfile,'Upgrade: %s\r'%self.headers['Upgrade']
print >>self.wfile,'Connection: %s\r'%self.headers['Connection'],'\r'
import hashlib
if (not self.headers.has_key('Sec-WebSocket-Version')
or int(self.headers['Sec-WebSocket-Version'])<8):
print >>self.wfile,('Sec-WebSocket-Origin: %s\r'
%self.headers['Origin'])
print >>self.wfile,('Sec-WebSocket-Location: ws://%s%s\r'
%(self.headers['Host'], self.path))
key1 = self.headers['Sec-WebSocket-Key1']
key2 = self.headers['Sec-WebSocket-Key2']
code = self.rfile.read(8)
def websocket_key_calc(key1,key2,code):
i1=int(filter(lambda x: x.isdigit(),key1))/key1.count(' ')
i2=int(filter(lambda x: x.isdigit(),key2))/key2.count(' ')
return hashlib.md5(struct.pack('!II',i1,i2)+code).digest()
print >>self.wfile,'\r'
self.wfile.write(websocket_key_calc(key1,key2,code))
elif int(self.headers['Sec-WebSocket-Version'])>=8:
key = self.headers['Sec-WebSocket-Key']
magic_guid = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
import base64
sha1 = hashlib.sha1(key+magic_guid)
result = base64.b64encode(sha1.digest())
print >>self.wfile,'Sec-WebSocket-Accept: %s\r'%result
if self.headers.has_key('Sec-WebSocket-Protocol'):
print >>self.wfile,'Sec-WebSocket-Protocol: webmapper\r'
print >>self.wfile,'\r'
self.wfile.flush()
if not self.headers.has_key('Sec-WebSocket-Version'):
return 0
else:
return int(self.headers['Sec-WebSocket-Version'])
# There ought to be a more elegant way to do this
def handler_page(out, args):
print >>out, """<!DOCTYPE html>
<html>
<head>
<title>mapperGUI</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="includes/jquery-2.0.3.min.js"></script> <!-- JQuery -->
<script type="text/javascript" src="includes/jquery-ui-1.10.0.custom.js"></script> <!-- JQuery UI -->
<script type="text/javascript" src="includes/json2.js"></script> <!-- JSON -->
<script type="text/javascript" src="js/util.js"></script>
<script type="text/javascript" src="js/command.js"></script>
<script type="text/javascript" src="js/LibMapperModel.js"></script>
<!-- List View includes -->
<script type="text/javascript" src="js/viz/list.js"></script>
<script type="text/javascript" src="includes/raphael.js"></script>
<script type="text/javascript" src="includes/jquery.tablesorter.min.js"></script>
<!-- Grid View includes -->
<script type="text/javascript" src="js/viz/grid.js"></script>
<script type="text/javascript" src="js/viz/grid/GridSVG.js"></script>
<script type="text/javascript" src="js/viz/grid/GridSignals.js"></script>
<script type="text/javascript" src="js/viz/grid/GridDevices.js"></script>
<link rel="stylesheet" type="text/css" href="js/viz/grid/GridView_style.css"></link>
<link rel="stylesheet" href="js/viz/grid/ui-lightness/jquery-ui-1.10.0.custom.css" />
<!-- HivePlot View includes -->
<script type="text/javascript" src="js/viz/hive.js"></script>
<link rel="stylesheet" type="text/css" href="js/viz/hive/HivePlot_style.css"></link>
<!-- Balloon Plot View includes -->
<script type="text/javascript" src="js/viz/balloon.js"></script>
<link rel="stylesheet" type="text/css" href="js/viz/balloon/BalloonPlot_style.css"></link>
<!-- Main includes -->
<script type="text/javascript" src="js/main.js"></script>
<script type="text/javascript" src="js/TopMenu.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css"></link>
<link type="text/css" rel="stylesheet" href="includes/jquery.qtip.custom/jquery.qtip.min.css" /> <!-- qTip CSS -->
<script type="text/javascript" src="includes/jquery.qtip.custom/jquery.qtip.min.js"></script> <!-- qTip -->
</head>
<body></body>
</html>"""
def handler_wait_command(out, args):
i=0
ref.inc()
while len(message_pipe)==0:
time.sleep(0.1)
i = i + 1
if (i>50):
r, w, e=select([out._sock],[],[out._sock], 0)
ref.dec()
if len(r)>0 or len(e)>0:
return
print >>out, json.dumps( {"id": int(args['id'])} );
return
ref.dec()
r, w, e=select([out._sock],[],[out._sock], 0)
if len(r)>0 or len(e)>0:
return
# Receive command from back-end
msg = message_pipe.get()
if tracing: print 'hxr_send:',msg
print >>out, json.dumps( {"id": int(args['id']),
"cmd": msg[0],
"args": msg[1]} )
def handler_send_command(out, args):
try:
msgstring = args['msg']
vals = json.loads(msgstring)
h = cmd_handlers[vals['cmd']]
except KeyError:
print 'send_command: no message found in "%s"'%str(msgstring)
return
except ValueError, e:
print 'send_command: bad embedded JSON "%s"'%msgstring
raise e
return
# JSON decoding returns unicode which doesn't work well with
# our C library, so convert any strings to str.
vals['args'] = deunicode(vals['args'])
res = h(vals['args'])
if res:
print >>out, json.dumps( { "cmd": res[0],
"args": res[1] } )
def handler_sock(out, args):
pass
def handler_save(out, args):
if not 'save' in cmd_handlers:
print >>out
print >>out, "Error, no save handler registered."
return
f = cmd_handlers['save']
result = f(args)
if result==None:
print >>out
print >>out, "Error saving", args
return
fn, content = result
print >>out, 'Expires: 0'
print >>out, 'Cache-Control: no-store'
print >>out, 'Content-Description: File Transfer'
print >>out, 'Content-Disposition: attachment; filename="%s"'%fn
print >>out, 'Content-Type: text/javascript'
print >>out, 'Content-Transfer-Encoding: binary'
print >>out, 'Content-Length: %d'%len(content)
print >>out
print >>out, content
handlers = {'/': [handler_page, 'html'],
'/wait_cmd': [handler_wait_command, 'json'],
'/send_cmd': [handler_send_command, 'json'],
'/sock': [handler_sock, 'socket'],
'/save': [handler_save, 'dl']}
cmd_handlers = {}
def deunicode(o):
d = dir(o)
if 'items' in d:
p = dict([(deunicode(x),deunicode(y)) for (x,y) in o.items()])
elif '__dict__' in d:
p = o.copy()
p.__dict__ = dict([(deunicode(x),deunicode(y))
for (x,y) in o.__dict__.items()])
elif '__iter__' in d:
p = [deunicode(x) for x in o]
elif o.__class__==unicode:
p = o.encode('ascii','replace')
else:
p = o
return p
def send_command(cmd, args):
message_pipe.put((cmd, args))
def add_command_handler(cmd, handler):
cmd_handlers[cmd] = handler
def serve(port=8000, poll=lambda: time.sleep(10), on_open=lambda: (),
quit_on_disconnect=True):
httpd = ReuseTCPServer(('', port), MapperHTTPServer)
on_open()
http_thread = threading.Thread(target=httpd.serve_forever)
http_thread.start()
print "serving at port", port
try:
while ref.count > 0 or not quit_on_disconnect:
for i in range(100):
poll()
print "Lost connection."
except KeyboardInterrupt:
pass
print "shutting down..."
httpd.shutdown()
http_thread.join()
print 'bye.'