diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java index 6eb97ffcc2a..5f0b498d6f3 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketServerFactory.java @@ -188,7 +188,7 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc catch (URISyntaxException e) { throw new IOException("Unable to accept websocket due to mangled URI", e); - } + } finally { Thread.currentThread().setContextClassLoader(old); @@ -340,13 +340,22 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc @Override public boolean isUpgradeRequest(HttpServletRequest request, HttpServletResponse response) { - if (!"GET".equalsIgnoreCase(request.getMethod())) + // Tests sorted by least common to most common. + + String upgrade = request.getHeader("Upgrade"); + if (upgrade == null) { - // not a "GET" request (not a websocket upgrade) + // no "Upgrade: websocket" header present. return false; } - - String connection = request.getHeader("connection"); + + if (!"websocket".equalsIgnoreCase(upgrade)) + { + // Not a websocket upgrade + return false; + } + + String connection = request.getHeader("Connection"); if (connection == null) { // no "Connection: upgrade" header present. @@ -370,17 +379,10 @@ public class WebSocketServerFactory extends ContainerLifeCycle implements WebSoc { return false; } - - String upgrade = request.getHeader("Upgrade"); - if (upgrade == null) + + if (!"GET".equalsIgnoreCase(request.getMethod())) { - // no "Upgrade: websocket" header present. - return false; - } - - if (!"websocket".equalsIgnoreCase(upgrade)) - { - LOG.debug("Not a 'Upgrade: WebSocket' (was [Upgrade: " + upgrade + "])"); + // not a "GET" request (not a websocket upgrade) return false; } diff --git a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java index 453062ce046..d6fd7b024b9 100644 --- a/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java +++ b/jetty-websocket/websocket-server/src/main/java/org/eclipse/jetty/websocket/server/WebSocketUpgradeFilter.java @@ -20,6 +20,7 @@ package org.eclipse.jetty.websocket.server; import java.io.IOException; import java.util.EnumSet; + import javax.servlet.DispatcherType; import javax.servlet.Filter; import javax.servlet.FilterChain; @@ -160,17 +161,19 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter chain.doFilter(request,response); return; } - - if (LOG.isDebugEnabled()) + + try { - LOG.debug(".doFilter({}) - {}",fname,chain); - } - - if ((request instanceof HttpServletRequest) && (response instanceof HttpServletResponse)) - { - HttpServletRequest httpreq = (HttpServletRequest)request; - HttpServletResponse httpresp = (HttpServletResponse)response; - + HttpServletRequest httpreq = (HttpServletRequest) request; + HttpServletResponse httpresp = (HttpServletResponse) response; + + if (!factory.isUpgradeRequest(httpreq, httpresp)) + { + // Not an upgrade request, skip it + chain.doFilter(request, response); + return; + } + // Since this is a filter, we need to be smart about determining the target path String contextPath = httpreq.getContextPath(); String target = httpreq.getRequestURI(); @@ -178,45 +181,52 @@ public class WebSocketUpgradeFilter extends ContainerLifeCycle implements Filter { target = target.substring(contextPath.length()); } - - if (factory.isUpgradeRequest(httpreq,httpresp)) + + MappedResource resource = pathmap.getMatch(target); + if (resource == null) { - LOG.debug("target = [{}]",target); - - MappedResource resource = pathmap.getMatch(target); - if (resource == null) + if (LOG.isDebugEnabled()) { - if (LOG.isDebugEnabled()) - { - LOG.debug("WebSocket Upgrade on {} has no associated endpoint",target); - LOG.debug("PathMappings: {}",pathmap.dump()); - } - // no match. - chain.doFilter(request,response); - return; - } - LOG.debug("WebSocket Upgrade detected on {} for endpoint {}",target,resource); - - WebSocketCreator creator = resource.getResource(); - - // Store PathSpec resource mapping as request attribute - httpreq.setAttribute(PathSpec.class.getName(),resource.getPathSpec()); - - // We have an upgrade request - if (factory.acceptWebSocket(creator,httpreq,httpresp)) - { - // We have a socket instance created - return; - } - - // If we reach this point, it means we had an incoming request to upgrade - // but it was either not a proper websocket upgrade, or it was possibly rejected - // due to incoming request constraints (controlled by WebSocketCreator) - if (response.isCommitted()) - { - // not much we can do at this point. - return; + LOG.debug("WebSocket Upgrade on {} has no associated endpoint", target); + LOG.debug("PathMappings: {}", pathmap.dump()); } + // no match. + chain.doFilter(request, response); + return; + } + + if(LOG.isDebugEnabled()) + { + LOG.debug("WebSocket Upgrade detected on {} for endpoint {}", target, resource); + } + + WebSocketCreator creator = resource.getResource(); + + // Store PathSpec resource mapping as request attribute + httpreq.setAttribute(PathSpec.class.getName(), resource.getPathSpec()); + + // We have an upgrade request + if (factory.acceptWebSocket(creator, httpreq, httpresp)) + { + // We have a socket instance created + return; + } + + // If we reach this point, it means we had an incoming request to upgrade + // but it was either not a proper websocket upgrade, or it was possibly rejected + // due to incoming request constraints (controlled by WebSocketCreator) + if (response.isCommitted()) + { + // not much we can do at this point. + return; + } + } + catch (ClassCastException e) + { + // We are in some kind of funky non-http environment. + if (LOG.isDebugEnabled()) + { + LOG.debug("Not a HttpServletRequest, skipping WebSocketUpgradeFilter"); } }