Skip to content
Open
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,8 @@ func main() {

This method retrieves a route by its name.

The returned `Route` can be inspected or used to generate a URL directly with `route.URL(params)`.

```go title="Signature"
func (app *App) GetRoute(name string) Route
```
Expand All @@ -554,12 +556,17 @@ func main() {
app := fiber.New()

app.Get("/", handler).Name("index")
app.Get("/user/:name/:id", handler).Name("user")

route := app.GetRoute("index")

data, _ := json.MarshalIndent(route, "", " ")
fmt.Println(string(data))

userRoute := app.GetRoute("user")
location, _ := userRoute.URL(fiber.Map{"name": "john", "id": 1})
fmt.Println(location) // /user/john/1

log.Fatal(app.Listen(":3000"))
}
```
Expand Down
2 changes: 2 additions & 0 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ app.RouteChain("/api").RouteChain("/user/:id?")

You can find more information about `app.RouteChain` and `app.Route` in the API documentation ([RouteChain](./api/app#routechain), [Route](./api/app#route)).

Named routes retrieved with `app.GetRoute(name)` also support `route.URL(params)` for generating relative URLs directly from the route definition, including parameter substitution for named, wildcard (`*`), and plus (`+`) segments.

### Domain routing

`Domain` creates a router scoped to a specific hostname pattern. Routes registered through the returned `Router` only match requests whose hostname (from `c.Hostname()`) matches the pattern. When `TrustProxy` is enabled and the proxy is trusted (as defined by [`TrustProxyConfig`](./api/app#trustproxyconfig)), the hostname may be derived from the `X-Forwarded-Host` header. Be sure to configure `TrustProxyConfig` to restrict which proxies are trusted and prevent header spoofing when enabling `TrustProxy`.
Expand Down
2 changes: 0 additions & 2 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ const (
var (
// slash has a special role, unlike the other parameters it must not be interpreted as a parameter
routeDelimiter = []byte{slashDelimiter, '-', '.'}
// list of greedy parameters
greedyParameters = []byte{wildcardParam, plusParam}
// list of chars for the parameter recognizing
parameterStartChars = [256]bool{
wildcardParam: true,
Expand Down
33 changes: 4 additions & 29 deletions res.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fiber

import (
"bufio"
"bytes"
"fmt"
"html/template"
"io"
Expand Down Expand Up @@ -646,38 +645,14 @@ func (r *DefaultRes) ViewBind(vars Map) error {
return r.c.ViewBind(vars)
}

// getLocationFromRoute get URL location from route using parameters
// getLocationFromRoute get URL location from route using parameters.
// Nil receivers and missing routes return ErrNotFound to match Route.URL semantics.
func (r *DefaultRes) getLocationFromRoute(route *Route, params Map) (string, error) {
if route == nil || route.Path == "" {
if r == nil || route == nil || route.Path == "" {
return "", ErrNotFound
}

app := r.c.app
buf := bytebufferpool.Get()
for _, segment := range route.routeParser.segs {
if !segment.IsParam {
_, err := buf.WriteString(segment.Const)
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
continue
}

for key, val := range params {
isSame := key == segment.ParamName || (!app.config.CaseSensitive && utils.EqualFold(key, segment.ParamName))
isGreedy := segment.IsGreedy && len(key) == 1 && bytes.IndexByte(greedyParameters, key[0]) >= 0
if isSame || isGreedy {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
}
}
}
location := buf.String()
// release buffer
bytebufferpool.Put(buf)
return location, nil
return buildRouteURL(route, params)
}

// GetRouteURL generates URLs to named routes, with parameters. URLs are relative, for example: "/user/1831"
Expand Down
141 changes: 127 additions & 14 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/gofiber/utils/v2"
utilsstrings "github.com/gofiber/utils/v2/strings"
"github.com/valyala/bytebufferpool"
"github.com/valyala/fasthttp"
)

Expand Down Expand Up @@ -58,11 +59,120 @@ type Route struct {
routeParser routeParser // Parameter parser

// Data for routing
use bool // USE matches path prefixes
mount bool // Indicated a mounted app on a specific route
star bool // Path equals '*'
root bool // Path equals '/'
autoHead bool // Automatically generated HEAD route
use bool // USE matches path prefixes
mount bool // Indicated a mounted app on a specific route
star bool // Path equals '*'
root bool // Path equals '/'
autoHead bool // Automatically generated HEAD route
caseSensitive bool // Whether parameter matching is case-sensitive
}

var (
defaultGreedyParameterKeys = []string{"*", "+"}
preferredWildcardGreedyParameters = []string{"*", "+"}
preferredPlusGreedyParameters = []string{"+", "*"}
)

// URL generates a URL from the route path and parameters.
// This method fills in the route parameters with the provided values.
// Parameter matching respects the app's CaseSensitive configuration:
// case-insensitive by default, case-sensitive when CaseSensitive is true.
//
// Example:
//
// app.Get("/user/:name/:id", handler).Name("user")
// url, err := app.GetRoute("user").URL(Map{"name": "john", "id": "123"})
// // Returns: "/user/john/123"
//
//nolint:gocritic // hugeParam: app.GetRoute returns a value, so URL must be callable on that value directly.
func (r Route) URL(params Map) (string, error) {
if r.Path == "" {
return "", ErrNotFound
}

return buildRouteURL(&r, params)
}

// buildRouteURL generates a URL from route segments and parameters.
// This shared helper is used by both Route.URL() and DefaultRes.getLocationFromRoute()
// to ensure consistent URL generation behavior across APIs.
//
// Parameter resolution uses a deterministic three-step lookup:
// 1. Exact key match on segment.ParamName
// 2. Case-insensitive fallback picking the lexicographically-smallest matching key (when !caseSensitive)
// 3. Greedy parameter fallback for wildcard (*) and plus (+) parameters
func buildRouteURL(route *Route, params Map) (string, error) {
if len(route.routeParser.segs) == 0 {
return route.Path, nil
}

buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

for _, segment := range route.routeParser.segs {
if !segment.IsParam {
_, err := buf.WriteString(segment.Const)
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
continue
}

var (
val any
found bool
)

// Prefer an exact parameter name match
if val, found = params[segment.ParamName]; !found && !route.caseSensitive {
// Fall back to a case-insensitive match using a deterministic winner
var matchedKey string
for key := range params {
if utils.EqualFold(key, segment.ParamName) && (!found || key < matchedKey) {
matchedKey = key
found = true
}
}
if found {
val = params[matchedKey]
}
}

// For greedy parameters, fall back to generic greedy keys
if !found && segment.IsGreedy {
for _, greedyKey := range preferredGreedyParameters(segment.ParamName) {
if val, found = params[greedyKey]; found {
break
}
}
}

if found {
_, err := buf.WriteString(utils.ToString(val))
if err != nil {
return "", fmt.Errorf("failed to write string: %w", err)
}
}
}

return buf.String(), nil
}

// preferredGreedyParameters returns the generic greedy fallback lookup order
// for a route parameter name.
// Parameter names starting with '+' prefer '+' before '*', names starting with
// '*' prefer '*' before '+', and all other names fall back to the default order.
func preferredGreedyParameters(paramName string) []string {
if paramName != "" {
switch paramName[0] {
case plusParam:
return preferredPlusGreedyParameters
case wildcardParam:
return preferredWildcardGreedyParameters
}
}

return defaultGreedyParameterKeys
}

func (r *Route) match(detectionPath, path string, params *[maxParams]string) bool {
Expand Down Expand Up @@ -369,18 +479,20 @@ func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {
route.routeParser = parseRoute(prettyPath, app.customConstraints...)
route.root = false
route.star = false
route.caseSensitive = app.config.CaseSensitive

return route
}

func (*App) copyRoute(route *Route) *Route {
return &Route{
// Router booleans
use: route.use,
mount: route.mount,
star: route.star,
root: route.root,
autoHead: route.autoHead,
use: route.use,
mount: route.mount,
star: route.star,
root: route.root,
autoHead: route.autoHead,
caseSensitive: route.caseSensitive,

// Path data
path: route.path,
Expand Down Expand Up @@ -554,10 +666,11 @@ func (app *App) register(methods []string, pathRaw string, group *Group, handler
isRoot := pathClean == "/"

route := Route{
use: isUse,
mount: isMount,
star: isStar,
root: isRoot,
use: isUse,
mount: isMount,
star: isStar,
root: isRoot,
caseSensitive: app.config.CaseSensitive,

path: pathClean,
routeParser: parsedPretty,
Expand Down
Loading
Loading