Issue #2337 - slightly more efficient getSubProtocols() behavior

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2018-03-28 11:16:10 -05:00
parent 464b74ed60
commit 8bf695bf49
1 changed files with 11 additions and 14 deletions

View File

@ -18,13 +18,10 @@
package org.eclipse.jetty.websocket.servlet; package org.eclipse.jetty.websocket.servlet;
import java.io.UnsupportedEncodingException;
import java.net.HttpCookie; import java.net.HttpCookie;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.security.Principal; import java.security.Principal;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.ArrayList; import java.util.ArrayList;
@ -56,7 +53,6 @@ public class ServletUpgradeRequest implements UpgradeRequest
private final boolean secure; private final boolean secure;
private List<HttpCookie> cookies; private List<HttpCookie> cookies;
private Map<String, List<String>> parameterMap; private Map<String, List<String>> parameterMap;
private List<String> subprotocols;
public ServletUpgradeRequest(HttpServletRequest httpRequest) throws URISyntaxException public ServletUpgradeRequest(HttpServletRequest httpRequest) throws URISyntaxException
{ {
@ -386,22 +382,23 @@ public class ServletUpgradeRequest implements UpgradeRequest
@Override @Override
public List<String> getSubProtocols() public List<String> getSubProtocols()
{
if (subprotocols == null)
{ {
Enumeration<String> requestProtocols = request.getHeaders("Sec-WebSocket-Protocol"); Enumeration<String> requestProtocols = request.getHeaders("Sec-WebSocket-Protocol");
if (requestProtocols != null) if (requestProtocols != null && requestProtocols.hasMoreElements())
{ {
subprotocols = new ArrayList<>(2); ArrayList subprotocols = new ArrayList<>(2);
while (requestProtocols.hasMoreElements()) while (requestProtocols.hasMoreElements())
{ {
String candidate = requestProtocols.nextElement(); String candidate = requestProtocols.nextElement();
Collections.addAll(subprotocols,parseProtocols(candidate)); Collections.addAll(subprotocols,parseProtocols(candidate));
} }
}
}
return subprotocols; return subprotocols;
} }
else
{
return Collections.emptyList();
}
}
/** /**
* Equivalent to {@link HttpServletRequest#getUserPrincipal()} * Equivalent to {@link HttpServletRequest#getUserPrincipal()}