mirror of
https://github.com/woodchen-ink/proxy-go.git
synced 2025-07-18 16:41:54 +08:00
feat(internal/handler): enhance proxy handler to preserve compression and content type
This commit is contained in:
parent
d8e9b90105
commit
62b82bfb5a
@ -97,8 +97,16 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置接受压缩内容的header
|
||||||
|
proxyReq.Header.Set("Accept-Encoding", r.Header.Get("Accept-Encoding"))
|
||||||
|
|
||||||
// 发送代理请求
|
// 发送代理请求
|
||||||
client := &http.Client{}
|
client := &http.Client{
|
||||||
|
Transport: &http.Transport{
|
||||||
|
// 禁用自动处理压缩,让我们能完整传递压缩内容
|
||||||
|
DisableCompression: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
resp, err := client.Do(proxyReq)
|
resp, err := client.Do(proxyReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "Error forwarding request", http.StatusBadGateway)
|
http.Error(w, "Error forwarding request", http.StatusBadGateway)
|
||||||
@ -108,8 +116,23 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
// 复制响应 header
|
// 复制所有响应头
|
||||||
copyHeader(w.Header(), resp.Header)
|
for k, vv := range resp.Header {
|
||||||
|
for _, v := range vv {
|
||||||
|
w.Header().Add(k, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 确保Content-Type正确设置
|
||||||
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
if contentType != "" {
|
||||||
|
w.Header().Set("Content-Type", contentType)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 保持原始的Content-Encoding
|
||||||
|
if encoding := resp.Header.Get("Content-Encoding"); encoding != "" {
|
||||||
|
w.Header().Set("Content-Encoding", encoding)
|
||||||
|
}
|
||||||
|
|
||||||
// 设置响应状态码
|
// 设置响应状态码
|
||||||
w.WriteHeader(resp.StatusCode)
|
w.WriteHeader(resp.StatusCode)
|
||||||
@ -138,17 +161,18 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// 如果不支持 Flusher,使用普通的 io.Copy
|
|
||||||
bytesCopied, err = io.Copy(w, resp.Body)
|
bytesCopied, err = io.Copy(w, resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error copying response: %v", err)
|
log.Printf("Error copying response: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 记录访问日志
|
// 记录访问日志,添加内容类型信息
|
||||||
log.Printf("[%s] %s %s -> %s -> %d (%d bytes) [%v]",
|
log.Printf("[%s] %s %s -> %s -> %d (%d bytes) [%v] Content-Type: %s, Encoding: %s",
|
||||||
getClientIP(r), r.Method, r.URL.Path, targetURL,
|
getClientIP(r), r.Method, r.URL.Path, targetURL,
|
||||||
resp.StatusCode, bytesCopied, time.Since(startTime))
|
resp.StatusCode, bytesCopied, time.Since(startTime),
|
||||||
|
resp.Header.Get("Content-Type"),
|
||||||
|
resp.Header.Get("Content-Encoding"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func copyHeader(dst, src http.Header) {
|
func copyHeader(dst, src http.Header) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user