428266 - HttpRequest mangles URI query string.

Now raw query string is preserved.
This commit is contained in:
Simone Bordet 2014-02-21 16:24:52 +01:00
parent 7db67ff58f
commit 3e52b60452
3 changed files with 113 additions and 5 deletions

View File

@ -206,10 +206,22 @@ public class HttpRequest implements Request
@Override
public Request param(String name, String value)
{
return param(name, value, false);
}
private Request param(String name, String value, boolean fromQuery)
{
params.add(name, value);
this.query = buildQuery();
this.uri = null;
if (!fromQuery)
{
// If we have an existing query string, preserve it and append the new parameter.
if (query != null)
query += "&" + name + "=" + urlEncode(value);
else
query = buildQuery();
uri = null;
}
return this;
}
@ -640,6 +652,9 @@ public class HttpRequest implements Request
private String urlEncode(String value)
{
if (value == null)
return "";
String encoding = "UTF-8";
try
{
@ -663,7 +678,7 @@ public class HttpRequest implements Request
String name = parts[0];
if (name.trim().length() == 0)
continue;
param(name, parts.length < 2 ? "" : urlDecode(parts[1]));
param(name, parts.length < 2 ? "" : urlDecode(parts[1]), true);
}
}
}

View File

@ -277,6 +277,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
{
final String paramName = "a";
final String paramValue = "\u20AC";
final String encodedParamValue = URLEncoder.encode(paramValue, "UTF-8");
start(new AbstractHandler()
{
@Override
@ -293,7 +294,7 @@ public class HttpClientTest extends AbstractHttpClientServerTest
}
});
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + paramValue);
URI uri = URI.create(scheme + "://localhost:" + connector.getLocalPort() + "/path?" + paramName + "=" + encodedParamValue);
ContentResponse response = client.newRequest(uri)
.method(HttpMethod.PUT)
.timeout(5, TimeUnit.SECONDS)

View File

@ -21,7 +21,6 @@ package org.eclipse.jetty.client;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@ -339,4 +338,97 @@ public class HttpClientURITest extends AbstractHttpClientServerTest
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
@Test
public void testRawQueryIsPreservedInURI() throws Exception
{
final String name = "a";
final String rawValue = "Hello%20World";
final String rawQuery = name + "=" + rawValue;
final String value = "Hello World";
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
Assert.assertEquals(rawQuery, request.getQueryString());
Assert.assertEquals(value, request.getParameter(name));
}
});
String uri = scheme + "://localhost:" + connector.getLocalPort() + "/path?" + rawQuery;
Request request = client.newRequest(uri)
.timeout(5, TimeUnit.SECONDS);
Assert.assertEquals(rawQuery, request.getQuery());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
@Test
public void testRawQueryIsPreservedInPath() throws Exception
{
final String name = "a";
final String rawValue = "Hello%20World";
final String rawQuery = name + "=" + rawValue;
final String value = "Hello World";
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
Assert.assertEquals(rawQuery, request.getQueryString());
Assert.assertEquals(value, request.getParameter(name));
}
});
Request request = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/path?" + rawQuery)
.timeout(5, TimeUnit.SECONDS);
Assert.assertEquals(rawQuery, request.getQuery());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
@Test
public void testRawQueryIsPreservedWithParam() throws Exception
{
final String name1 = "a";
final String name2 = "b";
final String rawValue1 = "Hello%20World";
final String rawQuery1 = name1 + "=" + rawValue1;
final String value1 = "Hello World";
final String value2 = "alfa omega";
final String encodedQuery2 = name2 + "=" + URLEncoder.encode(value2, "UTF-8");
final String query = rawQuery1 + "&" + encodedQuery2;
start(new AbstractHandler()
{
@Override
public void handle(String target, org.eclipse.jetty.server.Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
baseRequest.setHandled(true);
Assert.assertEquals(query, request.getQueryString());
Assert.assertEquals(value1, request.getParameter(name1));
Assert.assertEquals(value2, request.getParameter(name2));
}
});
Request request = client.newRequest("localhost", connector.getLocalPort())
.scheme(scheme)
.path("/path?" + rawQuery1)
.param(name2, value2)
.timeout(5, TimeUnit.SECONDS);
Assert.assertEquals(query, request.getQuery());
ContentResponse response = request.send();
Assert.assertEquals(HttpStatus.OK_200, response.getStatus());
}
}