From 76cab4510901024026c5711f3e857592bcef662b Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Wed, 2 Dec 2009 02:30:19 +0000 Subject: [PATCH] common websocket initiation git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1115 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../jetty/websocket/WebSocketFactory.java | 69 +++++++++++++++++++ .../jetty/websocket/WebSocketHandler.java | 41 +++-------- .../jetty/websocket/WebSocketServlet.java | 39 +++-------- 3 files changed, 86 insertions(+), 63 deletions(-) create mode 100644 jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketFactory.java diff --git a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketFactory.java b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketFactory.java new file mode 100644 index 00000000000..891996b7682 --- /dev/null +++ b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketFactory.java @@ -0,0 +1,69 @@ +package org.eclipse.jetty.websocket; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.http.HttpParser; +import org.eclipse.jetty.io.ConnectedEndPoint; +import org.eclipse.jetty.io.UpgradeConnectionException; +import org.eclipse.jetty.server.HttpConnection; + + +/* ------------------------------------------------------------ */ +/** Factory to create WebSocket connections + */ +public class WebSocketFactory extends WebSocketBuffers +{ + public WebSocketFactory(int bufferSize) + { + super(bufferSize); + } + + /* ------------------------------------------------------------ */ + /** Upgrade the request/response to a WebSocket Connection. + *

This method will not normally return, but will instead throw a + * UpgradeConnectionException, to exit HTTP handling and initiate + * WebSocket handling of the connection. + * @param request The request to upgrade + * @param response The response to upgrade + * @param websocket The websocket handler implementation to use + * @param origin The origin of the websocket connection + * @param protocol The protocol + * @throws UpgradeConnectionException Thrown to upgrade the connection + * @throws IOException + */ + public void upgrade(HttpServletRequest request,HttpServletResponse response, WebSocket websocket, String origin, String protocol) + throws UpgradeConnectionException, IOException + { + if (!"WebSocket".equals(request.getHeader("Upgrade"))) + throw new IllegalStateException("!Upgrade:websocket"); + if (!"HTTP/1.1".equals(request.getProtocol())) + throw new IllegalStateException("!HTTP/1.1"); + + HttpConnection http = HttpConnection.getCurrentConnection(); + ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint(); + WebSocketConnection connection = new WebSocketConnection(this,endp,http.getTimeStamp(),websocket); + + String uri=request.getRequestURI(); + String host=request.getHeader("Host"); + + response.setHeader("Upgrade","WebSocket"); + response.addHeader("Connection","Upgrade"); + response.addHeader("WebSocket-Origin",origin); + response.addHeader("WebSocket-Location","ws://"+host+uri); + if (protocol!=null) + response.addHeader("WebSocket-Protocol",protocol); + response.sendError(101,"Web Socket Protocol Handshake"); + response.flushBuffer(); + + connection.fill(((HttpParser)http.getParser()).getHeaderBuffer()); + connection.fill(((HttpParser)http.getParser()).getBodyBuffer()); + + websocket.onConnect(connection); + throw new UpgradeConnectionException(connection); + + } + +} diff --git a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketHandler.java b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketHandler.java index c079ef25bbb..4dc88bfe4d9 100644 --- a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketHandler.java +++ b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketHandler.java @@ -15,7 +15,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper; public abstract class WebSocketHandler extends HandlerWrapper { - private WebSocketBuffers _buffers = new WebSocketBuffers(8192); + private WebSocketFactory _websocket; private int _bufferSize=8192; @@ -44,7 +44,7 @@ public abstract class WebSocketHandler extends HandlerWrapper @Override protected void doStart() throws Exception { - _buffers=new WebSocketBuffers(_bufferSize); + _websocket=new WebSocketFactory(_bufferSize); super.doStart(); } @@ -56,49 +56,26 @@ public abstract class WebSocketHandler extends HandlerWrapper protected void doStop() throws Exception { super.doStop(); - _buffers=null; + _websocket=null; } /* ------------------------------------------------------------ */ @Override public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { - if ("WebSocket".equals(request.getHeader("Upgrade")) && - "HTTP/1.1".equals(request.getProtocol())) + if ("WebSocket".equals(request.getHeader("Upgrade"))) { String protocol=request.getHeader("WebSocket-Protocol"); WebSocket websocket=doWebSocketConnect(request,protocol); + String host=request.getHeader("Host"); + String origin=request.getHeader("Origin"); + origin=checkOrigin(request,host,origin); + if (websocket!=null) - { - HttpConnection http = HttpConnection.getCurrentConnection(); - ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint(); - WebSocketConnection connection = new WebSocketConnection(_buffers,endp,http.getTimeStamp(),websocket); - - String uri=request.getRequestURI(); - String host=request.getHeader("Host"); - String origin=request.getHeader("Origin"); - origin=checkOrigin(request,host,origin); - - response.setHeader("Upgrade","WebSocket"); - response.addHeader("Connection","Upgrade"); - response.addHeader("WebSocket-Origin",origin); - response.addHeader("WebSocket-Location","ws://"+host+uri); - if (protocol!=null) - response.addHeader("WebSocket-Protocol",protocol); - response.sendError(101,"Web Socket Protocol Handshake"); - response.flushBuffer(); - - connection.fill(((HttpParser)http.getParser()).getHeaderBuffer()); - connection.fill(((HttpParser)http.getParser()).getBodyBuffer()); - - websocket.onConnect(connection); - throw new UpgradeConnectionException(connection); - } + _websocket.upgrade(request,response,websocket,origin,protocol); else - { response.sendError(503); - } } else { diff --git a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketServlet.java b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketServlet.java index ec22818f3ae..ea8701955b5 100644 --- a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketServlet.java +++ b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketServlet.java @@ -26,7 +26,7 @@ import org.eclipse.jetty.server.HttpConnection; */ public abstract class WebSocketServlet extends HttpServlet { - WebSocketBuffers _buffers; + WebSocketFactory _websocket; /* ------------------------------------------------------------ */ /** @@ -36,7 +36,7 @@ public abstract class WebSocketServlet extends HttpServlet public void init() throws ServletException { String bs=getInitParameter("bufferSize"); - _buffers = new WebSocketBuffers(bs==null?8192:Integer.parseInt(bs)); + _websocket = new WebSocketFactory(bs==null?8192:Integer.parseInt(bs)); } /* ------------------------------------------------------------ */ @@ -46,42 +46,19 @@ public abstract class WebSocketServlet extends HttpServlet @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - if ("WebSocket".equals(request.getHeader("Upgrade")) && - "HTTP/1.1".equals(request.getProtocol())) + if ("WebSocket".equals(request.getHeader("Upgrade"))) { String protocol=request.getHeader("WebSocket-Protocol"); WebSocket websocket=doWebSocketConnect(request,protocol); + + String host=request.getHeader("Host"); + String origin=request.getHeader("Origin"); + origin=checkOrigin(request,host,origin); if (websocket!=null) - { - HttpConnection http = HttpConnection.getCurrentConnection(); - ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint(); - WebSocketConnection connection = new WebSocketConnection(_buffers,endp,http.getTimeStamp(),websocket); - - String uri=request.getRequestURI(); - String host=request.getHeader("Host"); - String origin=request.getHeader("Origin"); - origin=checkOrigin(request,host,origin); - - response.setHeader("Upgrade","WebSocket"); - response.addHeader("Connection","Upgrade"); - response.addHeader("WebSocket-Origin",origin); - response.addHeader("WebSocket-Location","ws://"+host+uri); - if (protocol!=null) - response.addHeader("WebSocket-Protocol",protocol); - response.sendError(101,"Web Socket Protocol Handshake"); - response.flushBuffer(); - - connection.fill(((HttpParser)http.getParser()).getHeaderBuffer()); - connection.fill(((HttpParser)http.getParser()).getBodyBuffer()); - - websocket.onConnect(connection); - throw new UpgradeConnectionException(connection); - } + _websocket.upgrade(request,response,websocket,origin,protocol); else - { response.sendError(503); - } } else super.service(request,response);