Skip to content

Commit

Permalink
AntRegexRequestMatcher Optimization
Browse files Browse the repository at this point in the history
Closes gh-11234
  • Loading branch information
rwinch committed May 16, 2022
1 parent 9059fb3 commit 7f121e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@
* @since 3.1
*/
public final class RegexRequestMatcher implements RequestMatcher {

private final static Log logger = LogFactory.getLog(RegexRequestMatcher.class);

private static final int DEFAULT = Pattern.DOTALL;

private static final int CASE_INSENSITIVE = DEFAULT | Pattern.CASE_INSENSITIVE;

private final Pattern pattern;
private final HttpMethod httpMethod;

Expand All @@ -64,14 +69,8 @@ public RegexRequestMatcher(String pattern, String httpMethod) {
* {@link Pattern#CASE_INSENSITIVE} flag set.
*/
public RegexRequestMatcher(String pattern, String httpMethod, boolean caseInsensitive) {
if (caseInsensitive) {
this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
}
else {
this.pattern = Pattern.compile(pattern);
}
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod
.valueOf(httpMethod) : null;
this.pattern = Pattern.compile(pattern, caseInsensitive ? CASE_INSENSITIVE : DEFAULT);
this.httpMethod = StringUtils.hasText(httpMethod) ? HttpMethod.valueOf(httpMethod) : null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ public void matchesWithInvalidMethod() {
assertThat(matcher.matches(request)).isFalse();
}

@Test
public void matchesWithCarriageReturn() {
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0a");
request.setServletPath("/blah\n");
assertThat(matcher.matches(request)).isTrue();
}

@Test
public void matchesWithLineFeed() {
RegexRequestMatcher matcher = new RegexRequestMatcher(".*", null);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/blah%0d");
request.setServletPath("/blah\r");
assertThat(matcher.matches(request)).isTrue();
}

@Test
public void toStringThenFormatted() {
RegexRequestMatcher matcher = new RegexRequestMatcher("/blah", "GET");
Expand Down

0 comments on commit 7f121e8

Please sign in to comment.