Skip to content

Commit 12d07e7

Browse files
authored
Cleanup: Use Envoy contains for header routing (#5585)
Fixes #5572 Signed-off-by: Sotiris Nanopoulos <[email protected]>
1 parent 0585d5b commit 12d07e7

File tree

2 files changed

+14
-26
lines changed

2 files changed

+14
-26
lines changed

internal/envoy/v3/route.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"bytes"
1818
"fmt"
1919
"net/http"
20-
"regexp"
2120
"sort"
2221
"strings"
2322
"text/template"
@@ -825,23 +824,11 @@ func queryParamMatcher(queryParams []dag.QueryParamMatchCondition) []*envoy_rout
825824
// containsMatch returns a HeaderMatchSpecifier which will match the
826825
// supplied substring
827826
func containsMatch(s string, ignoreCase bool) *envoy_route_v3.HeaderMatcher_StringMatch {
828-
// convert the substring s into a regular expression that matches s.
829-
// note that Envoy expects the expression to match the entire string, not just the substring
830-
// formed from s. see [projectcontour/contour/#1751 & envoyproxy/envoy#8283]
831-
var regex string
832-
if ignoreCase {
833-
regex = fmt.Sprintf("(?i).*%s.*", regexp.QuoteMeta(s))
834-
} else {
835-
regex = fmt.Sprintf(".*%s.*", regexp.QuoteMeta(s))
836-
}
837-
838-
// This could also be implemented with contains envoy Matcher but the problem
839-
// would be that it will be a breaking change for contour due to the way the
840-
// envoy handles empty strings.
841827
return &envoy_route_v3.HeaderMatcher_StringMatch{
842828
StringMatch: &matcher.StringMatcher{
843-
MatchPattern: &matcher.StringMatcher_SafeRegex{
844-
SafeRegex: SafeRegexMatch(regex),
829+
IgnoreCase: ignoreCase,
830+
MatchPattern: &matcher.StringMatcher_Contains{
831+
Contains: s,
845832
},
846833
},
847834
}

internal/envoy/v3/route_test.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,8 +1872,8 @@ func TestRouteMatch(t *testing.T) {
18721872
InvertMatch: false,
18731873
HeaderMatchSpecifier: &envoy_route_v3.HeaderMatcher_StringMatch{
18741874
StringMatch: &matcher.StringMatcher{
1875-
MatchPattern: &matcher.StringMatcher_SafeRegex{
1876-
SafeRegex: SafeRegexMatch(".*11-22-33-44.*"),
1875+
MatchPattern: &matcher.StringMatcher_Contains{
1876+
Contains: "11-22-33-44",
18771877
},
18781878
},
18791879
},
@@ -1895,8 +1895,8 @@ func TestRouteMatch(t *testing.T) {
18951895
InvertMatch: false,
18961896
HeaderMatchSpecifier: &envoy_route_v3.HeaderMatcher_StringMatch{
18971897
StringMatch: &matcher.StringMatcher{
1898-
MatchPattern: &matcher.StringMatcher_SafeRegex{
1899-
SafeRegex: SafeRegexMatch(".*11\\.22\\.33\\.44.*"),
1898+
MatchPattern: &matcher.StringMatcher_Contains{
1899+
Contains: "11.22.33.44",
19001900
},
19011901
},
19021902
},
@@ -1918,8 +1918,8 @@ func TestRouteMatch(t *testing.T) {
19181918
InvertMatch: false,
19191919
HeaderMatchSpecifier: &envoy_route_v3.HeaderMatcher_StringMatch{
19201920
StringMatch: &matcher.StringMatcher{
1921-
MatchPattern: &matcher.StringMatcher_SafeRegex{
1922-
SafeRegex: SafeRegexMatch(".*11\\.\\[22\\]\\.\\*33\\.44.*"),
1921+
MatchPattern: &matcher.StringMatcher_Contains{
1922+
Contains: "11.[22].*33.44",
19231923
},
19241924
},
19251925
},
@@ -2141,8 +2141,8 @@ func TestRouteMatch(t *testing.T) {
21412141
Name: "x-header-foo",
21422142
HeaderMatchSpecifier: &envoy_route_v3.HeaderMatcher_StringMatch{
21432143
StringMatch: &matcher.StringMatcher{
2144-
MatchPattern: &matcher.StringMatcher_SafeRegex{
2145-
SafeRegex: SafeRegexMatch(".*bar.*"),
2144+
MatchPattern: &matcher.StringMatcher_Contains{
2145+
Contains: "bar",
21462146
},
21472147
},
21482148
},
@@ -2163,8 +2163,9 @@ func TestRouteMatch(t *testing.T) {
21632163
Name: "x-header-foo",
21642164
HeaderMatchSpecifier: &envoy_route_v3.HeaderMatcher_StringMatch{
21652165
StringMatch: &matcher.StringMatcher{
2166-
MatchPattern: &matcher.StringMatcher_SafeRegex{
2167-
SafeRegex: SafeRegexMatch("(?i).*bar.*"),
2166+
IgnoreCase: true,
2167+
MatchPattern: &matcher.StringMatcher_Contains{
2168+
Contains: "bar",
21682169
},
21692170
},
21702171
},

0 commit comments

Comments
 (0)