mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-09 20:10:27 +08:00
请求源站时支持自动gzip回源
This commit is contained in:
@@ -50,6 +50,7 @@ type HTTPRequest struct {
|
|||||||
isHealthCheck bool
|
isHealthCheck bool
|
||||||
|
|
||||||
// 共享参数
|
// 共享参数
|
||||||
|
// DO NOT change the variable after created
|
||||||
nodeConfig *nodeconfigs.NodeConfig
|
nodeConfig *nodeconfigs.NodeConfig
|
||||||
|
|
||||||
// ln request
|
// ln request
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||||||
|
"github.com/TeaOSLab/EdgeNode/internal/compressions"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||||
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
|
"github.com/TeaOSLab/EdgeNode/internal/utils/fnv"
|
||||||
@@ -289,8 +290,44 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId
|
|||||||
this.RawReq.URL.Scheme = "http"
|
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)
|
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源站
|
} else if origin.OSS != nil { // OSS源站
|
||||||
var goNext bool
|
var goNext bool
|
||||||
resp, goNext, requestErrCode, _, requestErr = this.doOSSOrigin(origin)
|
resp, goNext, requestErrCode, _, requestErr = this.doOSSOrigin(origin)
|
||||||
|
|||||||
Reference in New Issue
Block a user