aboutsummaryrefslogtreecommitdiffstats
path: root/deckit
diff options
context:
space:
mode:
Diffstat (limited to 'deckit')
-rw-r--r--deckit/module.py3
-rw-r--r--deckit/server.py11
2 files changed, 10 insertions, 4 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):