mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
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 <AiFLYF@users.noreply.github.com>
This commit is contained in:
1 parent
5c5100c470
commit
4e2edbf1e6
2 files changed
+11
-2
No files matched your search
@@ -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 != "" {
|
||||
|
||||
+11
-1
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user