forked from mtusi2026/mtusi2026lab4
344 lines
14 KiB
Python
344 lines
14 KiB
Python
import base64
|
|
import json
|
|
import logging
|
|
import os
|
|
import re
|
|
import signal
|
|
import threading
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from urllib.parse import parse_qs, unquote, urlsplit
|
|
from uuid import uuid4
|
|
|
|
from attachments import Attachments, S3_ERRORS
|
|
from storage import DB_UNAVAILABLE, Store, json_value, unauthorized, unavailable
|
|
from validation import ApiError, canonical_id, invalid, not_found, require_fields, require_text
|
|
|
|
CHAT_PATH = re.compile(r"/api/v1/chats/([^/]+)(?:/(messages|attachments))?")
|
|
ATTACHMENT_PATH = re.compile(r"/api/v1/attachments/([^/]+)(/content)?")
|
|
MAX_JSON_BYTES = 64 * 1024
|
|
LOG = logging.getLogger("messenger")
|
|
|
|
|
|
def unique_object(pairs):
|
|
result = dict(pairs)
|
|
if len(result) != len(pairs):
|
|
raise ValueError
|
|
return result
|
|
|
|
|
|
def page_query(query):
|
|
try:
|
|
params = parse_qs(query, keep_blank_values=True, max_num_fields=16)
|
|
except ValueError:
|
|
raise invalid("Invalid query") from None
|
|
limits = params.get("limit", ["50"])
|
|
cursors = params.get("cursor", [None])
|
|
if len(limits) != 1 or not re.fullmatch(r"[0-9]{1,3}", limits[0]):
|
|
raise invalid("Invalid limit")
|
|
limit = int(limits[0])
|
|
if not 1 <= limit <= 100:
|
|
raise invalid("Invalid limit")
|
|
if len(cursors) != 1:
|
|
raise invalid("Invalid cursor")
|
|
return limit, cursors[0]
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_GET(self):
|
|
self.handle_api()
|
|
|
|
def do_POST(self):
|
|
self.handle_api()
|
|
|
|
def do_DELETE(self):
|
|
self.handle_api()
|
|
|
|
def log_message(self, format, *args):
|
|
pass
|
|
|
|
def log_request(self, code="-", size="-"):
|
|
LOG.info("request_id=%s status=%s", self.request_id, code)
|
|
|
|
def send_error(self, code, message=None, explain=None):
|
|
self.request_id = uuid4().hex
|
|
self.response_headers = {}
|
|
self.send_json(code, {"error": {
|
|
"code": "invalid_request",
|
|
"message": "Invalid HTTP request",
|
|
"request_id": self.request_id,
|
|
}})
|
|
|
|
def handle_api(self):
|
|
self.request_id = uuid4().hex
|
|
self.response_headers = {}
|
|
try:
|
|
try:
|
|
status, payload = self.route()
|
|
except ApiError as error:
|
|
status, payload = self.error_payload(error)
|
|
except DB_UNAVAILABLE:
|
|
status, payload = self.error_payload(unavailable())
|
|
except (TimeoutError, ConnectionError):
|
|
return
|
|
except Exception:
|
|
LOG.error("request_id=%s unexpected error", self.request_id)
|
|
status, payload = self.error_payload(
|
|
ApiError(500, "internal_error", "Internal server error")
|
|
)
|
|
if isinstance(payload, tuple):
|
|
self.send_attachment(*payload)
|
|
else:
|
|
self.send_json(status, payload)
|
|
except (TimeoutError, ConnectionError, *S3_ERRORS):
|
|
return
|
|
|
|
def error_payload(self, error):
|
|
self.response_headers = error.headers
|
|
return error.status, {"error": {
|
|
"code": error.code,
|
|
"message": str(error),
|
|
"request_id": self.request_id,
|
|
}}
|
|
|
|
def send_json(self, status, payload):
|
|
data = b"" if status == 204 else json.dumps(payload, ensure_ascii=False, default=json_value).encode("utf-8")
|
|
self.send_response(status)
|
|
self.send_header("X-Request-Id", self.request_id)
|
|
self.send_header("X-Instance-Id", self.server.instance_id)
|
|
if status != 204:
|
|
self.send_header("Content-Type", "application/json")
|
|
for name, value in self.response_headers.items():
|
|
self.send_header(name, value)
|
|
self.send_header("Content-Length", str(len(data)))
|
|
self.end_headers()
|
|
if self.command != "HEAD":
|
|
self.wfile.write(data)
|
|
|
|
def send_attachment(self, stream, attachment):
|
|
with stream:
|
|
self.send_response(200)
|
|
self.send_header("X-Request-Id", self.request_id)
|
|
self.send_header("X-Instance-Id", self.server.instance_id)
|
|
self.send_header("Content-Type", attachment["content_type"])
|
|
self.send_header("Content-Length", str(attachment["size_bytes"]))
|
|
self.end_headers()
|
|
for chunk in stream.iter_chunks(64 * 1024):
|
|
self.wfile.write(chunk)
|
|
|
|
def read_json(self):
|
|
lengths = self.headers.get_all("Content-Length", [])
|
|
if (
|
|
self.headers.get("Transfer-Encoding")
|
|
or len(lengths) != 1
|
|
or not re.fullmatch(r"[0-9]{1,10}", lengths[0])
|
|
or self.headers.get_content_type() != "application/json"
|
|
):
|
|
raise invalid("Expected a JSON body")
|
|
length = int(lengths[0])
|
|
if length > MAX_JSON_BYTES:
|
|
raise invalid("JSON body is too large")
|
|
try:
|
|
data = self.rfile.read(length)
|
|
if len(data) != length:
|
|
raise ValueError
|
|
return json.loads(data.decode("utf-8"), object_pairs_hook=unique_object)
|
|
except (ValueError, RecursionError):
|
|
raise invalid("Invalid JSON") from None
|
|
|
|
def route(self):
|
|
try:
|
|
target = urlsplit(self.path)
|
|
except ValueError:
|
|
raise invalid("Invalid request path") from None
|
|
path, method = target.path, self.command
|
|
app = self.server.app
|
|
|
|
if method == "GET" and path == "/health/live":
|
|
return 200, {"status": "ok"}
|
|
if method == "GET" and path == "/health/ready":
|
|
app.ready()
|
|
self.server.attachments.ready()
|
|
return 200, {"status": "ok"}
|
|
if method == "POST" and path == "/api/v1/users":
|
|
body = self.read_json()
|
|
require_fields(body, "username", "display_name", "password")
|
|
username = body["username"]
|
|
if not isinstance(username, str) or not re.fullmatch(r"[a-z0-9_]{3,32}", username):
|
|
raise invalid("Invalid username")
|
|
password = body["password"]
|
|
if not isinstance(password, str) or not 12 <= len(password) <= 128:
|
|
raise invalid("Invalid password")
|
|
try:
|
|
password.encode("utf-8")
|
|
except UnicodeError:
|
|
raise invalid("Invalid password") from None
|
|
return 201, app.register(username, require_text(body["display_name"], 100), password)
|
|
if method == "POST" and path == "/api/v1/auth/sessions":
|
|
return 201, app.login(*self.basic_credentials())
|
|
|
|
match = CHAT_PATH.fullmatch(path)
|
|
attachment_match = ATTACHMENT_PATH.fullmatch(path)
|
|
if path not in ("/api/v1/users/me", "/api/v1/chats", "/api/v1/auth/sessions/current") and not (match or attachment_match):
|
|
raise not_found()
|
|
header = self.headers.get("Authorization", "")
|
|
bearer = re.fullmatch(r"(?i:Bearer) ([A-Za-z0-9_-]{43})", header)
|
|
if not bearer:
|
|
raise unauthorized("Bearer")
|
|
digest = app.token_hash(bearer[1])
|
|
actor = app.actor(digest)
|
|
if path == "/api/v1/auth/sessions/current" and method == "DELETE":
|
|
app.logout(digest)
|
|
return 204, None
|
|
if path == "/api/v1/users/me" and method == "GET":
|
|
return 200, app.user(actor)
|
|
if path == "/api/v1/chats":
|
|
if method == "POST":
|
|
body = self.read_json()
|
|
require_fields(body, "title", "member_ids")
|
|
title = require_text(body["title"], 100)
|
|
members = body["member_ids"]
|
|
if not isinstance(members, list) or not 1 <= len(members) <= 100:
|
|
raise invalid("Invalid member list")
|
|
members = [canonical_id(member) for member in members]
|
|
if actor not in members or len(set(members)) != len(members):
|
|
raise invalid("Invalid member list")
|
|
return 201, app.create_chat(title, members)
|
|
if method == "GET":
|
|
return 200, app.chat_page(actor, *page_query(target.query))
|
|
if attachment_match and method == "GET":
|
|
attachment_id = canonical_id(unquote(attachment_match[1]))
|
|
attachment = self.server.attachments.get(actor, attachment_id)
|
|
if not attachment_match[2]:
|
|
return 200, attachment
|
|
params = self.attachment_query(target.query)
|
|
if params.get("variant", ["original"]) != ["original"]:
|
|
raise not_found()
|
|
return 200, (self.server.attachments.download(attachment), attachment)
|
|
if match:
|
|
chat_id = canonical_id(unquote(match[1]))
|
|
chat = app.chat(actor, chat_id)
|
|
if match[2] == "attachments" and method == "POST":
|
|
filenames = self.attachment_query(target.query).get("filename", [])
|
|
if len(filenames) != 1:
|
|
raise invalid("Filename required")
|
|
return 201, self.server.attachments.upload(self, actor, chat_id, filenames[0])
|
|
if match[2] == "messages":
|
|
if method == "POST":
|
|
body = self.read_json()
|
|
if not isinstance(body, dict) or "text" not in body or body.keys() - {"text", "attachment_ids"}:
|
|
raise invalid("Invalid request fields")
|
|
attachment_ids = body.get("attachment_ids", [])
|
|
if not isinstance(attachment_ids, list) or len(attachment_ids) > 10:
|
|
raise invalid("Invalid attachment IDs")
|
|
attachment_ids = [canonical_id(item) for item in attachment_ids]
|
|
if len(set(attachment_ids)) != len(attachment_ids):
|
|
raise invalid("Duplicate attachment IDs")
|
|
text = body["text"]
|
|
if text != "" or not attachment_ids:
|
|
text = require_text(text, 4000)
|
|
key = self.headers.get("Idempotency-Key", "")
|
|
if not re.fullmatch(r"[A-Za-z0-9._:-]{1,128}", key):
|
|
raise invalid("Invalid Idempotency-Key")
|
|
return 201, app.send(actor, chat_id, text, key, attachment_ids)
|
|
if method == "GET":
|
|
return 200, app.message_page(chat_id, *page_query(target.query))
|
|
elif match[2] is None and method == "GET":
|
|
return 200, chat
|
|
raise not_found()
|
|
|
|
@staticmethod
|
|
def attachment_query(query):
|
|
try:
|
|
return parse_qs(query, keep_blank_values=True, max_num_fields=16, errors="strict")
|
|
except ValueError:
|
|
raise invalid("Invalid query") from None
|
|
|
|
def basic_credentials(self):
|
|
if self.headers.get("Content-Length", "0") != "0" or self.headers.get("Transfer-Encoding"):
|
|
raise invalid("Login does not accept a body")
|
|
match = re.fullmatch(r"(?i:Basic) ([A-Za-z0-9+/=]{1,800})",
|
|
self.headers.get("Authorization", ""))
|
|
if not match:
|
|
raise unauthorized("Basic")
|
|
try:
|
|
value = base64.b64decode(match[1], validate=True).decode("utf-8")
|
|
except ValueError:
|
|
raise unauthorized("Basic") from None
|
|
username, separator, password = value.partition(":")
|
|
if not separator or not re.fullmatch(r"[a-z0-9_]{3,32}", username) or not 12 <= len(password) <= 128:
|
|
raise unauthorized("Basic")
|
|
return username, password
|
|
|
|
|
|
class Server(ThreadingHTTPServer):
|
|
timeout = 0.25
|
|
request_queue_size = 64
|
|
|
|
def __init__(self, address):
|
|
self.app = Store()
|
|
self.attachments = Attachments(self.app)
|
|
self.instance_id = os.getenv("INSTANCE_ID") or uuid4().hex
|
|
self.stopping = False
|
|
self.active = 0
|
|
self.workers = threading.Condition()
|
|
super().__init__(address, Handler)
|
|
|
|
def get_request(self):
|
|
connection, address = super().get_request()
|
|
connection.settimeout(4)
|
|
return connection, address
|
|
|
|
def verify_request(self, request, client_address):
|
|
return not self.stopping
|
|
|
|
def process_request(self, request, client_address):
|
|
with self.workers:
|
|
self.active += 1
|
|
try:
|
|
super().process_request(request, client_address)
|
|
except Exception:
|
|
self.worker_done()
|
|
raise
|
|
|
|
def process_request_thread(self, request, client_address):
|
|
try:
|
|
super().process_request_thread(request, client_address)
|
|
finally:
|
|
self.worker_done()
|
|
|
|
def worker_done(self):
|
|
with self.workers:
|
|
self.active -= 1
|
|
self.workers.notify_all()
|
|
|
|
def handle_error(self, request, client_address):
|
|
LOG.error("HTTP connection handler failed")
|
|
|
|
def stop(self, signum, frame):
|
|
self.stopping = True
|
|
|
|
def run(self):
|
|
while not self.stopping:
|
|
self.handle_request()
|
|
self.server_close()
|
|
with self.workers:
|
|
self.workers.wait_for(lambda: self.active == 0, timeout=5)
|
|
self.app.close()
|
|
|
|
|
|
def main():
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
host = os.getenv("APP_HOST", "0.0.0.0")
|
|
port = int(os.getenv("APP_PORT", "8080"))
|
|
environment = os.getenv("APP_ENV", "lab")
|
|
with Server((host, port)) as server:
|
|
signal.signal(signal.SIGTERM, server.stop)
|
|
signal.signal(signal.SIGINT, server.stop)
|
|
LOG.info("started host=%s port=%s env=%s", host, server.server_port, environment)
|
|
server.run()
|
|
LOG.info("stopped")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|