Merge pull request #799 from samwafgo/feat_header_delete

Feat header delete
This commit is contained in:
samwafgo
2026-05-09 16:07:14 +08:00
committed by GitHub
4 changed files with 35 additions and 4 deletions
+1
View File
@@ -9,6 +9,7 @@ permissions:
contents: write
env:
REGISTRY: docker.io
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
jobs:
# Linux 和 Windows 编译任务
goreleaser-linux-windows:
+13
View File
@@ -21,6 +21,10 @@ import (
func (waf *WafEngine) ProxyHTTP(w http.ResponseWriter, r *http.Request, host string, remoteUrl *url.URL, clientIp string, ctx context.Context, weblog *innerbean.WebLog, hostTarget *wafenginmodel.HostSafe) {
// 在 director 修改请求头之前,将客户端原始 Accept-Encoding 存入 context
// 供响应压缩逻辑使用(自定义头可能把转发请求的 Accept-Encoding 清空)
ctx = context.WithValue(ctx, "original_accept_encoding", r.Header.Get("Accept-Encoding"))
//检测是否启动负载
if hostTarget.Host.IsEnableLoadBalance > 0 {
lb := &hostTarget.LoadBalanceRuntime
@@ -173,6 +177,15 @@ func (waf *WafEngine) createTransport(r *http.Request, host string, isEnableLoad
transport.ResponseHeaderTimeout = time.Duration(hostTarget.Host.ResponseTimeOut) * time.Second
// 若自定义头里将 Accept-Encoding 设为空值,则禁用 Transport 自动追加 gzip
customHeadersConfig := model.ParseCustomHeadersConfig(hostTarget.Host.CustomHeadersJSON)
for _, h := range customHeadersConfig.Headers {
if strings.EqualFold(h.HeaderName, "Accept-Encoding") && h.HeaderValue == "" {
transport.DisableCompression = true
break
}
}
//把下面的参数一次性使用格式化实现
zlog.Debug(fmt.Sprintf("Transport配置信息:\nMaxIdleConns: %d\nMaxIdleConnsPerHost: %d\nMaxConnsPerHost: %d\nIdleConnTimeout: %v\nTLSHandshakeTimeout: %v\nExpectContinueTimeout: %v\nResponseHeaderTimeout: %v\nDisableKeepAlives: %v\nDisableCompression: %v",
transport.MaxIdleConns,
+15 -2
View File
@@ -45,7 +45,7 @@ func (waf *WafEngine) maybeApplyResponseCompress(req *http.Request, resp *http.R
if !responseCompressTypeOrExtMatches(resp.Header.Get("Content-Type"), urlPath, cfg) {
return body
}
enc := chooseCompressEncoding(req.Header.Get("Accept-Encoding"), cfg.Prefer)
enc := chooseCompressEncoding(getAcceptEncoding(req), cfg.Prefer)
if enc == "" {
return body
}
@@ -128,7 +128,7 @@ func (waf *WafEngine) maybeCompressStaticAssistResponse(req *http.Request, resp
resp.Header.Set("Content-Length", strconv.FormatInt(int64(len(raw)), 10))
return
}
enc := chooseCompressEncoding(req.Header.Get("Accept-Encoding"), cfg.Prefer)
enc := chooseCompressEncoding(getAcceptEncoding(req), cfg.Prefer)
if enc == "" {
resp.Body = io.NopCloser(bytes.NewReader(raw))
resp.ContentLength = int64(len(raw))
@@ -175,6 +175,19 @@ func appendVaryAcceptEncoding(h http.Header) {
h.Set("Vary", v+", Accept-Encoding")
}
// getAcceptEncoding 返回用于压缩决策的 Accept-Encoding 值。
// 优先从 context 中读取原始客户端值(proxy 路径会在转发前保存),
// 避免自定义头将转发请求的 Accept-Encoding 清空后误判客户端不支持压缩。
func getAcceptEncoding(req *http.Request) string {
if req == nil {
return ""
}
if v, ok := req.Context().Value("original_accept_encoding").(string); ok {
return v
}
return req.Header.Get("Accept-Encoding")
}
func chooseCompressEncoding(acceptEncoding, prefer string) string {
ae := strings.ToLower(acceptEncoding)
switch strings.ToLower(strings.TrimSpace(prefer)) {
+6 -2
View File
@@ -195,9 +195,13 @@ func NewSingleHostReverseProxyCustomHeader(target *url.URL, customHeaders map[st
}
}
// 添加自定义 header
// 添加自定义 header(空值表示删除该 header
for key, value := range customHeaders {
req.Header.Set(key, value)
if value == "" {
req.Header.Del(key)
} else {
req.Header.Set(key, value)
}
}
if customConfig["IsTransBackDomain"] == "1" {
// 拆分主机名和端口