From 01c7f2b714b6a098bc913f22cdc77704d5906d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Tue, 7 Dec 2021 10:43:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=94=AF=E6=8C=81=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E6=96=B9=E6=B3=95=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/serverconfigs/http_cache_ref.go | 30 +++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pkg/serverconfigs/http_cache_ref.go b/pkg/serverconfigs/http_cache_ref.go index 8d67ca8..8e68f63 100644 --- a/pkg/serverconfigs/http_cache_ref.go +++ b/pkg/serverconfigs/http_cache_ref.go @@ -3,6 +3,7 @@ package serverconfigs import ( "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/iwind/TeaGo/lists" + "net/http" "strings" ) @@ -17,6 +18,7 @@ type HTTPCacheRef struct { Status []int `yaml:"status" json:"status"` // 缓存的状态码列表 MinSize *shared.SizeCapacity `yaml:"minSize" json:"minSize"` // 能够缓存的最小尺寸 MaxSize *shared.SizeCapacity `yaml:"maxSize" json:"maxSize"` // 能够缓存的最大尺寸 + Methods []string `yaml:"methods" json:"methods"` // 支持的请求方法 SkipResponseCacheControlValues []string `yaml:"skipCacheControlValues" json:"skipCacheControlValues"` // 可以跳过的响应的Cache-Control值 SkipResponseSetCookie bool `yaml:"skipSetCookie" json:"skipSetCookie"` // 是否跳过响应的Set-Cookie Header @@ -33,6 +35,8 @@ type HTTPCacheRef struct { minSize int64 maxSize int64 uppercaseSkipCacheControlValues []string + + methodMap map[string]bool } func (this *HTTPCacheRef) Init() error { @@ -68,6 +72,19 @@ func (this *HTTPCacheRef) Init() error { } } + // methods + this.methodMap = map[string]bool{} + if len(this.Methods) > 0 { + for _, method := range this.Methods { + this.methodMap[strings.ToUpper(method)] = true + } + } else { + this.methodMap = map[string]bool{ + "GET": true, + "HEAD": true, + } + } + return nil } @@ -90,3 +107,16 @@ func (this *HTTPCacheRef) LifeSeconds() int64 { func (this *HTTPCacheRef) ContainsCacheControl(value string) bool { return lists.ContainsString(this.uppercaseSkipCacheControlValues, strings.ToUpper(value)) } + +// MatchRequest 匹配请求 +func (this *HTTPCacheRef) MatchRequest(req *http.Request) bool { + // 请求方法 + if len(this.methodMap) > 0 { + _, ok := this.methodMap[req.Method] + if !ok { + return false + } + } + + return true +}