mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
Revert "net/http/httputil: allow ReverseProxy to call ModifyResponse on failed requests"
This reverts commit https://golang.org/cl/54030 Reason for revert: to not paint ourselves into a corner. See https://github.com/golang/go/issues/23009 Fixes #23009 Updates #21255 Change-Id: I68caab078839b9d2bf645a7bbed8405a5a30cd22 Reviewed-on: https://go-review.googlesource.com/86435 Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
parent
91f99852f6
commit
24df1d06bc
2 changed files with 8 additions and 42 deletions
|
|
@ -191,11 +191,10 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := transport.RoundTrip(outreq)
|
res, err := transport.RoundTrip(outreq)
|
||||||
if res == nil {
|
if err != nil {
|
||||||
res = &http.Response{
|
p.logf("http: proxy error: %v", err)
|
||||||
StatusCode: http.StatusBadGateway,
|
rw.WriteHeader(http.StatusBadGateway)
|
||||||
Body: http.NoBody,
|
return
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
removeConnectionHeaders(res.Header)
|
removeConnectionHeaders(res.Header)
|
||||||
|
|
@ -205,16 +204,12 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.ModifyResponse != nil {
|
if p.ModifyResponse != nil {
|
||||||
if err != nil {
|
if err := p.ModifyResponse(res); err != nil {
|
||||||
p.logf("http: proxy error: %v", err)
|
p.logf("http: proxy error: %v", err)
|
||||||
|
rw.WriteHeader(http.StatusBadGateway)
|
||||||
|
res.Body.Close()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
err = p.ModifyResponse(res)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
p.logf("http: proxy error: %v", err)
|
|
||||||
rw.WriteHeader(http.StatusBadGateway)
|
|
||||||
res.Body.Close()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
copyHeader(rw.Header(), res.Header)
|
copyHeader(rw.Header(), res.Header)
|
||||||
|
|
|
||||||
|
|
@ -631,35 +631,6 @@ func TestReverseProxyModifyResponse(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Issue 21255. Test ModifyResponse when an error from transport.RoundTrip
|
|
||||||
// occurs, and that the proxy returns StatusOK.
|
|
||||||
func TestReverseProxyModifyResponse_OnError(t *testing.T) {
|
|
||||||
// Always returns an error
|
|
||||||
errBackend := httptest.NewUnstartedServer(nil)
|
|
||||||
errBackend.Config.ErrorLog = log.New(ioutil.Discard, "", 0) // quiet for tests
|
|
||||||
defer errBackend.Close()
|
|
||||||
|
|
||||||
rpURL, _ := url.Parse(errBackend.URL)
|
|
||||||
rproxy := NewSingleHostReverseProxy(rpURL)
|
|
||||||
rproxy.ModifyResponse = func(resp *http.Response) error {
|
|
||||||
// Will be set for a non-nil error
|
|
||||||
resp.StatusCode = http.StatusOK
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
frontend := httptest.NewServer(rproxy)
|
|
||||||
defer frontend.Close()
|
|
||||||
|
|
||||||
resp, err := http.Get(frontend.URL)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to reach proxy: %v", err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
t.Errorf("err != nil: got res.StatusCode %d; expected %d", resp.StatusCode, http.StatusOK)
|
|
||||||
}
|
|
||||||
resp.Body.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Issue 16659: log errors from short read
|
// Issue 16659: log errors from short read
|
||||||
func TestReverseProxy_CopyBuffer(t *testing.T) {
|
func TestReverseProxy_CopyBuffer(t *testing.T) {
|
||||||
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
backendServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue