Skip to content

Commit

Permalink
Merge pull request #250 from bdw429s/handle-encoded-URIs
Browse files Browse the repository at this point in the history
isFile handles encoded URIs
  • Loading branch information
paultuckey committed Apr 5, 2023
2 parents cf885c1 + 9b35a66 commit a7f371b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -48,6 +48,8 @@
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Calendar;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Conditions must be met when the filter is processing a url.
Expand Down Expand Up @@ -267,7 +269,11 @@ public ConditionMatch getConditionMatch(final HttpServletRequest hsRequest) {
if (requestURI.startsWith(hsRequest.getContextPath() + "/")){
requestURI = requestURI.substring(hsRequest.getContextPath().length());
}
String fileName = rule.getServletContext().getRealPath(requestURI);
// Decode any encoded URI chars
try {
requestURI = new URI( requestURI ).getPath();
} catch( URISyntaxException URIe ) {}
String fileName = rule.getServletContext().getRealPath( requestURI );
if ( log.isDebugEnabled() ) log.debug("fileName found is " + fileName);
return evaluateStringCondition(fileName);
} else {
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/org/tuckey/web/filters/urlrewrite/RuleTest.java
Expand Up @@ -820,6 +820,19 @@ public void testFilename() throws InvocationTargetException, IOException, Servle
assertEquals("/found-the-file", rewrittenUrl.getTarget());
}

public void testEncodedFilename() throws InvocationTargetException, IOException, ServletException {
NormalRule rule = new NormalRule();
rule.setTo("/found-the-file");
Condition condition = new Condition();
condition.setType("request-filename");
condition.setOperator("isfile");
rule.addCondition(condition);
rule.initialise(new MockServletContext());
MockRequest request = new MockRequest("/conf%2Dtest1.xml");
NormalRewrittenUrl rewrittenUrl = (NormalRewrittenUrl) rule.matches(request.getRequestURI(), request, response);
assertEquals("/found-the-file", rewrittenUrl.getTarget());
}

public void testNotFilename() throws InvocationTargetException, IOException, ServletException {
NormalRule rule = new NormalRule();
rule.setTo("/not-found-the-file");
Expand Down

0 comments on commit a7f371b

Please sign in to comment.