mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-06 00:05:30 +00:00
The /v1/responses handler validates the request body with gjson.ValidBytes before doing anything else, then hands the same bytes to ParseGatewayRequest, which validates them a second time. After the single-root-walk change the validation is the majority of what that call does: at 1MiB it is about 510us of the remaining 730us. Add ParseGatewayRequestValidated for callers that have already established the body is valid JSON, and use it in the handler, where the only rewrite between the two points is applyOpenAIReasoningEffortPolicyForRequest (sjson, which preserves validity). The validating entry point is unchanged, so invalid JSON keeps producing a clean error everywhere else. ReplaceBody / CloneForBody keep validating as well, because their bytes can differ from the ones a caller validated. Equivalence tests assert the trusted variant produces identical results to the validating one on valid JSON, and the fuzz target checks the same for every input that parses. Benchmark (Apple M5 Pro, 3 allocs unchanged): responses 1MiB: 735us -> 239us anthropic 1MiB: 723us -> 207us (not yet wired: that handler has no earlier validation, so it keeps the validating path)