缓存结束后增加Content-Length对比

This commit is contained in:
刘祥超
2021-07-20 19:48:08 +08:00
parent 6bd7da5e6e
commit 01cfccebbd
+24 -11
View File
@@ -9,6 +9,7 @@ import (
"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/iwind/TeaGo/lists" "github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/types"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@@ -223,17 +224,29 @@ func (this *HTTPWriter) Close() {
// cache writer // cache writer
if this.cacheWriter != nil { if this.cacheWriter != nil {
if this.isOk { if this.isOk {
err := this.cacheWriter.Close() // 对比Content-Length
if err == nil { contentLengthString := this.Header().Get("Content-Length")
this.cacheStorage.AddToList(&caches.Item{ if len(contentLengthString) > 0 {
Type: this.cacheWriter.ItemType(), contentLength := types.Int64(contentLengthString)
Key: this.cacheWriter.Key(), if contentLength != this.cacheWriter.BodySize() {
ExpiredAt: this.cacheWriter.ExpiredAt(), this.isOk = false
HeaderSize: this.cacheWriter.HeaderSize(), _ = this.cacheWriter.Discard()
BodySize: this.cacheWriter.BodySize(), }
Host: this.req.Host, }
ServerId: this.req.Server.Id,
}) if this.isOk {
err := this.cacheWriter.Close()
if err == nil {
this.cacheStorage.AddToList(&caches.Item{
Type: this.cacheWriter.ItemType(),
Key: this.cacheWriter.Key(),
ExpiredAt: this.cacheWriter.ExpiredAt(),
HeaderSize: this.cacheWriter.HeaderSize(),
BodySize: this.cacheWriter.BodySize(),
Host: this.req.Host,
ServerId: this.req.Server.Id,
})
}
} }
} else { } else {
_ = this.cacheWriter.Discard() _ = this.cacheWriter.Discard()