From 30d012de8023f62cbe36b26bccf30536396c1857 Mon Sep 17 00:00:00 2001 From: Sergey Lukashenko Date: Tue, 17 Nov 2026 16:34:10 +0300 Subject: [PATCH] fix: serve WebP with a portable MIME type --- AGENTS.md | 1 + MEMORY.md | 2 ++ app/main.py | 7 ++++++- structure_and_data.md | 6 ++++++ tests/test_lu_trainer.py | 8 ++++++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/AGENTS.md b/AGENTS.md index 62996db..31f34e3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -27,6 +27,7 @@ - Weather easter egg: `python -m unittest discover -s tests -p test_weather.py -q` and `node --check web/weather-effect.js`; inspect `/belki` and `/admin` with rain/snow/mixed fixtures, mobile widths, reduced motion, and API failure. Prefer the remote test-zone MCP when exposed; otherwise use an isolated local server with temporary SQLite paths and stop it after validation. Keep screenshots outside the repo. - For CI-only changes, prefer a targeted YAML parse and a diff review. - For image/runtime changes, check the `Dockerfile`, Python entrypoint, static asset serving, and CI job together. +- Python 3.12 slim does not recognize WebP in strict `mimetypes.guess_type()` without system MIME data. Keep the explicit `image/webp` response in `Handler._file`; `tests.test_lu_trainer` covers a missing MIME mapping. Do not rely only on workstation MIME tables when adding media formats. - For k3s changes, validate YAML and current cluster inventory first; keep `KUBE_CONFIG_B64` masked/protected in GitLab. - Do not add unnecessary local build artifacts or temporary files to the repository. - Admin report UI changes should cover both the persisted history flag and browser behavior: run the focused history/admin filter tests, Python compilation, `git diff --check`, and a JavaScript syntax check for `web/admin.html`. The report list defaults to the calendar current month; do not silently replace the existing sort order. diff --git a/MEMORY.md b/MEMORY.md index 3541779..7e3d435 100644 --- a/MEMORY.md +++ b/MEMORY.md @@ -1,5 +1,7 @@ # MEMORY +- 2026-11-17: CI Python 3.12 slim returns `(None, None)` for `mimetypes.guess_type("forest.webp")`; WebP is only in its non-strict common table without system MIME data. `Handler._file` now explicitly serves `.webp` as `image/webp`, independent of host MIME tables. HTTP regression coverage simulates an absent mapping; retain it when modifying static serving. + - 2026-11-17: owner renamed the trainer public page to `/trainer` (also `/trainer/`). Homepage navigation, Python/Nginx page aliases and browser entry URLs follow that address. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are unchanged; the prior `/lu-trainer` page address below is historical. No push/deployment. - 2026-11-17: implemented the owner-requested LU trainer at `/lu-trainer` and `/lu-trainer/`. The DOM-free original JS engine preserves the documented upstream training model (not verified CatWar rules); both grids consume snapshots without hidden hazards, with temporary hints disclosed only as action results. One in-memory attempt, separate notes, confirmed resets, terminal action locks and no CatWar account/report integration. Original imagegen forest/cat provenance is in `web/lu-trainer/assets/README.md`. No new runtime dependency. Local Node/Python/Chromium and isolated Nginx checks are recorded in `structure_and_data.md`; no registry push or deployment. diff --git a/app/main.py b/app/main.py index 85bffef..3e26ab5 100644 --- a/app/main.py +++ b/app/main.py @@ -585,8 +585,13 @@ class Handler(BaseHTTPRequestHandler): def _file(self, path: Path) -> None: """Отдаёт существующий статический файл с подходящим MIME-типом.""" content = path.read_bytes() + # Python slim images may lack the system MIME database for WebP. + content_type = ( + "image/webp" if path.suffix.lower() == ".webp" + else mimetypes.guess_type(path.name)[0] or "application/octet-stream" + ) self.send_response(200) - self.send_header("Content-Type", mimetypes.guess_type(path.name)[0] or "application/octet-stream") + self.send_header("Content-Type", content_type) self.send_header("Content-Length", str(len(content))) self.end_headers() self.wfile.write(content) diff --git a/structure_and_data.md b/structure_and_data.md index 3ef9f72..4a2ba2b 100644 --- a/structure_and_data.md +++ b/structure_and_data.md @@ -1,5 +1,11 @@ # Structure and Data +## Portable WebP MIME response — 2026-11-17 + +- Reproduced the reported CI mismatch in stock `python:3.12-slim` (Python 3.12.16): strict `mimetypes.guess_type("forest.webp")` returns `(None, None)` while the non-strict common table contains `image/webp`. The workstation MIME database masked this during the original checks. +- `app/main.py` `Handler._file` now explicitly sets `image/webp` for `.webp` (case-insensitive); all other formats keep the existing lookup/fallback. No dependency, image definition, route, game logic or gateway configuration change. `tests/test_lu_trainer.py` adds an HTTP regression check with MIME guessing unavailable. +- Validation passed: 4 focused trainer HTTP tests locally; all 178 Python discovery tests in disposable `python:3.12-slim` (Python 3.12.16), with source mounted read-only and test stores under container `/tmp`; Python source compilation and diff checks. The container, pulled image and temporary local venv/data were removed. Remote test-zone/token-optimizer MCP are unavailable. No push/deployment. + ## Trainer public route — 2026-11-17 - Owner requested `/trainer` instead of `/lu-trainer`. Python and Nginx now expose `/trainer` and `/trainer/`; the homepage link and browser entry URLs use `/trainer`. Internal `web/lu-trainer.html` and `/lu-trainer/` asset URLs are retained. No game mechanics, runtime dependencies or deployment changes. diff --git a/tests/test_lu_trainer.py b/tests/test_lu_trainer.py index c4259cf..5e42b3e 100644 --- a/tests/test_lu_trainer.py +++ b/tests/test_lu_trainer.py @@ -2,6 +2,7 @@ import http.client import threading import unittest +from unittest.mock import patch from app.main import BelkiHTTPServer, Handler @@ -44,6 +45,13 @@ class TrainerRoutesTests(unittest.TestCase): self.assertIn(media_type, content_type) self.assertGreater(len(content), 0) + def test_webp_mime_does_not_depend_on_system_mime_database(self): + with patch('app.main.mimetypes.guess_type', return_value=(None, None)): + status, content_type, content = self.get('/lu-trainer/assets/forest.webp') + self.assertEqual(status, 200) + self.assertEqual(content_type, 'image/webp') + self.assertTrue(content.startswith(b'RIFF')) + def test_missing_and_escaping_assets_are_not_served(self): for path in ['/lu-trainer/missing.js', '/lu-trainer/%2e%2e/%2e%2e/project-info.txt']: self.assertEqual(self.get(path)[0], 404)