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");
String wsScheme = inputUri.getScheme();
String httpScheme = null;
if ("http".equalsIgnoreCase(wsScheme) || "https".equalsIgnoreCase(wsScheme))
{
// leave alone
httpScheme = wsScheme;
}
else if ("ws".equalsIgnoreCase(wsScheme))
{
// convert to http
httpScheme = "http";
}
else if ("wss".equalsIgnoreCase(wsScheme))
{
// convert to https
httpScheme = "https";
}
else
{
throw new URISyntaxException(inputUri.toString(),"Unrecognized WebSocket scheme");
return inputUri; // TODO should this be cloned?
}
return new URI(httpScheme,inputUri.getUserInfo(),inputUri.getHost(),inputUri.getPort(),inputUri.getPath(),inputUri.getQuery(),inputUri.getFragment());
if ("ws".equalsIgnoreCase(wsScheme))
{
// convert to http
return new URI("http"+inputUri.toString().substring(2));
}
if ("wss".equalsIgnoreCase(wsScheme))
{
// convert to https
return new URI("http"+inputUri.toString().substring(2));
}
throw new URISyntaxException(inputUri.toString(),"Unrecognized WebSocket scheme");
}
/**

View File

@ -136,7 +136,7 @@ public class RequestHeadersTest
@Test
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);
client.setTimeout(1,TimeUnit.SECONDS);
@ -149,10 +149,10 @@ public class RequestHeadersTest
UpgradeRequest req = echoCreator.getLastRequest();
assertThat("Last Request",req,notNullValue());
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.query", req.getRequestURI().getQuery(), is("abc=x z"));
assertThat("Request.uri.rawQuery", req.getRequestURI().getRawQuery(), is("abc=x%20z"));
assertThat("Request.uri.rawQuery", req.getRequestURI().getRawQuery(), is("abc=x%20z&breakfast=bacon%26eggs&2*2%3d5=false"));
assertThat("Request.uri.query", req.getRequestURI().getQuery(), is("abc=x z&breakfast=bacon&eggs&2*2=5=false"));
}
finally
{

View File

@ -60,26 +60,14 @@ public class ServletUpgradeRequest implements UpgradeRequest
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();
String decodedQuery = null;
this.secure = httpRequest.isSecure();
StringBuffer uri = httpRequest.getRequestURL();
if (this.queryString!=null)
{
try
{
decodedQuery = URLDecoder.decode(queryString, StandardCharsets.UTF_8.toString());
}
catch (UnsupportedEncodingException e)
{
decodedQuery = queryString;
}
}
String fragment = null;
this.requestURI = new URI(scheme,authority,path, decodedQuery, fragment);
uri.append("?").append(this.queryString);
uri.replace(0,uri.indexOf(":"),secure ? "wss" : "ws");
this.requestURI = new URI(uri.toString());
this.request = new UpgradeHttpServletRequest(httpRequest);
}