mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-01-04 14:36:34 +08:00
根据Accept-Encoding决定是否解压响应内容
This commit is contained in:
@@ -5,10 +5,12 @@ import (
|
||||
"errors"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/caches"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/compressions"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/remotelogs"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/rpc"
|
||||
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
@@ -431,16 +433,28 @@ func (this *HTTPRequest) doCacheRead(useStale bool) (shouldStop bool) {
|
||||
return true
|
||||
}
|
||||
} else { // 没有Range
|
||||
var body io.Reader = reader
|
||||
var contentEncoding = this.writer.Header().Get("Content-Encoding")
|
||||
if len(contentEncoding) > 0 && !httpAcceptEncoding(this.RawReq.Header.Get("Accept-Encoding"), contentEncoding) {
|
||||
decompressReader, err := compressions.NewReader(body, contentEncoding)
|
||||
if err == nil {
|
||||
body = decompressReader
|
||||
defer func() {
|
||||
_ = decompressReader.Close()
|
||||
}()
|
||||
|
||||
this.writer.Header().Del("Content-Encoding")
|
||||
this.writer.Header().Del("Content-Length")
|
||||
}
|
||||
}
|
||||
|
||||
this.writer.PrepareCompression(reader.BodySize())
|
||||
this.writer.WriteHeader(reader.Status())
|
||||
|
||||
err = reader.ReadBody(buf, func(n int) (goNext bool, err error) {
|
||||
_, err = this.writer.Write(buf[:n])
|
||||
if err != nil {
|
||||
return false, errWritingToClient
|
||||
}
|
||||
return true, nil
|
||||
})
|
||||
_, err = io.CopyBuffer(this.writer, body, buf)
|
||||
if err == io.EOF {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
this.varMapping["cache.status"] = "MISS"
|
||||
|
||||
|
||||
@@ -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"
|
||||
"io"
|
||||
@@ -253,6 +254,24 @@ func (this *HTTPRequest) doReverseProxy() {
|
||||
}
|
||||
}
|
||||
|
||||
// 解压
|
||||
if !resp.Uncompressed {
|
||||
var contentEncoding = resp.Header.Get("Content-Encoding")
|
||||
if len(contentEncoding) > 0 && !httpAcceptEncoding(this.RawReq.Header.Get("Accept-Encoding"), contentEncoding) {
|
||||
reader, err := compressions.NewReader(resp.Body, contentEncoding)
|
||||
if err == nil {
|
||||
var body = resp.Body
|
||||
defer func() {
|
||||
_ = body.Close()
|
||||
}()
|
||||
|
||||
resp.Body = reader
|
||||
resp.Header.Del("Content-Encoding")
|
||||
resp.Header.Del("Content-Length")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 响应Header
|
||||
this.writer.AddHeaders(resp.Header)
|
||||
this.processResponseHeaders(resp.StatusCode)
|
||||
|
||||
@@ -153,3 +153,19 @@ func httpRequestNextId() string {
|
||||
// timestamp + requestId + nodeId
|
||||
return strconv.FormatInt(unixTime, 10) + teaconst.NodeIdString + strconv.Itoa(int(atomic.AddInt32(&httpRequestId, 1)))
|
||||
}
|
||||
|
||||
// 检查是否可以接受某个编码
|
||||
func httpAcceptEncoding(acceptEncodings string, encoding string) bool {
|
||||
var pieces = strings.Split(acceptEncodings, ",")
|
||||
for _, piece := range pieces {
|
||||
var qualityIndex = strings.Index(piece, ";")
|
||||
if qualityIndex >= 0 {
|
||||
piece = piece[:qualityIndex]
|
||||
}
|
||||
|
||||
if strings.TrimSpace(piece) == encoding {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user