From 4d8565d5835c5da38b9e1f267530b893f2ddb58a Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Mon, 4 Oct 2021 08:41:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=90=AF=E7=BC=93=E5=AD=98=E5=90=8E?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E6=BA=90=E7=AB=99=E7=9A=84ETag=E5=92=8CLast-?= =?UTF-8?q?Modified?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_cache.go | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index ae4ccbb..6c2f5ad 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -181,25 +181,22 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { } // ETag + // 这里强制设置ETag,如果先前源站设置了ETag,将会被覆盖,避免因为源站的ETag导致源站返回304 Not Modified var respHeader = this.writer.Header() - var eTag = respHeader.Get("ETag") + var eTag = "" var lastModifiedAt = reader.LastModified() - if len(eTag) == 0 { - if lastModifiedAt > 0 { - eTag = "\"" + strconv.FormatInt(lastModifiedAt, 10) + "\"" - respHeader["ETag"] = []string{eTag} - } + if lastModifiedAt > 0 { + eTag = "\"" + strconv.FormatInt(lastModifiedAt, 10) + "\"" + respHeader.Del("Etag") + respHeader["ETag"] = []string{eTag} } // 支持 Last-Modified - var modifiedTime = respHeader.Get("Last-Modified") - if len(modifiedTime) == 0 { - if lastModifiedAt > 0 { - modifiedTime = time.Unix(lastModifiedAt, 0).Format("Mon, 02 Jan 2006 15:04:05 GMT") - if len(respHeader.Get("Last-Modified")) == 0 { - respHeader.Set("Last-Modified", modifiedTime) - } - } + // 这里强制设置Last-Modified,如果先前源站设置了Last-Modified,将会被覆盖,避免因为源站的Last-Modified导致源站返回304 Not Modified + var modifiedTime = "" + if lastModifiedAt > 0 { + modifiedTime = time.Unix(lastModifiedAt, 0).Format("Mon, 02 Jan 2006 15:04:05 GMT") + respHeader.Set("Last-Modified", modifiedTime) } // 支持 If-None-Match