Skip to content

Commit 7b451fc

Browse files
author
OpenShift Bot
authored
Merge pull request #13923 from liggitt/router-subpath-regex-1.5
Merged by openshift-bot
2 parents 214e4c3 + 7b4335d commit 7b451fc

File tree

2 files changed

+158
-1
lines changed

2 files changed

+158
-1
lines changed

pkg/router/template/router.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,24 @@ func generateRouteRegexp(hostname, path string, wildcard bool) string {
250250
}
251251
}
252252

253-
return fmt.Sprintf("^%s(|:[0-9]+)%s(|/.*)$", hostRE, regexp.QuoteMeta(path))
253+
portRE := "(:[0-9]+)?"
254+
255+
// build the correct subpath regex, depending on whether path ends with a segment separator
256+
var pathRE, subpathRE string
257+
switch {
258+
case strings.TrimRight(path, "/") == "":
259+
// Special-case paths consisting solely of "/" to match a root request to "" as well
260+
pathRE = ""
261+
subpathRE = "(/.*)?"
262+
case strings.HasSuffix(path, "/"):
263+
pathRE = regexp.QuoteMeta(path)
264+
subpathRE = "(.*)?"
265+
default:
266+
pathRE = regexp.QuoteMeta(path)
267+
subpathRE = "(/.*)?"
268+
}
269+
270+
return "^" + hostRE + portRE + pathRE + subpathRE + "$"
254271
}
255272

256273
// Generates the host name to use for serving/certificate matching.

pkg/router/template/router_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/md5"
55
"fmt"
66
"reflect"
7+
"regexp"
78
"testing"
89

910
kapi "k8s.io/kubernetes/pkg/api"
@@ -670,3 +671,142 @@ func TestAddRouteEdgeTerminationInsecurePolicy(t *testing.T) {
670671
}
671672
}
672673
}
674+
675+
func TestGenerateRouteRegexp(t *testing.T) {
676+
tests := []struct {
677+
name string
678+
hostname string
679+
path string
680+
wildcard bool
681+
682+
match []string
683+
nomatch []string
684+
}{
685+
{
686+
name: "no path",
687+
hostname: "example.com",
688+
path: "",
689+
wildcard: false,
690+
match: []string{
691+
"example.com",
692+
"example.com:80",
693+
"example.com/",
694+
"example.com/sub",
695+
"example.com/sub/",
696+
},
697+
nomatch: []string{"other.com"},
698+
},
699+
{
700+
name: "root path with trailing slash",
701+
hostname: "example.com",
702+
path: "/",
703+
wildcard: false,
704+
match: []string{
705+
"example.com",
706+
"example.com:80",
707+
"example.com/",
708+
"example.com/sub",
709+
"example.com/sub/",
710+
},
711+
nomatch: []string{"other.com"},
712+
},
713+
{
714+
name: "subpath with trailing slash",
715+
hostname: "example.com",
716+
path: "/sub/",
717+
wildcard: false,
718+
match: []string{
719+
"example.com/sub/",
720+
"example.com/sub/subsub",
721+
},
722+
nomatch: []string{
723+
"other.com",
724+
"example.com",
725+
"example.com:80",
726+
"example.com/",
727+
"example.com/sub", // path with trailing slash doesn't match URL without
728+
"example.com/subpar", // path segment boundary match required
729+
},
730+
},
731+
{
732+
name: "subpath without trailing slash",
733+
hostname: "example.com",
734+
path: "/sub",
735+
wildcard: false,
736+
match: []string{
737+
"example.com/sub",
738+
"example.com/sub/",
739+
"example.com/sub/subsub",
740+
},
741+
nomatch: []string{
742+
"other.com",
743+
"example.com",
744+
"example.com:80",
745+
"example.com/",
746+
"example.com/subpar", // path segment boundary match required
747+
},
748+
},
749+
{
750+
name: "wildcard",
751+
hostname: "www.example.com",
752+
path: "/",
753+
wildcard: true,
754+
match: []string{
755+
"www.example.com",
756+
"www.example.com/",
757+
"www.example.com/sub",
758+
"www.example.com/sub/",
759+
"www.example.com:80",
760+
"www.example.com:80/",
761+
"www.example.com:80/sub",
762+
"www.example.com:80/sub/",
763+
"foo.example.com",
764+
"foo.example.com/",
765+
"foo.example.com/sub",
766+
"foo.example.com/sub/",
767+
},
768+
nomatch: []string{
769+
"wwwexample.com",
770+
"foo.bar.example.com",
771+
},
772+
},
773+
{
774+
name: "non-wildcard",
775+
hostname: "www.example.com",
776+
path: "/",
777+
wildcard: false,
778+
match: []string{
779+
"www.example.com",
780+
"www.example.com/",
781+
"www.example.com/sub",
782+
"www.example.com/sub/",
783+
"www.example.com:80",
784+
"www.example.com:80/",
785+
"www.example.com:80/sub",
786+
"www.example.com:80/sub/",
787+
},
788+
nomatch: []string{
789+
"foo.example.com",
790+
"foo.example.com/",
791+
"foo.example.com/sub",
792+
"foo.example.com/sub/",
793+
"wwwexample.com",
794+
"foo.bar.example.com",
795+
},
796+
},
797+
}
798+
799+
for _, tt := range tests {
800+
r := regexp.MustCompile(generateRouteRegexp(tt.hostname, tt.path, tt.wildcard))
801+
for _, s := range tt.match {
802+
if !r.Match([]byte(s)) {
803+
t.Errorf("%s: expected %s to match %s, but didn't", tt.name, r, s)
804+
}
805+
}
806+
for _, s := range tt.nomatch {
807+
if r.Match([]byte(s)) {
808+
t.Errorf("%s: expected %s not to match %s, but did", tt.name, r, s)
809+
}
810+
}
811+
}
812+
}

0 commit comments

Comments
 (0)