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

feat: decode url params #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
14 changes: 10 additions & 4 deletions flow.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ package flow
import (
"context"
"net/http"
"net/url"
"regexp"
"slices"
"strings"
Expand Down Expand Up @@ -227,15 +228,20 @@ func (r *route) match(ctx context.Context, urlSegments []string) (context.Contex
if strings.HasPrefix(routeSegment, ":") {
key, rxPattern, containsRx := strings.Cut(strings.TrimPrefix(routeSegment, ":"), "|")

value, err := url.QueryUnescape(urlSegments[i])
if err != nil {
return ctx, false
}

if containsRx {
if compiledRXPatterns[rxPattern].MatchString(urlSegments[i]) {
ctx = context.WithValue(ctx, contextKey(key), urlSegments[i])
if compiledRXPatterns[rxPattern].MatchString(value) {
ctx = context.WithValue(ctx, contextKey(key), value)
continue
}
}

if !containsRx && urlSegments[i] != "" {
ctx = context.WithValue(ctx, contextKey(key), urlSegments[i])
if !containsRx && value != "" {
ctx = context.WithValue(ctx, contextKey(key), value)
continue
}

Expand Down
2 changes: 1 addition & 1 deletion flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestMatching(t *testing.T) {
{
[]string{"GET"}, "/path-params/:era",
"GET", "/path-params/a%3A%2F%2Fb%2Fc",
http.StatusOK, map[string]string{"era": "a%3A%2F%2Fb%2Fc"}, "",
http.StatusOK, map[string]string{"era": "a://b/c"}, "",
},
// regexp
{
Expand Down