Fix #360 - Correctly parse spaces in URL parameters

This commit is contained in:
James Agnew 2016-05-04 10:07:35 -04:00
parent b4d3a7bb74
commit ff562a3f00
3 changed files with 28 additions and 8 deletions

View File

@ -151,7 +151,7 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
* </p>
*/
public void addHeadersToResponse(HttpServletResponse theHttpResponse) {
theHttpResponse.addHeader("X-Powered-By", "HAPI FHIR " + VersionUtil.getVersion() + " RESTful Server");
theHttpResponse.addHeader("X-Powered-By", "HAPI FHIR " + VersionUtil.getVersion() + " REST Server (FHIR Server; FHIR " + myFhirContext.getVersion().getVersion().name() + ")");
}
private void addLocationHeader(RequestDetails theRequest, HttpServletResponse theResponse, MethodOutcome response, String headerLocation, String resourceName) {
@ -570,6 +570,12 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
Map<String, String[]> params = null;
if (StringUtils.isNotBlank(theRequest.getQueryString())) {
completeUrl = requestUrl + "?" + theRequest.getQueryString();
/*
* By default, we manually parse the request params (the URL params, or the body for
* POST form queries) since Java containers can't be trusted to use UTF-8 encoding
* when parsing. Specifically Tomcat 7 and Glassfish 4.0 use 8859-1 for some dumb
* reason.... grr.....
*/
if (isIgnoreServerParsedRequestParameters()) {
String contentType = theRequest.getHeader(Constants.HEADER_CONTENT_TYPE);
if (theRequestType == RequestTypeEnum.POST && isNotBlank(contentType) && contentType.startsWith(Constants.CT_X_FORM_URLENCODED)) {

View File

@ -289,14 +289,17 @@ public class UrlUtil {
if (theString == null) {
return null;
}
if (theString.indexOf('%') == -1) {
return theString;
}
try {
return URLDecoder.decode(theString, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error("UTF-8 not supported, this shouldn't happen", e);
for (int i = 0; i < theString.length(); i++) {
char nextChar = theString.charAt(i);
if (nextChar == '%' || nextChar == '+') {
try {
return URLDecoder.decode(theString, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new Error("UTF-8 not supported, this shouldn't happen", e);
}
}
}
return theString;
}
public static class UrlParts {

View File

@ -81,6 +81,17 @@ public class ServerSearchDstu2Test {
assertEquals("param2value", ourLastRef.getValue());
}
@Test
public void testSearchParamWithSpace() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/?param2=param+value&foo=bar");
HttpResponse status = ourClient.execute(httpGet);
String responseContent = IOUtils.toString(status.getEntity().getContent());
IOUtils.closeQuietly(status.getEntity().getContent());
ourLog.info(responseContent);
assertEquals("searchParam2", ourLastMethod);
assertEquals("param value", ourLastRef.getValue());
}
@Test
public void testUnknownSearchParam() throws Exception {
HttpGet httpGet = new HttpGet("http://localhost:" + ourPort + "/?foo=bar");