mirror of https://github.com/gofiber/fiber.git
🐛 bug: fix regex constraints that contain comma (#2256)
parent
6b9601fb99
commit
077a5dc3d4
5
path.go
5
path.go
|
@ -271,12 +271,12 @@ func (routeParser *routeParser) analyseParameterPart(pattern string) (string, *r
|
||||||
// Assign constraint
|
// Assign constraint
|
||||||
if start != -1 && end != -1 {
|
if start != -1 && end != -1 {
|
||||||
constraint := &Constraint{
|
constraint := &Constraint{
|
||||||
ID: getParamConstraintType(c[:start]),
|
ID: getParamConstraintType(c[:start]),
|
||||||
Data: splitNonEscaped(c[start+1:end], string(parameterConstraintDataSeparatorChars)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove escapes from data
|
// remove escapes from data
|
||||||
if constraint.ID != regexConstraint {
|
if constraint.ID != regexConstraint {
|
||||||
|
constraint.Data = splitNonEscaped(c[start+1:end], string(parameterConstraintDataSeparatorChars))
|
||||||
if len(constraint.Data) == 1 {
|
if len(constraint.Data) == 1 {
|
||||||
constraint.Data[0] = RemoveEscapeChar(constraint.Data[0])
|
constraint.Data[0] = RemoveEscapeChar(constraint.Data[0])
|
||||||
} else if len(constraint.Data) == 2 {
|
} else if len(constraint.Data) == 2 {
|
||||||
|
@ -287,6 +287,7 @@ func (routeParser *routeParser) analyseParameterPart(pattern string) (string, *r
|
||||||
|
|
||||||
// Precompile regex if has regex constraint
|
// Precompile regex if has regex constraint
|
||||||
if constraint.ID == regexConstraint {
|
if constraint.ID == regexConstraint {
|
||||||
|
constraint.Data = []string{c[start+1 : end]}
|
||||||
constraint.RegexCompiler = regexp.MustCompile(constraint.Data[0])
|
constraint.RegexCompiler = regexp.MustCompile(constraint.Data[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ package fiber
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2/utils"
|
"github.com/gofiber/fiber/v2/utils"
|
||||||
|
@ -526,6 +527,12 @@ func Test_Path_matchParams(t *testing.T) {
|
||||||
{url: "/api/v1/peach", params: []string{"peach"}, match: true},
|
{url: "/api/v1/peach", params: []string{"peach"}, match: true},
|
||||||
{url: "/api/v1/p34ch", params: nil, match: false},
|
{url: "/api/v1/p34ch", params: nil, match: false},
|
||||||
})
|
})
|
||||||
|
testCase("/api/v1/:param<regex(^[a-z0-9]([a-z0-9-]{1,61}[a-z0-9])?$)>", []testparams{
|
||||||
|
{url: "/api/v1/12", params: nil, match: false},
|
||||||
|
{url: "/api/v1/xy", params: nil, match: false},
|
||||||
|
{url: "/api/v1/test", params: []string{"test"}, match: true},
|
||||||
|
{url: "/api/v1/" + strings.Repeat("a", 64), params: nil, match: false},
|
||||||
|
})
|
||||||
testCase("/api/v1/:param<regex(\\d{4}-\\d{2}-\\d{2})}>", []testparams{
|
testCase("/api/v1/:param<regex(\\d{4}-\\d{2}-\\d{2})}>", []testparams{
|
||||||
{url: "/api/v1/ent", params: nil, match: false},
|
{url: "/api/v1/ent", params: nil, match: false},
|
||||||
{url: "/api/v1/15", params: nil, match: false},
|
{url: "/api/v1/15", params: nil, match: false},
|
||||||
|
|
Loading…
Reference in New Issue