Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prefix match #590

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ func (c RespConditionFunc) HandleResp(resp *http.Response, ctx *ProxyCtx) bool {
// requests to url 'http://host/x'
func UrlHasPrefix(prefix string) ReqConditionFunc {
return func(req *http.Request, ctx *ProxyCtx) bool {
return strings.HasPrefix(req.URL.Path, prefix) ||
strings.HasPrefix(req.URL.Host+req.URL.Path, prefix) ||
strings.HasPrefix(req.URL.Scheme+req.URL.Host+req.URL.Path, prefix)
// Make sure to include the / as the first path character
relativePath := req.URL.Path
if length := len(relativePath); length == 0 || (length > 0 && relativePath[0] != '/') {
relativePath = "/" + relativePath
}
return strings.HasPrefix(relativePath, prefix) ||
strings.HasPrefix(req.URL.Host+relativePath, prefix) ||
// Scheme value is something like "https", we must include the :// characters
strings.HasPrefix(req.URL.Scheme+"://"+req.URL.Host+relativePath, prefix)
}
}

Expand Down
Loading