#!/usr/bin/env python3 import json import urllib.parse import urllib.request from http.server import BaseHTTPRequestHandler, HTTPServer def _json_get(path: str): with urllib.request.urlopen(f"http://127.0.0.1:9222{path}", timeout=2) as resp: return json.loads(resp.read().decode("utf-8")) def _json_put(path: str): req = urllib.request.Request(f"http://127.0.0.1:9222{path}", method="PUT") with urllib.request.urlopen(req, timeout=2) as resp: return json.loads(resp.read().decode("utf-8")) def chromium_open(url: str) -> None: encoded = urllib.parse.quote(url, safe=':/?#[]@!$&\'()*+,;=%') opened = _json_put(f"/json/new?{encoded}") opened_id = opened.get("id") # Keep exactly one active page tab to prevent tab/memory explosion in warm containers. pages = _json_get("/json/list") for page in pages: page_id = page.get("id") if page_id and page_id != opened_id: try: _json_put(f"/json/close/{page_id}") except Exception: pass class Handler(BaseHTTPRequestHandler): def _json(self, code: int, payload: dict): body = json.dumps(payload).encode("utf-8") self.send_response(code) self.send_header("Content-Type", "application/json") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def do_GET(self): if self.path == "/health": self._json(200, {"ok": True}) return self._json(404, {"detail": "Not found"}) def do_POST(self): if self.path != "/open": self._json(404, {"detail": "Not found"}) return try: length = int(self.headers.get("Content-Length", "0")) raw = self.rfile.read(length) data = json.loads(raw.decode("utf-8")) if raw else {} url = (data.get("url") or "").strip() if not url.startswith("http://") and not url.startswith("https://"): self._json(400, {"detail": "Invalid URL"}) return chromium_open(url) print(f"open_ok url={url}", flush=True) self._json(200, {"ok": True, "url": url}) except Exception as exc: print(f"open_fail err={exc}", flush=True) self._json(500, {"detail": str(exc)}) def log_message(self, format, *args): return if __name__ == "__main__": server = HTTPServer(("0.0.0.0", 7000), Handler) server.serve_forever()