From 283d1a3956994e572481c2f7b9513a5181465c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= Date: Tue, 18 Jan 2022 11:32:28 +0800 Subject: [PATCH] Update alist-proxy.go --- alist-proxy.go | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/alist-proxy.go b/alist-proxy.go index c0bd723..949cd2d 100644 --- a/alist-proxy.go +++ b/alist-proxy.go @@ -101,8 +101,15 @@ func downHandle(w http.ResponseWriter, r *http.Request) { req, _ := http.NewRequest("POST", fmt.Sprintf("%s/api/admin/link", host), bytes.NewBuffer(dataByte)) req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", token) - res, _ := HttpClient.Do(req) - dataByte, err := ioutil.ReadAll(res.Body) + res, err := HttpClient.Do(req) + if err != nil { + errorResponse(w, 500, err.Error()) + return + } + defer func() { + _ = res.Body.Close() + }() + dataByte, err = ioutil.ReadAll(res.Body) if err != nil { errorResponse(w, 500, err.Error()) return @@ -137,15 +144,18 @@ func downHandle(w http.ResponseWriter, r *http.Request) { req.Header.Set(header.Name, header.Value) } } - res, err = HttpClient.Do(req) + res2, err := HttpClient.Do(req) if err != nil { errorResponse(w, 500, err.Error()) return } - for h, v := range res.Header { + defer func() { + _ = res2.Body.Close() + }() + for h, v := range res2.Header { w.Header()[h] = v } - _, err = io.Copy(w, res.Body) + _, err = io.Copy(w, res2.Body) if err != nil { errorResponse(w, 500, err.Error()) return @@ -169,6 +179,9 @@ func apiHandle(w http.ResponseWriter, r *http.Request) { errorResponse(w, 500, err.Error()) return } + defer func() { + _ = res.Body.Close() + }() w.WriteHeader(res.StatusCode) for h, v := range res.Header { w.Header()[h] = v