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

@@ -19,8 +19,8 @@ const (
)
type SSEServerWrapper struct {
BaseServer *common.SSEServer
DomainList []string
BaseServer *common.SSEServer
HostMatchers []common.HostMatcher // Pre-parsed host matchers for efficient matching
}
type config struct {
@@ -68,15 +68,18 @@ func (p *Parser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (int
return nil, fmt.Errorf("server %s path is not set", serverType)
}
serverDomainList := []string{}
// Parse domain list directly into HostMatchers for efficient matching
var hostMatchers []common.HostMatcher
if domainList, ok := serverConfigMap["domain_list"].([]interface{}); ok {
hostMatchers = make([]common.HostMatcher, 0, len(domainList))
for _, domain := range domainList {
if domainStr, ok := domain.(string); ok {
serverDomainList = append(serverDomainList, domainStr)
hostMatchers = append(hostMatchers, common.ParseHostPattern(domainStr))
}
}
} else {
serverDomainList = []string{"*"}
// Default to match all domains
hostMatchers = []common.HostMatcher{common.ParseHostPattern("*")}
}
serverName, ok := serverConfigMap["name"].(string)
@@ -108,7 +111,7 @@ func (p *Parser) Parse(any *anypb.Any, callbacks api.ConfigCallbackHandler) (int
BaseServer: common.NewSSEServer(serverInstance,
common.WithSSEEndpoint(fmt.Sprintf("%s%s", serverPath, mcp_session.GlobalSSEPathSuffix)),
common.WithMessageEndpoint(serverPath)),
DomainList: serverDomainList,
HostMatchers: hostMatchers,
})
api.LogDebug(fmt.Sprintf("Registered MCP Server: %s", serverType))
}

View File

@@ -30,7 +30,7 @@ func (f *filter) DecodeHeaders(header api.RequestHeaderMap, endStream bool) api.
f.host = url.Host
for _, server := range f.config.servers {
if common.MatchDomainList(f.host, server.DomainList) && strings.HasPrefix(f.path, server.BaseServer.GetMessageEndpoint()) {
if common.MatchDomainWithMatchers(f.host, server.HostMatchers) && strings.HasPrefix(f.path, server.BaseServer.GetMessageEndpoint()) {
if url.Method != http.MethodPost {
f.callbacks.DecoderFilterCallbacks().SendLocalReply(http.StatusMethodNotAllowed, "Method not allowed", nil, 0, "")
return api.LocalReply
@@ -62,7 +62,7 @@ func (f *filter) DecodeHeaders(header api.RequestHeaderMap, endStream bool) api.
func (f *filter) DecodeData(buffer api.BufferInstance, endStream bool) api.StatusType {
if f.message {
for _, server := range f.config.servers {
if common.MatchDomainList(f.host, server.DomainList) && strings.HasPrefix(f.path, server.BaseServer.GetMessageEndpoint()) {
if common.MatchDomainWithMatchers(f.host, server.HostMatchers) && strings.HasPrefix(f.path, server.BaseServer.GetMessageEndpoint()) {
if !endStream {
return api.StopAndBuffer
}