473624 - ProxyServlet.Transparent / TransparentDelegate add trailing slash before query when using prefix.

This commit is contained in:
Simone Bordet 2015-07-28 16:39:49 +02:00
parent aa75bcf078
commit 837d1a74bb
2 changed files with 81 additions and 4 deletions

View File

@ -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()))

View File

@ -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,9 +651,75 @@ 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()
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
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