Files
magpie/internal/netproxy/overhttp1_test.go
T
yetone 1fd0bb9670 update: a download whose HTTP/2 transfer broke off tries again over HTTP/1.1
biubiu8802 on X (tweet 2118719081872540038): behind their proxy every
update failed with "download broke off, a network problem", while the
same proxy carried ordinary downloads. Confirmed in the code: download()
retried up to 5 times with Range, but each try went through
netproxy.Dispatch over HTTP/2 again; Dispatch moves a host to HTTP/1.1
only on a TLS record error (recordError: bad record MAC and kin), and
"unexpected EOF" or a stream reset is not one. So a proxy that cuts
HTTP/2 streams part way cut every try the same way.

- netproxy: OverHTTP1(ctx) sends every request under ctx through
  Dispatch's HTTP/1.1 copy (route.h1), kept through With.
  BrokeOff(err) says a transfer broke off (net.Error, unexpected EOF,
  HTTP/2 stream error / GOAWAY / lost connection, a reset; not a
  context that ended). AgainOverHTTP1 runs a download once more over
  HTTP/1.1 after such a failure.
- update: the tries after a broken one go over HTTP/1.1, and still
  resume with Range from the bytes kept.
- Siblings, the other big downloads through Dispatch's client: the
  masking model and pf-serve (pii.fetch, one more try over HTTP/1.1
  from what is on the .part file), Bun's zip (plugin getURLFrom),
  Claude Code's binary (claudecode.download) and the Grok CLI
  (grokDownload) each try once more over HTTP/1.1.
- docs/subsystems/providers-accounts.md names it.

Run (mote, Go 1.26.8, at this tree, base 80ae904c2 = origin/main fetched
just before push):
- TestDownloadBrokenOverHTTP2TriesHTTP1 (httptest TLS HTTP/2 server
  that resets each HTTP/2 stream after 100KB, serves HTTP/1.1 whole):
  red with the update.go line taken out, all six tries over HTTP/2
  ("stream error: ... INTERNAL_ERROR"); green after, asked
  ["HTTP/2.0 ", "HTTP/1.1 bytes=102400-"].
- TestOverHTTP1EveryHost, TestAgainOverHTTP1, TestBrokeOff: green.
- go test ./internal/netproxy ./internal/update ./internal/pii
  ./internal/claudecode ./internal/plugin ./internal/provider: ok.
- go test ./... -count=1 -timeout 45m: 86 ok, gateway 795s and agent
  303s ok; internal/gui red only on two AppKit tests already filed for
  mote's macOS 27: TestMainWindowCorners/macOS_26_SDK (#3949) and
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322). Nothing in gui is
  touched here.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet of the touched
  packages clean.

Not tried: a real proxy that cuts HTTP/2 (the reporter's); the retry of
pii, Bun, Claude Code and Grok against a real host (unit-covered by
AgainOverHTTP1 only); Windows/Linux test runs.
2026-11-06 23:44:58 +08:00

97 lines
2.8 KiB
Go

package netproxy
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"syscall"
"testing"
)
// A request under OverHTTP1 goes over HTTP/1.1 to every host, through a
// provider's proxy (With) too; one without it over HTTP/2 as before.
func TestOverHTTP1EveryHost(t *testing.T) {
clearProxyEnv(t)
srv, d := h2Server(t, nil)
other, _ := h2Server(t, nil)
h1 := OverHTTP1(context.Background())
for _, u := range []string{srv.URL, other.URL} {
if got := protoOf(t, d, h1, u); got != "HTTP/1.1" {
t.Errorf("%s under OverHTTP1 spoken %s", u, got)
}
}
if got := protoOf(t, d, With(h1, ""), srv.URL); got != "HTTP/1.1" {
t.Errorf("With dropped OverHTTP1: spoken %s", got)
}
if got := protoOf(t, d, context.Background(), srv.URL); got != "HTTP/2.0" {
t.Errorf("not asked, spoken %s", got)
}
}
// AgainOverHTTP1 tries once more over HTTP/1.1 after a transfer an
// HTTP/2 stream reset broke off, and not after a failure of another kind
// or a context that ended.
func TestAgainOverHTTP1(t *testing.T) {
clearProxyEnv(t)
srv, d := h2Server(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", "1000000")
io.WriteString(w, strings.Repeat("x", 4096))
w.(http.Flusher).Flush()
if r.ProtoMajor == 2 {
panic(http.ErrAbortHandler)
}
io.WriteString(w, strings.Repeat("x", 1000000-4096))
})
c := &http.Client{Transport: d}
var protos []string
get := func(ctx context.Context) error {
req, _ := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL, nil)
res, err := c.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
protos = append(protos, res.Proto)
_, err = io.Copy(io.Discard, res.Body)
return err
}
if err := AgainOverHTTP1(context.Background(), get); err != nil {
t.Fatalf("AgainOverHTTP1: %v (protos %q)", err, protos)
}
if fmt.Sprint(protos) != "[HTTP/2.0 HTTP/1.1]" {
t.Fatalf("protos = %q", protos)
}
n := 0
notNet := errors.New("checksum")
AgainOverHTTP1(context.Background(), func(context.Context) error { n++; return notNet })
ctx, cancel := context.WithCancel(context.Background())
cancel()
AgainOverHTTP1(ctx, func(context.Context) error { n++; return io.ErrUnexpectedEOF })
if n != 2 {
t.Fatalf("tried %d times, want 2 (no second try)", n)
}
}
func TestBrokeOff(t *testing.T) {
for _, err := range []error{
io.ErrUnexpectedEOF,
fmt.Errorf("read: %w", syscall.ECONNRESET),
errors.New("stream error: stream ID 1; INTERNAL_ERROR; received from peer"),
errors.New("http2: server sent GOAWAY and closed the connection"),
errors.New("http2: client connection lost"),
} {
if !BrokeOff(err) {
t.Errorf("BrokeOff(%v) = false", err)
}
}
for _, err := range []error{nil, context.Canceled, fmt.Errorf("x: %w", context.DeadlineExceeded), errors.New("download magpie: 404 Not Found")} {
if BrokeOff(err) {
t.Errorf("BrokeOff(%v) = true", err)
}
}
}