-
Notifications
You must be signed in to change notification settings - Fork 0
fix: scrub late cover response trailers without buffering #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+394
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package cover | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestReverseProxyResponseBodyCancellationReachesOrigin(t *testing.T) { | ||
| canceled := make(chan struct{}) | ||
| origin := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.Header().Set("Trailer", "X-End") | ||
| _, _ = io.WriteString(w, "x") | ||
| w.(http.Flusher).Flush() | ||
| <-r.Context().Done() | ||
| close(canceled) | ||
| })) | ||
| t.Cleanup(origin.Close) | ||
| originURL, err := url.Parse(origin.URL) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| upstream := &http.Transport{Proxy: nil} | ||
| t.Cleanup(upstream.CloseIdleConnections) | ||
| bodyClosed := make(chan struct{}) | ||
| var closeOnce sync.Once | ||
| handler, err := NewReverseProxyHandler(originURL, roundTripFunc(func(r *http.Request) (*http.Response, error) { | ||
| response, err := upstream.RoundTrip(r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| body := response.Body | ||
| response.Body = &trailerTestBody{read: body.Read, close: func() error { | ||
| err := body.Close() | ||
| closeOnce.Do(func() { close(bodyClosed) }) | ||
| return err | ||
| }} | ||
| return response, nil | ||
| })) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| frontDone := make(chan struct{}) | ||
| front := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| defer close(frontDone) | ||
| handler.ServeHTTP(w, r) | ||
| })) | ||
| t.Cleanup(front.Close) | ||
| transport := &http.Transport{Proxy: nil} | ||
| t.Cleanup(transport.CloseIdleConnections) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||
| t.Cleanup(cancel) | ||
| request, err := http.NewRequestWithContext(ctx, http.MethodGet, front.URL, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| response, err := (&http.Client{Transport: transport}).Do(request) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { _ = response.Body.Close() }) | ||
| first := make([]byte, 1) | ||
| if _, err := io.ReadFull(response.Body, first); err != nil || first[0] != 'x' { | ||
| t.Fatalf("initial streamed byte = %q, error %v", first, err) | ||
| } | ||
| readDone := make(chan error, 1) | ||
| go func() { _, err := response.Body.Read(make([]byte, 1)); readDone <- err }() | ||
| cancel() | ||
| select { | ||
| case err := <-readDone: | ||
| if err == nil { | ||
| t.Fatal("body read succeeded after cancellation") | ||
| } | ||
| case <-time.After(time.Second): | ||
| t.Fatal("response body read did not stop after cancellation") | ||
| } | ||
| for name, done := range map[string]<-chan struct{}{ | ||
| "upstream request": canceled, | ||
| "upstream body": bodyClosed, | ||
| "front handler": frontDone, | ||
| } { | ||
| select { | ||
| case <-done: | ||
| case <-time.After(time.Second): | ||
| t.Fatalf("client cancellation did not release %s", name) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the supplied
RoundTripperreplacesResponse.Trailer, this cleanup can make the final map's length equal the number of initially announced trailers even though its keys changed—for example,{X-Old}becomes{Authorization, X-New}, then filtering leaves{X-New}.httputil.ReverseProxyuses that length equality to assume the declarations are unchanged and copiesX-Newas an ordinary header afterWriteHeader, so it is discarded on the wire; the safe late trailer is therefore lost. Preserve the original safe declarations or otherwise ensure changed trailer key sets take the late-trailer path.Useful? React with 👍 / 👎.