Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/displaydev/remotedisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def __init__(self, display, port=80, period_ms=40, sweep_ms=2000, bind="0.0.0.0"
self.url = "http://%s%s/" % (_own_address(), at)
name = _own_name()
self.name_url = "http://%s.local%s/" % (name, at) if name else None
self._page = _titled(PAGE, _board_name())
self._timer = every(period_ms, self.poll, name="remotedisplay")
if self.name_url:
print("Display at %s and %s" % (self.url, self.name_url))
Expand Down Expand Up @@ -630,8 +631,8 @@ def _serve(self, conn):
if target in ("/", "/index.html"):
conn.out = (
"HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n"
"Cache-Control: no-cache\r\nConnection: close\r\nContent-Length: %d\r\n\r\n" % len(PAGE)
).encode() + PAGE
"Cache-Control: no-cache\r\nConnection: close\r\nContent-Length: %d\r\n\r\n" % len(self._page)
).encode() + self._page
else:
conn.out = b"HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
conn.done = True
Expand Down Expand Up @@ -967,6 +968,24 @@ def _put_head(buf, off, x, y, w, h, enc, n):
buf[off + 15] = n >> 24


def _board_name():
"""The board's name from `boarddev`, for the page's title; None without it."""
try:
import boarddev

return boarddev.name() or None
except (ImportError, AttributeError, OSError, ValueError):
return None


def _titled(page, name):
"""The page with the board's name as its title, so a browser tab says which board it shows."""
if not name:
return page
safe = name.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
return page.replace(b"<title>Display</title>", b"<title>" + safe.encode() + b"</title>", 1)


def _own_name():
"""The host name mDNS answers to on a board, without ``.local``; None on a desktop."""
try:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_remotedisplay.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,22 @@ def test_page(self):
data = _get_page(self.port)
self.assertTrue(data.startswith(b"HTTP/1.1 200"))
self.assertIn(b'<canvas id="c"', data)
self.assertIn(b"Content-Length: %d\r\n" % len(data.split(b"\r\n\r\n", 1)[1]), data)

def test_the_page_is_titled_with_the_board_name(self):
from displaydev import remotedisplay

page = remotedisplay._titled(remotedisplay.PAGE, "kitchen-<panel>")
self.assertIn(b"<title>kitchen-&lt;panel&gt;</title>", page)
self.assertNotIn(b"<title>Display</title>", page)
self.assertEqual(remotedisplay._titled(remotedisplay.PAGE, None), remotedisplay.PAGE)
board = types.ModuleType("boarddev")
board.name = lambda: "omnipython-d0a23a"
with mock.patch.dict(sys.modules, {"boarddev": board}):
self.assertEqual(remotedisplay._board_name(), "omnipython-d0a23a")
name = remotedisplay._board_name()
want = b"<title>%s</title>" % (name.encode() if name else b"Display")
self.assertIn(want, _get_page(self.port), "the served page carries the board's name")

def test_frame_click_and_takeover(self):
a = _Client(self.port)
Expand Down