5
5
package mux
6
6
7
7
import (
8
- "bytes"
9
8
"fmt"
10
9
"net/http"
11
10
"net/url"
@@ -15,8 +14,9 @@ import (
15
14
)
16
15
17
16
type routeRegexpOptions struct {
18
- strictSlash bool
19
- useEncodedPath bool
17
+ strictSlash bool
18
+ useEncodedPath bool
19
+ strictQueryParamSep bool
20
20
}
21
21
22
22
type regexpType int
@@ -245,7 +245,8 @@ func (r *routeRegexp) getURLQuery(req *http.Request) string {
245
245
return ""
246
246
}
247
247
templateKey := strings .SplitN (r .template , "=" , 2 )[0 ]
248
- val , ok := findFirstQueryKey (req .URL .RawQuery , templateKey )
248
+ strict := r .options .strictQueryParamSep
249
+ val , ok := findFirstQueryKey (req .URL .RawQuery , templateKey , strict )
249
250
if ok {
250
251
return templateKey + "=" + val
251
252
}
@@ -254,34 +255,32 @@ func (r *routeRegexp) getURLQuery(req *http.Request) string {
254
255
255
256
// findFirstQueryKey returns the same result as (*url.URL).Query()[key][0].
256
257
// If key was not found, empty string and false is returned.
257
- func findFirstQueryKey (rawQuery , key string ) (value string , ok bool ) {
258
- query := []byte (rawQuery )
259
- for len (query ) > 0 {
260
- foundKey := query
261
- if i := bytes .IndexAny (foundKey , "&;" ); i >= 0 {
262
- foundKey , query = foundKey [:i ], foundKey [i + 1 :]
258
+ func findFirstQueryKey (rawQuery , key string , strict bool ) (value string , ok bool ) {
259
+ for len (rawQuery ) > 0 {
260
+ foundKey := rawQuery
261
+ if strict {
262
+ foundKey , rawQuery , _ = strings .Cut (foundKey , "&" )
263
+ } else if i := strings .IndexAny (foundKey , "&;" ); i >= 0 {
264
+ foundKey , rawQuery = foundKey [:i ], foundKey [i + 1 :]
263
265
} else {
264
- query = query [:0 ]
266
+ rawQuery = rawQuery [:0 ]
265
267
}
266
268
if len (foundKey ) == 0 {
267
269
continue
268
270
}
269
- var value []byte
270
- if i := bytes .IndexByte (foundKey , '=' ); i >= 0 {
271
- foundKey , value = foundKey [:i ], foundKey [i + 1 :]
272
- }
271
+ foundKey , value , _ := strings .Cut (foundKey , "=" )
273
272
if len (foundKey ) < len (key ) {
274
273
// Cannot possibly be key.
275
274
continue
276
275
}
277
- keyString , err := url .QueryUnescape (string ( foundKey ) )
276
+ keyString , err := url .QueryUnescape (foundKey )
278
277
if err != nil {
279
278
continue
280
279
}
281
280
if keyString != key {
282
281
continue
283
282
}
284
- valueString , err := url .QueryUnescape (string ( value ) )
283
+ valueString , err := url .QueryUnescape (value )
285
284
if err != nil {
286
285
continue
287
286
}
0 commit comments