From e9aed8ed79a6e3435f87895fe98b91918acee464 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Thu, 24 Oct 2024 23:22:55 +0800 Subject: [PATCH] Close the http body of HEAD simulated by GET --- crproxy.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/crproxy.go b/crproxy.go index d95e067..935bfca 100644 --- a/crproxy.go +++ b/crproxy.go @@ -1,7 +1,6 @@ package crproxy import ( - "bytes" "context" "errors" "fmt" @@ -436,18 +435,17 @@ func emptyTagsList(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, emptyTagsList) } -var emptyBody = io.NopCloser(bytes.NewBuffer([]byte{})) - -func (c *CRProxy) do(cli *http.Client, r *http.Request) (*http.Response, error) { +func (c *CRProxy) do(cli *http.Client, r *http.Request) (resp *http.Response, err error) { if !c.allowHeadMethod && r.Method == http.MethodHead { r.Method = http.MethodGet defer func() { r.Method = http.MethodHead - _ = r.Body.Close() - r.Body = emptyBody + resp.Body.Close() + resp.Body = http.NoBody }() } - return cli.Do(r) + resp, err = cli.Do(r) + return resp, err } func (c *CRProxy) doWithAuth(cli *http.Client, r *http.Request, host string) (*http.Response, error) {