MCPcopy
hub / github.com/gofiber/fiber / RoutePatternMatch

Function RoutePatternMatch

path.go:174–230  ·  view source on GitHub ↗

RoutePatternMatch reports whether path matches the provided Fiber route pattern. Patterns use the same syntax as routes registered on an App, including parameters (for example `:id`), wildcards (`*`, `+`), and optional segments. The optional Config argument can be used to control case sensitivity a

(path, pattern string, cfg ...Config)

Source from the content-addressed store, hash-verified

172// strict routing behavior. This helper allows checking potential matches
173// without registering a route.
174func RoutePatternMatch(path, pattern string, cfg ...Config) bool {
175 // See logic in (*Route).match and (*App).register
176 var ctxParams [maxParams]string
177
178 config := Config{}
179 if len(cfg) > 0 {
180 config = cfg[0]
181 }
182 config.RegexHandler = validateRegexHandler(config.RegexHandler)
183
184 if path == "" {
185 path = "/"
186 }
187
188 // Cannot have an empty pattern
189 if pattern == "" {
190 pattern = "/"
191 }
192 // Pattern always start with a '/'
193 if pattern[0] != '/' {
194 pattern = "/" + pattern
195 }
196
197 patternPretty := []byte(pattern)
198
199 // Case-sensitive routing, all to lowercase
200 if !config.CaseSensitive {
201 patternPretty = utilsbytes.UnsafeToLower(patternPretty)
202 path = utilsstrings.ToLower(path)
203 }
204 // Strict routing, remove trailing slashes
205 if !config.StrictRouting && len(patternPretty) > 1 {
206 patternPretty = utils.TrimRight(patternPretty, '/')
207 }
208
209 parser, _ := routerParserPool.Get().(*routeParser) //nolint:errcheck // only contains routeParser
210 parser.reset()
211 patternStr := string(patternPretty)
212 parser.parseRoute(patternStr, config.RegexHandler)
213 defer routerParserPool.Put(parser)
214
215 // '*' wildcard matches any path
216 if (patternStr == "/" && path == "/") || patternStr == "/*" {
217 return true
218 }
219
220 // Does this route have parameters
221 if len(parser.params) > 0 {
222 if match := parser.getMatch(path, path, &ctxParams, false); match {
223 return true
224 }
225 }
226 // Check for a simple match
227 patternPretty = RemoveEscapeCharBytes(patternPretty)
228
229 return string(patternPretty) == path
230}
231

Calls 7

validateRegexHandlerFunction · 0.85
RemoveEscapeCharBytesFunction · 0.85
resetMethod · 0.80
parseRouteMethod · 0.80
getMatchMethod · 0.80
GetMethod · 0.65
PutMethod · 0.65