Skip to content

Commit

Permalink
Merge pull request #411 from chilic/main
Browse files Browse the repository at this point in the history
fix: Add support for Percent-Encoding RFC 3986
  • Loading branch information
danielgtaylor authored Apr 28, 2024
2 parents ed2c3cd + 32f0c2d commit babafa8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 11 additions & 5 deletions adapters/humaflow/flow/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 @@ -165,7 +166,7 @@ func (m *Mux) Group(fn func(*Mux)) {

// ServeHTTP makes the router implement the http.Handler interface.
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
urlSegments := strings.Split(r.URL.Path, "/")
urlSegments := strings.Split(r.URL.EscapedPath(), "/")
allowedMethods := []string{}

for _, route := range *m.routes {
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
5 changes: 5 additions & 0 deletions adapters/humaflow/flow/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func TestMatching(t *testing.T) {
"GET", "/path-params/60/beatles/lennon/bar",
http.StatusNotFound, map[string]string{"era": "60", "group": "beatles", "member": "lennon"}, "",
},
{
[]string{"GET"}, "/path-params/:era",
"GET", "/path-params/a%3A%2F%2Fb%2Fc",
http.StatusOK, map[string]string{"era": "a://b/c"}, "",
},
// regexp
{
[]string{"GET"}, "/path-params/:era|^[0-9]{2}$/:group|^[a-z].+$",
Expand Down

0 comments on commit babafa8

Please sign in to comment.