The gateway's client speaks HTTP/2 wherever a server offers it, so every
Codex stream, every account's, went to chatgpt.com on one shared HTTP/2
connection, where Cloudflare's RST_STREAM ended a reply mid-way. The
official clients never speak HTTP/2 there: codex-cli 0.162.0, pointed at
a local TLS server, offers no ALPN at all (HTTP/1.1), and Pi's own Codex
provider (pi-ai) uses a WebSocket or Node's fetch, HTTP/1.1 both.
Requests to provider.CodexBase's host now go through an HTTP/1.1-only
transport (codex_http1.go), otherwise the same as the gateway's: proxy
dispatch, IPv4 fallback, head timeout, idle pool. Every other vendor
still gets HTTP/2. Covers the Codex subscription served to any agent, the
Codex agent's own passthrough and the models list, as all go through
s.client.
What was run:
- TestCodexBackendOverHTTP1: two TLS servers offering HTTP/2; the Codex
one is asked over HTTP/1.1, the other over HTTP/2.0. Fails without the
change ("asked over HTTP/2.0, not HTTP/1.1").
- go test -tags nogui ./internal/gateway ./internal/netproxy
./internal/provider under a scratch HOME pass; gofmt; go vet (darwin,
GOOS=linux nogui, GOOS=windows).
- curl --http1.1 to chatgpt.com/backend-api/codex/models answers over
HTTP/1.1 (401 unsigned).
Not reproduced: the reset itself is occasional and wasn't seen here, so
whether this alone ends it is for the reporter's use to say. A cut that
comes after the reply has begun still ends it with "connection lost
mid-reply", which Pi retries.
54 lines
1.9 KiB
Go
54 lines
1.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"net/http"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/netproxy"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// upstreamTLS is what the gateway's transports dial TLS with: nil, the
|
|
// system's roots, but for a test trusting its own server.
|
|
var upstreamTLS *tls.Config
|
|
|
|
// upstreamTransport is the gateway's transport to the vendors: a provider
|
|
// with a proxy of its own is sent through a transport kept for that proxy
|
|
// (#237). protocols nil speaks HTTP/2 wherever the server offers it.
|
|
func upstreamTransport(protocols *http.Protocols) http.RoundTripper {
|
|
return netproxy.Dispatch(&http.Transport{
|
|
Proxy: netproxy.Func,
|
|
ResponseHeaderTimeout: 10 * time.Minute,
|
|
MaxIdleConnsPerHost: 8,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
ForceAttemptHTTP2: protocols == nil,
|
|
Protocols: protocols,
|
|
TLSClientConfig: upstreamTLS,
|
|
})
|
|
}
|
|
|
|
// codexHTTP1 sends what goes to the ChatGPT backend (provider.CodexBase's
|
|
// host, chatgpt.com) over HTTP/1.1, and the rest over next. Codex itself
|
|
// offers no ALPN at all and so speaks HTTP/1.1 to it, and Pi's own Codex
|
|
// provider a WebSocket or Node's fetch, HTTP/1.1 both; the gateway's
|
|
// HTTP/2 put every Codex stream, every account's, on one connection,
|
|
// where Cloudflare in front reset one now and then mid-reply ("stream
|
|
// error: stream ID 21; INTERNAL_ERROR; received from peer", jasonz3g on
|
|
// X: Codex in Pi through magpie, never Codex or Pi alone).
|
|
type codexHTTP1 struct{ next, h1 http.RoundTripper }
|
|
|
|
func newCodexHTTP1(next http.RoundTripper) codexHTTP1 {
|
|
var h1 http.Protocols
|
|
h1.SetHTTP1(true)
|
|
return codexHTTP1{next: next, h1: upstreamTransport(&h1)}
|
|
}
|
|
|
|
func (t codexHTTP1) RoundTrip(r *http.Request) (*http.Response, error) {
|
|
if u, err := url.Parse(provider.CodexBase); err == nil && r.URL.Host == u.Host {
|
|
return t.h1.RoundTrip(r)
|
|
}
|
|
return t.next.RoundTrip(r)
|
|
}
|