aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--deckit/module.py3
-rw-r--r--deckit/server.py11
-rw-r--r--tests/test_server.py18
3 files changed, 27 insertions, 5 deletions
diff --git a/deckit/module.py b/deckit/module.py
index 9dad62a..9680c58 100644
--- a/deckit/module.py
+++ b/deckit/module.py
@@ -35,6 +35,9 @@ class Module:
title: str
state: Callable[[], dict]
actions: dict[str, Callable[[dict], dict | None]] = field(default_factory=dict)
+ # action -> (content type, max bytes): actions that take a raw body,
+ # such as a voice clip, instead of a small JSON object.
+ uploads: dict[str, tuple[str, int]] = field(default_factory=dict)
start: Callable[[], None] | None = None
stop: Callable[[], None] | None = None
diff --git a/deckit/server.py b/deckit/server.py
index 6bea422..f644d24 100644
--- a/deckit/server.py
+++ b/deckit/server.py
@@ -200,19 +200,22 @@ class Handler(BaseHTTPRequestHandler):
return self._send(404)
_, name, action = parts
try:
- fn = REGISTRY.get(name).actions.get(action)
+ mod_ = REGISTRY.get(name)
except module.ModuleError as e:
return self._send(404, str(e).encode())
+ fn = mod_.actions.get(action)
if fn is None:
return self._send(404, b"unknown action")
try:
length = int(self.headers.get("Content-Length", 0) or 0)
except ValueError:
return self._send(400, b"Invalid Content-Length")
- if self.headers.get("Content-Type") != "application/json" or not 0 < length <= 4096:
- return self._send(400, b"Expected a small JSON body")
+ ctype, limit = mod_.uploads.get(action, ("application/json", 4096))
+ if self.headers.get("Content-Type") != ctype or not 0 < length <= limit:
+ return self._send(400, b"Unexpected body")
+ data = self.rfile.read(length)
try:
- result = fn(json.loads(self.rfile.read(length)))
+ result = fn(data if action in mod_.uploads else json.loads(data))
except module.ModuleError as e:
return self._send(400, str(e).encode())
except (ValueError, AttributeError, TypeError, KeyError):
diff --git a/tests/test_server.py b/tests/test_server.py
index 7f2ee1c..0bec492 100644
--- a/tests/test_server.py
+++ b/tests/test_server.py
@@ -55,7 +55,9 @@ class Server(unittest.TestCase):
srv.REGISTRY = mod.Registry([
mod.Module("demo", "Demo", lambda: {"ok": True},
{"ping": lambda body: {"got": body.get("n")},
- "boom": lambda body: int(body.get("x"))}),
+ "boom": lambda body: int(body.get("x")),
+ "put": lambda data: {"len": len(data)}},
+ uploads={"put": ("audio/wav", 10)}),
])
cls.server = srv.UnixHTTPServer(cls.sock, srv.Handler)
threading.Thread(target=cls.server.serve_forever, daemon=True).start()
@@ -74,6 +76,20 @@ class Server(unittest.TestCase):
conn.close()
return r.status, data
+ def test_upload_takes_raw_bytes(self):
+ h = {**HOST, **ORIGIN, "Content-Type": "audio/wav"}
+ status, body = self.req("POST", "/api/demo/put", h, b"RIFF1234")
+ self.assertEqual((status, json.loads(body)), (200, {"len": 8}))
+
+ def test_upload_limits(self):
+ h = {**HOST, **ORIGIN, "Content-Type": "audio/wav"}
+ self.assertEqual(self.req("POST", "/api/demo/put", h, b"x" * 11)[0], 400)
+ self.assertEqual(self.req("POST", "/api/demo/put",
+ {**h, "Content-Type": "application/json"}, b"{}")[0], 400)
+ self.assertEqual(self.req("POST", "/api/demo/put",
+ {"Host": "127.0.0.1:8765", **ORIGIN, "Content-Type": "audio/wav"},
+ b"RIFF")[0], 401)
+
def test_assets_and_api(self):
self.assertEqual(self.req("GET", "/", HOST)[0], 200)
self.assertEqual(self.req("GET", "/theme.css", HOST)[0], 200)