#!/usr/bin/env python3 import base64 import binascii import hashlib import json import logging import os import re import signal import socket import sys import tempfile import threading import unicodedata import uuid import warnings from datetime import datetime, timezone from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path from urllib.parse import parse_qs, urlsplit from PIL import Image, UnidentifiedImageError from auth import ( create_session, hash_password, now_iso, parse_basic_auth, parse_bearer_token, validate_password, verify_password, ) from db import ApiError, Store from s3store import ObjectStore, S3Unavailable from sessions import SessionStore MAX_BODY_BYTES = 1024 * 1024 MAX_UPLOAD_BYTES = min(10 * 1024 * 1024, int(os.environ.get('MAX_UPLOAD_BYTES', 10 * 1024 * 1024))) MAX_IMAGE_PIXELS = min(40_000_000, int(os.environ.get('MAX_IMAGE_PIXELS', 40_000_000))) MAX_CONCURRENT_UPLOADS = int(os.environ.get('MAX_CONCURRENT_UPLOADS', '16')) if (MAX_UPLOAD_BYTES < 1 or MAX_IMAGE_PIXELS < 1 or not 1 <= MAX_CONCURRENT_UPLOADS <= 64): raise RuntimeError('Upload limits must be positive and concurrent uploads cannot exceed 64') UPLOAD_SLOTS = threading.BoundedSemaphore(MAX_CONCURRENT_UPLOADS) USERNAME_PATTERN = re.compile(r'^[a-z0-9_]{3,32}$') CURSOR_TIMESTAMP_PATTERN = re.compile( r'^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}(?:\.[0-9]+)?Z$' ) IDEMPOTENCY_KEY_PATTERN = re.compile(r'^[A-Za-z0-9._:-]{1,128}$') LOG = logging.getLogger('messenger') OPENAPI_PATH = Path(__file__).resolve().parent / 'contracts' / 'openapi.json' SWAGGER_HTML = b''' MTUSI Messenger API
''' def cursor_encode(resource, item): value = json.dumps( {'resource': resource, 'created_at': item['created_at'], 'id': item['id']}, separators=(',', ':'), ).encode('utf-8') return base64.urlsafe_b64encode(value).rstrip(b'=').decode('ascii') def cursor_decode(cursor, resource): try: padding = '=' * (-len(cursor) % 4) decoded = base64.b64decode(cursor + padding, altchars=b'-_', validate=True) value = json.loads(decoded.decode('utf-8')) except (ValueError, UnicodeError, binascii.Error): raise ApiError(400, 'invalid_request', 'Invalid cursor') from None if (not isinstance(value, dict) or set(value) != {'resource', 'created_at', 'id'} or value.get('resource') != resource or not isinstance(value.get('created_at'), str) or not CURSOR_TIMESTAMP_PATTERN.fullmatch(value['created_at']) or not isinstance(value.get('id'), str)): raise ApiError(400, 'invalid_request', 'Invalid cursor') try: created_at = datetime.fromisoformat(value['created_at'][:-1] + '+00:00') item_id = str(uuid.UUID(value['id'])) except (ValueError, AttributeError): raise ApiError(400, 'invalid_request', 'Invalid cursor') from None if item_id != value['id']: raise ApiError(400, 'invalid_request', 'Invalid cursor') return created_at, item_id def required_text(value, name, maximum): if not isinstance(value, str) or not value.strip() or len(value) > maximum: raise ApiError(400, 'invalid_request', 'Invalid ' + name) try: value.encode('utf-8') except UnicodeEncodeError: raise ApiError(400, 'invalid_request', 'Invalid ' + name) from None return value def parse_uuid(value): if not isinstance(value, str): raise ApiError(400, 'invalid_request', 'Invalid UUID') try: parsed = uuid.UUID(value) except (ValueError, AttributeError): raise ApiError(400, 'invalid_request', 'Invalid UUID') from None if str(parsed) != value.lower(): raise ApiError(400, 'invalid_request', 'Invalid UUID') return str(parsed) def attachment_filename(query): try: values = parse_qs(query, keep_blank_values=True, strict_parsing=True) except ValueError: raise ApiError(400, 'invalid_request', 'Invalid attachment query') from None if set(values) != {'filename'} or len(values['filename']) != 1: raise ApiError(400, 'invalid_request', 'Invalid attachment query') filename = values['filename'][0] if (not filename or len(filename) > 255 or '/' in filename or '\\' in filename or any(unicodedata.category(char) in {'Cc', 'Cf'} for char in filename)): raise ApiError(400, 'invalid_request', 'Invalid filename') try: filename.encode('utf-8') except UnicodeEncodeError: raise ApiError(400, 'invalid_request', 'Invalid filename') from None return filename class MessengerHandler(BaseHTTPRequestHandler): server_version = 'MTUSI-Messenger-Lab3' protocol_version = 'HTTP/1.1' def log_message(self, _format, *args): LOG.info('request method=%s status=%s request_id=%s', self.command, self._status, self._request_id) def _send_bytes(self, status, request_id, body, content_type, extra_headers=None): self._status = status self.send_response(status) self.send_header('X-Request-Id', request_id) self.send_header('Content-Length', str(len(body))) if status != 204: self.send_header('Content-Type', content_type) for name, value in (extra_headers or {}).items(): self.send_header(name, value) self.send_header('X-Instance-Id', self.server.instance_id) self.end_headers() self._headers_sent = True if body: self.wfile.write(body) def _send_stream(self, request_id, stream, length, content_type): self._status = 200 self.send_response(200) self.send_header('X-Request-Id', request_id) self.send_header('Content-Length', str(length)) self.send_header('Content-Type', content_type) self.send_header('X-Instance-Id', self.server.instance_id) self.end_headers() self._headers_sent = True remaining = length while remaining: chunk = stream.read(min(64 * 1024, remaining)) if not chunk: raise OSError('S3 object ended before the stored content length') self.wfile.write(chunk) remaining -= len(chunk) def _send(self, status, request_id, payload=None, extra_headers=None): body = b'' if status == 204 else json.dumps( payload, ensure_ascii=False, separators=(',', ':') ).encode('utf-8') self._send_bytes( status, request_id, body, 'application/json; charset=utf-8', extra_headers ) def _error(self, request_id, error, extra_headers=None): self._send(error.status, request_id, { 'error': { 'code': error.code, 'message': error.message, 'request_id': request_id, }, }, extra_headers=extra_headers) def _read_json(self): if self.headers.get_content_type() != 'application/json': raise ApiError(415, 'unsupported_media_type', 'Expected application/json') raw_length = self.headers.get('Content-Length') try: length = int(raw_length) except (TypeError, ValueError): raise ApiError(400, 'invalid_request', 'Invalid Content-Length') from None if length < 0: raise ApiError(400, 'invalid_request', 'Invalid Content-Length') if length > MAX_BODY_BYTES: raise ApiError(413, 'payload_too_large', 'Request body is too large') try: value = json.loads(self.rfile.read(length).decode('utf-8')) except (ValueError, UnicodeError): raise ApiError(400, 'invalid_request', 'Malformed JSON') from None if not isinstance(value, dict): raise ApiError(400, 'invalid_request', 'Expected a JSON object') return value def _read_attachment(self): content_type = self.headers.get_content_type() if content_type not in ('image/png', 'image/jpeg'): self.close_connection = True raise ApiError(415, 'unsupported_media_type', 'Expected PNG or JPEG') try: length = int(self.headers.get('Content-Length')) except (TypeError, ValueError): raise ApiError(400, 'invalid_request', 'Invalid Content-Length') from None if length < 0: self.close_connection = True raise ApiError(400, 'invalid_request', 'Invalid Content-Length') if length > MAX_UPLOAD_BYTES: self.close_connection = True raise ApiError(413, 'payload_too_large', 'Image exceeds the upload limit') if length == 0: raise ApiError(415, 'unsupported_media_type', 'Image is empty or invalid') temp_file = tempfile.SpooledTemporaryFile(max_size=1024 * 1024, mode='w+b') digest = hashlib.sha256() remaining = length try: while remaining: chunk = self.rfile.read(min(64 * 1024, remaining)) if not chunk: raise ApiError(400, 'invalid_request', 'Incomplete image upload') temp_file.write(chunk) digest.update(chunk) remaining -= len(chunk) temp_file.seek(0) try: with warnings.catch_warnings(): warnings.simplefilter('ignore', Image.DecompressionBombWarning) image = Image.open(temp_file) detected_type = { 'PNG': 'image/png', 'JPEG': 'image/jpeg', }.get(image.format) width, height = image.size if width * height > MAX_IMAGE_PIXELS: raise ApiError(413, 'payload_too_large', 'Image dimensions exceed the pixel limit') if detected_type != content_type: raise ApiError(415, 'unsupported_media_type', 'Image content does not match Content-Type') image.verify() except Image.DecompressionBombError: raise ApiError(413, 'payload_too_large', 'Image dimensions exceed the pixel limit') from None except ApiError: raise except (UnidentifiedImageError, OSError, SyntaxError, ValueError): raise ApiError(415, 'unsupported_media_type', 'Image is invalid or unsupported') from None temp_file.seek(0) return temp_file, { 'content_type': detected_type, 'size_bytes': length, 'sha256': digest.hexdigest(), 'width': width, 'height': height, } except Exception: temp_file.close() raise def _dependencies_ok(self): try: self.server.store.ping() self.server.sessions.ping() self.server.objects.ping() return True except Exception: LOG.exception('dependency check failed') return False def _unauthorized_bearer(self, request_id): self._error( request_id, ApiError(401, 'unauthorized', 'Authentication required'), extra_headers={'WWW-Authenticate': 'Bearer realm="messenger"'}, ) def _unauthorized_basic(self, request_id): self._error( request_id, ApiError(401, 'unauthorized', 'Authentication required'), extra_headers={'WWW-Authenticate': 'Basic realm="messenger", charset="UTF-8"'}, ) def _authenticated_user(self, request_id): auth = self.headers.get('Authorization', '') if auth.startswith('Basic '): self._unauthorized_bearer(request_id) return None token = parse_bearer_token(auth) if not token: self._unauthorized_bearer(request_id) return None try: user_id = self.server.sessions.get_user_id(token) except Exception: LOG.exception('session lookup failed') self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return None if not user_id: self._unauthorized_bearer(request_id) return None try: user = self.server.store.get_user(user_id) except Exception: LOG.exception('user lookup failed') self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return None if user is None: self._unauthorized_bearer(request_id) return None user['_token'] = token return user @staticmethod def _query(query): try: values = parse_qs(query, keep_blank_values=True, strict_parsing=True) except ValueError: raise ApiError(400, 'invalid_request', 'Invalid query parameters') from None if set(values) - {'limit', 'cursor'} or any(len(items) != 1 for items in values.values()): raise ApiError(400, 'invalid_request', 'Invalid query parameters') try: limit = int(values.get('limit', ['50'])[0]) except ValueError: raise ApiError(400, 'invalid_request', 'Invalid limit') from None if not 1 <= limit <= 100: raise ApiError(400, 'invalid_request', 'Invalid limit') cursor = values.get('cursor', [''])[0] if ('cursor' in values and not cursor) or len(cursor) > 2048: raise ApiError(400, 'invalid_request', 'Invalid cursor') return limit, cursor def _create_session(self, request_id): credentials = parse_basic_auth(self.headers.get('Authorization')) if not credentials: self._unauthorized_basic(request_id) return username, password = credentials if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username): self._unauthorized_basic(request_id) return if not validate_password(password): self._unauthorized_basic(request_id) return try: row = self.server.store.get_user_with_password(username) except Exception: LOG.exception('login lookup failed') self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return if row is None or not verify_password(row['password_hash'], password): self._unauthorized_basic(request_id) return user = self.server.store.get_user(str(row['id'])) try: token, expires_at = create_session(self.server.sessions, user['id']) except Exception: LOG.exception('session create failed') self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return self._send(201, request_id, { 'token': token, 'token_type': 'Bearer', 'expires_at': expires_at, 'user': user, }) def _logout(self, request_id, user): try: self.server.sessions.revoke(user['_token']) except Exception: LOG.exception('session revoke failed') self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return self._send_bytes(204, request_id, b'', 'application/json; charset=utf-8') def _compensate_upload(self, attachment_id, object_key, remove_object): object_removed = True if remove_object: try: self.server.objects.delete_object(object_key) except S3Unavailable: LOG.error('upload compensation could not delete object id=%s', attachment_id) object_removed = False if not object_removed: return try: self.server.store.mark_attachment_failed( attachment_id, 'upload_failed', now_iso() ) except ApiError as error: LOG.error( 'upload compensation could not update metadata id=%s code=%s', attachment_id, error.code, ) def _upload_attachment(self, request_id, user, chat_id, query): filename = attachment_filename(query) self.server.store.get_chat(user['id'], chat_id) if not UPLOAD_SLOTS.acquire(timeout=5): raise ApiError(503, 'unavailable', 'Upload capacity is temporarily unavailable') temp_file = None try: temp_file, image = self._read_attachment() attachment_id = str(uuid.uuid4()) object_key = 'objects/' + uuid.uuid4().hex created_at = now_iso() self.server.store.create_attachment_upload( attachment_id, chat_id, user['id'], object_key, filename, image['content_type'], image['size_bytes'], image['sha256'], image['width'], image['height'], created_at, ) try: self.server.objects.put_file( object_key, temp_file, image['size_bytes'], image['content_type'] ) except S3Unavailable: self._compensate_upload(attachment_id, object_key, True) raise ApiError(503, 'unavailable', 'Attachment storage is unavailable') from None try: attachment = self.server.store.mark_attachment_ready( attachment_id, now_iso() ) except ApiError: self._compensate_upload(attachment_id, object_key, True) raise self._send(201, request_id, attachment) finally: if temp_file is not None: temp_file.close() UPLOAD_SLOTS.release() def _download_attachment(self, request_id, user, attachment_id, query): try: values = parse_qs(query, keep_blank_values=True, strict_parsing=True) except ValueError: raise ApiError(400, 'invalid_request', 'Invalid attachment query') from None if set(values) - {'variant'} or any(len(items) != 1 for items in values.values()): raise ApiError(400, 'invalid_request', 'Invalid attachment query') if values.get('variant', ['original'])[0] != 'original': raise ApiError(404, 'not_found', 'Attachment variant not found') attachment = self.server.store.get_attachment_for_download( user['id'], attachment_id ) if attachment['status'] != 'ready': raise ApiError(409, 'conflict', 'Attachment is not ready') result = self.server.objects.get_object(attachment['object_key']) if result is None: self.server.store.mark_reconciled_missing(attachment_id, now_iso()) raise ApiError(503, 'unavailable', 'Attachment content is unavailable') stream = result['Body'] try: if result.get('ContentLength') != attachment['size_bytes']: self.server.store.mark_reconciled_missing(attachment_id, now_iso()) raise ApiError(503, 'unavailable', 'Attachment content is inconsistent') self._send_stream( request_id, stream, attachment['size_bytes'], attachment['content_type'], ) finally: stream.close() def _register(self, request_id, body): if set(body) != {'username', 'display_name', 'password'}: raise ApiError(400, 'invalid_request', 'Invalid user fields') username = body['username'] if not isinstance(username, str) or not USERNAME_PATTERN.fullmatch(username): raise ApiError(400, 'invalid_request', 'Invalid username') display_name = required_text(body['display_name'], 'display_name', 100) password = body['password'] if not validate_password(password): raise ApiError(400, 'invalid_request', 'Invalid password') created_at = now_iso() password_hash = hash_password(password) user = self.server.store.register_user(username, display_name, password_hash, created_at) self._send(201, request_id, user) def _dispatch(self): request_id = str(uuid.uuid4()) self._request_id = request_id self._status = 500 self._headers_sent = False try: if self.server.stopping: raise ApiError(503, 'unavailable', 'Server is shutting down') route = urlsplit(self.path) path = route.path if self.command == 'GET' and path in ('/docs', '/docs/'): self._send_bytes(200, request_id, SWAGGER_HTML, 'text/html; charset=utf-8') return if self.command == 'GET' and path == '/openapi.json': self._send_bytes( 200, request_id, self.server.openapi_json, 'application/json; charset=utf-8' ) return if path == '/health/live' and self.command == 'GET': self._send(200, request_id, {'status': 'ok'}) return if path == '/health/ready' and self.command == 'GET': if self._dependencies_ok(): self._send(200, request_id, {'status': 'ok'}) else: self._error(request_id, ApiError(503, 'unavailable', 'Service unavailable')) return if path == '/api/v1/users' and self.command == 'POST': self._register(request_id, self._read_json()) return if path == '/api/v1/auth/sessions' and self.command == 'POST': self._create_session(request_id) return if path.startswith('/api/v1/'): user = self._authenticated_user(request_id) if user is None: return user_id = user['id'] if path == '/api/v1/auth/sessions/current' and self.command == 'DELETE': self._logout(request_id, user) return if path == '/api/v1/users/me' and self.command == 'GET': self._send(200, request_id, { k: user[k] for k in ('id', 'username', 'display_name', 'created_at') }) return if path == '/api/v1/chats' and self.command == 'POST': body = self._read_json() if set(body) != {'title', 'member_ids'}: raise ApiError(400, 'invalid_request', 'Invalid chat fields') title = required_text(body['title'], 'title', 100) member_ids = body['member_ids'] if (not isinstance(member_ids, list) or not member_ids or len(member_ids) > 100 or any(not isinstance(value, str) for value in member_ids)): raise ApiError(400, 'invalid_request', 'Invalid chat members') normalized = [parse_uuid(value) for value in member_ids] if len(set(normalized)) != len(normalized) or user_id not in normalized: raise ApiError(400, 'invalid_request', 'Invalid chat members') chat = self.server.store.create_chat( user_id, title, normalized, now_iso() ) self._send(201, request_id, chat) return if path == '/api/v1/chats' and self.command == 'GET': limit, cursor = self._query(route.query) key = cursor_decode(cursor, 'chats') if cursor else None items, next_key = self.server.store.list_chats(user_id, limit, key) next_cursor = cursor_encode('chats', items[-1]) if next_key else None self._send(200, request_id, {'items': items, 'next_cursor': next_cursor}) return match = re.fullmatch(r'/api/v1/chats/([^/]+)', path) if match and self.command == 'GET': chat_id = parse_uuid(match.group(1)) self._send(200, request_id, self.server.store.get_chat(user_id, chat_id)) return match = re.fullmatch(r'/api/v1/chats/([^/]+)/messages', path) if match: chat_id = parse_uuid(match.group(1)) resource = 'messages:' + chat_id if self.command == 'POST': body = self._read_json() if set(body) - {'text', 'attachment_ids'} or 'text' not in body: raise ApiError(400, 'invalid_request', 'Invalid message fields') idempotency_key = self.headers.get('Idempotency-Key', '') if not IDEMPOTENCY_KEY_PATTERN.fullmatch(idempotency_key): raise ApiError(400, 'invalid_request', 'Invalid Idempotency-Key') 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(value, str) for value in attachment_ids)): raise ApiError(400, 'invalid_request', 'Invalid message fields') normalized_attachments = [ parse_uuid(value) for value in attachment_ids ] if len(set(normalized_attachments)) != len(normalized_attachments): raise ApiError(400, 'invalid_request', 'Duplicate attachment IDs') if not text.strip() and not normalized_attachments: raise ApiError(400, 'invalid_request', 'Message text or attachment is required') try: text.encode('utf-8') except UnicodeEncodeError: raise ApiError(400, 'invalid_request', 'Invalid message text') from None message = self.server.store.create_message_idempotent( user_id, chat_id, text, normalized_attachments, idempotency_key, now_iso(), ) self._send(201, request_id, message) return if self.command == 'GET': limit, cursor = self._query(route.query) key = cursor_decode(cursor, resource) if cursor else None items, next_key = self.server.store.list_messages(user_id, chat_id, limit, key) next_cursor = cursor_encode(resource, items[-1]) if next_key else None self._send(200, request_id, {'items': items, 'next_cursor': next_cursor}) return match = re.fullmatch(r'/api/v1/chats/([^/]+)/attachments', path) if match and self.command == 'POST': self._upload_attachment( request_id, user, parse_uuid(match.group(1)), route.query ) return match = re.fullmatch(r'/api/v1/attachments/([^/]+)', path) if match and self.command == 'GET': self._send( 200, request_id, self.server.store.get_attachment( user_id, parse_uuid(match.group(1)) ), ) return match = re.fullmatch( r'/api/v1/attachments/([^/]+)/content', path ) if match and self.command == 'GET': self._download_attachment( request_id, user, parse_uuid(match.group(1)), route.query ) return raise ApiError(404, 'not_found', 'Resource not found') except ApiError as error: self._error(request_id, error) except (BrokenPipeError, ConnectionResetError): LOG.info('request disconnected request_id=%s', request_id) except OSError: if self._headers_sent: LOG.info('response stream disconnected request_id=%s', request_id) else: LOG.exception('request I/O failed request_id=%s', request_id) self._error( request_id, ApiError(503, 'unavailable', 'Service unavailable'), ) except Exception: LOG.exception('request failed request_id=%s', request_id) self._error(request_id, ApiError(500, 'internal_error', 'Internal server error')) def do_GET(self): self._dispatch() def do_POST(self): self._dispatch() def do_PUT(self): self._dispatch() def do_PATCH(self): self._dispatch() def do_DELETE(self): self._dispatch() def do_OPTIONS(self): self._dispatch() class MessengerServer(ThreadingHTTPServer): daemon_threads = False block_on_close = True request_queue_size = 128 def __init__(self, address, store, sessions, objects): super().__init__(address, MessengerHandler) self.store = store self.sessions = sessions self.objects = objects self.stopping = False self.instance_id = os.environ.get('INSTANCE_ID') or socket.gethostname() try: spec = json.loads(OPENAPI_PATH.read_text(encoding='utf-8')) spec['servers'] = [{'url': '/', 'description': 'Текущий адрес API'}] self.openapi_json = json.dumps( spec, ensure_ascii=False, separators=(',', ':') ).encode('utf-8') except (OSError, ValueError) as error: self.server_close() raise RuntimeError('Unable to load the OpenAPI contract') from error def main(): host = os.environ.get('APP_HOST', '0.0.0.0') try: port = int(os.environ.get('APP_PORT', '8080')) except ValueError: raise SystemExit('APP_PORT must be an integer') if not 1 <= port <= 65535: raise SystemExit('APP_PORT must be between 1 and 65535') if os.environ.get('SESSION_STORE', 'redis').lower() != 'redis': raise SystemExit('SESSION_STORE must be redis for this implementation') logging.basicConfig( level=os.environ.get('LOG_LEVEL', 'INFO').upper(), format='%(asctime)s %(levelname)s %(name)s %(message)s', stream=sys.stdout, ) store = Store.connect(run_migrations=False) sessions = SessionStore.from_env() objects = ObjectStore.from_env() server = MessengerServer((host, port), store, sessions, objects) def stop(_signum, _frame): server.stopping = True threading.Thread(target=server.shutdown, name='http-shutdown', daemon=True).start() signal.signal(signal.SIGTERM, stop) signal.signal(signal.SIGINT, stop) LOG.info('server started host=%s port=%d', host, port) try: server.serve_forever(poll_interval=0.2) finally: server.server_close() store.close() objects.client.close() LOG.info('server stopped') if __name__ == '__main__': main()