devmandan's picture
Claude Opus 5.5
Facts from each code at its own size; the design test's server, not yet on the page
5b3cd1e verified
Raw History Blame Contribute Delete
8.91 kB
"""QR Scan Difficulty Check: the measured bell, and a place on it for your own pictures.
GET / the page (web/)
POST /api/measure one picture (JPEG, PNG or WebP, at most 25 MB) as the request body; returns its reads by
the five scanners at the seven frame sizes, the decodes the many-codes rule left out
("not_run"), its place on the scale, its level and how sure that level is (rating.py), the
picture-quality checks (quality.py; null when nothing could be measured), which are
diagnostics only, zxing-cpp's facts about the code (its unused error correction "uec", the
lowest over its reads of the content in the frame "uec_frame", and from one of those reads
"ec_level", "mask", the "squares" it sampled, "raw_sha256", "raw_bytes" and the "copies" of
its code there; see reading_facts.chosen) and the content's digests (content.sha256 and
content.sha256s, for pairing pictures in the page)
GET /api/health what is running, and whether the design test is on ("design": on, busy, same_frames)
POST /api/design/slot the design test (design.py), one at a time across all visitors, in a worker of its own: a slot
for this page, then POST /api/design?slot= with the picture marked as the original graphic,
GET /api/design/<test> for its answer (and its scenes and frames when the page reads them
too), and DELETE /api/design/<id> when the page leaves it
Pictures are read from a temporary file that is deleted when the reading ends; nothing is stored, and no access log is
kept.
"""
import asyncio
import os
import tempfile
from contextlib import asynccontextmanager
from importlib.metadata import version
from pathlib import Path
from starlette.applications import Starlette
from starlette.concurrency import run_in_threadpool
from starlette.requests import ClientDisconnect, Request
from starlette.responses import JSONResponse
from starlette.routing import Mount, Route
from starlette.staticfiles import StaticFiles
from .design import design_health, design_routes, start_design, stop_design
from .place import place
from .rating import rating
from .reading import FRAMES, SCANNERS, PictureError, Reader
WEB = Path(__file__).resolve().parent.parent / "web"
MAX_BYTES = 25 * 1024 * 1024
SLOTS = int(os.environ.get("QSD_SLOTS", "2")) # pictures read at once: one decoder worker each
MAX_WAITING = 16 # pictures waiting beyond those (arriving, or waiting for a slot); more are turned away
SLOT_WAIT_S = 10 # the longest a picture waits for a slot, so that a request ends within a minute
BODY_S = 60 # the longest a picture may take to arrive; a stalled upload doesn't keep its place
BUSY = "Many pictures are being read right now. Try again in a minute."
UPLOAD = { # the answers to a picture that didn't arrive whole, for /api/measure and the design test alike
400: "The picture did not arrive.",
408: "This picture took too long to arrive. Try again, or send a smaller copy.",
413: "This picture is over 25 MB.",
415: "Send a JPEG, PNG or WebP picture.",
}
CSP = "; ".join([
"default-src 'self'", "img-src 'self' blob: data:", "style-src 'self' 'unsafe-inline'", "script-src 'self'",
"connect-src 'self'", "object-src 'none'", "base-uri 'none'", "form-action 'none'",
"frame-ancestors 'self' https://huggingface.co",
])
STATE: dict = {}
def sniff(data: bytes) -> str | None:
"""The picture's format from its first bytes: only JPEG, PNG and WebP are opened here (the page converts others)."""
if data[:3] == b"\xff\xd8\xff":
return ".jpg"
if data[:8] == b"\x89PNG\r\n\x1a\n":
return ".png"
if data[:4] == b"RIFF" and data[8:12] == b"WEBP":
return ".webp"
return None
def problem(status: int, message: str) -> JSONResponse:
return JSONResponse({"error": message}, status_code=status, headers={"Cache-Control": "no-store"})
async def body(request: Request) -> bytes | None:
"""The request body, or None past MAX_BYTES (read no further)."""
n = request.headers.get("content-length", "")
if n.isdigit() and int(n) > MAX_BYTES:
return None
buf = bytearray() # one buffer, so a picture costs its own size in memory however small its chunks
async for chunk in request.stream():
buf += chunk
if len(buf) > MAX_BYTES:
return None
return bytes(buf)
def read_picture(data: bytes, suffix: str) -> dict:
with tempfile.NamedTemporaryFile(prefix="qsd-", suffix=suffix) as f:
f.write(data)
f.flush()
result = STATE["reader"].read(f.name)
result["order"] = FRAMES # the frame size behind each position of "reads" and "frames"
result["place"] = place(result["reads"], FRAMES)
result["count1280"] = sum(result["reads"][k][FRAMES.index(1280)] for k in SCANNERS)
result["rating"] = rating(result["place"], result["count1280"], result["partial"], result["size"],
bool(result.get("not_run")))
result.setdefault("checks", None) # passed through as the reader gives them; null when nothing was measured
return result
async def measure(request: Request) -> JSONResponse:
if STATE["busy"] >= SLOTS + MAX_WAITING:
return problem(503, BUSY)
STATE["busy"] += 1 # counted from the first byte, so at most SLOTS + MAX_WAITING pictures are held in memory
try:
try:
async with asyncio.timeout(BODY_S):
data = await body(request)
except TimeoutError:
return problem(408, UPLOAD[408])
except ClientDisconnect: # the visitor left mid-upload: nobody will read this answer
return problem(400, UPLOAD[400])
if data is None:
return problem(413, UPLOAD[413])
suffix = sniff(data)
if suffix is None:
return problem(415, UPLOAD[415])
try:
await asyncio.wait_for(STATE["gate"].acquire(), SLOT_WAIT_S)
except TimeoutError:
return problem(503, BUSY)
try:
result = await run_in_threadpool(read_picture, data, suffix)
finally:
STATE["gate"].release()
except PictureError as e:
return problem(422, str(e))
finally:
STATE["busy"] -= 1
return JSONResponse(result, headers={"Cache-Control": "no-store"})
async def health(request: Request) -> JSONResponse:
return JSONResponse({
"ok": True, "scanners": SCANNERS, "frames": FRAMES, "slots": SLOTS,
"versions": {"opencv-contrib": version("opencv-contrib-python-headless"), "zxing-cpp": version("zxing-cpp"),
"zxing": "3.5.2"},
"design": design_health(),
}, headers={"Cache-Control": "no-store"})
@asynccontextmanager
async def lifespan(app):
STATE["reader"] = await run_in_threadpool(Reader, SLOTS)
STATE["gate"] = asyncio.Semaphore(SLOTS)
STATE["busy"] = 0
await run_in_threadpool(start_design, WEB / "data") # never raises: on a failure the design test is off
yield
stop_design()
STATE["reader"].close()
class SecurityHeaders:
"""Plain ASGI middleware: the same headers on every response, and no-cache on the page's own files."""
def __init__(self, app):
self.app = app
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
async def send_with_headers(message):
if message["type"] == "http.response.start":
message.setdefault("headers", [])
message["headers"] += [(b"content-security-policy", CSP.encode()),
(b"x-content-type-options", b"nosniff"),
(b"referrer-policy", b"strict-origin-when-cross-origin")]
# the page's own files are checked on every visit (a cheap 304), so an update never pairs an old
# cached module with a new one
path = scope.get("path", "")
if path == "/" or path.endswith((".html", ".js", ".css", ".json")):
message["headers"].append((b"cache-control", b"no-cache"))
await send(message)
return await self.app(scope, receive, send_with_headers)
app = SecurityHeaders(Starlette(lifespan=lifespan, routes=[
Route("/api/measure", measure, methods=["POST"]),
Route("/api/health", health, methods=["GET"]),
*design_routes({"body": body, "sniff": sniff, "problem": problem, "body_s": BODY_S, "messages": UPLOAD,
"readings_waiting": lambda: STATE["busy"] > SLOTS}),
Mount("/", StaticFiles(directory=WEB, html=True), name="web"),
]))