更新 proxy/handler.go

This commit is contained in:
XOF
2025-12-15 03:16:49 +08:00
parent 18b1007cc9
commit 510e129499

View File

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