mirror of
https://github.com/alibaba/higress.git
synced 2026-06-09 04:37:31 +08:00
wasm: strip port from host when match host (#626)
This commit is contained in:
@@ -109,7 +109,6 @@ func (m *RuleMatcher[PluginConfig]) ParseRuleConfig(config gjson.Result,
|
||||
if keyCount > 0 {
|
||||
err := parsePluginConfig(config, &pluginConfig)
|
||||
if err != nil {
|
||||
proxywasm.LogWarnf("parse global config failed, err:%v", err)
|
||||
globalConfigError = err
|
||||
} else {
|
||||
m.globalConfig = pluginConfig
|
||||
@@ -185,7 +184,25 @@ func (m RuleMatcher[PluginConfig]) parseHostMatchConfig(config gjson.Result) []H
|
||||
return hostMatchers
|
||||
}
|
||||
|
||||
func stripPortFromHost(reqHost string) string {
|
||||
// Port removing code is inspired by
|
||||
// https://github.com/envoyproxy/envoy/blob/v1.17.0/source/common/http/header_utility.cc#L219
|
||||
portStart := strings.LastIndexByte(reqHost, ':')
|
||||
if portStart != -1 {
|
||||
// According to RFC3986 v6 address is always enclosed in "[]".
|
||||
// section 3.2.2.
|
||||
v6EndIndex := strings.LastIndexByte(reqHost, ']')
|
||||
if v6EndIndex == -1 || v6EndIndex < portStart {
|
||||
if portStart+1 <= len(reqHost) {
|
||||
return reqHost[:portStart]
|
||||
}
|
||||
}
|
||||
}
|
||||
return reqHost
|
||||
}
|
||||
|
||||
func (m RuleMatcher[PluginConfig]) hostMatch(rule RuleConfig[PluginConfig], reqHost string) bool {
|
||||
reqHost = stripPortFromHost(reqHost)
|
||||
for _, hostMatch := range rule.hosts {
|
||||
switch hostMatch.matchType {
|
||||
case Suffix:
|
||||
|
||||
@@ -118,6 +118,19 @@ func TestHostMatch(t *testing.T) {
|
||||
host: "example.com",
|
||||
result: false,
|
||||
},
|
||||
{
|
||||
name: "exact port",
|
||||
config: RuleConfig[customConfig]{
|
||||
hosts: []HostMatcher{
|
||||
{
|
||||
matchType: Exact,
|
||||
host: "www.example.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
host: "www.example.com:8080",
|
||||
result: true,
|
||||
},
|
||||
{
|
||||
name: "any",
|
||||
config: RuleConfig[customConfig]{
|
||||
|
||||
Reference in New Issue
Block a user