FELIX-4920 : sslfilter does not preserve fragment in redirect. Apply patch from Jörg Hoh
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1685714 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/http/sslfilter/src/main/java/org/apache/felix/http/sslfilter/internal/SslFilterResponse.java b/http/sslfilter/src/main/java/org/apache/felix/http/sslfilter/internal/SslFilterResponse.java
index 2768a4f..535f937 100644
--- a/http/sslfilter/src/main/java/org/apache/felix/http/sslfilter/internal/SslFilterResponse.java
+++ b/http/sslfilter/src/main/java/org/apache/felix/http/sslfilter/internal/SslFilterResponse.java
@@ -28,6 +28,8 @@
import java.io.IOException;
import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
import javax.servlet.http.HttpServletRequest;
@@ -96,7 +98,12 @@
{
if (HDR_LOCATION.equalsIgnoreCase(name))
{
- URL rewritten = rewriteUrlIfNeeded(value);
+ URL rewritten = null;
+ try {
+ rewritten = rewriteUrlIfNeeded(value);
+ } catch (URISyntaxException e) {
+ // ignore
+ }
// Trying to set a redirect location to the original client-side URL, which should be https...
if (rewritten != null)
{
@@ -109,7 +116,12 @@
@Override
public void sendRedirect(String location) throws IOException
{
- URL rewritten = rewriteUrlIfNeeded(location);
+ URL rewritten = null;
+ try {
+ rewritten = rewriteUrlIfNeeded(location);
+ } catch (URISyntaxException e) {
+ throw new IOException (e);
+ }
if (rewritten != null)
{
location = rewritten.toExternalForm();
@@ -130,7 +142,7 @@
return HTTP_PORT;
}
- private URL rewriteUrlIfNeeded(String value)
+ private URL rewriteUrlIfNeeded(String value) throws URISyntaxException
{
if (value == null)
{
@@ -139,38 +151,48 @@
try
{
- URL url;
+ URI uri;
if (value.startsWith(this.serverProto.concat("://")))
{
- url = new URL(value);
+
+ uri = new URI (value);
}
else
{
- url = new URL(this.requestURL, value);
+ URL url = new URL(this.requestURL, value);
+ uri = url.toURI();
}
- String actualProto = url.getProtocol();
+ String actualProto = uri.getScheme();
+
if (!this.serverProto.equalsIgnoreCase(actualProto))
{
+ // protocol is already correct
return null;
}
- if (!this.serverName.equals(url.getHost()))
+ if (!this.serverName.equals(uri.getHost()))
{
+ // going to a different host
return null;
}
- if (normalizePort(this.serverProto, this.serverPort) != normalizePort(actualProto, url.getPort()))
+ if (normalizePort(this.serverProto, this.serverPort) != normalizePort(actualProto, uri.getPort()))
{
+ // not to default port
return null;
}
- return new URL(this.clientProto, this.serverName, this.clientPort, url.getFile());
+
+ return new URI(this.clientProto,null, this.serverName, this.clientPort, uri.getPath(),uri.getQuery(),uri.getFragment()).toURL();
}
catch (MalformedURLException e)
{
return null;
}
}
+
+
+
}
diff --git a/http/sslfilter/src/test/java/org/apache/felix/http/sslfilter/internal/SslFilterResponseTest.java b/http/sslfilter/src/test/java/org/apache/felix/http/sslfilter/internal/SslFilterResponseTest.java
index cb0d4f4..af5740a 100644
--- a/http/sslfilter/src/test/java/org/apache/felix/http/sslfilter/internal/SslFilterResponseTest.java
+++ b/http/sslfilter/src/test/java/org/apache/felix/http/sslfilter/internal/SslFilterResponseTest.java
@@ -100,6 +100,26 @@
assertEquals(expected, resp.getHeader(LOCATION));
}
+
+
+ @Test
+ public void testSetHttpLocationHeaderToOriginalRequestURIWithFragment() throws Exception
+ {
+ String location, expected;
+
+ TestHttpServletResponse resp = createServletResponse();
+ HttpServletRequest req = createServletRequest(BACKEND_SERVER, PATH);
+
+ SslFilterResponse sresp = new SslFilterResponse(resp, req);
+
+ location = HTTP + "://" + BACKEND_SERVER + "/foo#abc";
+ expected = HTTPS + "://" + BACKEND_SERVER + "/foo#abc";
+
+ sresp.setHeader(LOCATION, location);
+
+ assertEquals(expected, resp.getHeader(LOCATION));
+ }
+
@Test
public void testSetHttpLocationHeaderToOriginalRequestWithExplicitPort() throws Exception