Skip to content

Commit 519ceaa

Browse files
webrepl: support access from LAN, without internet
Host everything on the device itself, rather than redirecting to https://micropython.org/webrepl/ because that doesn't work when there is not internet, including when the device is in Access Point mode. This was a bit slow, because of the many files and being pretty large, but the inline_minify_webrepl.py makes this much better and brings it down to around 1s to load the page, versus 20 seconds. The minification also reduces the size from around 160KB to 80KB.
1 parent 7d70724 commit 519ceaa

12 files changed

Lines changed: 6853 additions & 2 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ __pycache__/
2828
*$py.class
2929
*.so
3030

31-
# these get created:
31+
# these get created by the build system, don't know why:
3232
c_mpos/c_mpos
3333

3434
# build files
3535
*.bin
36+
37+
# auto created by inline_minify_webrepl.py
38+
internal_filesystem/builtin/html/webrepl_inlined_minified.html
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This folder will be filled by the inline_minify_webrepl.py script.

internal_filesystem/lib/mpos/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ async def asyncio_repl():
223223

224224
try:
225225
import webrepl
226-
webrepl.start(port=7890,password="MPOSweb26") # password is max 9 characters
226+
from mpos.webserver import accept_handler as webrepl_accept_handler
227+
webrepl.start(port=7890, password="MPOSweb26", accept_handler=webrepl_accept_handler) # password is max 9 characters
227228
except Exception as e:
228229
print(f"Could not start webrepl - this is normal on desktop systems: {e}")
229230

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""Web server helpers for MicroPythonOS."""
2+
3+
from .webrepl_http import accept_handler
4+
5+
__all__ = ["accept_handler"]
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import os
2+
import socket
3+
import uio
4+
5+
import _webrepl
6+
import webrepl
7+
import websocket
8+
9+
WEBREPL_HTML_PATH = "builtin/html/webrepl_inlined_minified.html"
10+
'''
11+
# Unused as these files are minified and inlined:
12+
#WEBREPL_HTML_PATH = "/builtin/html/webrepl.html"
13+
WEBREPL_CONTENT_PATH = "/builtin/html/webrepl.js"
14+
WEBREPL_TERM_PATH = "/builtin/html/term.js"
15+
WEBREPL_CSS_PATH = "/builtin/html/webrepl.css"
16+
WEBREPL_FILE_SAVER_PATH = "/builtin/html/FileSaver.js"
17+
'''
18+
19+
WEBREPL_ASSETS = {
20+
b"/": (WEBREPL_HTML_PATH, b"text/html"),
21+
b"/index.html": (WEBREPL_HTML_PATH, b"text/html"),
22+
#b"/webrepl.css": (WEBREPL_CSS_PATH, b"text/css"),
23+
#b"/webrepl.js": (WEBREPL_CONTENT_PATH, b"application/javascript"),
24+
#b"/term.js": (WEBREPL_TERM_PATH, b"application/javascript"),
25+
#b"/FileSaver.js": (WEBREPL_FILE_SAVER_PATH, b"application/javascript"),
26+
}
27+
28+
29+
class _MakefileSocket:
30+
def __init__(self, sock, raw_request):
31+
self._sock = sock
32+
self._raw_request = raw_request
33+
34+
def makefile(self, *args, **kwargs):
35+
return uio.BytesIO(self._raw_request)
36+
37+
def __getattr__(self, name):
38+
return getattr(self._sock, name)
39+
40+
41+
def _read_http_request(cl):
42+
req = cl.makefile("rwb", 0)
43+
first_line = req.readline()
44+
if not first_line:
45+
return None, None, b""
46+
47+
raw_request = first_line
48+
headers = {}
49+
while True:
50+
line = req.readline()
51+
if not line:
52+
break
53+
raw_request += line
54+
if line == b"\r\n":
55+
break
56+
if b":" in line:
57+
key, value = line.split(b":", 1)
58+
headers[key.strip().lower()] = value.strip().lower()
59+
60+
parts = first_line.split()
61+
path = parts[1] if len(parts) >= 2 else b"/"
62+
if b"?" in path:
63+
path = path.split(b"?", 1)[0]
64+
65+
return path, headers, raw_request
66+
67+
68+
def _is_websocket_request(headers):
69+
connection = headers.get(b"connection", b"")
70+
upgrade = headers.get(b"upgrade", b"")
71+
return b"upgrade" in connection and upgrade == b"websocket"
72+
73+
74+
def _send_response(cl, status, content_type, body):
75+
cl.send(b"HTTP/1.0 " + status + b"\r\n")
76+
cl.send(b"Server: MicroPythonOS\r\n")
77+
cl.send(b"Content-Type: " + content_type + b"\r\n")
78+
cl.send(b"Content-Length: %d\r\n\r\n" % len(body))
79+
cl.send(body)
80+
cl.close()
81+
82+
83+
def _send_file_response(cl, path, content_type):
84+
try:
85+
with open(path, "rb") as handle:
86+
body = handle.read()
87+
except OSError:
88+
_send_response(cl, b"404 Not Found", b"text/plain", b"Not Found")
89+
return False
90+
91+
_send_response(cl, b"200 OK", content_type, body)
92+
return False
93+
94+
95+
def _start_webrepl_session(cl, remote_addr):
96+
print("\nWebREPL connection from:", remote_addr)
97+
webrepl.client_s = cl
98+
99+
ws = websocket.websocket(cl, True)
100+
ws = _webrepl._webrepl(ws)
101+
cl.setblocking(False)
102+
if hasattr(os, "dupterm_notify"):
103+
cl.setsockopt(socket.SOL_SOCKET, 20, os.dupterm_notify)
104+
os.dupterm(ws)
105+
106+
return True
107+
108+
109+
def accept_handler(listen_sock):
110+
cl, remote_addr = listen_sock.accept()
111+
print("\webrepl_http connection from:", remote_addr)
112+
try:
113+
path, headers, raw_request = _read_http_request(cl)
114+
if not path:
115+
cl.close()
116+
return False
117+
118+
if _is_websocket_request(headers):
119+
if not webrepl.server_handshake(_MakefileSocket(cl, raw_request)):
120+
cl.close()
121+
return False
122+
return _start_webrepl_session(cl, remote_addr)
123+
124+
if path in WEBREPL_ASSETS:
125+
asset_path, content_type = WEBREPL_ASSETS[path]
126+
return _send_file_response(cl, asset_path, content_type)
127+
128+
_send_response(cl, b"404 Not Found", b"text/plain", b"Not Found")
129+
return False
130+
except Exception as exc:
131+
print("webrepl_http: error handling connection:", exc)
132+
try:
133+
cl.close()
134+
except Exception:
135+
pass
136+
return False

webrepl/FileSaver.js

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/* FileSaver.js
2+
* A saveAs() FileSaver implementation.
3+
* 1.3.2
4+
* 2016-06-16 18:25:19
5+
*
6+
* By Eli Grey, http://eligrey.com
7+
* License: MIT
8+
* See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md
9+
*/
10+
11+
/*global self */
12+
/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */
13+
14+
/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */
15+
16+
var saveAs = saveAs || (function(view) {
17+
"use strict";
18+
// IE <10 is explicitly unsupported
19+
if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
20+
return;
21+
}
22+
var
23+
doc = view.document
24+
// only get URL when necessary in case Blob.js hasn't overridden it yet
25+
, get_URL = function() {
26+
return view.URL || view.webkitURL || view;
27+
}
28+
, save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
29+
, can_use_save_link = "download" in save_link
30+
, click = function(node) {
31+
var event = new MouseEvent("click");
32+
node.dispatchEvent(event);
33+
}
34+
, is_safari = /constructor/i.test(view.HTMLElement)
35+
, is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
36+
, throw_outside = function(ex) {
37+
(view.setImmediate || view.setTimeout)(function() {
38+
throw ex;
39+
}, 0);
40+
}
41+
, force_saveable_type = "application/octet-stream"
42+
// the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
43+
, arbitrary_revoke_timeout = 1000 * 40 // in ms
44+
, revoke = function(file) {
45+
var revoker = function() {
46+
if (typeof file === "string") { // file is an object URL
47+
get_URL().revokeObjectURL(file);
48+
} else { // file is a File
49+
file.remove();
50+
}
51+
};
52+
setTimeout(revoker, arbitrary_revoke_timeout);
53+
}
54+
, dispatch = function(filesaver, event_types, event) {
55+
event_types = [].concat(event_types);
56+
var i = event_types.length;
57+
while (i--) {
58+
var listener = filesaver["on" + event_types[i]];
59+
if (typeof listener === "function") {
60+
try {
61+
listener.call(filesaver, event || filesaver);
62+
} catch (ex) {
63+
throw_outside(ex);
64+
}
65+
}
66+
}
67+
}
68+
, auto_bom = function(blob) {
69+
// prepend BOM for UTF-8 XML and text/* types (including HTML)
70+
// note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
71+
if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
72+
return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
73+
}
74+
return blob;
75+
}
76+
, FileSaver = function(blob, name, no_auto_bom) {
77+
if (!no_auto_bom) {
78+
blob = auto_bom(blob);
79+
}
80+
// First try a.download, then web filesystem, then object URLs
81+
var
82+
filesaver = this
83+
, type = blob.type
84+
, force = type === force_saveable_type
85+
, object_url
86+
, dispatch_all = function() {
87+
dispatch(filesaver, "writestart progress write writeend".split(" "));
88+
}
89+
// on any filesys errors revert to saving with object URLs
90+
, fs_error = function() {
91+
if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
92+
// Safari doesn't allow downloading of blob urls
93+
var reader = new FileReader();
94+
reader.onloadend = function() {
95+
var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
96+
var popup = view.open(url, '_blank');
97+
if(!popup) view.location.href = url;
98+
url=undefined; // release reference before dispatching
99+
filesaver.readyState = filesaver.DONE;
100+
dispatch_all();
101+
};
102+
reader.readAsDataURL(blob);
103+
filesaver.readyState = filesaver.INIT;
104+
return;
105+
}
106+
// don't create more object URLs than needed
107+
if (!object_url) {
108+
object_url = get_URL().createObjectURL(blob);
109+
}
110+
if (force) {
111+
view.location.href = object_url;
112+
} else {
113+
var opened = view.open(object_url, "_blank");
114+
if (!opened) {
115+
// Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
116+
view.location.href = object_url;
117+
}
118+
}
119+
filesaver.readyState = filesaver.DONE;
120+
dispatch_all();
121+
revoke(object_url);
122+
}
123+
;
124+
filesaver.readyState = filesaver.INIT;
125+
126+
if (can_use_save_link) {
127+
object_url = get_URL().createObjectURL(blob);
128+
setTimeout(function() {
129+
save_link.href = object_url;
130+
save_link.download = name;
131+
click(save_link);
132+
dispatch_all();
133+
revoke(object_url);
134+
filesaver.readyState = filesaver.DONE;
135+
});
136+
return;
137+
}
138+
139+
fs_error();
140+
}
141+
, FS_proto = FileSaver.prototype
142+
, saveAs = function(blob, name, no_auto_bom) {
143+
return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
144+
}
145+
;
146+
// IE 10+ (native saveAs)
147+
if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
148+
return function(blob, name, no_auto_bom) {
149+
name = name || blob.name || "download";
150+
151+
if (!no_auto_bom) {
152+
blob = auto_bom(blob);
153+
}
154+
return navigator.msSaveOrOpenBlob(blob, name);
155+
};
156+
}
157+
158+
FS_proto.abort = function(){};
159+
FS_proto.readyState = FS_proto.INIT = 0;
160+
FS_proto.WRITING = 1;
161+
FS_proto.DONE = 2;
162+
163+
FS_proto.error =
164+
FS_proto.onwritestart =
165+
FS_proto.onprogress =
166+
FS_proto.onwrite =
167+
FS_proto.onabort =
168+
FS_proto.onerror =
169+
FS_proto.onwriteend =
170+
null;
171+
172+
return saveAs;
173+
}(
174+
typeof self !== "undefined" && self
175+
|| typeof window !== "undefined" && window
176+
|| this.content
177+
));
178+
// `self` is undefined in Firefox for Android content script context
179+
// while `this` is nsIContentFrameMessageManager
180+
// with an attribute `content` that corresponds to the window
181+
182+
if (typeof module !== "undefined" && module.exports) {
183+
module.exports.saveAs = saveAs;
184+
} else if ((typeof define !== "undefined" && define !== null) && (define.amd !== null)) {
185+
define([], function() {
186+
return saveAs;
187+
});
188+
}

webrepl/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# WebREPL content
2+
3+
These files were sourced from commit `fff7b87` of https://github.com/micropython/webrepl.

0 commit comments

Comments
 (0)