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: ".*",