mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-22 17:24:27 +08:00
ci: harden local pdc mock transport handling
This commit is contained in:
33
.github/workflows/release.yml
vendored
33
.github/workflows/release.yml
vendored
@@ -1061,6 +1061,39 @@ jobs:
|
|||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
retention-days: 90
|
retention-days: 90
|
||||||
|
|
||||||
|
- name: Dump PDC Diagnostics
|
||||||
|
if: failure()
|
||||||
|
shell: pwsh
|
||||||
|
run: |
|
||||||
|
if (Test-Path "pdc-work/pdc-mock.out.log") {
|
||||||
|
Write-Host "===== pdc-mock stdout ====="
|
||||||
|
Get-Content "pdc-work/pdc-mock.out.log" -ErrorAction SilentlyContinue | Write-Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Test-Path "pdc-work/pdc-mock.err.log") {
|
||||||
|
Write-Host "===== pdc-mock stderr ====="
|
||||||
|
Get-Content "pdc-work/pdc-mock.err.log" -ErrorAction SilentlyContinue | Write-Host
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Test-Path "pdc-output/mock-pdc") {
|
||||||
|
Write-Host "===== pdc-mock captured payloads ====="
|
||||||
|
Get-ChildItem "pdc-output/mock-pdc" -Recurse -File | ForEach-Object {
|
||||||
|
Write-Host "--- $($_.FullName) ---"
|
||||||
|
Get-Content $_.FullName -ErrorAction SilentlyContinue | Write-Host
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- name: Upload PDC Diagnostics Artifact
|
||||||
|
if: always()
|
||||||
|
uses: actions/upload-artifact@v4
|
||||||
|
with:
|
||||||
|
name: pdc-diagnostics
|
||||||
|
path: |
|
||||||
|
pdc-work/pdc-mock*.log
|
||||||
|
pdc-output/mock-pdc/**
|
||||||
|
if-no-files-found: ignore
|
||||||
|
retention-days: 30
|
||||||
|
|
||||||
github-release:
|
github-release:
|
||||||
needs: [ prepare, build-windows, build-linux, build-macos, publish-pdc ]
|
needs: [ prepare, build-windows, build-linux, build-macos, publish-pdc ]
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ def _utc_now_text() -> str:
|
|||||||
|
|
||||||
|
|
||||||
class PdcMockHandler(BaseHTTPRequestHandler):
|
class PdcMockHandler(BaseHTTPRequestHandler):
|
||||||
|
protocol_version = "HTTP/1.1"
|
||||||
token = ""
|
token = ""
|
||||||
data_dir = Path(".")
|
data_dir = Path(".")
|
||||||
|
|
||||||
@@ -20,22 +21,74 @@ class PdcMockHandler(BaseHTTPRequestHandler):
|
|||||||
self.send_response(status_code)
|
self.send_response(status_code)
|
||||||
self.send_header("Content-Type", "application/json; charset=utf-8")
|
self.send_header("Content-Type", "application/json; charset=utf-8")
|
||||||
self.send_header("Content-Length", str(len(body)))
|
self.send_header("Content-Length", str(len(body)))
|
||||||
|
self.send_header("Connection", "close")
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(body)
|
self.wfile.write(body)
|
||||||
|
self.wfile.flush()
|
||||||
|
self.close_connection = True
|
||||||
|
|
||||||
|
def handle_expect_100(self) -> bool:
|
||||||
|
self.send_response_only(100)
|
||||||
|
self.end_headers()
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _read_chunked_body(self) -> bytes:
|
||||||
|
chunks = bytearray()
|
||||||
|
while True:
|
||||||
|
size_line = self.rfile.readline()
|
||||||
|
if not size_line:
|
||||||
|
break
|
||||||
|
|
||||||
|
size_line = size_line.strip()
|
||||||
|
if not size_line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
size_text = size_line.split(b";", 1)[0]
|
||||||
|
chunk_size = int(size_text, 16)
|
||||||
|
if chunk_size == 0:
|
||||||
|
# Consume optional trailer headers until the terminating blank line.
|
||||||
|
while True:
|
||||||
|
trailer = self.rfile.readline()
|
||||||
|
if trailer in (b"", b"\r\n", b"\n"):
|
||||||
|
break
|
||||||
|
break
|
||||||
|
|
||||||
|
remaining = chunk_size
|
||||||
|
while remaining > 0:
|
||||||
|
part = self.rfile.read(remaining)
|
||||||
|
if not part:
|
||||||
|
raise ConnectionError("unexpected end of stream while reading chunked request body")
|
||||||
|
chunks.extend(part)
|
||||||
|
remaining -= len(part)
|
||||||
|
|
||||||
|
chunk_terminator = self.rfile.read(2)
|
||||||
|
if chunk_terminator == b"\r\n":
|
||||||
|
continue
|
||||||
|
if chunk_terminator[:1] != b"\n":
|
||||||
|
raise ValueError("invalid chunk terminator")
|
||||||
|
|
||||||
|
return bytes(chunks)
|
||||||
|
|
||||||
|
def _read_request_body(self) -> bytes:
|
||||||
|
transfer_encoding = (self.headers.get("Transfer-Encoding") or "").lower()
|
||||||
|
if "chunked" in transfer_encoding:
|
||||||
|
return self._read_chunked_body()
|
||||||
|
|
||||||
def _read_json_body(self) -> dict:
|
|
||||||
length = int(self.headers.get("Content-Length", "0"))
|
length = int(self.headers.get("Content-Length", "0"))
|
||||||
if length <= 0:
|
if length <= 0:
|
||||||
return {}
|
return b""
|
||||||
raw = self.rfile.read(length)
|
return self.rfile.read(length)
|
||||||
if not raw:
|
|
||||||
return {}
|
|
||||||
try:
|
|
||||||
return json.loads(raw.decode("utf-8"))
|
|
||||||
except Exception:
|
|
||||||
return {}
|
|
||||||
|
|
||||||
def _save_payload(self, name: str, payload: dict) -> None:
|
def _read_json_body(self) -> tuple[dict, bytes]:
|
||||||
|
raw = self._read_request_body()
|
||||||
|
if not raw:
|
||||||
|
return {}, raw
|
||||||
|
try:
|
||||||
|
return json.loads(raw.decode("utf-8")), raw
|
||||||
|
except Exception:
|
||||||
|
return {}, raw
|
||||||
|
|
||||||
|
def _save_payload(self, name: str, payload: dict, raw_body: bytes) -> None:
|
||||||
out = self.data_dir / f"{name}.json"
|
out = self.data_dir / f"{name}.json"
|
||||||
out.parent.mkdir(parents=True, exist_ok=True)
|
out.parent.mkdir(parents=True, exist_ok=True)
|
||||||
out.write_text(
|
out.write_text(
|
||||||
@@ -43,6 +96,10 @@ class PdcMockHandler(BaseHTTPRequestHandler):
|
|||||||
{
|
{
|
||||||
"savedAtUtc": _utc_now_text(),
|
"savedAtUtc": _utc_now_text(),
|
||||||
"path": self.path,
|
"path": self.path,
|
||||||
|
"method": self.command,
|
||||||
|
"headers": {key: value for key, value in self.headers.items()},
|
||||||
|
"rawBodyLength": len(raw_body),
|
||||||
|
"rawBodyPreview": raw_body[:4096].decode("utf-8", errors="replace"),
|
||||||
"payload": payload,
|
"payload": payload,
|
||||||
},
|
},
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
@@ -66,16 +123,23 @@ class PdcMockHandler(BaseHTTPRequestHandler):
|
|||||||
self._write_json(404, {"error": "not_found", "path": self.path})
|
self._write_json(404, {"error": "not_found", "path": self.path})
|
||||||
|
|
||||||
def do_POST(self) -> None:
|
def do_POST(self) -> None:
|
||||||
|
print(
|
||||||
|
f"[pdc-mock] {self.command} {self.path} "
|
||||||
|
f"content-length={self.headers.get('Content-Length', '')} "
|
||||||
|
f"transfer-encoding={self.headers.get('Transfer-Encoding', '')} "
|
||||||
|
f"expect={self.headers.get('Expect', '')}"
|
||||||
|
)
|
||||||
|
|
||||||
if not self._check_token():
|
if not self._check_token():
|
||||||
self._write_json(401, {"error": "unauthorized"})
|
self._write_json(401, {"error": "unauthorized"})
|
||||||
return
|
return
|
||||||
|
|
||||||
payload = self._read_json_body()
|
payload, raw_body = self._read_json_body()
|
||||||
|
|
||||||
if self.path == "/api/v1/fileMaps/diff":
|
if self.path == "/api/v1/fileMaps/diff":
|
||||||
items = payload.get("items") if isinstance(payload, dict) else {}
|
items = payload.get("items") if isinstance(payload, dict) else {}
|
||||||
keys = sorted(items.keys()) if isinstance(items, dict) else []
|
keys = sorted(items.keys()) if isinstance(items, dict) else []
|
||||||
self._save_payload("filemaps-diff-request", payload)
|
self._save_payload("filemaps-diff-request", payload, raw_body)
|
||||||
# CI fallback mode: return empty diff to avoid long object uploads
|
# CI fallback mode: return empty diff to avoid long object uploads
|
||||||
# against a local mock endpoint. Real PDC endpoint will return
|
# against a local mock endpoint. Real PDC endpoint will return
|
||||||
# actual missing object hashes.
|
# actual missing object hashes.
|
||||||
@@ -91,7 +155,7 @@ class PdcMockHandler(BaseHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if self.path == "/api/v1/fileMaps/upload":
|
if self.path == "/api/v1/fileMaps/upload":
|
||||||
self._save_payload("filemaps-upload-request", payload)
|
self._save_payload("filemaps-upload-request", payload, raw_body)
|
||||||
result = {
|
result = {
|
||||||
"success": True,
|
"success": True,
|
||||||
"code": 0,
|
"code": 0,
|
||||||
@@ -106,14 +170,11 @@ class PdcMockHandler(BaseHTTPRequestHandler):
|
|||||||
if m:
|
if m:
|
||||||
primary_version = m.group(1)
|
primary_version = m.group(1)
|
||||||
version = m.group(2)
|
version = m.group(2)
|
||||||
distribution_id = f"{primary_version}-{version}"
|
self._save_payload("distribution-request", payload, raw_body)
|
||||||
self._save_payload("distribution-request", payload)
|
|
||||||
result = {
|
result = {
|
||||||
"success": True,
|
"success": True,
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "ok",
|
"message": "ok",
|
||||||
"content": {"distributionId": distribution_id},
|
|
||||||
"Content": {"distributionId": distribution_id},
|
|
||||||
}
|
}
|
||||||
self._write_json(200, result)
|
self._write_json(200, result)
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user