更新 proxy/handler.go

This commit is contained in:
XOF
2025-12-15 02:54:12 +08:00
parent 39451e0a4b
commit 3dd381b60f

View File

@@ -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")