420012 - Improve ProxyServlet.Transparent configuration in case prefix="/".

This commit is contained in:
Simone Bordet 2013-10-21 22:41:39 +02:00
parent 0c80567c12
commit eaefd17652
3 changed files with 22 additions and 7 deletions

View File

@ -18,6 +18,8 @@
package org.eclipse.jetty.client;
import java.util.Objects;
import org.eclipse.jetty.util.URIUtil;
public class Origin
@ -32,7 +34,7 @@ public class Origin
public Origin(String scheme, Address address)
{
this.scheme = scheme;
this.scheme = Objects.requireNonNull(scheme);
this.address = address;
}
@ -77,7 +79,7 @@ public class Origin
public Address(String host, int port)
{
this.host = host;
this.host = Objects.requireNonNull(host);
this.port = port;
}

View File

@ -31,9 +31,7 @@ import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.servlet.AsyncContext;
import javax.servlet.GenericServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
@ -631,7 +629,12 @@ public class ProxyServlet extends HttpServlet
return null;
StringBuilder uri = new StringBuilder(_proxyTo);
uri.append(path.substring(_prefix.length()));
if (_proxyTo.endsWith("/"))
uri.setLength(uri.length() - 1);
String rest = path.substring(_prefix.length());
if (!rest.startsWith("/"))
uri.append("/");
uri.append(rest);
String query = request.getQueryString();
if (query != null)
uri.append("?").append(query);

View File

@ -650,6 +650,17 @@ public class ProxyServletTest
@Test
public void testTransparentProxy() throws Exception
{
testTransparentProxyWithPrefix("/proxy");
}
@Test
public void testTransparentProxyWithRootContext() throws Exception
{
testTransparentProxyWithPrefix("/");
}
private void testTransparentProxyWithPrefix(String prefix) throws Exception
{
final String target = "/test";
prepareServer(new HttpServlet()
@ -664,13 +675,12 @@ public class ProxyServletTest
});
String proxyTo = "http://localhost:" + serverConnector.getLocalPort();
String prefix = "/proxy";
ProxyServlet.Transparent proxyServlet = new ProxyServlet.Transparent(proxyTo, prefix);
prepareProxy(proxyServlet);
// Make the request to the proxy, it should transparently forward to the server
ContentResponse response = client.newRequest("localhost", proxyConnector.getLocalPort())
.path(prefix + target)
.path((prefix + target).replaceAll("//", "/"))
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());