From 0520b4cd249a6b5f215d66a252f398be58f0f1b6 Mon Sep 17 00:00:00 2001 From: Ryan Bourgeois Date: Mon, 1 Jun 2015 11:45:23 -0700 Subject: [PATCH] proxy: Reuse a bytes buffer as proxy request body. The call to transport.RoundTrip closes the request body regardless of the value of request.Closed. This causes subsequent calls to RoundTrip using the same request body to fail. Fixes #2895 --- proxy/reverse.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/proxy/reverse.go b/proxy/reverse.go index ee0b9cbcd..0db5bd111 100644 --- a/proxy/reverse.go +++ b/proxy/reverse.go @@ -15,8 +15,10 @@ package proxy import ( + "bytes" "fmt" "io" + "io/ioutil" "log" "net" "net/http" @@ -55,6 +57,21 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request proxyreq := new(http.Request) *proxyreq = *clientreq + var ( + proxybody []byte + err error + ) + + if clientreq.Body != nil { + proxybody, err = ioutil.ReadAll(clientreq.Body) + if err != nil { + msg := fmt.Sprintf("proxy: failed to read request body: %v", err) + e := httptypes.NewHTTPError(http.StatusInternalServerError, msg) + e.WriteTo(rw) + return + } + } + // deep-copy the headers, as these will be modified below proxyreq.Header = make(http.Header) copyHeader(proxyreq.Header, clientreq.Header) @@ -93,9 +110,11 @@ func (p *reverseProxy) ServeHTTP(rw http.ResponseWriter, clientreq *http.Request } var res *http.Response - var err error for _, ep := range endpoints { + if proxybody != nil { + proxyreq.Body = ioutil.NopCloser(bytes.NewBuffer(proxybody)) + } redirectRequest(proxyreq, ep.URL) res, err = p.transport.RoundTrip(proxyreq)