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.6 KiB
Go
54 lines
1.6 KiB
Go
package gateway
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// The ChatGPT backend is spoken to over HTTP/1.1, as Codex and Pi speak
|
|
// to it, while every other vendor offering HTTP/2 still gets it: on one
|
|
// HTTP/2 connection every Codex stream was reset now and then mid-reply
|
|
// (jasonz3g on X, "stream ID 21; INTERNAL_ERROR; received from peer").
|
|
func TestCodexBackendOverHTTP1(t *testing.T) {
|
|
fresh(t)
|
|
proto := func(w http.ResponseWriter, r *http.Request) { io.WriteString(w, r.Proto) }
|
|
chatgpt := httptest.NewUnstartedServer(http.HandlerFunc(proto))
|
|
chatgpt.EnableHTTP2 = true
|
|
chatgpt.StartTLS()
|
|
t.Cleanup(chatgpt.Close)
|
|
other := httptest.NewUnstartedServer(http.HandlerFunc(proto))
|
|
other.EnableHTTP2 = true
|
|
other.StartTLS()
|
|
t.Cleanup(other.Close)
|
|
pool := x509.NewCertPool()
|
|
pool.AddCert(chatgpt.Certificate())
|
|
pool.AddCert(other.Certificate())
|
|
was, wasTLS := provider.CodexBase, upstreamTLS
|
|
provider.CodexBase = chatgpt.URL + "/backend-api/codex"
|
|
upstreamTLS = &tls.Config{RootCAs: pool}
|
|
t.Cleanup(func() { provider.CodexBase, upstreamTLS = was, wasTLS })
|
|
s := New()
|
|
got := func(u string) string {
|
|
t.Helper()
|
|
res, err := s.client.Post(u, "application/json", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer res.Body.Close()
|
|
b, _ := io.ReadAll(res.Body)
|
|
return string(b)
|
|
}
|
|
if p := got(provider.CodexBase + "/responses"); p != "HTTP/1.1" {
|
|
t.Errorf("the ChatGPT backend was asked over %s, not HTTP/1.1", p)
|
|
}
|
|
if p := got(other.URL + "/v1/responses"); p != "HTTP/2.0" {
|
|
t.Errorf("another vendor was asked over %s, not HTTP/2.0", p)
|
|
}
|
|
}
|