From d2e80ef4752381ae802207b2bed4212932baecc6 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 10 Aug 2023 11:27:00 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9D=99=E6=80=81=E5=88=86=E5=8F=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BE=8B=E5=A4=96URL=E3=80=81=E9=99=90=E5=88=B6URL?= =?UTF-8?q?=E3=80=81=E6=8E=92=E9=99=A4=E9=9A=90=E8=97=8F=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=AD=89=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/serverconfigs/http_root_config.go | 71 +++++++++++++++++--- pkg/serverconfigs/shared/url_pattern_test.go | 6 ++ 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/pkg/serverconfigs/http_root_config.go b/pkg/serverconfigs/http_root_config.go index 1a4b55a..b32e131 100644 --- a/pkg/serverconfigs/http_root_config.go +++ b/pkg/serverconfigs/http_root_config.go @@ -1,27 +1,76 @@ package serverconfigs -import "github.com/TeaOSLab/EdgeCommon/pkg/configutils" +import ( + "github.com/TeaOSLab/EdgeCommon/pkg/configutils" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" +) -// Web文档目录配置 +func NewHTTPRootConfig() *HTTPRootConfig { + return &HTTPRootConfig{ + ExceptHiddenFiles: true, + } +} + +// HTTPRootConfig Web文档目录配置 type HTTPRootConfig struct { - IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否优先 - IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 - Dir string `yaml:"dir" json:"dir"` // 目录 - Indexes []string `yaml:"indexes" json:"indexes"` // 默认首页文件 - StripPrefix string `yaml:"stripPrefix" json:"stripPrefix"` // 去除URL前缀 - DecodePath bool `yaml:"decodePath" json:"decodePath"` // 是否对请求路径进行解码 - IsBreak bool `yaml:"isBreak" json:"isBreak"` // 找不到文件的情况下是否终止 + IsPrior bool `yaml:"isPrior" json:"isPrior"` // 是否优先 + IsOn bool `yaml:"isOn" json:"isOn"` // 是否启用 + Dir string `yaml:"dir" json:"dir"` // 目录 + Indexes []string `yaml:"indexes" json:"indexes"` // 默认首页文件 + StripPrefix string `yaml:"stripPrefix" json:"stripPrefix"` // 去除URL前缀 + DecodePath bool `yaml:"decodePath" json:"decodePath"` // 是否对请求路径进行解码 + IsBreak bool `yaml:"isBreak" json:"isBreak"` // 找不到文件的情况下是否终止 + ExceptHiddenFiles bool `yaml:"exceptHiddenFiles" json:"exceptHiddenFiles"` // 排除隐藏文件 + OnlyURLPatterns []*shared.URLPattern `yaml:"onlyURLPatterns" json:"onlyURLPatterns"` // 仅限的URL + ExceptURLPatterns []*shared.URLPattern `yaml:"exceptURLPatterns" json:"exceptURLPatterns"` // 排除的URL hasVariables bool } -// 初始化 +// Init 初始化 func (this *HTTPRootConfig) Init() error { this.hasVariables = configutils.HasVariables(this.Dir) + + for _, pattern := range this.OnlyURLPatterns { + err := pattern.Init() + if err != nil { + return err + } + } + + for _, pattern := range this.ExceptURLPatterns { + err := pattern.Init() + if err != nil { + return err + } + } + return nil } -// 判断是否有变量 +// HasVariables 判断是否有变量 func (this *HTTPRootConfig) HasVariables() bool { return this.hasVariables } + +func (this *HTTPRootConfig) MatchURL(url string) bool { + // except + if len(this.ExceptURLPatterns) > 0 { + for _, pattern := range this.ExceptURLPatterns { + if pattern.Match(url) { + return false + } + } + } + + if len(this.OnlyURLPatterns) > 0 { + for _, pattern := range this.OnlyURLPatterns { + if pattern.Match(url) { + return true + } + } + return false + } + + return true +} diff --git a/pkg/serverconfigs/shared/url_pattern_test.go b/pkg/serverconfigs/shared/url_pattern_test.go index 6e5ae29..c1f419e 100644 --- a/pkg/serverconfigs/shared/url_pattern_test.go +++ b/pkg/serverconfigs/shared/url_pattern_test.go @@ -70,6 +70,12 @@ func TestURLPattern_Match(t *testing.T) { url: "https://example-test.com/123/hello/world", result: false, }, + { + patternType: "wildcard", + pattern: "/hidden/*", + url: "/hidden/index.html", + result: false, // because don't have https://HOST in url + }, { patternType: "regexp", pattern: ".*",