class Handler(BaseHTTPRequestHandler):
def send(self, code, body, ctype="application/json"):
data = body if isinstance(body, bytes) else (
json.dumps(body) if ctype == "application/json" else body).encode()
self.send_response(code)
self.send_header("Content-Type", ctype)
self.send_header("Content-Length", str(len(data)))
self.end_headers()
self.wfile.write(data)
def body(self):
raw = self.rfile.read(int(self.headers.get("Content-Length") or 0)).decode()
if raw.strip().startswith("{"):
return json.loads(raw)
return {k: v[0] for k, v in urllib.parse.parse_qs(raw).items()}
def authed(self):
tok = self.headers.get("Authorization", "")[len("Bearer "):]
if tok in TOKENS:
return True
self.send(401, {"error": "invalid_token"})
return False