The session-stream web app: a single HTTP listener (9988) that serves a page
per enabled session, keyed by a spoken phrase. Off by default, so a stream
exists only for the sessions it was enabled on.
src/hhstream_live/phrase.py four EFF-large words + a 5-char base-36 tail
(~77.5 bits); the phrase is the password
src/hhstream_live/publisher.py one capture thread + one frame slot per
output; one slot/no queue; capture paused at
zero viewers
src/hhstream_live/server.py the router: /, /<phrase>/, /stream.mjpg,
/frame.jpg, /status.json; unknown phrase is
an empty 404, rate-limited
src/hhstream_live/pages.py the built-in dark pages (no CDN, no build)
src/hhstream_live/__main__.py hhstream-live serve|phrase, standalone
The root page is a form, never a list: the phrase is known only by whoever
enabled the stream. Transport is MJPEG (multipart/x-mixed-replace), reusing
the JPEG grim already produces. Not yet wired to the broker -- that is
stage 2. Tests: tests/live (51).
166 lines
5.4 KiB
Python
166 lines
5.4 KiB
Python
"""The web app's obligations: the router, the pages, and MJPEG over one port."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
import time
|
|
|
|
from live_helpers import PHRASE, StubCapture, read_parts
|
|
from hhstream_live.server import BOUNDARY
|
|
|
|
WRONG = "no-such-phrase-here-00000"
|
|
|
|
|
|
def get(conn, path):
|
|
conn.request("GET", path)
|
|
resp = conn.getresponse()
|
|
body = resp.read()
|
|
return resp.status, resp, body
|
|
|
|
|
|
def test_landing_page_is_a_form_and_names_no_phrase(serve, http_conn):
|
|
with serve() as (server, _capture):
|
|
conn = http_conn(server)
|
|
status, resp, body = get(conn, "/")
|
|
assert status == 200
|
|
assert resp.getheader("Content-Type").startswith("text/html")
|
|
assert PHRASE not in body.decode()
|
|
assert "hh-demo" not in body.decode()
|
|
assert b"phrase" in body.lower()
|
|
|
|
|
|
def test_healthz(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, _, body = get(http_conn(server), "/healthz")
|
|
assert status == 200 and body.strip() == b"ok"
|
|
|
|
|
|
def test_unknown_phrase_is_404_with_an_empty_body(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, resp, body = get(http_conn(server), f"/{WRONG}/")
|
|
assert status == 404
|
|
assert body == b""
|
|
assert WRONG not in resp.getheader("Content-Type", "")
|
|
|
|
|
|
def test_a_wrong_subpath_under_a_valid_phrase_is_404(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, _, body = get(http_conn(server), f"/{PHRASE}/secrets.txt")
|
|
assert status == 404 and body == b""
|
|
|
|
|
|
def test_viewer_page_names_the_output_and_points_at_the_stream(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, _, body = get(http_conn(server), f"/{PHRASE}/")
|
|
assert status == 200
|
|
page = body.decode()
|
|
assert f"/{PHRASE}/stream.mjpg" in page
|
|
assert "hh-demo" in page
|
|
|
|
|
|
def test_phrase_is_case_insensitive(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, _, _ = get(http_conn(server), f"/{PHRASE.upper()}/")
|
|
assert status == 200
|
|
|
|
|
|
def test_status_json_shape(serve, http_conn):
|
|
import json
|
|
|
|
with serve() as (server, _):
|
|
status, resp, body = get(http_conn(server), f"/{PHRASE}/status.json")
|
|
assert status == 200
|
|
assert resp.getheader("Content-Type").startswith("application/json")
|
|
doc = json.loads(body)
|
|
assert doc["session"] == "demo"
|
|
assert doc["output"] == "hh-demo"
|
|
assert doc["fps"] == 5
|
|
assert doc["viewers"] == 0
|
|
assert doc["stopped"] is False
|
|
|
|
|
|
def test_snapshot_is_503_until_someone_has_watched(serve, http_conn):
|
|
with serve() as (server, _):
|
|
status, _, body = get(http_conn(server), f"/{PHRASE}/frame.jpg")
|
|
assert status == 503 and body == b""
|
|
|
|
|
|
def test_stream_is_mjpeg_and_delivers_valid_jpeg_parts(serve, http_conn):
|
|
with serve(fps=10) as (server, capture):
|
|
conn = http_conn(server)
|
|
conn.request("GET", f"/{PHRASE}/stream.mjpg")
|
|
resp = conn.getresponse()
|
|
assert resp.status == 200
|
|
assert resp.getheader("Content-Type") == (
|
|
f"multipart/x-mixed-replace; boundary={BOUNDARY}"
|
|
)
|
|
parts = read_parts(resp, 2)
|
|
assert len(parts) == 2
|
|
for part in parts:
|
|
assert part.startswith(b"\xff\xd8") # JPEG SOI
|
|
assert part.endswith(b"\xff\xd9") # JPEG EOI
|
|
conn.close()
|
|
|
|
|
|
def test_snapshot_works_after_a_viewer_has_pulled_a_frame(serve, http_conn):
|
|
with serve(fps=10) as (server, _):
|
|
stream_conn = http_conn(server)
|
|
stream_conn.request("GET", f"/{PHRASE}/stream.mjpg")
|
|
resp = stream_conn.getresponse()
|
|
assert read_parts(resp, 1)
|
|
stream_conn.close()
|
|
time.sleep(0.05)
|
|
|
|
status, ctype, body = get(http_conn(server), f"/{PHRASE}/frame.jpg")
|
|
assert status == 200
|
|
assert ctype.getheader("Content-Type") == "image/jpeg"
|
|
assert body.startswith(b"\xff\xd8")
|
|
|
|
|
|
def test_two_viewers_both_receive_frames(serve, http_conn):
|
|
with serve(fps=10) as (server, _):
|
|
results: dict[str, object] = {}
|
|
|
|
def watch(name: str) -> None:
|
|
conn = http_conn(server)
|
|
conn.request("GET", f"/{PHRASE}/stream.mjpg")
|
|
resp = conn.getresponse()
|
|
results[name] = read_parts(resp, 1)
|
|
conn.close()
|
|
|
|
threads = [threading.Thread(target=watch, args=(n,)) for n in ("a", "b")]
|
|
for t in threads:
|
|
t.start()
|
|
for t in threads:
|
|
t.join(timeout=10)
|
|
assert results["a"] and results["b"]
|
|
|
|
|
|
def test_no_capture_until_a_request_arrives(serve):
|
|
with serve() as (server, capture):
|
|
time.sleep(0.4)
|
|
assert capture.calls == 0
|
|
import http.client
|
|
|
|
conn = http.client.HTTPConnection("127.0.0.1", server.port, timeout=10)
|
|
conn.request("GET", f"/{PHRASE}/stream.mjpg")
|
|
resp = conn.getresponse()
|
|
assert read_parts(resp, 1)
|
|
conn.close()
|
|
assert capture.calls >= 1
|
|
|
|
|
|
def test_repeated_wrong_phrases_are_rate_limited(serve, http_conn):
|
|
with serve(limit=3) as (server, _):
|
|
conn = http_conn(server)
|
|
seen = [get(conn, f"/{WRONG}-{i}/")[0] for i in range(3)]
|
|
assert seen == [404, 404, 404]
|
|
assert get(conn, f"/{WRONG}-9/")[0] == 429
|
|
|
|
|
|
def test_the_server_binds_and_reports_its_port(serve):
|
|
with serve() as (server, _):
|
|
assert server.ready.is_set()
|
|
assert server.port > 0
|
|
assert server.url_for(PHRASE) == f"http://127.0.0.1:{server.port}/{PHRASE}/"
|