408782 - Transparent Proxy - rewrite URL is ignoring query strings.

This commit is contained in:
Simone Bordet 2013-06-06 15:38:51 +02:00
parent 5c8b785962
commit 4c3a777ed5
2 changed files with 49 additions and 5 deletions

View File

@ -615,7 +615,12 @@ public class ProxyServlet extends HttpServlet
if (!path.startsWith(_prefix))
return null;
URI rewrittenURI = URI.create(_proxyTo + path.substring(_prefix.length())).normalize();
StringBuilder uri = new StringBuilder(_proxyTo);
uri.append(path.substring(_prefix.length()));
String query = request.getQueryString();
if (query != null)
uri.append("?").append(query);
URI rewrittenURI = URI.create(uri.toString()).normalize();
if (!validateDestination(rewrittenURI.getHost(), rewrittenURI.getPort()))
return null;

View File

@ -242,9 +242,9 @@ public class ProxyServletTest
threadPool.setMaxThreads(2);
result.setExecutor(threadPool);
result.start();
ContentResponse[] responses = new ContentResponse[10];
final byte[] content = new byte[1024];
Arrays.fill(content, (byte)'A');
prepareServer(new HttpServlet()
@ -265,8 +265,8 @@ public class ProxyServletTest
.timeout(5, TimeUnit.SECONDS)
.send();
}
for ( int i = 0; i < 10; ++i )
{
@ -678,6 +678,45 @@ public class ProxyServletTest
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
@Test
public void testTransparentProxyWithQuery() throws Exception
{
final String target = "/test";
final String query = "a=1&b=2";
prepareServer(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()))
{
resp.setStatus(200);
return;
}
}
resp.setStatus(404);
}
});
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 + "?" + query)
.timeout(5, TimeUnit.SECONDS)
.send();
Assert.assertEquals(200, response.getStatus());
Assert.assertTrue(response.getHeaders().containsKey(PROXIED_HEADER));
}
@Test
public void testCachingProxy() throws Exception
{