diff options
| author | SALLEYRON Julien <julien.salleyron@gmail.com> | 2018-12-03 20:46:23 +0000 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-12-03 21:45:17 +0000 |
| commit | 48399cae9f828668bd2c010dc46e4767f2acd011 (patch) | |
| tree | 5b9dc36b502d50255144030e20f809cad3a1c764 /src/net/http | |
| parent | a48a15cd502395627293f85cdbe23aebc3e7e95f (diff) | |
| download | go-48399cae9f828668bd2c010dc46e4767f2acd011.tar.xz | |
net/http/httputil: fix unannounced trailers when body is empty
Fix unannounced trailers when body is empty and without announced trailers.
Fixes #29031
Change-Id: If49951a42fe56d4be4436a999627db4c2678659d
GitHub-Last-Rev: 3469adc8f5fd977438350274134950687853a468
GitHub-Pull-Request: golang/go#29032
Reviewed-on: https://go-review.googlesource.com/c/151898
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/net/http')
| -rw-r--r-- | src/net/http/httputil/reverseproxy.go | 18 | ||||
| -rw-r--r-- | src/net/http/httputil/reverseproxy_test.go | 48 |
2 files changed, 49 insertions, 17 deletions
diff --git a/src/net/http/httputil/reverseproxy.go b/src/net/http/httputil/reverseproxy.go index f0607a68ea..5d07ba3d36 100644 --- a/src/net/http/httputil/reverseproxy.go +++ b/src/net/http/httputil/reverseproxy.go @@ -282,14 +282,7 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } rw.WriteHeader(res.StatusCode) - if len(res.Trailer) > 0 { - // Force chunking if we saw a response trailer. - // This prevents net/http from calculating the length for short - // bodies and adding a Content-Length. - if fl, ok := rw.(http.Flusher); ok { - fl.Flush() - } - } + err = p.copyResponse(rw, res.Body, p.flushInterval(req, res)) if err != nil { defer res.Body.Close() @@ -304,6 +297,15 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) { } res.Body.Close() // close now, instead of defer, to populate res.Trailer + if len(res.Trailer) > 0 { + // Force chunking if we saw a response trailer. + // This prevents net/http from calculating the length for short + // bodies and adding a Content-Length. + if fl, ok := rw.(http.Flusher); ok { + fl.Flush() + } + } + if len(res.Trailer) == announcedTrailers { copyHeader(rw.Header(), res.Trailer) return diff --git a/src/net/http/httputil/reverseproxy_test.go b/src/net/http/httputil/reverseproxy_test.go index 039273e7c5..588022c066 100644 --- a/src/net/http/httputil/reverseproxy_test.go +++ b/src/net/http/httputil/reverseproxy_test.go @@ -7,23 +7,23 @@ package httputil import ( - "bufio" - "bytes" - "errors" - "fmt" - "io" "io/ioutil" "log" "net/http" "net/http/httptest" "net/url" - "os" + "testing" + "time" "reflect" - "strconv" + "io" "strings" + "bufio" "sync" - "testing" - "time" + "strconv" + "bytes" + "errors" + "fmt" + "os" ) const fakeHopHeader = "X-Fake-Hop-Header-For-Test" @@ -1048,3 +1048,33 @@ func TestReverseProxyWebSocket(t *testing.T) { t.Errorf("got %#q, want %#q", got, want) } } + +func TestUnannouncedTrailer(t *testing.T) { + backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusOK) + w.(http.Flusher).Flush() + w.Header().Set(http.TrailerPrefix+"X-Unannounced-Trailer", "unannounced_trailer_value") + })) + defer backend.Close() + backendURL, err := url.Parse(backend.URL) + if err != nil { + t.Fatal(err) + } + proxyHandler := NewSingleHostReverseProxy(backendURL) + proxyHandler.ErrorLog = log.New(ioutil.Discard, "", 0) // quiet for tests + frontend := httptest.NewServer(proxyHandler) + defer frontend.Close() + frontendClient := frontend.Client() + + res, err := frontendClient.Get(frontend.URL) + if err != nil { + t.Fatalf("Get: %v", err) + } + + ioutil.ReadAll(res.Body) + + if g, w := res.Trailer.Get("X-Unannounced-Trailer"), "unannounced_trailer_value"; g != w { + t.Errorf("Trailer(X-Unannounced-Trailer) = %q; want %q", g, w) + } + +} |
