From 510e1294999442f5b359c3109d4e4b00cc2960c6 Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 03:16:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20proxy/handler.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/handler.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/proxy/handler.go b/proxy/handler.go index 889fa87..f14c94d 100644 --- a/proxy/handler.go +++ b/proxy/handler.go @@ -82,7 +82,6 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { client := &http.Client{ Timeout: 30 * time.Second, CheckRedirect: func(req *http.Request, via []*http.Request) error { - // 验证重定向 URL if err := h.validator.ValidateURL(req.URL.String()); err != nil { return err } @@ -98,7 +97,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer resp.Body.Close() - // 读取响应体 + // 读取响应体(会自动解压 gzip) body, err := h.readResponseBody(resp) if err != nil { log.Printf("Failed to read response: %v", err) @@ -110,10 +109,14 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { contentType := resp.Header.Get("Content-Type") body = h.rewriteContent(body, targetURL, contentType) - // 缓存响应 - 转换 http.Header 为 map[string]string + // 缓存响应 - 转换 headers 并删除 Content-Encoding if h.shouldCache(resp) { headers := make(map[string]string) for key, values := range resp.Header { + // 跳过 Content-Encoding,因为我们已经解压了 + if key == "Content-Encoding" || key == "Content-Length" { + continue + } if len(values) > 0 { headers[key] = values[0] }