From 3dd381b60ff5d67e73b552d7669b3b4cceac0a45 Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 02:54:12 +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 | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/proxy/handler.go b/proxy/handler.go index f616cc7..bf57a39 100644 --- a/proxy/handler.go +++ b/proxy/handler.go @@ -2,12 +2,10 @@ package proxy import ( - "bytes" "compress/gzip" "io" "log" "net/http" - "net/url" "strings" "time" @@ -112,9 +110,15 @@ 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 if h.shouldCache(resp) { - h.cache.Set(targetURL, body, resp.Header) + headers := make(map[string]string) + for key, values := range resp.Header { + if len(values) > 0 { + headers[key] = values[0] + } + } + h.cache.Set(targetURL, body, headers) } // 发送响应 @@ -145,9 +149,6 @@ func (h *ProxyHandler) rewriteContent(body []byte, targetURL, contentType string return rewriter.RewriteCSS(body) } - // JavaScript 内容 - 暂时不重写,可能会破坏功能 - // 未来可以添加更智能的 JS 重写 - return body } @@ -253,8 +254,8 @@ func (h *ProxyHandler) sendResponse(w http.ResponseWriter, resp *http.Response, w.Header().Set("X-Cache-Status", "MISS") // 移除不需要的头 - w.Header().Del("Content-Encoding") // 我们已经解压了 - w.Header().Del("Content-Length") // 长度可能已改变 + w.Header().Del("Content-Encoding") + w.Header().Del("Content-Length") // 安全头 w.Header().Set("X-Content-Type-Options", "nosniff")