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] }