diff --git a/internal/nodes/http_request_cache.go b/internal/nodes/http_request_cache.go index daa2e3e..7cc65e6 100644 --- a/internal/nodes/http_request_cache.go +++ b/internal/nodes/http_request_cache.go @@ -241,7 +241,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { // 这里强制设置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") + modifiedTime = time.Unix(utils.GMTUnixTime(lastModifiedAt), 0).Format("Mon, 02 Jan 2006 15:04:05") + " GMT" respHeader.Set("Last-Modified", modifiedTime) } @@ -268,6 +268,7 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { } this.processResponseHeaders(reader.Status()) + this.addExpiresHeader(reader.ExpiresAt()) // 输出Body if this.RawReq.Method == http.MethodHead { @@ -441,3 +442,19 @@ func (this *HTTPRequest) doCacheRead() (shouldStop bool) { return true } + +// 设置Expires Header +func (this *HTTPRequest) addExpiresHeader(expiresAt int64) { + if this.cacheRef.ExpiresTime != nil && this.cacheRef.ExpiresTime.IsPrior && this.cacheRef.ExpiresTime.IsOn { + if this.cacheRef.ExpiresTime.Overwrite || len(this.writer.Header().Get("Expires")) == 0 { + if this.cacheRef.ExpiresTime.AutoCalculate { + this.writer.Header().Set("Expires", time.Unix(utils.GMTUnixTime(expiresAt), 0).Format("Mon, 2 Jan 2006 15:04:05")+" GMT") + } else if this.cacheRef.ExpiresTime.Duration != nil { + var duration = this.cacheRef.ExpiresTime.Duration.Duration() + if duration > 0 { + this.writer.Header().Set("Expires", utils.GMTTime(time.Now().Add(duration)).Format("Mon, 2 Jan 2006 15:04:05")+" GMT") + } + } + } + } +} diff --git a/internal/utils/time.go b/internal/utils/time.go index 2094b5e..65450ed 100644 --- a/internal/utils/time.go +++ b/internal/utils/time.go @@ -27,3 +27,15 @@ func UnixTime() int64 { func UnixTimeMilli() int64 { return unixTimeMilli } + +// GMTUnixTime 计算GMT时间戳 +func GMTUnixTime(timestamp int64) int64 { + _, offset := time.Now().Zone() + return timestamp - int64(offset) +} + +// GMTTime 计算GMT时间 +func GMTTime(t time.Time) time.Time { + _, offset := time.Now().Zone() + return t.Add(-time.Duration(offset) * time.Second) +} diff --git a/internal/utils/time_test.go b/internal/utils/time_test.go index 7df1064..cf0da76 100644 --- a/internal/utils/time_test.go +++ b/internal/utils/time_test.go @@ -11,3 +11,11 @@ func TestUnixTime(t *testing.T) { time.Sleep(1 * time.Second) } } + +func TestGMTUnixTime(t *testing.T) { + t.Log(GMTUnixTime(time.Now().Unix())) +} + +func TestGMTTime(t *testing.T) { + t.Log(GMTTime(time.Now())) +}