Download tasks/breakoutgames-2/environment/reference/harbor_entry.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 5.35 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/breakoutgames-2/environment/reference/harbor_entry.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/breakoutgames-2/environment/reference/harbor_entry.py
-
curl -L -o harbor_entry.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/breakoutgames-2/environment/reference/harbor_entry.py
5.35 kB
| #!/usr/bin/env python3 | |
| """Harbor reference adapter for the frozen breakoutgames-2 clone. | |
| The Breakout Games clone runs its account flows at a truthful | |
| disabled-local-mail boundary: the frozen backend contract declares | |
| ``mail.enabled: false`` with no purposes, registration verifies immediately, | |
| and no message is ever produced or sent. The adapter therefore injects no | |
| mail transport at all; it adds the harness lifecycle ABI (health, site | |
| header, admin-token-guarded reset) around the clone's stdlib HTTP server and | |
| owns the anonymous session contract declared in ``backend/runtime.json`` -- | |
| the frozen clone keeps its own application cookie, so the adapter hands out | |
| the one opaque host-only ``__Host-`` session cookie the harness expects. | |
| """ | |
| from __future__ import annotations | |
| import hmac | |
| import json | |
| import os | |
| import random | |
| import secrets | |
| import sys | |
| import time | |
| from http.server import ThreadingHTTPServer | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parent | |
| SITE_ID = "breakoutgames-2" | |
| DATABASE_NAME = "breakoutgames-2.sqlite3" | |
| 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["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(data_dir / DATABASE_NAME) | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(ROOT / "backend" / "runtime.json") | |
| os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor" | |
| os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = "local-sandbox" | |
| os.environ["PYTHONDONTWRITEBYTECODE"] = "1" | |
| for name in ( | |
| "STRIPE_SECRET_KEY", | |
| "STRIPE_WEBHOOK_SECRET", | |
| "STRIPE_TEST_SECRET_KEY", | |
| "STRIPE_TEST_WEBHOOK_SECRET", | |
| "RESEND_API_KEY", | |
| "RESEND_FROM_EMAIL", | |
| ): | |
| os.environ.pop(name, None) | |
| sys.dont_write_bytecode = True | |
| sys.path.insert(0, str(ROOT / "clone")) | |
| import server as clone_server # noqa: E402 | |
| from backend.site_backend_integration import open_site_services # noqa: E402 | |
| _ADMIN_TOKEN = os.environ.get("WEBSITEBENCH_ADMIN_TOKEN", "") | |
| _RESET_PATH = "/__admin/reset" | |
| # The frozen clone carries its own application cookie; the harness session | |
| # contract declared in backend/runtime.json (host-only, http-only, Secure, | |
| # SameSite=Lax) is owned by this adapter, which hands out one opaque | |
| # ``__Host-`` cookie on the first response of a visit. | |
| _SESSION_COOKIE = f"__Host-websitebench-{SITE_ID}-session" | |
| def _reset_site_state() -> None: | |
| """Reset exactly the rows the vendored backend and auth own.""" | |
| backend, auth = open_site_services() | |
| def _site_reset(connection) -> None: | |
| backend.lifecycle.reset_embedded(connection, confirm_site_id=SITE_ID) | |
| # The clone owns these fixture tables (its /api/reset clears the same | |
| # set); the adapter reset must leave the runtime in the same state. | |
| for table in ( | |
| "local_booking_action", | |
| "booking_interest", | |
| "local_draft", | |
| "gift_card_plan", | |
| "local_booking", | |
| "local_favorite", | |
| ): | |
| connection.execute(f"DELETE FROM {table}") | |
| auth.reset_site_state(site_reset=_site_reset, seed_accounts=[]) | |
| class HarborHandler(clone_server.Handler): | |
| """Add the harness lifecycle ABI in front of the frozen clone routes.""" | |
| def _send_adapter_json(self, payload: dict, status: int = 200) -> None: | |
| body = json.dumps(payload, separators=(",", ":")).encode() | |
| self.send_response(status) | |
| self.send_header("Content-Type", "application/json") | |
| self.send_header("Content-Length", str(len(body))) | |
| self.end_headers() | |
| self.wfile.write(body) | |
| def end_headers(self): | |
| self.send_header("X-WebsiteBench-Site", SITE_ID) | |
| headers = getattr(self, "headers", None) | |
| cookies = headers.get("Cookie", "") if headers is not None else "" | |
| if _SESSION_COOKIE + "=" not in cookies: | |
| self.send_header( | |
| "Set-Cookie", | |
| f"{_SESSION_COOKIE}={secrets.token_urlsafe(24)}; " | |
| "Path=/; Secure; HttpOnly; SameSite=Lax", | |
| ) | |
| super().end_headers() | |
| def do_GET(self): | |
| path = self.path.split("?", 1)[0] | |
| if path == "/__websitebench/health": | |
| return self._send_adapter_json({"status": "ok"}) | |
| if path == _RESET_PATH: | |
| return self._send_adapter_json({"error": "Page not found"}, 404) | |
| return super().do_GET() | |
| def do_POST(self): | |
| path = self.path.split("?", 1)[0] | |
| if path == _RESET_PATH: | |
| supplied = self.headers.get("X-WebsiteBench-Admin-Token", "") | |
| if not _ADMIN_TOKEN or not hmac.compare_digest(supplied, _ADMIN_TOKEN): | |
| return self._send_adapter_json({"error": "forbidden"}, 403) | |
| _reset_site_state() | |
| return self._send_adapter_json({"reset": True, "site_id": SITE_ID}) | |
| return super().do_POST() | |
| if __name__ == "__main__": | |
| ThreadingHTTPServer( | |
| (os.environ["HOST"], int(os.environ["PORT"])), HarborHandler | |
| ).serve_forever() | |