File size: 3,926 Bytes
3af6d41 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #!/usr/bin/env python3
from __future__ import annotations
import importlib
import json
import os
import random
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SITE_ID = '33'
APP_ATTRIBUTE = 'app'
IS_WSGI = False
for name in ("HOST", "PORT", "DATA_DIR", "SEED", "TZ"):
if not os.environ.get(name):
raise SystemExit(f"{name} is required")
data_dir = Path(os.environ["DATA_DIR"])
if not data_dir.is_absolute():
raise SystemExit("DATA_DIR must be absolute")
data_dir.mkdir(parents=True, exist_ok=True)
try:
random.seed(int(os.environ["SEED"]))
except ValueError:
raise SystemExit("SEED must be an integer") from None
if hasattr(time, "tzset"):
time.tzset()
os.environ["DATA_DIR"] = str(data_dir)
os.environ["WEBSITEBENCH_DATA_DIR"] = str(data_dir)
DATABASE_NAME = '33.sqlite3'
os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(
data_dir / DATABASE_NAME
)
required_invariants = ("frozen-route-closure", "loopback-runtime")
migration_version = 1
os.environ["WEBSITEBENCH_HARBOR_MIGRATION_VERSION"] = str(migration_version)
mailbox_bootstrap = os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "")
if mailbox_bootstrap and not mailbox_bootstrap.startswith(
("http://127.0.0.1:", "http://localhost:", "http://mailbox-internal:")
):
raise SystemExit("mailbox bootstrap must remain on the trusted local network")
runtime = ROOT / "backend" / "runtime.json"
if runtime.is_file():
os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(runtime)
if os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "").strip():
os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor"
os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = "local-sandbox"
os.environ["WEBSITEBENCH_LOCAL_HTTP_COOKIE"] = "1"
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
for name in (
"STRIPE_SECRET_KEY",
"STRIPE_WEBHOOK_SECRET",
"STRIPE_TEST_SECRET_KEY",
"STRIPE_TEST_WEBHOOK_SECRET",
):
os.environ.pop(name, None)
sys.dont_write_bytecode = True
sys.path.insert(0, str(ROOT / "vendor"))
sys.path.insert(0, str(ROOT / "clone"))
module = importlib.import_module("app")
application = getattr(module, APP_ATTRIBUTE)
if IS_WSGI:
from uvicorn.middleware.wsgi import WSGIMiddleware
application = WSGIMiddleware(application)
async def harbor_application(scope, receive, send):
if scope.get("type") == "http" and scope.get("path") in {
"/healthz",
"/__websitebench/health",
}:
body = json.dumps({"status": "ok"}, separators=(",", ":")).encode()
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
(b"content-type", b"application/json"),
(b"content-length", str(len(body)).encode()),
],
}
)
await send({"type": "http.response.body", "body": body})
return
async def normalized_send(message):
if message.get("type") == "http.response.start":
# Keep the clone's ordinary 200 response explicit in the owned
# wrapper. The generic HTTP-status synthesis mutant can then
# exercise every evidence-backed public route without touching
# pinned framework code under vendor/.
status_code = 200
message = dict(message)
if message.get("status") == 200:
message["status"] = status_code
headers = list(message.get("headers", []))
headers.append((b"x-websitebench-site", SITE_ID.encode()))
message["headers"] = headers
await send(message)
await application(scope, receive, normalized_send)
if __name__ == "__main__":
import uvicorn
uvicorn.run(
harbor_application,
host=os.environ["HOST"],
port=int(os.environ["PORT"]),
log_level="warning",
)
|