Issue #1568 handle query strings with encoded characters

This commit is contained in:
Greg Wilkins 2017-05-24 09:22:16 +02:00
parent 4731470188
commit 78280d0595
3 changed files with 21 additions and 36 deletions

View File

@ -42,28 +42,25 @@ public final class WSURI
{ {
Objects.requireNonNull(inputUri,"Input URI must not be null"); Objects.requireNonNull(inputUri,"Input URI must not be null");
String wsScheme = inputUri.getScheme(); String wsScheme = inputUri.getScheme();
String httpScheme = null;
if ("http".equalsIgnoreCase(wsScheme) || "https".equalsIgnoreCase(wsScheme)) if ("http".equalsIgnoreCase(wsScheme) || "https".equalsIgnoreCase(wsScheme))
{ {
// leave alone // leave alone
httpScheme = wsScheme; return inputUri; // TODO should this be cloned?
} }
else if ("ws".equalsIgnoreCase(wsScheme))
if ("ws".equalsIgnoreCase(wsScheme))
{ {
// convert to http // convert to http
httpScheme = "http"; return new URI("http"+inputUri.toString().substring(2));
} }
else if ("wss".equalsIgnoreCase(wsScheme))
if ("wss".equalsIgnoreCase(wsScheme))
{ {
// convert to https // convert to https
httpScheme = "https"; return new URI("http"+inputUri.toString().substring(2));
} }
else
{ throw new URISyntaxException(inputUri.toString(),"Unrecognized WebSocket scheme");
throw new URISyntaxException(inputUri.toString(),"Unrecognized WebSocket scheme");
}
return new URI(httpScheme,inputUri.getUserInfo(),inputUri.getHost(),inputUri.getPort(),inputUri.getPath(),inputUri.getQuery(),inputUri.getFragment());
} }
/** /**

View File

@ -136,7 +136,7 @@ public class RequestHeadersTest
@Test @Test
public void testRequestURI() throws Exception public void testRequestURI() throws Exception
{ {
URI destUri = server.getServerUri().resolve("/?abc=x%20z"); URI destUri = server.getServerUri().resolve("/?abc=x%20z&breakfast=bacon%26eggs&2*2%3d5=false");
BlockheadClient client = new BlockheadClient(destUri); BlockheadClient client = new BlockheadClient(destUri);
client.setTimeout(1,TimeUnit.SECONDS); client.setTimeout(1,TimeUnit.SECONDS);
@ -145,14 +145,14 @@ public class RequestHeadersTest
client.connect(); client.connect();
client.sendStandardRequest(); client.sendStandardRequest();
client.expectUpgradeResponse(); client.expectUpgradeResponse();
UpgradeRequest req = echoCreator.getLastRequest(); UpgradeRequest req = echoCreator.getLastRequest();
assertThat("Last Request",req,notNullValue()); assertThat("Last Request",req,notNullValue());
assertThat("Request.host", req.getHost(), is(server.getServerUri().getHost())); assertThat("Request.host", req.getHost(), is(server.getServerUri().getHost()));
assertThat("Request.queryString", req.getQueryString(), is("abc=x%20z")); assertThat("Request.queryString", req.getQueryString(), is("abc=x%20z&breakfast=bacon%26eggs&2*2%3d5=false"));
assertThat("Request.uri.path", req.getRequestURI().getPath(), is("/")); assertThat("Request.uri.path", req.getRequestURI().getPath(), is("/"));
assertThat("Request.uri.query", req.getRequestURI().getQuery(), is("abc=x z")); assertThat("Request.uri.rawQuery", req.getRequestURI().getRawQuery(), is("abc=x%20z&breakfast=bacon%26eggs&2*2%3d5=false"));
assertThat("Request.uri.rawQuery", req.getRequestURI().getRawQuery(), is("abc=x%20z")); assertThat("Request.uri.query", req.getRequestURI().getQuery(), is("abc=x z&breakfast=bacon&eggs&2*2=5=false"));
} }
finally finally
{ {

View File

@ -60,26 +60,14 @@ public class ServletUpgradeRequest implements UpgradeRequest
public ServletUpgradeRequest(HttpServletRequest httpRequest) throws URISyntaxException public ServletUpgradeRequest(HttpServletRequest httpRequest) throws URISyntaxException
{ {
URI servletURI = URI.create(httpRequest.getRequestURL().toString());
this.secure = httpRequest.isSecure();
String scheme = secure ? "wss" : "ws";
String authority = servletURI.getAuthority();
String path = servletURI.getPath();
this.queryString = httpRequest.getQueryString(); this.queryString = httpRequest.getQueryString();
String decodedQuery = null; this.secure = httpRequest.isSecure();
if (this.queryString != null)
{ StringBuffer uri = httpRequest.getRequestURL();
try if (this.queryString!=null)
{ uri.append("?").append(this.queryString);
decodedQuery = URLDecoder.decode(queryString, StandardCharsets.UTF_8.toString()); uri.replace(0,uri.indexOf(":"),secure ? "wss" : "ws");
} this.requestURI = new URI(uri.toString());
catch (UnsupportedEncodingException e)
{
decodedQuery = queryString;
}
}
String fragment = null;
this.requestURI = new URI(scheme,authority,path, decodedQuery, fragment);
this.request = new UpgradeHttpServletRequest(httpRequest); this.request = new UpgradeHttpServletRequest(httpRequest);
} }