forked from longtao.wu/droboticflow
285 lines
14 KiB
Python
285 lines
14 KiB
Python
"""Delay selected real control responses without substituting their contents.
|
|
|
|
The relay has no mock responses. Each request is forwarded to the localhost
|
|
acceptance API; successful upstream status/body bytes are delivered unchanged.
|
|
The default mode holds an already-terminal Session status until release().
|
|
Mapping-plan mode holds only a selected mapping's genuine planned status before
|
|
that client can reserve its file, allowing a real competing client to commit.
|
|
Lost-completion mode drops exactly one genuine successful direct-completion
|
|
response by closing the downstream connection; all retries reach the real API.
|
|
BOS upload URLs and request bodies/headers never enter the evidence file.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import hashlib
|
|
import http.client
|
|
import http.server
|
|
import json
|
|
import pathlib
|
|
import re
|
|
import socket
|
|
import threading
|
|
import time
|
|
import urllib.parse
|
|
from datetime import UTC, datetime
|
|
|
|
|
|
_HOP_HEADERS = {
|
|
"connection", "keep-alive", "proxy-authenticate", "proxy-authorization",
|
|
"te", "trailer", "transfer-encoding", "upgrade",
|
|
}
|
|
_STATUS_PATH = re.compile(r"^/api/external/object-sync/sessions/[^/]+/status$")
|
|
_DIRECT_COMPLETE_PATH = re.compile(r"^/api/external/object-sync/items/[^/]+/uploads/direct/complete$")
|
|
|
|
|
|
class RealResponseGate:
|
|
"""One-run localhost relay; release permanently opens the response gate."""
|
|
|
|
def __init__(self, backend_url: str, evidence_file, port: int = 19442, mode: str = "terminal_progress") -> None:
|
|
if mode not in {"terminal_progress", "mapping_plan", "lost_completion"}:
|
|
raise ValueError("mode must be terminal_progress, mapping_plan, or lost_completion")
|
|
parsed = urllib.parse.urlsplit(backend_url)
|
|
if parsed.scheme != "http" or parsed.hostname not in {"127.0.0.1", "localhost", "::1"}:
|
|
raise ValueError("backend must be the real localhost acceptance HTTP API")
|
|
if parsed.username or parsed.password or parsed.query or parsed.fragment or parsed.path not in {"", "/"}:
|
|
raise ValueError("backend must contain only scheme, localhost, and port")
|
|
self.backend_url = backend_url.rstrip("/")
|
|
self.evidence_file = pathlib.Path(evidence_file)
|
|
self.port = port
|
|
self.mode = mode
|
|
self._hold_mapping_id = ""
|
|
self._host, self._backend_port = parsed.hostname, parsed.port or 80
|
|
self._released = threading.Event()
|
|
self._held = threading.Event()
|
|
self._dropped = threading.Event()
|
|
self._drop_lock = threading.Lock()
|
|
self._drop_used = False
|
|
self._evidence_lock = threading.Lock()
|
|
self._connection_lock = threading.Lock()
|
|
self._active_condition = threading.Condition()
|
|
self._active_handlers = 0
|
|
self._connections: set[http.client.HTTPConnection] = set()
|
|
self._server: http.server.ThreadingHTTPServer | None = None
|
|
self._thread: threading.Thread | None = None
|
|
|
|
@property
|
|
def base_url(self) -> str:
|
|
return f"http://127.0.0.1:{self.port}"
|
|
|
|
@property
|
|
def held(self) -> threading.Event:
|
|
"""Signal that the selected real response reached the gate."""
|
|
return self._held
|
|
|
|
@property
|
|
def dropped(self) -> threading.Event:
|
|
"""Signal that one successful real completion response was discarded."""
|
|
return self._dropped
|
|
|
|
@property
|
|
def hold_mapping_id(self) -> str:
|
|
return self._hold_mapping_id
|
|
|
|
@hold_mapping_id.setter
|
|
def hold_mapping_id(self, value: str) -> None:
|
|
if not isinstance(value, str):
|
|
raise ValueError("hold_mapping_id must be a string")
|
|
self._hold_mapping_id = value
|
|
|
|
def start(self) -> "RealResponseGate":
|
|
if self._server is not None:
|
|
raise RuntimeError("relay already started")
|
|
gate = self
|
|
|
|
class Handler(http.server.BaseHTTPRequestHandler):
|
|
protocol_version = "HTTP/1.1"
|
|
|
|
def log_message(self, *_args) -> None:
|
|
pass
|
|
|
|
def handle_expect_100(self) -> bool:
|
|
# Forwarding begins only after the complete control body arrives.
|
|
# Native control requests do not use Expect. Do not invent an
|
|
# interim response for unsupported clients.
|
|
self.close_connection = True
|
|
return False
|
|
|
|
def _request_body(self) -> bytes:
|
|
if "chunked" in self.headers.get("Transfer-Encoding", "").lower():
|
|
chunks = []
|
|
while True:
|
|
line = self.rfile.readline(8192)
|
|
size = int(line.split(b";", 1)[0].strip(), 16)
|
|
if size == 0:
|
|
while self.rfile.readline(8192) not in {b"\r\n", b"\n", b""}:
|
|
pass
|
|
return b"".join(chunks)
|
|
block = self.rfile.read(size)
|
|
if len(block) != size or self.rfile.read(2) != b"\r\n":
|
|
raise ValueError("incomplete_request_entity")
|
|
chunks.append(block)
|
|
length = int(self.headers.get("Content-Length", "0"))
|
|
if length < 0:
|
|
raise ValueError("invalid_request_length")
|
|
body = self.rfile.read(length)
|
|
if len(body) != length:
|
|
raise ValueError("incomplete_request_entity")
|
|
return body
|
|
|
|
def _forward(self) -> None:
|
|
with gate._active_condition:
|
|
gate._active_handlers += 1
|
|
endpoint = urllib.parse.urlsplit(self.path).path
|
|
record = {
|
|
"at": datetime.now(UTC).isoformat(), "method": self.command,
|
|
"endpoint": endpoint, "upstreamStatus": None, "upstreamBytes": 0,
|
|
"upstreamElapsedMs": None, "heldElapsedMs": 0,
|
|
"requestBodySha256": None, "responseBodySha256": None,
|
|
"responseDelivered": False, "connectionLost": False,
|
|
"responseDropped": False,
|
|
"gateMode": gate.mode, "gateTimedOut": False,
|
|
}
|
|
connection = None
|
|
try:
|
|
if not self.path.startswith("/"):
|
|
raise ValueError("absolute_request_target_not_supported")
|
|
body = self._request_body()
|
|
record["requestBodySha256"] = hashlib.sha256(body).hexdigest()
|
|
connection = http.client.HTTPConnection(gate._host, gate._backend_port, timeout=60)
|
|
with gate._connection_lock:
|
|
gate._connections.add(connection)
|
|
started = time.monotonic()
|
|
# Preserve business headers, including duplicate headers.
|
|
# HTTP framing is regenerated after decoding chunked input.
|
|
connection.putrequest(self.command, self.path, skip_host=True, skip_accept_encoding=True)
|
|
excluded = _HOP_HEADERS | {"content-length"}
|
|
excluded.update(part.strip().lower() for part in self.headers.get("Connection", "").split(",") if part.strip())
|
|
for name, value in self.headers.raw_items():
|
|
if name.lower() not in excluded:
|
|
connection.putheader(name, value)
|
|
if "Host" not in self.headers:
|
|
connection.putheader("Host", gate._host + ":" + str(gate._backend_port))
|
|
if body or "Content-Length" in self.headers or "Transfer-Encoding" in self.headers:
|
|
connection.putheader("Content-Length", str(len(body)))
|
|
connection.endheaders(body if body else None)
|
|
response = connection.getresponse()
|
|
raw = response.read()
|
|
record.update(upstreamStatus=response.status, upstreamBytes=len(raw),
|
|
upstreamElapsedMs=round((time.monotonic() - started) * 1000),
|
|
responseBodySha256=hashlib.sha256(raw).hexdigest())
|
|
drop = False
|
|
if gate.mode == "lost_completion" and self.command == "POST" and _DIRECT_COMPLETE_PATH.fullmatch(endpoint) and response.status == 200:
|
|
with gate._drop_lock:
|
|
if not gate._drop_used:
|
|
gate._drop_used = True
|
|
drop = True
|
|
if drop:
|
|
# The real API has already supplied its entire response.
|
|
# Send no status line or body: force a real downstream
|
|
# EOF, then allow every subsequent request to forward.
|
|
record["responseDropped"] = True
|
|
record["connectionLost"] = True
|
|
try:
|
|
self.connection.shutdown(socket.SHUT_RDWR)
|
|
except OSError:
|
|
pass
|
|
self.connection.close()
|
|
gate._held.set()
|
|
gate._dropped.set()
|
|
return
|
|
hold = False
|
|
if gate.mode == "terminal_progress" and self.command == "GET" and _STATUS_PATH.fullmatch(endpoint) and response.status == 200:
|
|
try:
|
|
progress = json.loads(raw).get("progress", {})
|
|
total, terminal = progress.get("totalItems"), progress.get("terminalItems")
|
|
hold = isinstance(total, int) and total > 0 and terminal == total
|
|
except (ValueError, AttributeError, TypeError):
|
|
pass
|
|
if gate.mode == "mapping_plan" and gate.hold_mapping_id and self.command == "GET" and _STATUS_PATH.fullmatch(endpoint) and response.status == 200:
|
|
try:
|
|
status_body = json.loads(raw)
|
|
hold = status_body.get("mappingId") == gate.hold_mapping_id and status_body.get("status") == "planned" and status_body.get("progress", {}).get("terminalItems") == 0
|
|
except (ValueError, AttributeError, TypeError):
|
|
pass
|
|
if hold:
|
|
held_started = time.monotonic()
|
|
gate._held.set()
|
|
released = gate._released.wait(timeout=240 if gate.mode == "terminal_progress" else 60)
|
|
record["heldElapsedMs"] = round((time.monotonic() - held_started) * 1000)
|
|
record["gateTimedOut"] = not released
|
|
# Only framing headers change; upstream status/reason and
|
|
# every body byte remain exactly what the real API returned.
|
|
self.send_response_only(response.status, response.reason)
|
|
excluded = _HOP_HEADERS | {"content-length"}
|
|
excluded.update(part.strip().lower() for part in response.getheader("Connection", "").split(",") if part.strip())
|
|
for name, value in response.getheaders():
|
|
if name.lower() not in excluded:
|
|
self.send_header(name, value)
|
|
self.send_header("Content-Length", str(len(raw)))
|
|
self.send_header("Connection", "close")
|
|
self.end_headers()
|
|
self.wfile.write(raw)
|
|
self.wfile.flush()
|
|
record["responseDelivered"] = True
|
|
except (OSError, http.client.HTTPException, ValueError, OverflowError):
|
|
# A transport failure closes the connection. No fake HTTP
|
|
# status or replacement JSON/body is generated.
|
|
record["connectionLost"] = True
|
|
finally:
|
|
self.close_connection = True
|
|
if connection is not None:
|
|
connection.close()
|
|
with gate._connection_lock:
|
|
gate._connections.discard(connection)
|
|
try:
|
|
gate._record(record)
|
|
finally:
|
|
with gate._active_condition:
|
|
gate._active_handlers -= 1
|
|
gate._active_condition.notify_all()
|
|
|
|
do_GET = do_POST = do_PUT = do_DELETE = do_HEAD = do_OPTIONS = _forward
|
|
|
|
class Server(http.server.ThreadingHTTPServer):
|
|
daemon_threads = True
|
|
|
|
def handle_error(self, _request, _client_address) -> None:
|
|
# Base error tracebacks could reveal request information.
|
|
pass
|
|
|
|
self.evidence_file.parent.mkdir(parents=True, exist_ok=True)
|
|
self._server = Server(("127.0.0.1", self.port), Handler)
|
|
self.port = self._server.server_address[1]
|
|
self._thread = threading.Thread(target=self._server.serve_forever, name="real-control-response-gate", daemon=True)
|
|
self._thread.start()
|
|
return self
|
|
|
|
def _record(self, value: dict) -> None:
|
|
with self._evidence_lock:
|
|
with self.evidence_file.open("a", encoding="utf-8") as output:
|
|
output.write(json.dumps(value, ensure_ascii=False, separators=(",", ":")) + "\n")
|
|
|
|
def release(self) -> None:
|
|
self._released.set()
|
|
|
|
def close(self) -> None:
|
|
self.release()
|
|
if self._server is None:
|
|
return
|
|
self._server.shutdown()
|
|
self._server.server_close()
|
|
if self._thread is not None:
|
|
self._thread.join(timeout=5)
|
|
with self._connection_lock:
|
|
connections = tuple(self._connections)
|
|
for connection in connections:
|
|
try:
|
|
if connection.sock is not None:
|
|
connection.sock.shutdown(socket.SHUT_RDWR)
|
|
except OSError:
|
|
pass
|
|
connection.close()
|
|
with self._active_condition:
|
|
self._active_condition.wait_for(lambda: self._active_handlers == 0, timeout=5)
|
|
self._server = None
|