diff --git a/internal/nodes/http_writer.go b/internal/nodes/http_writer.go index fd32d7d..fb40120 100644 --- a/internal/nodes/http_writer.go +++ b/internal/nodes/http_writer.go @@ -552,11 +552,6 @@ func (this *HTTPWriter) prepareCache(size int64) { return } - // 不支持Range - if len(this.Header().Get("Content-Range")) > 0 { - return - } - cachePolicy := this.req.Server.HTTPCachePolicy if cachePolicy == nil || !cachePolicy.IsOn { return @@ -567,17 +562,36 @@ func (this *HTTPWriter) prepareCache(size int64) { return } + var addStatusHeader = this.req.web != nil && this.req.web.Cache != nil && this.req.web.Cache.AddStatusHeader + + // 不支持Range + if len(this.Header().Get("Content-Range")) > 0 { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, not supported Content-Range") + } + return + } + // 如果允许 ChunkedEncoding,就无需尺寸的判断,因为此时的 size 为 -1 if !cacheRef.AllowChunkedEncoding && size < 0 { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, ChunkedEncoding") + } return } if size >= 0 && ((cacheRef.MaxSizeBytes() > 0 && size > cacheRef.MaxSizeBytes()) || (cachePolicy.MaxSizeBytes() > 0 && size > cachePolicy.MaxSizeBytes()) || (cacheRef.MinSizeBytes() > size)) { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, Content-Length") + } return } // 检查状态 if len(cacheRef.Status) > 0 && !lists.ContainsInt(cacheRef.Status, this.StatusCode()) { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, Status: "+types.String(this.StatusCode())) + } return } @@ -588,6 +602,9 @@ func (this *HTTPWriter) prepareCache(size int64) { values := strings.Split(cacheControl, ",") for _, value := range values { if cacheRef.ContainsCacheControl(strings.TrimSpace(value)) { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, Cache-Control: "+cacheControl) + } return } } @@ -596,19 +613,29 @@ func (this *HTTPWriter) prepareCache(size int64) { // Set-Cookie if cacheRef.SkipResponseSetCookie && len(this.writer.Header().Get("Set-Cookie")) > 0 { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, Set-Cookie") + } return } // 校验其他条件 if cacheRef.Conds != nil && cacheRef.Conds.HasResponseConds() && !cacheRef.Conds.MatchResponse(this.req.Format) { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, ResponseConds") + } return } // 打开缓存写入 storage := caches.SharedManager.FindStorageWithPolicy(cachePolicy.Id) if storage == nil { + if addStatusHeader { + this.Header().Set("X-Cache", "BYPASS, Storage") + } return } + this.cacheStorage = storage life := cacheRef.LifeSeconds() if life <= 60 { // 最小不能少于1分钟