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
{ {
@ -387,20 +383,21 @@ 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");
if (requestProtocols != null && requestProtocols.hasMoreElements())
{ {
Enumeration<String> requestProtocols = request.getHeaders("Sec-WebSocket-Protocol"); ArrayList subprotocols = new ArrayList<>(2);
if (requestProtocols != null) while (requestProtocols.hasMoreElements())
{ {
subprotocols = new ArrayList<>(2); String candidate = requestProtocols.nextElement();
while (requestProtocols.hasMoreElements()) Collections.addAll(subprotocols,parseProtocols(candidate));
{
String candidate = requestProtocols.nextElement();
Collections.addAll(subprotocols,parseProtocols(candidate));
}
} }
return subprotocols;
}
else
{
return Collections.emptyList();
} }
return subprotocols;
} }
/** /**