From 9c79d5ff859334a5ccd6626f6eb9e874c44ba24c Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Wed, 8 May 2024 10:30:47 +0800 Subject: [PATCH] =?UTF-8?q?Partial=20Content=E6=96=B0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=97=B6=E4=B8=8D=E5=AF=B9=E6=AF=94Content-MD5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_writer.go | 37 +++++++++++++++-------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index f8822fa..a899ece 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -355,23 +355,28 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) { } this.cacheWriter = cacheWriter - // 对比Content-MD5 - { + if this.isPartial { partialWriter, ok := cacheWriter.(*caches.PartialFileWriter) if ok { - if partialWriter.Ranges().Version >= 2 && partialWriter.Ranges().ContentMD5 != this.Header().Get("Content-MD5") { - _ = this.cacheWriter.Discard() - this.cacheWriter = nil - return + // 判断是否新创建的缓存文件 + this.partialFileIsNew = partialWriter.IsNew() + + if this.partialFileIsNew { + var contentMD5 = this.rawWriter.Header().Get("Content-MD5") + if len(contentMD5) > 0 { + partialWriter.SetContentMD5(contentMD5) + } + } else { + // 对比Content-MD5 + if partialWriter.Ranges().Version >= 2 && partialWriter.Ranges().ContentMD5 != this.Header().Get("Content-MD5") { + _ = this.cacheWriter.Discard() + this.cacheWriter = nil + return + } } } } - // 判断是否新创建的缓存文件 - if this.isPartial { - this.partialFileIsNew = cacheWriter.(*caches.PartialFileWriter).IsNew() - } - // 写入Header var headerBuf = utils.SharedBufferPool.Get() for k, v := range this.Header() { @@ -416,11 +421,6 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) { return } partialWriter.SetBodyLength(total) - - var contentMD5 = this.rawWriter.Header().Get("Content-MD5") - if len(contentMD5) > 0 { - partialWriter.SetContentMD5(contentMD5) - } } var filterReader = readers.NewFilterReaderCloser(resp.Body) this.cacheIsFinished = true @@ -480,11 +480,6 @@ func (this *HTTPWriter) PrepareCache(resp *http.Response, size int64) { // 写入total if !writtenTotal && total > 0 { partialWriter.SetBodyLength(total) - var contentMD5 = this.rawWriter.Header().Get("Content-MD5") - if len(contentMD5) > 0 { - partialWriter.SetContentMD5(contentMD5) - } - writtenTotal = true }