Skip to content

Commit 59d2fa3

Browse files
authored
Merge pull request #225 from byte-bandit/master
feat: add support for special characters in path
2 parents 37e565a + b7dedce commit 59d2fa3

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

chirouter/example_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ func ExamplePathToURLValues() {
4545

4646
// Serving example URL.
4747
w := httptest.NewRecorder()
48-
req, _ := http.NewRequest(http.MethodPost, "/foo/abc/bar/123?query1=321",
48+
49+
req, _ := http.NewRequest(http.MethodPost, `/foo/a%2Fbc/bar/123?query1=321`,
4950
bytes.NewBufferString("formData1=true&formData2=def"))
5051

5152
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
5253
req.Header.Set("X-Header-1", "1.23")
5354

5455
router.ServeHTTP(w, req)
5556
// Output:
56-
// {Query1:321 Path1:abc Path2:123 Header1:1.23 FormData1:true FormData2:def}
57+
// {Query1:321 Path1:a/bc Path2:123 Header1:1.23 FormData1:true FormData2:def}
5758
}

chirouter/path_decoder.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package chirouter
22

33
import (
4+
"fmt"
45
"net/http"
56
"net/url"
67

78
"github.com/go-chi/chi/v5"
89
)
910

1011
// PathToURLValues is a decoder function for parameters in path.
11-
func PathToURLValues(r *http.Request) (url.Values, error) { //nolint:unparam // Matches request.DecoderFactory.SetDecoderFunc.
12+
func PathToURLValues(r *http.Request) (url.Values, error) {
1213
if routeCtx := chi.RouteContext(r.Context()); routeCtx != nil {
1314
params := make(url.Values, len(routeCtx.URLParams.Keys))
1415

1516
for i, key := range routeCtx.URLParams.Keys {
1617
value := routeCtx.URLParams.Values[i]
18+
19+
value, err := url.PathUnescape(value)
20+
if err != nil {
21+
return nil, fmt.Errorf("unescaping path: %w", err)
22+
}
23+
1724
params[key] = []string{value}
1825
}
1926

0 commit comments

Comments
 (0)