更新 proxy/handler.go
This commit is contained in:
@@ -82,7 +82,6 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 30 * time.Second,
|
Timeout: 30 * time.Second,
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
// 验证重定向 URL
|
|
||||||
if err := h.validator.ValidateURL(req.URL.String()); err != nil {
|
if err := h.validator.ValidateURL(req.URL.String()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -98,7 +97,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// 读取响应体
|
// 读取响应体(会自动解压 gzip)
|
||||||
body, err := h.readResponseBody(resp)
|
body, err := h.readResponseBody(resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to read response: %v", err)
|
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")
|
contentType := resp.Header.Get("Content-Type")
|
||||||
body = h.rewriteContent(body, targetURL, contentType)
|
body = h.rewriteContent(body, targetURL, contentType)
|
||||||
|
|
||||||
// 缓存响应 - 转换 http.Header 为 map[string]string
|
// 缓存响应 - 转换 headers 并删除 Content-Encoding
|
||||||
if h.shouldCache(resp) {
|
if h.shouldCache(resp) {
|
||||||
headers := make(map[string]string)
|
headers := make(map[string]string)
|
||||||
for key, values := range resp.Header {
|
for key, values := range resp.Header {
|
||||||
|
// 跳过 Content-Encoding,因为我们已经解压了
|
||||||
|
if key == "Content-Encoding" || key == "Content-Length" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if len(values) > 0 {
|
if len(values) > 0 {
|
||||||
headers[key] = values[0]
|
headers[key] = values[0]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user