静态分发增加例外URL、限制URL、排除隐藏文件等选项

This commit is contained in:
GoEdgeLab
2023-08-10 11:27:00 +08:00
parent 24d4721c9c
commit d2e80ef475
2 changed files with 66 additions and 11 deletions

View File

@@ -1,8 +1,17 @@
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"` // 是否启用
@@ -11,17 +20,57 @@ type HTTPRootConfig struct {
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
}

View File

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