mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	可以在缓存条件里设置Expires Header
This commit is contained in:
		@@ -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")
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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()))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user