|
| 1 | +package http1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/gosuda/zeroproxy/internal/cookiejar" |
| 15 | +) |
| 16 | + |
| 17 | +func TestHTTP1KeepAliveReusesIdleConnectionAfterEOF(t *testing.T) { |
| 18 | + mux := &pipeMux{streams: make(chan net.Conn, 2)} |
| 19 | + engine := &Engine{Mux: mux} |
| 20 | + tab := &TabState{CookieJar: cookiejar.New(), StreamIsolationKey: []byte("0123456789abcdef0123456789abcdef")} |
| 21 | + target, _ := url.Parse("http://example.com/one") |
| 22 | + serverDone := make(chan error, 1) |
| 23 | + go func() { |
| 24 | + c := <-mux.streams |
| 25 | + defer c.Close() |
| 26 | + br := bufio.NewReader(c) |
| 27 | + if err := acceptSOCKSConnect(br, c); err != nil { |
| 28 | + serverDone <- err |
| 29 | + return |
| 30 | + } |
| 31 | + for _, wantPath := range []string{"/one", "/two"} { |
| 32 | + req, err := http.ReadRequest(br) |
| 33 | + if err != nil { |
| 34 | + serverDone <- err |
| 35 | + return |
| 36 | + } |
| 37 | + if req.URL.RequestURI() != wantPath { |
| 38 | + serverDone <- fmt.Errorf("request path = %q, want %q", req.URL.RequestURI(), wantPath) |
| 39 | + return |
| 40 | + } |
| 41 | + if _, err := io.WriteString(c, "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok"); err != nil { |
| 42 | + serverDone <- err |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | + serverDone <- nil |
| 47 | + }() |
| 48 | + |
| 49 | + roundTripAndClose(t, engine, tab, target) |
| 50 | + second := *target |
| 51 | + second.Path = "/two" |
| 52 | + roundTripAndClose(t, engine, tab, &second) |
| 53 | + if err := <-serverDone; err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + select { |
| 57 | + case c := <-mux.streams: |
| 58 | + _ = c.Close() |
| 59 | + t.Fatal("HTTP/1.1 keep-alive opened a second target connection") |
| 60 | + default: |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func TestHTTP1DoesNotReuseWhenBodyClosedBeforeEOF(t *testing.T) { |
| 65 | + mux := &pipeMux{streams: make(chan net.Conn, 2)} |
| 66 | + engine := &Engine{Mux: mux} |
| 67 | + tab := &TabState{CookieJar: cookiejar.New(), StreamIsolationKey: []byte("0123456789abcdef0123456789abcdef")} |
| 68 | + target, _ := url.Parse("http://example.com/partial") |
| 69 | + firstDone := serveOneHTTP1Response(t, mux, "/partial", "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\nbody") |
| 70 | + |
| 71 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 72 | + defer cancel() |
| 73 | + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil) |
| 74 | + resp, err := engine.RoundTrip(ctx, req, target, tab) |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + buf := make([]byte, 1) |
| 79 | + if _, err := io.ReadFull(resp.Body, buf); err != nil { |
| 80 | + t.Fatal(err) |
| 81 | + } |
| 82 | + if err := resp.Body.Close(); err != nil { |
| 83 | + t.Fatal(err) |
| 84 | + } |
| 85 | + assertNoIdleHTTP1(t, engine, target, tab) |
| 86 | + if err := <-firstDone; err != nil { |
| 87 | + t.Fatal(err) |
| 88 | + } |
| 89 | + |
| 90 | + second := *target |
| 91 | + second.Path = "/after-partial-close" |
| 92 | + secondDone := serveOneHTTP1Response(t, mux, "/after-partial-close", "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok") |
| 93 | + roundTripAndClose(t, engine, tab, &second) |
| 94 | + if err := <-secondDone; err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestHTTP1DoesNotReuseConnectionCloseResponse(t *testing.T) { |
| 100 | + mux := &pipeMux{streams: make(chan net.Conn, 2)} |
| 101 | + engine := &Engine{Mux: mux} |
| 102 | + tab := &TabState{CookieJar: cookiejar.New(), StreamIsolationKey: []byte("0123456789abcdef0123456789abcdef")} |
| 103 | + target, _ := url.Parse("http://example.com/close") |
| 104 | + firstDone := serveOneHTTP1Response(t, mux, "/close", "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: 2\r\n\r\nok") |
| 105 | + roundTripAndClose(t, engine, tab, target) |
| 106 | + assertNoIdleHTTP1(t, engine, target, tab) |
| 107 | + if err := <-firstDone; err != nil { |
| 108 | + t.Fatal(err) |
| 109 | + } |
| 110 | + |
| 111 | + second := *target |
| 112 | + second.Path = "/after-close" |
| 113 | + secondDone := serveOneHTTP1Response(t, mux, "/after-close", "HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok") |
| 114 | + roundTripAndClose(t, engine, tab, &second) |
| 115 | + if err := <-secondDone; err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +func roundTripAndClose(t *testing.T, engine *Engine, tab *TabState, target *url.URL) { |
| 121 | + t.Helper() |
| 122 | + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 123 | + defer cancel() |
| 124 | + req, _ := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil) |
| 125 | + resp, err := engine.RoundTrip(ctx, req, target, tab) |
| 126 | + if err != nil { |
| 127 | + t.Fatal(err) |
| 128 | + } |
| 129 | + body, err := io.ReadAll(resp.Body) |
| 130 | + if err != nil { |
| 131 | + t.Fatal(err) |
| 132 | + } |
| 133 | + if string(body) != "ok" { |
| 134 | + t.Fatalf("body = %q", string(body)) |
| 135 | + } |
| 136 | + if err := resp.Body.Close(); err != nil { |
| 137 | + t.Fatal(err) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func assertNoIdleHTTP1(t *testing.T, engine *Engine, target *url.URL, tab *TabState) { |
| 142 | + t.Helper() |
| 143 | + if pc := engine.reserveH1(h2PoolKey(target, tab)); pc != nil { |
| 144 | + engine.closeH1(pc) |
| 145 | + t.Fatal("HTTP/1.1 connection was left idle when it should have been closed") |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func serveOneHTTP1Response(t *testing.T, mux *pipeMux, wantPath, response string) <-chan error { |
| 150 | + t.Helper() |
| 151 | + done := make(chan error, 1) |
| 152 | + go func() { |
| 153 | + c := <-mux.streams |
| 154 | + defer c.Close() |
| 155 | + br := bufio.NewReader(c) |
| 156 | + if err := acceptSOCKSConnect(br, c); err != nil { |
| 157 | + done <- err |
| 158 | + return |
| 159 | + } |
| 160 | + req, err := http.ReadRequest(br) |
| 161 | + if err != nil { |
| 162 | + done <- err |
| 163 | + return |
| 164 | + } |
| 165 | + if req.URL.RequestURI() != wantPath { |
| 166 | + done <- fmt.Errorf("request path = %q, want %q", req.URL.RequestURI(), wantPath) |
| 167 | + return |
| 168 | + } |
| 169 | + _, err = io.WriteString(c, response) |
| 170 | + done <- err |
| 171 | + }() |
| 172 | + return done |
| 173 | +} |
0 commit comments