Skip to content

Commit 64c693b

Browse files
committed
路径规则、重写规则、URL跳转规则均支持匹配条件
1 parent df4b79c commit 64c693b

File tree

7 files changed

+208
-152
lines changed

7 files changed

+208
-152
lines changed

pkg/rpc/pb/service_http_location.pb.go

Lines changed: 121 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/rpc/pb/service_http_rewrite_rule.pb.go

Lines changed: 56 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/rpc/protos/service_http_location.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ message CreateHTTPLocationRequest {
3535
string description = 3;
3636
string pattern = 4;
3737
bool isBreak = 5;
38+
bytes condsJSON = 6;
3839
}
3940

4041
message CreateHTTPLocationResponse {
@@ -49,6 +50,7 @@ message UpdateHTTPLocationRequest {
4950
string pattern = 4;
5051
bool isOn = 5;
5152
bool isBreak = 6;
53+
bytes condsJSON = 7;
5254
}
5355

5456
// 查找路径规则配置

pkg/rpc/protos/service_http_rewrite_rule.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ message CreateHTTPRewriteRuleRequest {
2323
string proxyHost = 6;
2424
bool isOn = 7;
2525
bool withQuery = 8;
26+
bytes condsJSON = 9;
2627
}
2728

2829
message CreateHTTPRewriteRuleResponse {
@@ -40,4 +41,5 @@ message UpdateHTTPRewriteRuleRequest {
4041
string proxyHost = 7;
4142
bool isOn = 8;
4243
bool withQuery = 9;
44+
bytes condsJSON = 10;
4345
}

pkg/serverconfigs/http_host_redirect_config.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package serverconfigs
22

33
import (
4+
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
45
"net/url"
56
"regexp"
67
)
@@ -13,9 +14,10 @@ type HTTPHostRedirectConfig struct {
1314
BeforeURL string `yaml:"beforeURL" json:"beforeURL"` // 跳转前的地址
1415
AfterURL string `yaml:"afterURL" json:"afterURL"` // 跳转后的地址
1516

16-
MatchPrefix bool `yaml:"matchPrefix" json:"matchPrefix"` // 只匹配前缀部分
17-
MatchRegexp bool `yaml:"matchRegexp" json:"matchRegexp"` // 匹配正则表达式
18-
KeepRequestURI bool `yaml:"keepRequestURI" json:"keepRequestURI"` // 保留请求URI
17+
MatchPrefix bool `yaml:"matchPrefix" json:"matchPrefix"` // 只匹配前缀部分
18+
MatchRegexp bool `yaml:"matchRegexp" json:"matchRegexp"` // 匹配正则表达式
19+
KeepRequestURI bool `yaml:"keepRequestURI" json:"keepRequestURI"` // 保留请求URI
20+
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
1921

2022
realBeforeURL string
2123
beforeURLRegexp *regexp.Regexp
@@ -41,6 +43,13 @@ func (this *HTTPHostRedirectConfig) Init() error {
4143
this.beforeURLRegexp = reg
4244
}
4345

46+
if this.Conds != nil {
47+
err := this.Conds.Init()
48+
if err != nil {
49+
return err
50+
}
51+
}
52+
4453
return nil
4554
}
4655

@@ -53,3 +62,11 @@ func (this *HTTPHostRedirectConfig) RealBeforeURL() string {
5362
func (this *HTTPHostRedirectConfig) BeforeURLRegexp() *regexp.Regexp {
5463
return this.beforeURLRegexp
5564
}
65+
66+
// MatchRequest 判断请求是否符合条件
67+
func (this *HTTPHostRedirectConfig) MatchRequest(formatter func(source string) string) bool {
68+
if this.Conds == nil {
69+
return true
70+
}
71+
return this.Conds.MatchRequest(formatter)
72+
}

pkg/serverconfigs/http_location_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type HTTPLocationConfig struct {
1919
ReverseProxy *ReverseProxyConfig `yaml:"reverseProxy" json:"reverseProxy"` // 反向代理设置
2020
IsBreak bool `yaml:"isBreak" json:"isBreak"` // 终止向下解析
2121
Children []*HTTPLocationConfig `yaml:"children" json:"children"` // 子规则
22-
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件 TODO
22+
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
2323

2424
patternType HTTPLocationPatternType // 规则类型:LocationPattern*
2525
prefix string // 前缀

pkg/serverconfigs/http_rewrite_rule.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ const (
1919
HTTPRewriteModeProxy HTTPRewriteMode = "proxy" // 代理
2020
)
2121

22-
// 重写规则定义
23-
//
24-
// 参考
25-
// - http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
26-
// - https://httpd.apache.org/docs/current/mod/mod_rewrite.html
27-
// - https://httpd.apache.org/docs/2.4/rewrite/flags.html
22+
// HTTPRewriteRule 重写规则定义
2823
// TODO 实现对其他代理服务的引用
2924
type HTTPRewriteRule struct {
3025
Id int64 `yaml:"id" json:"id"` // ID
@@ -35,7 +30,7 @@ type HTTPRewriteRule struct {
3530
// - cond ${status} gte 200
3631
// - cond ${arg.name} eq lily
3732
// - cond ${requestPath} regexp .*\.png
38-
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件 TODO
33+
Conds *shared.HTTPRequestCondsConfig `yaml:"conds" json:"conds"` // 匹配条件
3934

4035
// 规则
4136
// 语法为:pattern regexp 比如:
@@ -61,7 +56,7 @@ type HTTPRewriteRule struct {
6156
proxyHostHasVariables bool
6257
}
6358

64-
// 校验
59+
// Init 校验
6560
func (this *HTTPRewriteRule) Init() error {
6661
this.replaceHasVariables = configutils.HasVariables(this.Replace)
6762
this.proxyHostHasVariables = configutils.HasVariables(this.ProxyHost)
@@ -83,7 +78,7 @@ func (this *HTTPRewriteRule) Init() error {
8378
return nil
8479
}
8580

86-
// 对某个请求执行规则
81+
// MatchRequest 对某个请求执行规则
8782
func (this *HTTPRewriteRule) MatchRequest(requestPath string, formatter func(source string) string) (replace string, varMapping map[string]string, matched bool) {
8883
if this.reg == nil {
8984
return "", nil, false
@@ -124,12 +119,12 @@ func (this *HTTPRewriteRule) MatchRequest(requestPath string, formatter func(sou
124119
return replace, varMapping, true
125120
}
126121

127-
// 判断是否是外部URL
122+
// IsExternalURL 判断是否是外部URL
128123
func (this *HTTPRewriteRule) IsExternalURL(url string) bool {
129124
return shared.RegexpExternalURL.MatchString(url)
130125
}
131126

132-
// 判断ProxyHost是否有变量
127+
// ProxyHostHasVariables 判断ProxyHost是否有变量
133128
func (this *HTTPRewriteRule) ProxyHostHasVariables() bool {
134129
return this.proxyHostHasVariables
135130
}

0 commit comments

Comments
 (0)