1

here is my code, when origin server return 302, reverse proxy will modify the request and serve it again, but if the request with a body, it print an error: http: invalid Read on closed Body

                proxy = httputil.NewSingleHostReverseProxy(url)
        proxy.ErrorLog = proxyLogger
        proxy.ModifyResponse = func(response *http.Response) error {
            if response.StatusCode > 300 && response.StatusCode < 400 {
                location, err := response.Location()
                if err != nil {
                    return err
                }
                return &RedirectErr{location: location}
            }
            return nil
        }
        var rewindBody func(r *http.Request)

        proxy.Director = func(request *http.Request) {
            if redirect := request.Header.Get("redirect"); redirect != "" {
                request.Header.Del("redirect")
                u, _ := urlpkg.Parse(redirect)
                rewriteRequestURL(request, u)
                return
            }
            rewriteRequestURL(request, url)
        }
        proxy.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
            var redirect *RedirectErr
            if redirect, ok = err.(*RedirectErr); !ok {
                proxy.ErrorLog.Printf("http: proxy error: %v", err)
                writer.WriteHeader(http.StatusBadGateway)
            } else {
                request.Header.Set("redirect", redirect.location.String())
                proxy.ServeHTTP(writer, request)
                return
            }
        }

I try to use fakeCloseReadCloser, but it comes new error: "error: net/http: HTTP/1.x transport connection broken: http: ContentLength=75 with Body length 0"

Kaniu
  • 23
  • 4

1 Answers1

1

Finally , I resoled this. We need ensure that the request body not close. and we should rewind the body before second transport.

// after transport, the request body will close,
// so if we need redirect it, we have to wrap the body and ensure that it won't be close
// we will handle it after we finished.
// see this issue: https://github.com/flynn/flynn/issues/872
type fakeCloseReadCloser struct {
    io.ReadCloser
}

func (w *fakeCloseReadCloser) Close() error {
    return nil
}

func (w *fakeCloseReadCloser) RealClose() error {
    if w.ReadCloser == nil {
        return nil
    }
    return w.ReadCloser.Close()
}

type Proxy struct {
    *httputil.ReverseProxy
}

func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
    req.Body = &fakeCloseReadCloser{req.Body}
    // The body can only be read once, so we use this func to make the body available on the second forwarding
    if req.Body != nil {
        bodyBytes, _ := io.ReadAll(req.Body)
        _ = req.Body.(*fakeCloseReadCloser).RealClose()
        req.Body = &fakeCloseReadCloser{io.NopCloser(bytes.NewBuffer(bodyBytes))}
        req.GetBody = func() (io.ReadCloser, error) {
            body := io.NopCloser(bytes.NewBuffer(bodyBytes))
            return body, nil
        }
    }

    p.ReverseProxy.ServeHTTP(rw, req)
    _ = req.Body.(*fakeCloseReadCloser).RealClose()
}

// Get returns a proxy for the given shard.
func (pp *ProxyPool) Get(url *urlpkg.URL) *Proxy {
    pp.mutex.Lock()
    defer pp.mutex.Unlock()

    p, ok := pp.pool[url.String()]
    if !ok {
        // When the original target server returns 302,
        // proxy.ModifyResponse will return a customized redirect error containing location,
        // proxy.ErrorHandler catches the error and modifies the request,
        // adds a "redirect" header field to it, and Serve it again.
        // At this point proxy.Director will detect the "redirect" field in the request header
        // and send the request to the final target server.
        rp := httputil.NewSingleHostReverseProxy(url)
        px := &Proxy{ReverseProxy: rp}

        px.ErrorLog = proxyLogger
        px.ModifyResponse = func(response *http.Response) error {
            if response.StatusCode > 300 && response.StatusCode < 400 {
                location, err := response.Location()
                if err != nil {
                    return err
                }
                return &RedirectErr{location: location}
            }

            return nil
        }

        px.Director = func(request *http.Request) {
            if redirect := request.Header.Get("redirect"); redirect != "" {
                request.Header.Del("redirect")
                u, _ := urlpkg.Parse(redirect)
                // rewind
                if request.GetBody != nil {
                    b, _ := request.GetBody()
                    request.Body = b
                }
                rewriteRequestURL(request, u)
                return
            }

            rewriteRequestURL(request, url)
        }
        px.ErrorHandler = func(writer http.ResponseWriter, request *http.Request, err error) {
            var redirect *RedirectErr
            if redirect, ok = err.(*RedirectErr); !ok {
                p.ErrorLog.Printf("http: proxy error: %v", err)
                writer.WriteHeader(http.StatusBadGateway)
            } else {
                request.Header.Set("redirect", redirect.location.String())
                px.ReverseProxy.ServeHTTP(writer, request)
                return
            }
        }

        pp.pool[url.Host] = px
        p = px
    }

    return p
}
eglease
  • 2,445
  • 11
  • 18
  • 28
Kaniu
  • 23
  • 4