Files
mtusi2026lab6/src/handlers.py
T
2026-11-04 12:18:07 +03:00

634 lines
28 KiB
Python

import base64
import hashlib
import json
import logging
import threading
import time
import tempfile
import uuid
import warnings
import zlib
from http.server import BaseHTTPRequestHandler
from typing import Any, BinaryIO, Dict, List, Optional, Tuple
from urllib.parse import parse_qs, urlsplit
import unicodedata
import psycopg
from opentelemetry import propagate
from opentelemetry.instrumentation.utils import suppress_instrumentation
from opentelemetry.trace import SpanKind, Status, StatusCode
from botocore.exceptions import BotoCoreError, ClientError
from PIL import Image, UnidentifiedImageError
from psycopg_pool import PoolTimeout
from .config import (
IDEMPOTENCY_KEY_RE,
INSTANCE_ID,
MAX_CONCURRENT_UPLOADS,
MAX_IMAGE_PIXELS,
MAX_INFLIGHT_REQUESTS,
MAX_REQUEST_BODY_BYTES,
MAX_UPLOAD_BYTES,
UPLOAD_TIMEOUT_SECONDS,
USER_RE,
)
from .errors import (
ApiError,
AttachmentNotReadyError,
CacheUnavailableError,
IdempotencyConflictError,
ObjectMissingError,
)
from .security import hash_password, isoformat, valid_uuid
from .store import Store
from .telemetry import REQUEST_ID, record_http_request, tracer
LOGGER = logging.getLogger("messenger.http")
class RequestGuard:
def __init__(self):
self.lock = threading.Lock()
self.inflight = 0
def acquire(self):
with self.lock:
if self.inflight >= MAX_INFLIGHT_REQUESTS:
raise ApiError(503, "unavailable", "Server is busy")
self.inflight += 1
def release(self):
with self.lock:
self.inflight = max(0, self.inflight - 1)
REQUEST_GUARD = RequestGuard()
UPLOAD_GUARD = threading.BoundedSemaphore(MAX_CONCURRENT_UPLOADS)
IMAGE_DECODE_GUARD = threading.BoundedSemaphore(1)
class MessengerHandler(BaseHTTPRequestHandler):
def __init__(self, request, client_address, server, store: Store):
self.store = store
super().__init__(request, client_address, server)
def setup(self):
super().setup()
self.connection.settimeout(15)
def log_message(self, format: str, *args):
return
def do_GET(self):
self._dispatch("GET")
def do_POST(self):
self._dispatch("POST")
def do_DELETE(self):
self._dispatch("DELETE")
def _dispatch(self, method: str):
path = urlsplit(self.path).path
route = self._route_template(path)
request_id = self.headers.get("X-Request-Id") or str(uuid.uuid4())
parent_context = propagate.extract({key.lower(): value for key, value in self.headers.items()})
started = time.perf_counter()
token = REQUEST_ID.set(request_id)
self._response_status = 500
if route.startswith("/health/"):
try:
self._dispatch_request(method, request_id)
finally:
record_http_request(method, route, self._response_status, time.perf_counter() - started)
REQUEST_ID.reset(token)
return
with tracer().start_as_current_span(
f"{method} {route}",
context=parent_context,
kind=SpanKind.SERVER,
attributes={
"http.request.method": method,
"http.route": route,
"http.request_id": request_id,
},
) as span:
try:
self._dispatch_request(method, request_id)
finally:
duration = time.perf_counter() - started
span.set_attribute("http.response.status_code", self._response_status)
if self._response_status >= 500:
span.set_status(Status(StatusCode.ERROR))
record_http_request(method, route, self._response_status, duration)
REQUEST_ID.reset(token)
@staticmethod
def _route_template(path: str) -> str:
if path in ("/health/live", "/health/ready", "/api/v1/users", "/api/v1/auth/sessions",
"/api/v1/auth/sessions/current", "/api/v1/users/me", "/api/v1/chats"):
return path
if path.startswith("/api/v1/chats/") and path.endswith("/attachments"):
return "/api/v1/chats/{chat_id}/attachments"
if path.startswith("/api/v1/chats/") and path.endswith("/messages"):
return "/api/v1/chats/{chat_id}/messages"
if path.startswith("/api/v1/chats/"):
return "/api/v1/chats/{chat_id}"
if path.startswith("/api/v1/attachments/") and path.endswith("/content"):
return "/api/v1/attachments/{attachment_id}/content"
if path.startswith("/api/v1/attachments/"):
return "/api/v1/attachments/{attachment_id}"
return "unmatched"
def _dispatch_request(self, method: str, request_id: str):
acquired = False
try:
REQUEST_GUARD.acquire()
acquired = True
parsed = urlsplit(self.path)
path = parsed.path
query = parse_qs(parsed.query, keep_blank_values=True)
if path == "/health/live" and method == "GET":
self._send_json(200, {"status": "ok"}, request_id)
elif path == "/health/ready" and method == "GET":
try:
with suppress_instrumentation():
self.store.ready()
except (psycopg.Error, PoolTimeout, OSError, BotoCoreError, ClientError):
raise ApiError(503, "unavailable", "A required storage service is unavailable")
self._send_json(200, {"status": "ok"}, request_id)
elif path == "/api/v1/users" and method == "POST":
self._handle_register(request_id)
elif path == "/api/v1/auth/sessions" and method == "POST":
self._handle_create_session(request_id)
elif path == "/api/v1/auth/sessions/current" and method == "DELETE":
self._handle_revoke_session(request_id)
elif path == "/api/v1/users/me" and method == "GET":
self._send_json(200, self._get_actor(request_id), request_id)
elif path == "/api/v1/chats" and method == "POST":
self._handle_create_chat(request_id)
elif path == "/api/v1/chats" and method == "GET":
self._handle_list_chats(query, request_id)
elif path.startswith("/api/v1/chats/") and path.endswith("/attachments") and method == "POST":
chat_id = path.rsplit("/", 2)[1]
self._handle_upload_attachment(chat_id, query, request_id)
elif path.startswith("/api/v1/attachments/") and method == "GET":
attachment_path = path.removeprefix("/api/v1/attachments/")
if attachment_path.endswith("/content"):
attachment_id = attachment_path.removesuffix("/content")
self._handle_download_attachment(attachment_id, query, request_id)
else:
self._handle_get_attachment(attachment_path, request_id)
elif path.startswith("/api/v1/chats/") and path.endswith("/messages") and method in ("GET", "POST"):
chat_id = path.rsplit("/", 2)[1]
if method == "POST":
self._handle_create_message(chat_id, request_id)
else:
self._handle_list_messages(chat_id, query, request_id)
elif path.startswith("/api/v1/chats/") and method == "GET":
self._handle_get_chat(path.rsplit("/", 1)[1], request_id)
else:
raise ApiError(404, "not_found", "Object not found")
except ApiError as exc:
self._send_error(exc.status, exc.code, exc.message, request_id, exc.headers)
except CacheUnavailableError:
self._send_error(503, "unavailable", "Session cache is unavailable", request_id)
except IdempotencyConflictError:
self._send_error(409, "conflict", "Idempotency-Key was already used with another request", request_id)
except AttachmentNotReadyError:
self._send_error(409, "conflict", "Attachment is not ready", request_id)
except ObjectMissingError:
LOGGER.error("attachment object missing for request %s", request_id)
self._send_error(503, "unavailable", "Attachment content is temporarily unavailable", request_id)
except (BotoCoreError, ClientError):
LOGGER.warning("object storage unavailable for request %s", request_id)
self._send_error(503, "unavailable", "Object storage is unavailable", request_id)
except (PoolTimeout, psycopg.OperationalError, psycopg.InterfaceError):
self._send_error(503, "unavailable", "Database is unavailable", request_id)
except psycopg.errors.UniqueViolation:
self._send_error(409, "conflict", "Username already exists", request_id)
except psycopg.errors.ForeignKeyViolation:
self._send_error(400, "invalid_request", "Unknown member", request_id)
except (BrokenPipeError, ConnectionResetError, TimeoutError):
self._set_response_status(499)
self.close_connection = True
LOGGER.info("client disconnected before response for request %s", request_id)
except Exception as exc:
LOGGER.error("request %s failed with %s", request_id, type(exc).__name__)
self._send_error(500, "internal_error", "Internal server error", request_id)
finally:
if acquired:
REQUEST_GUARD.release()
@staticmethod
def _trace_context():
carrier = {}
propagate.inject(carrier)
return {"traceparent": carrier.get("traceparent"), "tracestate": carrier.get("tracestate")}
def _set_response_status(self, status: int) -> None:
self._response_status = status
def _read_json(self, request_id: str) -> Any:
if self.headers.get_content_type() != "application/json":
raise ApiError(415, "unsupported_media_type", "Content-Type must be application/json")
length = self.headers.get("Content-Length")
if length is None:
raise ApiError(400, "invalid_request", "Missing Content-Length")
try:
size = int(length)
except ValueError:
raise ApiError(400, "invalid_request", "Invalid Content-Length")
if size < 0:
raise ApiError(400, "invalid_request", "Invalid Content-Length")
if size > MAX_REQUEST_BODY_BYTES:
raise ApiError(413, "payload_too_large", "Request body too large")
raw = self.rfile.read(size)
if not raw:
raise ApiError(400, "invalid_request", "Empty body")
try:
return json.loads(raw.decode("utf-8"))
except (UnicodeDecodeError, json.JSONDecodeError):
raise ApiError(400, "invalid_request", "Malformed JSON")
def _get_actor(self, request_id: str) -> Dict[str, Any]:
header = self.headers.get("Authorization", "")
scheme, separator, token = header.partition(" ")
if scheme.lower() != "bearer" or not separator or not token or " " in token:
raise ApiError(
401,
"unauthorized",
"Authentication required",
{"WWW-Authenticate": 'Bearer realm="messenger"'},
)
user = self.store.session_user(token)
if user is None:
raise ApiError(
401,
"unauthorized",
"Authentication required",
{"WWW-Authenticate": 'Bearer realm="messenger"'},
)
return user
def _handle_register(self, request_id: str):
body = self._read_json(request_id)
if not isinstance(body, dict) or set(body) != {"username", "display_name", "password"}:
raise ApiError(400, "invalid_request", "Expected username, display_name and password")
username = body["username"]
display_name = body["display_name"]
password = body["password"]
if not isinstance(username, str) or not USER_RE.fullmatch(username):
raise ApiError(400, "invalid_request", "Invalid username")
if not isinstance(display_name, str) or not display_name.strip() or len(display_name) > 100:
raise ApiError(400, "invalid_request", "Invalid display_name")
if not isinstance(password, str) or not (12 <= len(password) <= 128):
raise ApiError(400, "invalid_request", "Invalid password")
user = self.store.create_user(username, display_name, hash_password(password))
self._send_json(201, user, request_id)
def _basic_credentials(self, request_id: str) -> Tuple[str, str]:
value = self.headers.get("Authorization", "")
scheme, separator, encoded = value.partition(" ")
challenge = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
if scheme.lower() != "basic" or not separator or not encoded:
raise ApiError(401, "unauthorized", "Invalid credentials", challenge)
try:
decoded = base64.b64decode(encoded, validate=True).decode("utf-8")
username, password = decoded.split(":", 1)
except (ValueError, UnicodeError):
raise ApiError(401, "unauthorized", "Invalid credentials", challenge)
return username, password
def _handle_create_session(self, request_id: str):
username, password = self._basic_credentials(request_id)
user = self.store.authenticate(username, password)
if user is None:
raise ApiError(
401,
"unauthorized",
"Invalid credentials",
{"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
)
token, expires_at = self.store.create_session(user["id"])
self._send_json(
201,
{"token": token, "token_type": "Bearer", "expires_at": isoformat(expires_at), "user": user},
request_id,
)
def _handle_revoke_session(self, request_id: str):
self._get_actor(request_id)
token = self.headers["Authorization"].partition(" ")[2]
if not self.store.revoke_session(token):
raise ApiError(
401,
"unauthorized",
"Authentication required",
{"WWW-Authenticate": 'Bearer realm="messenger"'},
)
self._set_response_status(204)
self.send_response(204)
self.send_header("X-Request-Id", request_id)
self.send_header("X-Instance-Id", INSTANCE_ID)
self.end_headers()
def _handle_create_chat(self, request_id: str):
actor = self._get_actor(request_id)
body = self._read_json(request_id)
if not isinstance(body, dict) or set(body) != {"title", "member_ids"}:
raise ApiError(400, "invalid_request", "Expected title and member_ids")
title, member_ids = body["title"], body["member_ids"]
if not isinstance(title, str) or not title.strip() or len(title) > 100:
raise ApiError(400, "invalid_request", "Invalid title")
if (
not isinstance(member_ids, list)
or not 1 <= len(member_ids) <= 100
or any(not isinstance(item, str) or not valid_uuid(item) for item in member_ids)
or len(member_ids) != len(set(member_ids))
or actor["id"] not in member_ids
):
raise ApiError(400, "invalid_request", "Invalid member_ids")
chat = self.store.create_chat(title, member_ids, actor["id"])
self._send_json(201, chat, request_id)
def _handle_list_chats(self, query: Dict[str, List[str]], request_id: str):
actor = self._get_actor(request_id)
try:
limit = int(query.get("limit", ["50"])[0])
except ValueError:
raise ApiError(400, "invalid_request", "Invalid limit")
if not 1 <= limit <= 100:
raise ApiError(400, "invalid_request", "Invalid limit")
cursor = query.get("cursor", [None])[0]
try:
chats, next_cursor = self.store.list_chats(actor["id"], limit, cursor)
except ValueError:
raise ApiError(400, "invalid_request", "Invalid cursor")
self._send_json(200, {"items": chats, "next_cursor": next_cursor}, request_id)
def _handle_get_chat(self, chat_id: str, request_id: str):
actor = self._get_actor(request_id)
if not valid_uuid(chat_id):
raise ApiError(400, "invalid_request", "Invalid chat id")
chat = self.store.get_chat(chat_id, actor["id"])
if chat is None:
raise ApiError(404, "not_found", "Chat not found")
self._send_json(200, chat, request_id)
def _handle_create_message(self, chat_id: str, request_id: str):
actor = self._get_actor(request_id)
if not valid_uuid(chat_id):
raise ApiError(400, "invalid_request", "Invalid chat id")
chat_id = str(uuid.UUID(chat_id))
idempotency_key = self.headers.get("Idempotency-Key", "")
if not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key):
raise ApiError(400, "invalid_request", "Invalid or missing Idempotency-Key")
body = self._read_json(request_id)
if not isinstance(body, dict) or set(body).difference({"text", "attachment_ids"}) or "text" not in body:
raise ApiError(400, "invalid_request", "Expected text and optional attachment_ids")
text = body["text"]
attachment_ids = body.get("attachment_ids", [])
if (
not isinstance(text, str)
or len(text) > 4000
or not isinstance(attachment_ids, list)
or len(attachment_ids) > 10
or any(not isinstance(item, str) or not valid_uuid(item) for item in attachment_ids)
or len(attachment_ids) != len(set(attachment_ids))
or (not text.strip() and not attachment_ids)
):
raise ApiError(400, "invalid_request", "Invalid message text")
attachment_ids = [str(uuid.UUID(item)) for item in attachment_ids]
canonical_body = json.dumps(
{"text": text, "attachment_ids": attachment_ids},
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
)
message = self.store.create_idempotent_message(
chat_id,
actor["id"],
text,
attachment_ids,
idempotency_key,
canonical_body,
)
if message is None:
raise ApiError(404, "not_found", "Chat not found")
self._send_json(201, message, request_id)
def _read_image(self, request_id: str, declared_type: str) -> Tuple[BinaryIO, str, int, str]:
if declared_type not in ("image/png", "image/jpeg"):
raise ApiError(415, "unsupported_media_type", "Only PNG and JPEG are supported")
length = self.headers.get("Content-Length")
if length is None or self.headers.get("Transfer-Encoding"):
raise ApiError(400, "invalid_request", "A bounded Content-Length is required")
try:
size = int(length)
except ValueError:
raise ApiError(400, "invalid_request", "Invalid Content-Length")
if size <= 0:
raise ApiError(415, "unsupported_media_type", "Image is empty")
if size > MAX_UPLOAD_BYTES:
self.close_connection = True
remaining = min(size, MAX_UPLOAD_BYTES + 1)
deadline = time.monotonic() + UPLOAD_TIMEOUT_SECONDS
while remaining:
remaining_time = deadline - time.monotonic()
if remaining_time <= 0:
break
self.connection.settimeout(min(15, remaining_time))
try:
chunk = self.rfile.read(min(64 * 1024, remaining))
except (TimeoutError, OSError):
LOGGER.info("stopped bounded drain of oversized upload")
break
if not chunk:
break
remaining -= len(chunk)
raise ApiError(413, "payload_too_large", "Image exceeds the 10 MiB limit")
content = tempfile.SpooledTemporaryFile(max_size=1024 * 1024, mode="w+b")
digest = hashlib.sha256()
bytes_read = 0
deadline = time.monotonic() + UPLOAD_TIMEOUT_SECONDS
try:
while bytes_read < size:
remaining_time = deadline - time.monotonic()
if remaining_time <= 0:
raise ApiError(408, "invalid_request", "Image upload timed out")
self.connection.settimeout(min(15, remaining_time))
chunk = self.rfile.read(min(64 * 1024, size - bytes_read))
if not chunk:
raise ApiError(400, "invalid_request", "Incomplete image body")
content.write(chunk)
digest.update(chunk)
bytes_read += len(chunk)
except (TimeoutError, OSError):
content.close()
raise ApiError(400, "invalid_request", "Incomplete image body") from None
try:
content.seek(0)
with warnings.catch_warnings():
warnings.simplefilter("error", Image.DecompressionBombWarning)
with Image.open(content) as image:
actual_type = {"PNG": "image/png", "JPEG": "image/jpeg"}.get(image.format or "")
if actual_type is None or actual_type != declared_type:
raise ApiError(415, "unsupported_media_type", "Image content does not match Content-Type")
width, height = image.size
if width <= 0 or height <= 0 or width * height > MAX_IMAGE_PIXELS:
raise ApiError(413, "payload_too_large", "Image exceeds the 40 million pixel limit")
image.verify()
content.seek(0)
with IMAGE_DECODE_GUARD:
with warnings.catch_warnings():
warnings.simplefilter("error", Image.DecompressionBombWarning)
with Image.open(content) as image:
image.load()
content.seek(0)
return content, actual_type, size, digest.hexdigest()
except ApiError:
content.close()
raise
except (Image.DecompressionBombError, Image.DecompressionBombWarning):
content.close()
raise ApiError(413, "payload_too_large", "Image exceeds the 40 million pixel limit") from None
except (UnidentifiedImageError, OSError, SyntaxError, ValueError, zlib.error):
content.close()
raise ApiError(415, "unsupported_media_type", "Image is damaged or unsupported") from None
@staticmethod
def _valid_filename(filename: str) -> bool:
return (
1 <= len(filename) <= 255
and not any(char in ("/", "\\", "\x00") for char in filename)
and not any(unicodedata.category(char) == "Cc" for char in filename)
)
def _handle_upload_attachment(
self, chat_id: str, query: Dict[str, List[str]], request_id: str
):
actor = self._get_actor(request_id)
if not valid_uuid(chat_id):
raise ApiError(400, "invalid_request", "Invalid chat id")
chat_id = str(uuid.UUID(chat_id))
if not self.store.can_access_chat(chat_id, actor["id"]):
raise ApiError(404, "not_found", "Chat not found")
filenames = query.get("filename", [])
if len(filenames) != 1 or not self._valid_filename(filenames[0]):
raise ApiError(400, "invalid_request", "Invalid filename")
if not UPLOAD_GUARD.acquire(blocking=False):
raise ApiError(503, "unavailable", "Upload capacity is temporarily exhausted")
try:
content, content_type, size, digest = self._read_image(request_id, self.headers.get_content_type())
try:
attachment = self.store.create_attachment(
chat_id,
actor["id"],
filenames[0],
content_type,
content,
size,
digest,
**self._trace_context(),
)
finally:
content.close()
finally:
UPLOAD_GUARD.release()
if attachment is None:
raise ApiError(404, "not_found", "Chat not found")
self._send_json(202, attachment, request_id)
def _handle_get_attachment(self, attachment_id: str, request_id: str):
actor = self._get_actor(request_id)
if not valid_uuid(attachment_id):
raise ApiError(400, "invalid_request", "Invalid attachment id")
attachment = self.store.get_attachment(str(uuid.UUID(attachment_id)), actor["id"])
if attachment is None:
raise ApiError(404, "not_found", "Attachment not found")
self._send_json(200, attachment, request_id)
def _handle_download_attachment(
self, attachment_id: str, query: Dict[str, List[str]], request_id: str
):
actor = self._get_actor(request_id)
if not valid_uuid(attachment_id):
raise ApiError(400, "invalid_request", "Invalid attachment id")
variants = query.get("variant", ["original"])
if len(variants) != 1 or variants[0] not in ("original", "thumbnail", "crop", "optimized"):
raise ApiError(400, "invalid_request", "Invalid attachment variant")
variant = variants[0]
if variant != "original" and variant not in ("thumbnail", "crop", "optimized"):
raise ApiError(404, "not_found", "Attachment variant not found")
result = self.store.download_attachment(str(uuid.UUID(attachment_id)), actor["id"], variant)
if result is None:
raise ApiError(404, "not_found", "Attachment not found")
content_type, content = result
self._set_response_status(200)
self.send_response(200)
self.send_header("Content-Type", content_type)
self.send_header("Content-Length", str(len(content)))
self.send_header("X-Request-Id", request_id)
self.send_header("X-Instance-Id", INSTANCE_ID)
self.send_header("X-Content-Type-Options", "nosniff")
self.send_header("Cache-Control", "private, no-store")
self.end_headers()
self.wfile.write(content)
def _handle_list_messages(self, chat_id: str, query: Dict[str, List[str]], request_id: str):
actor = self._get_actor(request_id)
if not valid_uuid(chat_id):
raise ApiError(400, "invalid_request", "Invalid chat id")
try:
limit = int(query.get("limit", ["50"])[0])
except ValueError:
raise ApiError(400, "invalid_request", "Invalid limit")
if not 1 <= limit <= 100:
raise ApiError(400, "invalid_request", "Invalid limit")
cursor = query.get("cursor", [None])[0]
try:
result = self.store.list_messages(chat_id, actor["id"], limit, cursor)
except ValueError:
raise ApiError(400, "invalid_request", "Invalid cursor")
if result is None:
raise ApiError(404, "not_found", "Chat not found")
items, next_cursor = result
self._send_json(200, {"items": items, "next_cursor": next_cursor}, request_id)
def _send_json(
self,
status: int,
payload: Dict[str, Any],
request_id: str,
headers: Optional[Dict[str, str]] = None,
):
self._set_response_status(status)
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":")).encode("utf-8")
self.send_response(status)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.send_header("X-Request-Id", request_id)
self.send_header("X-Instance-Id", INSTANCE_ID)
if headers:
for key, value in headers.items():
self.send_header(key, value)
self.end_headers()
self.wfile.write(body)
def _send_error(
self,
status: int,
code: str,
message: str,
request_id: str,
extra_headers: Optional[Dict[str, str]] = None,
):
self._send_json(
status,
{"error": {"code": code, "message": message, "request_id": request_id}},
request_id,
extra_headers,
)