Nacre/tests/smoke/slow/server.py

84 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Slow-connection server for tests/smoke/slow.txt (S052): pages and assets paced like a slow line.
Usage: server.py [port] (default 8771, 127.0.0.1 only). Env: SLOW_RATE bytes/s per response (default 128000, about
1 Mbit/s), SLOW_LAT seconds before each response starts (default 0.25).
/ and /two.html: small HTML, `no-cache` + ETag (a revisit costs one conditional request, 304).
/app.css 20 KB `max-age=3600`; /hero.bin 400 KB `max-age=86400`; /lib.js 150 KB with Last-Modified and no
Cache-Control (heuristic caching); /bare.js 40 KB with no cache headers at all (always refetched).
/mark?p=<phase> starts a phase; /log gives JSON {phase: {path: count}} of what the server was asked for.
"""
import json
import os
import sys
import time
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
RATE = int(os.environ.get("SLOW_RATE", "128000"))
LAT = float(os.environ.get("SLOW_LAT", "0.25"))
LASTMOD = "Mon, 01 Jan 2024 00:00:00 GMT"
BODIES = {
"/app.css": (b"body { font: 16px sans-serif; } " * 600, "text/css", "max-age=3600", None),
"/hero.bin": (os.urandom(400_000), "application/octet-stream", "max-age=86400", None),
"/lib.js": (b"window.lib = 1; /* " + b"x" * 150_000 + b" */", "text/javascript", None, LASTMOD),
"/bare.js": (b"window.bare = 1; /* " + b"y" * 40_000 + b" */", "text/javascript", None, None),
}
PAGE = (
'<!doctype html><title>{name}</title><link rel="stylesheet" href="/app.css"><script src="/lib.js"></script>'
'<script src="/bare.js"></script><p>{name}</p><img src="/hero.bin" hidden>'
'<script>window.addEventListener("load", () => { window.done = true; });</script>'
)
phase = "start"
log = {}
class Handler(BaseHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def do_GET(self):
global phase
path = self.path.split("?")[0]
if path == "/mark":
phase = self.path.split("p=")[-1]
return self.send(b"ok", "text/plain", "no-store", None, 200, pace=False)
if path == "/log":
return self.send(json.dumps(log).encode(), "application/json", "no-store", None, 200, pace=False)
log.setdefault(phase, {}).setdefault(path, 0)
log[phase][path] += 1
if path in BODIES:
body, kind, cache, last = BODIES[path]
if last and self.headers.get("If-Modified-Since") == last:
return self.send(b"", kind, cache, last, 304)
return self.send(body, kind, cache, last, 200)
name = "two" if path == "/two.html" else "one"
body = PAGE.replace("{name}", name).encode()
etag = f'"{name}"'
if self.headers.get("If-None-Match") == etag:
return self.send(b"", "text/html", "no-cache", None, 304, etag=etag)
self.send(body, "text/html", "no-cache", None, 200, etag=etag)
def send(self, body, kind, cache, last, code, etag=None, pace=True):
if pace:
time.sleep(LAT)
self.send_response(code)
self.send_header("Content-Type", kind)
if cache:
self.send_header("Cache-Control", cache)
if last:
self.send_header("Last-Modified", last)
if etag:
self.send_header("ETag", etag)
self.send_header("Content-Length", str(len(body)))
self.end_headers()
step = 4096
for i in range(0, len(body), step):
self.wfile.write(body[i:i + step])
self.wfile.flush()
if pace:
time.sleep(step / RATE)
def log_message(self, *args):
pass
ThreadingHTTPServer(("127.0.0.1", int(sys.argv[1]) if len(sys.argv) > 1 else 8771), Handler).serve_forever()