fix: optimize host pattern matching and fix SSE newline bug (#2899)

This commit is contained in:
Jingze
2025-09-21 14:34:51 +08:00
committed by GitHub
parent de8a9c539b
commit f1345f9973
6 changed files with 34 additions and 30 deletions

View File

@@ -46,6 +46,7 @@ type MatchRule struct {
UpstreamType UpstreamType `json:"upstream_type"` // Type of upstream(s) matched by the rule
EnablePathRewrite bool `json:"enable_path_rewrite"` // Enable request path rewrite for matched routes
PathRewritePrefix string `json:"path_rewrite_prefix"` // Prefix the request path would be rewritten to.
HostMatcher HostMatcher // Host matcher for efficient matching
}
// ParseMatchList parses the match list from the config
@@ -91,6 +92,9 @@ func ParseMatchList(matchListConfig []interface{}) []MatchRule {
rule.PathRewritePrefix = "/" + rule.PathRewritePrefix
}
}
rule.HostMatcher = ParseHostPattern(rule.MatchRuleDomain)
matchList = append(matchList, rule)
}
}
@@ -115,8 +119,8 @@ func stripPortFromHost(reqHost string) string {
return reqHost
}
// parseHostPattern parses a host pattern and returns a HostMatcher
func parseHostPattern(pattern string) HostMatcher {
// ParseHostPattern parses a host pattern and returns a HostMatcher
func ParseHostPattern(pattern string) HostMatcher {
var hostMatcher HostMatcher
if strings.HasPrefix(pattern, "*") {
hostMatcher.matchType = HostSuffix
@@ -157,18 +161,11 @@ func matchPattern(pattern string, target string, ruleType RuleType) bool {
}
}
// matchDomain checks if the domain matches the pattern using HostMatcher approach
func matchDomain(domain string, pattern string) bool {
if pattern == "" || pattern == "*" {
return true
}
// matchDomainWithMatcher checks if the domain matches using a pre-parsed HostMatcher
func matchDomainWithMatcher(domain string, hostMatcher HostMatcher) bool {
// Strip port from domain
domain = stripPortFromHost(domain)
// Parse the pattern into a HostMatcher
hostMatcher := parseHostPattern(pattern)
// Perform matching based on match type
switch hostMatcher.matchType {
case HostSuffix:
@@ -184,7 +181,7 @@ func matchDomain(domain string, pattern string) bool {
// matchDomainAndPath checks if both domain and path match the rule
func matchDomainAndPath(domain, path string, rule MatchRule) bool {
return matchDomain(domain, rule.MatchRuleDomain) &&
return matchDomainWithMatcher(domain, rule.HostMatcher) &&
matchPattern(rule.MatchRulePath, path, rule.MatchRuleType)
}
@@ -204,9 +201,9 @@ func IsMatch(rules []MatchRule, host, path string) (bool, MatchRule) {
}
// MatchDomainList checks if the domain matches any of the domains in the list
func MatchDomainList(domain string, domainList []string) bool {
for _, d := range domainList {
if matchDomain(domain, d) {
func MatchDomainWithMatchers(domain string, hostMatchers []HostMatcher) bool {
for _, hostMatcher := range hostMatchers {
if matchDomainWithMatcher(domain, hostMatcher) {
return true
}
}

View File

@@ -223,7 +223,12 @@ func (s *SSEServer) HandleMessage(w http.ResponseWriter, r *http.Request, body j
}
// Send HTTP response
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
jsonData, err := json.Marshal(response)
if err != nil {
api.LogErrorf("Failed to marshal SSE Message response: %v", err)
status = http.StatusInternalServerError
}
w.Write(jsonData)
} else {
// For notifications, just send 202 Accepted with no body
w.WriteHeader(http.StatusAccepted)

View File

@@ -12,7 +12,6 @@ type RequestURL struct {
Scheme string
Host string
Path string
BaseURL string
ParsedURL *url.URL
InternalIP bool
}
@@ -23,12 +22,12 @@ func NewRequestURL(header api.RequestHeaderMap) *RequestURL {
host, _ := header.Get(":authority")
path, _ := header.Get(":path")
internalIP, _ := header.Get("x-envoy-internal")
baseURL := fmt.Sprintf("%s://%s", scheme, host)
parsedURL, err := url.Parse(path)
fullURL := fmt.Sprintf("%s://%s%s", scheme, host, path)
parsedURL, err := url.Parse(fullURL)
if err != nil {
api.LogWarnf("url parse path:%s failed:%s", path, err)
api.LogWarnf("url parse fullURL:%s failed:%s", fullURL, err)
return nil
}
api.LogDebugf("RequestURL: method=%s, scheme=%s, host=%s, path=%s", method, scheme, host, path)
return &RequestURL{Method: method, Scheme: scheme, Host: host, Path: path, BaseURL: baseURL, ParsedURL: parsedURL, InternalIP: internalIP == "true"}
return &RequestURL{Method: method, Scheme: scheme, Host: host, Path: path, ParsedURL: parsedURL, InternalIP: internalIP == "true"}
}

View File

@@ -264,7 +264,7 @@ func (f *filter) encodeDataFromRestUpstream(buffer api.BufferInstance, endStream
sessionID := f.proxyURL.Query().Get("sessionId")
if sessionID != "" {
channel := common.GetSSEChannelName(sessionID)
eventData := fmt.Sprintf("event: message\ndata: %s\n\n", strings.TrimSuffix(buffer.String(), "\n"))
eventData := fmt.Sprintf("event: message\ndata: %s\n\n", buffer.String())
publishErr := f.config.redisClient.Publish(channel, eventData)
if publishErr != nil {
api.LogErrorf("Failed to publish wasm mcp server message to Redis: %v", publishErr)