Skip to content

Commit

Permalink
Merge branch 'master' into feature/test-containers
Browse files Browse the repository at this point in the history
  • Loading branch information
paultuckey committed Jul 1, 2023
2 parents bb6d430 + 6dad8dc commit d50ec75
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 2 deletions.
Expand Up @@ -48,6 +48,8 @@
import jakarta.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
Expand Up @@ -60,6 +60,8 @@ public class NormalRewrittenUrl implements RewrittenUrl {
private boolean redirect = false;
private boolean permanentRedirect = false;
private boolean temporaryRedirect = false;
private boolean permanentRedirect308 = false;
private boolean temporaryRedirect307 = false;
private boolean preInclude = false;
private boolean postInclude = false;
private boolean proxy = false;
Expand All @@ -72,7 +74,7 @@ public class NormalRewrittenUrl implements RewrittenUrl {
private ServletContext targetContext = null;

/**
* Holds information about the rewirtten url.
* Holds information about the rewritten url.
*
* @param ruleExecutionOutput the url to rewrite to
*/
Expand Down Expand Up @@ -135,6 +137,22 @@ public boolean isTemporaryRedirect() {
return temporaryRedirect;
}

public void set308PermanentRedirect(boolean permanentRedirect308) {
this.permanentRedirect308 = permanentRedirect308;
}

public boolean is308PermanentRedirect() {
return permanentRedirect308;
}

public void set307TemporaryRedirect(boolean temporaryRedirect307) {
this.temporaryRedirect307 = temporaryRedirect307;
}

public boolean is307TemporaryRedirect() {
return temporaryRedirect307;
}

public void setEncode(boolean b) {
encode = b;
}
Expand Down Expand Up @@ -272,6 +290,34 @@ public boolean doRewrite(final HttpServletRequest hsRequest,
}
requestRewritten = true;

} else if (is307TemporaryRedirect()) {
if (hsResponse.isCommitted()) {
log.error("response is committed cannot temporary redirect (307) to " + target +
" (check you haven't done anything to the response (ie, written to it) before here)");
} else {
if (isEncode()) {
target = hsResponse.encodeRedirectURL(target);
}
hsResponse.setStatus(HttpServletResponse.SC_TEMPORARY_REDIRECT);
hsResponse.setHeader("Location", target);
if (log.isDebugEnabled()) log.debug("temporarily redirected (with response 307) to " + target);
}
requestRewritten = true;

} else if (is308PermanentRedirect()) {
if (hsResponse.isCommitted()) {
log.error("response is committed cannot permanent redirect (308) to " + target +
" (check you haven't done anything to the response (ie, written to it) before here)");
} else {
if (isEncode()) {
target = hsResponse.encodeRedirectURL(target);
}
hsResponse.setStatus(308);
hsResponse.setHeader("Location", target);
if (log.isDebugEnabled()) log.debug("permanently redirected (with response 308) to " + target);
}
requestRewritten = true;

} else if (isProxy()) {
if (hsResponse.isCommitted()) {
log.error("response is committed. cannot proxy " + target + ". Check that you havn't written to the response before.");
Expand Down
Expand Up @@ -65,6 +65,8 @@ public class NormalRule extends RuleBase implements Rule {
public static final short TO_TYPE_PRE_INCLUDE = 4;
public static final short TO_TYPE_POST_INCLUDE = 5;
public static final short TO_TYPE_PROXY = 6;
public static final short TO_TYPE_307_TEMPORARY_REDIRECT = 7;
public static final short TO_TYPE_308_PERMANENT_REDIRECT = 8;

private boolean dropCookies = true;
private boolean encodeToUrl = false;
Expand Down Expand Up @@ -164,6 +166,10 @@ public void setToType(final String toTypeStr) {
toType = TO_TYPE_PERMANENT_REDIRECT;
} else if ("temporary-redirect".equals(toTypeStr)) {
toType = TO_TYPE_TEMPORARY_REDIRECT;
} else if ("308-permanent-redirect".equals(toTypeStr)) {
toType = TO_TYPE_308_PERMANENT_REDIRECT;
} else if ("307-temporary-redirect".equals(toTypeStr)) {
toType = TO_TYPE_307_TEMPORARY_REDIRECT;
} else if ("pre-include".equals(toTypeStr)) {
toType = TO_TYPE_PRE_INCLUDE;
} else if ("post-include".equals(toTypeStr)) {
Expand All @@ -187,6 +193,8 @@ public String getToType() {
if (toType == TO_TYPE_REDIRECT) return "redirect";
if (toType == TO_TYPE_PERMANENT_REDIRECT) return "permanent-redirect";
if (toType == TO_TYPE_TEMPORARY_REDIRECT) return "temporary-redirect";
if (toType == TO_TYPE_308_PERMANENT_REDIRECT) return "permanent-redirect-308";
if (toType == TO_TYPE_307_TEMPORARY_REDIRECT) return "temporary-redirect-307";
if (toType == TO_TYPE_PRE_INCLUDE) return "pre-include";
if (toType == TO_TYPE_POST_INCLUDE) return "post-include";
if (toType == TO_TYPE_PROXY) return "proxy";
Expand Down
Expand Up @@ -84,6 +84,18 @@ public static RewrittenUrl getRewritenUrl(short toType, boolean encodeToUrl, Rul
}
rewrittenRequest.setTemporaryRedirect(true);

} else if (toType == NormalRule.TO_TYPE_308_PERMANENT_REDIRECT) {
if (log.isDebugEnabled()) {
log.debug("needs to be permanently redirected (with response code 308) to " + toUrl);
}
rewrittenRequest.set308PermanentRedirect(true);

} else if (toType == NormalRule.TO_TYPE_307_TEMPORARY_REDIRECT) {
if (log.isDebugEnabled()) {
log.debug("needs to be temporarily redirected (with response code 307) to " + toUrl);
}
rewrittenRequest.set307TemporaryRedirect(true);

} else if (toType == NormalRule.TO_TYPE_PRE_INCLUDE) {
if (log.isDebugEnabled()) {
log.debug(toUrl + " needs to be pre included");
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 d50ec75

Please sign in to comment.