common websocket initiation

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@1115 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2009-12-02 02:30:19 +00:00
parent d7f7382bf5
commit 76cab45109
3 changed files with 86 additions and 63 deletions

View File

@ -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.
* <p>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);
}
}

View File

@ -15,7 +15,7 @@ import org.eclipse.jetty.server.handler.HandlerWrapper;
public abstract class WebSocketHandler extends HandlerWrapper public abstract class WebSocketHandler extends HandlerWrapper
{ {
private WebSocketBuffers _buffers = new WebSocketBuffers(8192); private WebSocketFactory _websocket;
private int _bufferSize=8192; private int _bufferSize=8192;
@ -44,7 +44,7 @@ public abstract class WebSocketHandler extends HandlerWrapper
@Override @Override
protected void doStart() throws Exception protected void doStart() throws Exception
{ {
_buffers=new WebSocketBuffers(_bufferSize); _websocket=new WebSocketFactory(_bufferSize);
super.doStart(); super.doStart();
} }
@ -56,49 +56,26 @@ public abstract class WebSocketHandler extends HandlerWrapper
protected void doStop() throws Exception protected void doStop() throws Exception
{ {
super.doStop(); super.doStop();
_buffers=null; _websocket=null;
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@Override @Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{ {
if ("WebSocket".equals(request.getHeader("Upgrade")) && if ("WebSocket".equals(request.getHeader("Upgrade")))
"HTTP/1.1".equals(request.getProtocol()))
{ {
String protocol=request.getHeader("WebSocket-Protocol"); String protocol=request.getHeader("WebSocket-Protocol");
WebSocket websocket=doWebSocketConnect(request,protocol); WebSocket websocket=doWebSocketConnect(request,protocol);
String host=request.getHeader("Host");
String origin=request.getHeader("Origin");
origin=checkOrigin(request,host,origin);
if (websocket!=null) if (websocket!=null)
{ _websocket.upgrade(request,response,websocket,origin,protocol);
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);
}
else else
{
response.sendError(503); response.sendError(503);
}
} }
else else
{ {

View File

@ -26,7 +26,7 @@ import org.eclipse.jetty.server.HttpConnection;
*/ */
public abstract class WebSocketServlet extends HttpServlet 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 public void init() throws ServletException
{ {
String bs=getInitParameter("bufferSize"); 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 @Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{ {
if ("WebSocket".equals(request.getHeader("Upgrade")) && if ("WebSocket".equals(request.getHeader("Upgrade")))
"HTTP/1.1".equals(request.getProtocol()))
{ {
String protocol=request.getHeader("WebSocket-Protocol"); String protocol=request.getHeader("WebSocket-Protocol");
WebSocket websocket=doWebSocketConnect(request,protocol); WebSocket websocket=doWebSocketConnect(request,protocol);
String host=request.getHeader("Host");
String origin=request.getHeader("Origin");
origin=checkOrigin(request,host,origin);
if (websocket!=null) if (websocket!=null)
{ _websocket.upgrade(request,response,websocket,origin,protocol);
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);
}
else else
{
response.sendError(503); response.sendError(503);
}
} }
else else
super.service(request,response); super.service(request,response);