From 68cd1564796e6eb5c30a0d203417ab27ac46ca6e Mon Sep 17 00:00:00 2001 From: Erik Pellizzon Date: Sat, 21 Dec 2024 17:35:07 +0100 Subject: [PATCH] Fix prefix match --- dispatcher.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/dispatcher.go b/dispatcher.go index 9c32530e..49c267d7 100644 --- a/dispatcher.go +++ b/dispatcher.go @@ -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) } }