Go ReverseProxy 处理重定向错误: “http: invalid Read on closed Body”

huangapple go评论87阅读模式
英文:

Go ReverseProxy handle redirect error: "http: invalid Read on closed Body"

问题

这是我的代码,当原始服务器返回302时,反向代理会修改请求并重新提供服务,但如果请求带有主体,它会打印一个错误: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
    }
}

我尝试使用fakeCloseReadCloser,但出现了新的错误:"error: net/http: HTTP/1.x transport connection broken: http: ContentLength=75 with Body length 0"

英文:

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 &gt; 300 &amp;&amp; response.StatusCode &lt; 400 {
				location, err := response.Location()
				if err != nil {
					return err
				}
				return &amp;RedirectErr{location: location}
			}
			return nil
		}
		var rewindBody func(r *http.Request)

		proxy.Director = func(request *http.Request) {
			if redirect := request.Header.Get(&quot;redirect&quot;); redirect != &quot;&quot; {
				request.Header.Del(&quot;redirect&quot;)
				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(&quot;http: proxy error: %v&quot;, err)
				writer.WriteHeader(http.StatusBadGateway)
			} else {
				request.Header.Set(&quot;redirect&quot;, 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"

答案1

得分: 1

最后,我解决了这个问题。我们需要确保请求体不关闭,并且在第二次传输之前需要倒回请求体。

// 在传输之后,请求体将关闭,
// 因此,如果我们需要重定向它,我们必须包装请求体并确保它不会关闭
// 我们将在完成后处理它。
// 参见此问题: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}
	// 请求体只能读取一次,因此我们使用此函数使请求体在第二次转发时可用
	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()
}

// 根据给定的分片返回代理
func (pp *ProxyPool) Get(url *urlpkg.URL) *Proxy {
	pp.mutex.Lock()
	defer pp.mutex.Unlock()

	p, ok := pp.pool[url.String()]
	if !ok {
		// 当原始目标服务器返回302时,
		// proxy.ModifyResponse将返回一个包含位置的自定义重定向错误,
		// proxy.ErrorHandler捕获错误并修改请求,
		// 添加一个“redirect”头字段,并再次Serve它。
		// 此时,proxy.Director将检测到请求头中的“redirect”字段
		// 并将请求发送到最终目标服务器。
		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)
				// 倒回
				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
}

希望对你有帮助!

英文:

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&#39;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 = &amp;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 = &amp;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 &quot;redirect&quot; header field to it, and Serve it again.
// At this point proxy.Director will detect the &quot;redirect&quot; field in the request header
// and send the request to the final target server.
rp := httputil.NewSingleHostReverseProxy(url)
px := &amp;Proxy{ReverseProxy: rp}
px.ErrorLog = proxyLogger
px.ModifyResponse = func(response *http.Response) error {
if response.StatusCode &gt; 300 &amp;&amp; response.StatusCode &lt; 400 {
location, err := response.Location()
if err != nil {
return err
}
return &amp;RedirectErr{location: location}
}
return nil
}
px.Director = func(request *http.Request) {
if redirect := request.Header.Get(&quot;redirect&quot;); redirect != &quot;&quot; {
request.Header.Del(&quot;redirect&quot;)
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(&quot;http: proxy error: %v&quot;, err)
writer.WriteHeader(http.StatusBadGateway)
} else {
request.Header.Set(&quot;redirect&quot;, redirect.location.String())
px.ReverseProxy.ServeHTTP(writer, request)
return
}
}
pp.pool[url.Host] = px
p = px
}
return p
}

huangapple
  • 本文由 发表于 2023年7月13日 11:50:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定