From 2ec59f3ee1a8d51d7d90f02be25130207d4b5134 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 18 Apr 2024 16:18:49 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=B7=E6=B1=82=E6=BA=90=E7=AB=99=E6=97=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8gzip=E5=9B=9E=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request.go | 1 + internal/nodes/http_request_reverse_proxy.go | 37 ++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index 23c8130..ce6fdf2 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -50,6 +50,7 @@ type HTTPRequest struct { isHealthCheck bool // 共享参数 + // DO NOT change the variable after created nodeConfig *nodeconfigs.NodeConfig // ln request diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index e6ff77b..79c1153 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -5,6 +5,7 @@ import ( "errors" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" + "github.com/TeaOSLab/EdgeNode/internal/compressions" "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/fnv" @@ -289,8 +290,44 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId this.RawReq.URL.Scheme = "http" } + // request origin with Accept-Encoding: gzip, ... + var rawAcceptEncoding string + var acceptEncodingChanged bool + if this.nodeConfig != nil && + this.nodeConfig.GlobalServerConfig != nil && + this.nodeConfig.GlobalServerConfig.HTTPAll.RequestOriginsWithEncodings && + this.RawReq.ProtoAtLeast(1, 1) && + this.RawReq.Header != nil { + rawAcceptEncoding = this.RawReq.Header.Get("Accept-Encoding") + if len(rawAcceptEncoding) == 0 { + this.RawReq.Header.Set("Accept-Encoding", "gzip") + acceptEncodingChanged = true + } else if strings.Index(rawAcceptEncoding, "gzip") < 0 { + this.RawReq.Header.Set("Accept-Encoding", rawAcceptEncoding+", gzip") + acceptEncodingChanged = true + } + } + // 开始请求 resp, requestErr = client.Do(this.RawReq) + + // recover Accept-Encoding + if acceptEncodingChanged { + if len(rawAcceptEncoding) > 0 { + this.RawReq.Header.Set("Accept-Encoding", rawAcceptEncoding) + } else { + this.RawReq.Header.Del("Accept-Encoding") + } + + if resp != nil && resp.Header != nil && resp.Header.Get("Content-Encoding") == "gzip" { + bodyReader, gzipErr := compressions.NewGzipReader(resp.Body) + if gzipErr == nil { + resp.Body = bodyReader + } + resp.TransferEncoding = nil + resp.Header.Del("Content-Encoding") + } + } } else if origin.OSS != nil { // OSS源站 var goNext bool resp, goNext, requestErrCode, _, requestErr = this.doOSSOrigin(origin)