Skip to content

Commit cc5f536

Browse files
mattdhollowaySamMorrowDrums
authored andcommitted
refine pattern matching logic to prioritise non-wildcard handlers in multiHandlerTransport
1 parent 27c1051 commit cc5f536

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

pkg/github/helper_test.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,20 +582,45 @@ func (m *multiHandlerTransport) RoundTrip(req *http.Request) (*http.Response, er
582582
return executeHandler(handler, req), nil
583583
}
584584

585-
// Then try pattern matching
585+
// Then try pattern matching, prioritizing patterns without wildcards
586+
// This is important because wildcard patterns like /{owner}/{repo}/{sha}/{path:.*}
587+
// can incorrectly match API paths like /repos/owner/repo/pulls/42
588+
var wildcardPattern string
589+
var wildcardHandler http.HandlerFunc
590+
586591
for pattern, handler := range m.handlers {
587592
if pattern == "" {
588593
continue // Skip catch-all
589594
}
590595
parts := strings.SplitN(pattern, " ", 2)
591-
if len(parts) == 2 {
592-
method, pathPattern := parts[0], parts[1]
593-
if req.Method == method && matchPath(pathPattern, req.URL.Path) {
596+
if len(parts) != 2 {
597+
continue
598+
}
599+
method, pathPattern := parts[0], parts[1]
600+
if req.Method != method {
601+
continue
602+
}
603+
604+
// Check if this pattern contains a wildcard like {path:.*}
605+
isWildcard := strings.Contains(pathPattern, ":.*}")
606+
607+
if matchPath(pathPattern, req.URL.Path) {
608+
if isWildcard {
609+
// Save wildcard match for later, prefer non-wildcard patterns
610+
wildcardPattern = pattern
611+
wildcardHandler = handler
612+
} else {
613+
// Non-wildcard pattern takes priority
594614
return executeHandler(handler, req), nil
595615
}
596616
}
597617
}
598618

619+
// If we found a wildcard match but no specific match, use it
620+
if wildcardPattern != "" && wildcardHandler != nil {
621+
return executeHandler(wildcardHandler, req), nil
622+
}
623+
599624
// No handler found
600625
return &http.Response{
601626
StatusCode: http.StatusNotFound,

0 commit comments

Comments
 (0)