更新 proxy/handler.go
This commit is contained in:
@@ -2,12 +2,10 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -112,9 +110,15 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
contentType := resp.Header.Get("Content-Type")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
body = h.rewriteContent(body, targetURL, contentType)
|
body = h.rewriteContent(body, targetURL, contentType)
|
||||||
|
|
||||||
// 缓存响应
|
// 缓存响应 - 转换 http.Header 为 map[string]string
|
||||||
if h.shouldCache(resp) {
|
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)
|
return rewriter.RewriteCSS(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
// JavaScript 内容 - 暂时不重写,可能会破坏功能
|
|
||||||
// 未来可以添加更智能的 JS 重写
|
|
||||||
|
|
||||||
return body
|
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().Set("X-Cache-Status", "MISS")
|
||||||
|
|
||||||
// 移除不需要的头
|
// 移除不需要的头
|
||||||
w.Header().Del("Content-Encoding") // 我们已经解压了
|
w.Header().Del("Content-Encoding")
|
||||||
w.Header().Del("Content-Length") // 长度可能已改变
|
w.Header().Del("Content-Length")
|
||||||
|
|
||||||
// 安全头
|
// 安全头
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
|||||||
Reference in New Issue
Block a user