From 4e2edbf1e678ab6e770c056a9bf5c4b028fac5d3 Mon Sep 17 00:00:00 2001 From: FLYF <150890698+AiFLYF@users.noreply.github.com> Date: Fri, 23 Oct 2026 09:24:37 +0800 Subject: [PATCH] fix(upstream): let the runtime own Accept and Content-Length placement (#143) A captured 2.1.1 request carries neither an Accept header nor a trailing Content-Length: fetch fills Accept with */* and computes Content-Length as part of the common-header block. The gateway set both itself, so the v2 lane sent an Accept value the client never produces and put Content-Length last, where no fetch request has it. Header order and presence are part of what a fingerprint sees, so the v2 lane now sets no Accept and the Bun head emits Content-Length among the common headers instead of trailing the request. The v1 lane keeps its own Accept, which is what that client sends. Co-authored-by: AiFLYF --- internal/gateway/upstream.go | 1 - internal/httpx/bun.go | 12 +++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/gateway/upstream.go b/internal/gateway/upstream.go index 8aeaf82..29451fb 100644 --- a/internal/gateway/upstream.go +++ b/internal/gateway/upstream.go @@ -1583,7 +1583,6 @@ func setV1Headers(header http.Header, protocol wire.Protocol, body []byte, ids i // requests carry a fixed anthropic-beta set; upstreamRequest adds // anthropic-version and the beta query. func setV2Headers(header http.Header, protocol wire.Protocol, ids identity.RequestIDs) { - header.Set("Accept", "application/json, text/event-stream") header.Set("x-opencode-client", "cli") rootSession := ids.Session if ids.ParentSession != "" { diff --git a/internal/httpx/bun.go b/internal/httpx/bun.go index 369319e..23363a8 100644 --- a/internal/httpx/bun.go +++ b/internal/httpx/bun.go @@ -532,8 +532,18 @@ func bunRequestHead(req *http.Request, body []byte) ([]byte, bool, error) { } var head strings.Builder head.WriteString(method + " " + req.URL.RequestURI() + " HTTP/1.1\r\n") + // fetch computes Content-Length itself when it has a body, so it appears in + // the common-header block instead of trailing the request. Header order is + // part of what a fingerprint sees, so it belongs where the runtime puts it. + wroteLength := false for _, name := range bunCommonHeaders { lower := strings.ToLower(name) + if lower == "content-length" && body != nil { + head.WriteString(name + ": " + strconv.Itoa(len(body)) + "\r\n") + delete(values, lower) + wroteLength = true + continue + } if value, ok := values[lower]; ok { head.WriteString(name + ": " + value + "\r\n") delete(values, lower) @@ -563,7 +573,7 @@ func bunRequestHead(req *http.Request, body []byte) ([]byte, bool, error) { if !hasEncoding { head.WriteString("Accept-Encoding: gzip, deflate, br, zstd\r\n") } - if body != nil { + if body != nil && !wroteLength { head.WriteString("Content-Length: " + strconv.Itoa(len(body)) + "\r\n") } head.WriteString("\r\n")