Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2015-07-23 16:27:43 -0400
committerJoakim Erdfelt2015-07-23 16:27:43 -0400
commit0476d4d28d6350616bb41525d0b6cbe1067d57aa (patch)
tree88eaa3a56b9a700c0a6e7636445e970d58ef4cf4 /jetty-rewrite
parente24cc3ee5e4ea71ce7078f57e51b5cc0f46580f5 (diff)
downloadorg.eclipse.jetty.project-0476d4d28d6350616bb41525d0b6cbe1067d57aa.tar.gz
org.eclipse.jetty.project-0476d4d28d6350616bb41525d0b6cbe1067d57aa.tar.xz
org.eclipse.jetty.project-0476d4d28d6350616bb41525d0b6cbe1067d57aa.zip
Supplementing redirect rules with RedirectUtil
+ Redirect rules should produce full URI for "Location" header + Redirect rules should produce no response body content
Diffstat (limited to 'jetty-rewrite')
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java7
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java9
-rw-r--r--jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectUtil.java71
-rw-r--r--jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java12
4 files changed, 94 insertions, 5 deletions
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java
index 5735012814..81da7ed816 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectPatternRule.java
@@ -73,8 +73,11 @@ public class RedirectPatternRule extends PatternRule
@Override
public String apply(String target, HttpServletRequest request, HttpServletResponse response) throws IOException
{
- response.setHeader("Location",response.encodeRedirectURL(_location));
- response.sendError(_statusCode);
+ String location = response.encodeRedirectURL(_location);
+ response.setHeader("Location",RedirectUtil.toRedirectURL(request,location));
+ response.setStatus(_statusCode);
+ response.getOutputStream().flush(); // no output / content
+ response.getOutputStream().close();
return target;
}
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java
index aca659eab1..436a1aa819 100644
--- a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRule.java
@@ -83,9 +83,12 @@ public class RedirectRegexRule extends RegexRule
String group = matcher.group(g);
target=target.replaceAll("\\$"+g,group);
}
-
- response.setHeader("Location",response.encodeRedirectURL(target));
- response.sendError(_statusCode);
+
+ target = response.encodeRedirectURL(target);
+ response.setHeader("Location",RedirectUtil.toRedirectURL(request,target));
+ response.setStatus(_statusCode);
+ response.getOutputStream().flush(); // no output / content
+ response.getOutputStream().close();
return target;
}
diff --git a/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectUtil.java b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectUtil.java
new file mode 100644
index 0000000000..caabb7e055
--- /dev/null
+++ b/jetty-rewrite/src/main/java/org/eclipse/jetty/rewrite/handler/RedirectUtil.java
@@ -0,0 +1,71 @@
+//
+// ========================================================================
+// Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// All rights reserved. This program and the accompanying materials
+// are made available under the terms of the Eclipse Public License v1.0
+// and Apache License v2.0 which accompanies this distribution.
+//
+// The Eclipse Public License is available at
+// http://www.eclipse.org/legal/epl-v10.html
+//
+// The Apache License v2.0 is available at
+// http://www.opensource.org/licenses/apache2.0.php
+//
+// You may elect to redistribute this code under either of these licenses.
+// ========================================================================
+//
+
+package org.eclipse.jetty.rewrite.handler;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.eclipse.jetty.util.URIUtil;
+
+/**
+ * Utility for managing redirect based rules
+ */
+public final class RedirectUtil
+{
+ /**
+ * Common point to generate a proper "Location" header for redirects.
+ *
+ * @param request
+ * the request the redirect should be based on (needed when relative locations are provided, so that
+ * server name, scheme, port can be built out properly)
+ * @param location
+ * the location URL to redirect to (can be a relative path)
+ * @return the full redirect "Location" URL (including scheme, host, port, path, etc...)
+ */
+ public static String toRedirectURL(final HttpServletRequest request, String location)
+ {
+ if (!URIUtil.hasScheme(location))
+ {
+ StringBuilder url = new StringBuilder(128);
+ URIUtil.appendSchemeHostPort(url,request.getScheme(),request.getServerName(),request.getServerPort());
+
+ if (location.startsWith("/"))
+ {
+ // absolute in context
+ location = URIUtil.canonicalPath(location);
+ }
+ else
+ {
+ // relative to request
+ String path = request.getRequestURI();
+ String parent = (path.endsWith("/")) ? path : URIUtil.parentPath(path);
+ location = URIUtil.canonicalPath(URIUtil.addPaths(parent,location));
+ if (!location.startsWith("/"))
+ url.append('/');
+ }
+
+ if (location == null)
+ throw new IllegalStateException("path cannot be above root");
+ url.append(location);
+
+ location = url.toString();
+ }
+
+ return location;
+ }
+}
diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java
index 68b3ea9f65..dea0ce25f3 100644
--- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java
+++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/RedirectRegexRuleTest.java
@@ -53,6 +53,18 @@ public class RedirectRegexRuleTest extends AbstractRuleTestCase
rule.matchAndApply("/my/dir/file/", _request, _response);
assertRedirectResponse(HttpStatus.FOUND_302,"http://www.mortbay.org/");
}
+
+ @Test
+ public void testLocationWithPathReplacement() throws IOException
+ {
+ RedirectRegexRule rule = new RedirectRegexRule();
+ rule.setRegex("/documentation/(.*)$");
+ rule.setReplacement("/docs/$1");
+
+ // Resource is dir
+ rule.matchAndApply("/documentation/top.html", _request, _response);
+ assertRedirectResponse(HttpStatus.FOUND_302,"http://0.0.0.0/docs/top.html");
+ }
@Test
public void testLocationWithReplacmentGroupSimple() throws IOException

Back to the top