Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimone Bordet2015-07-28 14:39:49 +0000
committerSimone Bordet2015-07-28 14:39:49 +0000
commit837d1a74bb7d694220644a2539c4440ce55462cf (patch)
tree25348ddcca2e2176ecaae97b8c95233964f882a5
parentaa75bcf078d36ca7d03777b749ccbc2e42ef0175 (diff)
downloadorg.eclipse.jetty.project-837d1a74bb7d694220644a2539c4440ce55462cf.tar.gz
org.eclipse.jetty.project-837d1a74bb7d694220644a2539c4440ce55462cf.tar.xz
org.eclipse.jetty.project-837d1a74bb7d694220644a2539c4440ce55462cf.zip
473624 - ProxyServlet.Transparent / TransparentDelegate add trailing slash before query when using prefix.
-rw-r--r--jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java16
-rw-r--r--jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java69
2 files changed, 81 insertions, 4 deletions
diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
index 862d58236b..49f4d546c5 100644
--- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
+++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java
@@ -704,12 +704,22 @@ public abstract class AbstractProxyServlet extends HttpServlet
if (_proxyTo.endsWith("/"))
uri.setLength(uri.length() - 1);
String rest = path.substring(_prefix.length());
- if (!rest.startsWith("/"))
- uri.append("/");
- uri.append(rest);
+ if (!rest.isEmpty())
+ {
+ if (!rest.startsWith("/"))
+ uri.append("/");
+ uri.append(rest);
+ }
+
String query = request.getQueryString();
if (query != null)
+ {
+ // Is there at least one path segment ?
+ String separator = "://";
+ if (uri.indexOf("/", uri.indexOf(separator) + separator.length()) < 0)
+ uri.append("/");
uri.append("?").append(query);
+ }
URI rewrittenURI = URI.create(uri.toString()).normalize();
if (!proxyServlet.validateDestination(rewrittenURI.getHost(), rewrittenURI.getPort()))
diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java
index a4be2815b5..f4f36d6868 100644
--- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java
+++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/ProxyServletTest.java
@@ -44,6 +44,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.zip.GZIPOutputStream;
+
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
@@ -650,7 +651,29 @@ public class ProxyServletTest
@Test
public void testTransparentProxyWithQuery() throws Exception
{
- final String target = "/test";
+ testTransparentProxyWithQuery("/foo", "/proxy", "/test");
+ }
+
+ @Test
+ public void testTransparentProxyEmptyContextWithQuery() throws Exception
+ {
+ testTransparentProxyWithQuery("", "/proxy", "/test");
+ }
+
+ @Test
+ public void testTransparentProxyEmptyTargetWithQuery() throws Exception
+ {
+ testTransparentProxyWithQuery("/bar", "/proxy", "");
+ }
+
+ @Test
+ public void testTransparentProxyEmptyContextEmptyTargetWithQuery() throws Exception
+ {
+ testTransparentProxyWithQuery("", "/proxy", "");
+ }
+
+ private void testTransparentProxyWithQuery(final String proxyToContext, String prefix, final String target) throws Exception
+ {
final String query = "a=1&b=2";
startServer(new HttpServlet()
{
@@ -660,6 +683,50 @@ public class ProxyServletTest
if (req.getHeader("Via") != null)
resp.addHeader(PROXIED_HEADER, "true");
+ String expectedURI = proxyToContext + target;
+ if (expectedURI.isEmpty())
+ expectedURI = "/";
+ if (expectedURI.equals(req.getRequestURI()))
+ {
+ if (query.equals(req.getQueryString()))
+ {
+ resp.setStatus(200);
+ return;
+ }
+ }
+ resp.setStatus(404);
+ }
+ });
+ String proxyTo = "http://localhost:" + serverConnector.getLocalPort() + proxyToContext;
+ proxyServlet = new ProxyServlet.Transparent();
+ Map<String, String> params = new HashMap<>();
+ params.put("proxyTo", proxyTo);
+ params.put("prefix", prefix);
+ startProxy(params);
+ startClient();
+
+ // Make the request to the proxy, it should transparently forward to the server
+ ContentResponse response = client.newRequest("localhost", proxyConnector.getLocalPort())
+ .path(prefix + target + "?" + query)
+ .timeout(5, TimeUnit.SECONDS)
+ .send();
+ Assert.assertEquals(200, response.getStatus());
+ Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
+ }
+
+ @Test
+ public void testTransparentProxyWithQueryWithSpaces() throws Exception
+ {
+ final String target = "/test";
+ final String query = "a=1&b=2&c=1234%205678&d=hello+world";
+ startServer(new HttpServlet()
+ {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
+ {
+ if (req.getHeader("Via") != null)
+ resp.addHeader(PROXIED_HEADER, "true");
+
if (target.equals(req.getRequestURI()))
{
if (query.equals(req.getQueryString()))

Back to the top