summaryrefslogtreecommitdiffstatshomepage
path: root/extmod/webrepl/websocket_helper.py
diff options
context:
space:
mode:
authorJim Mussared <jim.mussared@gmail.com>2022-07-20 12:09:24 +1000
committerJim Mussared <jim.mussared@gmail.com>2022-07-23 12:43:08 +1000
commit924e55aca18978215209a2ed81a49b0a6bdcaaa7 (patch)
tree9656ad568db3aa1a049af50b882298770b0ee68b /extmod/webrepl/websocket_helper.py
parentd2e4cf00ccec432ccd38adea78099887ac578882 (diff)
downloadmicropython-924e55aca18978215209a2ed81a49b0a6bdcaaa7.tar.gz
micropython-924e55aca18978215209a2ed81a49b0a6bdcaaa7.zip
extmod/webrepl: Allow the page to run from the device (over HTTP).
The device will respond to a non-WS request with a simple page that loads websocket_content.js from a static host (http or https). However, even if the resources are https, the page is still http and therefore allows requesting to a WS (not WSS) websocket on the device. Removed unused client_handshake from websocket_helper, and then merges the remainder of this file (server_handshake) into webrepl.py (to reduce firmware size). Also added the respond-as-HTTP handling to server_handshake. The default HTTP response is a simple page that sets the base URL and then loads webrepl_content.js which document.write's the actual HTML. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'extmod/webrepl/websocket_helper.py')
-rw-r--r--extmod/webrepl/websocket_helper.py85
1 files changed, 0 insertions, 85 deletions
diff --git a/extmod/webrepl/websocket_helper.py b/extmod/webrepl/websocket_helper.py
deleted file mode 100644
index 3260acc52f..0000000000
--- a/extmod/webrepl/websocket_helper.py
+++ /dev/null
@@ -1,85 +0,0 @@
-try:
- import usys as sys
-except ImportError:
- 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)