Made interface Acceptor public, otherwise other packages such as CometD cannot implement it, and cleaned up the code.
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2920 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
0dcf64caa1
commit
7dfec125f2
|
@ -4,17 +4,16 @@
|
|||
// All rights reserved. This program and the accompanying materials
|
||||
// are made available under the terms of the Eclipse Public License v1.0
|
||||
// and Apache License v2.0 which accompanies this distribution.
|
||||
// The Eclipse Public License is available at
|
||||
// The Eclipse Public License is available at
|
||||
// http://www.eclipse.org/legal/epl-v10.html
|
||||
// The Apache License v2.0 is available at
|
||||
// http://www.opensource.org/licenses/apache2.0.php
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// You may elect to redistribute this code under either of these licenses.
|
||||
// ========================================================================
|
||||
|
||||
package org.eclipse.jetty.websocket;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
|
@ -23,39 +22,36 @@ import org.eclipse.jetty.http.HttpParser;
|
|||
import org.eclipse.jetty.io.ConnectedEndPoint;
|
||||
import org.eclipse.jetty.server.HttpConnection;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Factory to create WebSocket connections
|
||||
/**
|
||||
* Factory to create WebSocket connections
|
||||
*/
|
||||
public class WebSocketFactory
|
||||
{
|
||||
/* ------------------------------------------------------------ */
|
||||
interface Acceptor
|
||||
public interface Acceptor
|
||||
{
|
||||
WebSocket doWebSocketConnect(HttpServletRequest request,String protocol);
|
||||
String checkOrigin(HttpServletRequest request, String host, String origin);
|
||||
};
|
||||
|
||||
WebSocket doWebSocketConnect(HttpServletRequest request, String protocol);
|
||||
|
||||
String checkOrigin(HttpServletRequest request, String host, String origin);
|
||||
}
|
||||
|
||||
private final Acceptor _acceptor;
|
||||
private WebSocketBuffers _buffers;
|
||||
private int _maxIdleTime=300000;
|
||||
private int _maxIdleTime = 300000;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public WebSocketFactory(Acceptor acceptor)
|
||||
{
|
||||
_buffers=new WebSocketBuffers(64*1024);
|
||||
_acceptor=acceptor;
|
||||
this(acceptor, 64 * 1024);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public WebSocketFactory(Acceptor acceptor,int bufferSize)
|
||||
public WebSocketFactory(Acceptor acceptor, int bufferSize)
|
||||
{
|
||||
_buffers=new WebSocketBuffers(bufferSize);
|
||||
_acceptor=acceptor;
|
||||
_buffers = new WebSocketBuffers(bufferSize);
|
||||
_acceptor = acceptor;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the maxIdleTime.
|
||||
/**
|
||||
* Get the maxIdleTime.
|
||||
*
|
||||
* @return the maxIdleTime
|
||||
*/
|
||||
public long getMaxIdleTime()
|
||||
|
@ -63,8 +59,9 @@ public class WebSocketFactory
|
|||
return _maxIdleTime;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set the maxIdleTime.
|
||||
/**
|
||||
* Set the maxIdleTime.
|
||||
*
|
||||
* @param maxIdleTime the maxIdleTime to set
|
||||
*/
|
||||
public void setMaxIdleTime(int maxIdleTime)
|
||||
|
@ -72,8 +69,9 @@ public class WebSocketFactory
|
|||
_maxIdleTime = maxIdleTime;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the bufferSize.
|
||||
/**
|
||||
* Get the bufferSize.
|
||||
*
|
||||
* @return the bufferSize
|
||||
*/
|
||||
public int getBufferSize()
|
||||
|
@ -81,115 +79,117 @@ public class WebSocketFactory
|
|||
return _buffers.getBufferSize();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Set the bufferSize.
|
||||
/**
|
||||
* Set the bufferSize.
|
||||
*
|
||||
* @param bufferSize the bufferSize to set
|
||||
*/
|
||||
public void setBufferSize(int bufferSize)
|
||||
{
|
||||
if (bufferSize!=getBufferSize())
|
||||
_buffers=new WebSocketBuffers(bufferSize);
|
||||
if (bufferSize != getBufferSize())
|
||||
_buffers = new WebSocketBuffers(bufferSize);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Upgrade the request/response to a WebSocket Connection.
|
||||
/**
|
||||
* 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 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 subprotocol The protocol
|
||||
* @throws IOException
|
||||
* @param origin The origin of the websocket connection
|
||||
* @param protocol The websocket protocol
|
||||
* @throws IOException in case of I/O errors
|
||||
*/
|
||||
public void upgrade(HttpServletRequest request,HttpServletResponse response, WebSocket websocket, String origin, String subprotocol)
|
||||
throws IOException
|
||||
{
|
||||
public void upgrade(HttpServletRequest request, HttpServletResponse response, WebSocket websocket, String origin, String protocol)
|
||||
throws IOException
|
||||
{
|
||||
if (!"websocket".equalsIgnoreCase(request.getHeader("Upgrade")))
|
||||
throw new IllegalStateException("!Upgrade:websocket");
|
||||
if (!"HTTP/1.1".equals(request.getProtocol()))
|
||||
throw new IllegalStateException("!HTTP/1.1");
|
||||
|
||||
int draft=request.getIntHeader("Sec-WebSocket-Version");
|
||||
if (draft<0)
|
||||
draft=request.getIntHeader("Sec-WebSocket-Draft");
|
||||
|
||||
int draft = request.getIntHeader("Sec-WebSocket-Version");
|
||||
if (draft < 0)
|
||||
draft = request.getIntHeader("Sec-WebSocket-Draft");
|
||||
HttpConnection http = HttpConnection.getCurrentConnection();
|
||||
ConnectedEndPoint endp = (ConnectedEndPoint)http.getEndPoint();
|
||||
|
||||
|
||||
final WebSocketConnection connection;
|
||||
switch(draft)
|
||||
switch (draft)
|
||||
{
|
||||
case 6:
|
||||
connection=new WebSocketConnectionD06(websocket,endp,_buffers,http.getTimeStamp(), _maxIdleTime,subprotocol,draft);
|
||||
connection = new WebSocketConnectionD06(websocket, endp, _buffers, http.getTimeStamp(), _maxIdleTime, protocol, draft);
|
||||
break;
|
||||
case 5:
|
||||
case 4:
|
||||
case 3:
|
||||
case 2:
|
||||
throw new HttpException(400,"Unsupported draft specification: "+draft);
|
||||
throw new HttpException(400, "Unsupported draft specification: " + draft);
|
||||
default:
|
||||
connection=new WebSocketConnectionD00(websocket,endp,_buffers,http.getTimeStamp(), _maxIdleTime,subprotocol,draft);
|
||||
connection = new WebSocketConnectionD00(websocket, endp, _buffers, http.getTimeStamp(), _maxIdleTime, protocol, draft);
|
||||
}
|
||||
|
||||
|
||||
// Let the connection finish processing the handshake
|
||||
connection.handshake(request,response, origin, subprotocol);
|
||||
connection.handshake(request, response, origin, protocol);
|
||||
response.flushBuffer();
|
||||
|
||||
// Give the connection any unused data from the HTTP connection.
|
||||
connection.fillBuffersFrom(((HttpParser)http.getParser()).getHeaderBuffer());
|
||||
connection.fillBuffersFrom(((HttpParser)http.getParser()).getBodyBuffer());
|
||||
|
||||
// Tell jetty about the new connection
|
||||
request.setAttribute("org.eclipse.jetty.io.Connection",connection);
|
||||
}
|
||||
// Tell jetty about the new connection
|
||||
request.setAttribute("org.eclipse.jetty.io.Connection", connection);
|
||||
}
|
||||
|
||||
public static String[] parseProtocols(String protocol)
|
||||
{
|
||||
if (protocol==null)
|
||||
if (protocol == null)
|
||||
return new String[]{null};
|
||||
protocol=protocol.trim();
|
||||
if (protocol==null || protocol.length()==0)
|
||||
protocol = protocol.trim();
|
||||
if (protocol == null || protocol.length() == 0)
|
||||
return new String[]{null};
|
||||
String[] passed = protocol.split("\\s*,\\s*");
|
||||
String[] protocols = new String[passed.length+1];
|
||||
System.arraycopy(passed,0,protocols,0,passed.length);
|
||||
String[] passed = protocol.split("\\s*,\\s*");
|
||||
String[] protocols = new String[passed.length + 1];
|
||||
System.arraycopy(passed, 0, protocols, 0, passed.length);
|
||||
return protocols;
|
||||
}
|
||||
|
||||
public boolean acceptWebSocket(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException
|
||||
|
||||
public boolean acceptWebSocket(HttpServletRequest request, HttpServletResponse response)
|
||||
throws IOException
|
||||
{
|
||||
if ("websocket".equalsIgnoreCase(request.getHeader("Upgrade")))
|
||||
{
|
||||
String protocol=request.getHeader("Sec-WebSocket-Protocol");
|
||||
if (protocol==null) // TODO remove once draft period is over
|
||||
protocol=request.getHeader("WebSocket-Protocol");
|
||||
|
||||
WebSocket websocket=null;
|
||||
for (String p :WebSocketFactory.parseProtocols(protocol))
|
||||
String protocol = request.getHeader("Sec-WebSocket-Protocol");
|
||||
if (protocol == null) // TODO remove once draft period is over
|
||||
protocol = request.getHeader("WebSocket-Protocol");
|
||||
|
||||
WebSocket websocket = null;
|
||||
for (String p : WebSocketFactory.parseProtocols(protocol))
|
||||
{
|
||||
websocket=_acceptor.doWebSocketConnect(request,p);
|
||||
if (websocket!=null)
|
||||
websocket = _acceptor.doWebSocketConnect(request, p);
|
||||
if (websocket != null)
|
||||
{
|
||||
protocol=p;
|
||||
protocol = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String host=request.getHeader("Host");
|
||||
String origin=request.getHeader("Origin");
|
||||
origin=_acceptor.checkOrigin(request,host,origin);
|
||||
String host = request.getHeader("Host");
|
||||
String origin = request.getHeader("Origin");
|
||||
origin = _acceptor.checkOrigin(request, host, origin);
|
||||
|
||||
if (websocket!=null)
|
||||
if (websocket != null)
|
||||
{
|
||||
upgrade(request,response,websocket,origin,protocol);
|
||||
upgrade(request, response, websocket, origin, protocol);
|
||||
return true;
|
||||
}
|
||||
|
||||
response.sendError(503);
|
||||
|
||||
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue