From 01cfccebbdd0b028a34a9e46baedb303cd4f375b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Tue, 20 Jul 2021 19:48:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E7=BB=93=E6=9D=9F=E5=90=8E?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0Content-Length=E5=AF=B9=E6=AF=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_writer.go | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index a2cd3b9..352c2fc 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -9,6 +9,7 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/remotelogs" "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/iwind/TeaGo/lists" + "github.com/iwind/TeaGo/types" "net" "net/http" "strings" @@ -223,17 +224,29 @@ func (this *HTTPWriter) Close() { // cache writer if this.cacheWriter != nil { 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, - }) + // 对比Content-Length + contentLengthString := this.Header().Get("Content-Length") + if len(contentLengthString) > 0 { + contentLength := types.Int64(contentLengthString) + if contentLength != this.cacheWriter.BodySize() { + this.isOk = false + _ = this.cacheWriter.Discard() + } + } + + 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 { _ = this.cacheWriter.Discard()