diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2019-10-21 15:55:18 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2019-12-20 12:59:13 +1100 |
commit | 7ce1e0b1dc466e48606164aad223c81c93a9cea2 (patch) | |
tree | 7ee755742cb06393c13b420610525b85b77f10aa /extmod/webrepl/websocket_helper.py | |
parent | 7f235cbee924305e2d8a8aa86876770af66d7d82 (diff) | |
download | micropython-7ce1e0b1dc466e48606164aad223c81c93a9cea2.tar.gz micropython-7ce1e0b1dc466e48606164aad223c81c93a9cea2.zip |
extmod/webrepl: Move webrepl scripts to common place and use manifest.
Move webrepl support code from ports/esp8266/modules into extmod/webrepl
(to be alongside extmod/modwebrepl.c), and use frozen manifests to include
it in the build on esp8266 and esp32.
A small modification is made to webrepl.py to make it work on non-ESP
ports, i.e. don't call dupterm_notify if not available.
Diffstat (limited to 'extmod/webrepl/websocket_helper.py')
-rw-r--r-- | extmod/webrepl/websocket_helper.py | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/extmod/webrepl/websocket_helper.py b/extmod/webrepl/websocket_helper.py new file mode 100644 index 0000000000..9c06db5023 --- /dev/null +++ b/extmod/webrepl/websocket_helper.py @@ -0,0 +1,74 @@ +import sys +try: + import ubinascii as binascii +except: + import binascii +try: + import uhashlib as hashlib +except: + import hashlib + +DEBUG = 0 + +def server_handshake(sock): + clr = sock.makefile("rwb", 0) + l = clr.readline() + #sys.stdout.write(repr(l)) + + webkey = None + + while 1: + l = clr.readline() + if not l: + raise OSError("EOF in headers") + if l == b"\r\n": + break + # sys.stdout.write(l) + h, v = [x.strip() for x in l.split(b":", 1)] + if DEBUG: + print((h, v)) + if h == b'Sec-WebSocket-Key': + webkey = v + + if not webkey: + raise OSError("Not a websocket request") + + if DEBUG: + print("Sec-WebSocket-Key:", webkey, len(webkey)) + + d = hashlib.sha1(webkey) + d.update(b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11") + respkey = d.digest() + respkey = binascii.b2a_base64(respkey)[:-1] + if DEBUG: + print("respkey:", respkey) + + sock.send(b"""\ +HTTP/1.1 101 Switching Protocols\r +Upgrade: websocket\r +Connection: Upgrade\r +Sec-WebSocket-Accept: """) + sock.send(respkey) + sock.send("\r\n\r\n") + + +# Very simplified client handshake, works for MicroPython's +# websocket server implementation, but probably not for other +# servers. +def client_handshake(sock): + cl = sock.makefile("rwb", 0) + cl.write(b"""\ +GET / HTTP/1.1\r +Host: echo.websocket.org\r +Connection: Upgrade\r +Upgrade: websocket\r +Sec-WebSocket-Key: foo\r +\r +""") + l = cl.readline() +# print(l) + while 1: + l = cl.readline() + if l == b"\r\n": + break +# sys.stdout.write(l) |