337685 more common code
git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2880 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
parent
3078c8797e
commit
31d8e8b3dc
|
@ -189,8 +189,7 @@ public class WebSocketUpgradeTest extends TestCase
|
|||
newServer();
|
||||
_handler= new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
_websocket = new TestWebSocket();
|
||||
return _websocket;
|
||||
|
|
|
@ -29,8 +29,7 @@ public class TestServer extends Server
|
|||
addConnector(_connector);
|
||||
_handler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
if ("org.ietf.websocket.test-echo".equals(protocol) || "echo".equals(protocol) || "lws-mirror-protocol".equals(protocol))
|
||||
{
|
||||
|
|
|
@ -29,19 +29,29 @@ import org.eclipse.jetty.server.HttpConnection;
|
|||
*/
|
||||
public class WebSocketFactory
|
||||
{
|
||||
/* ------------------------------------------------------------ */
|
||||
interface Acceptor
|
||||
{
|
||||
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;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public WebSocketFactory()
|
||||
public WebSocketFactory(Acceptor acceptor)
|
||||
{
|
||||
_buffers=new WebSocketBuffers(8192);
|
||||
_buffers=new WebSocketBuffers(64*1024);
|
||||
_acceptor=acceptor;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
public WebSocketFactory(int bufferSize)
|
||||
public WebSocketFactory(Acceptor acceptor,int bufferSize)
|
||||
{
|
||||
_buffers=new WebSocketBuffers(bufferSize);
|
||||
_acceptor=acceptor;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -146,4 +156,40 @@ public class WebSocketFactory
|
|||
System.arraycopy(passed,0,protocols,0,passed.length);
|
||||
return protocols;
|
||||
}
|
||||
|
||||
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))
|
||||
{
|
||||
websocket=_acceptor.doWebSocketConnect(request,p);
|
||||
if (websocket!=null)
|
||||
{
|
||||
protocol=p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String host=request.getHeader("Host");
|
||||
String origin=request.getHeader("Origin");
|
||||
origin=_acceptor.checkOrigin(request,host,origin);
|
||||
|
||||
if (websocket!=null)
|
||||
{
|
||||
upgrade(request,response,websocket,origin,protocol);
|
||||
return true;
|
||||
}
|
||||
|
||||
response.sendError(503);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,13 +22,12 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.handler.HandlerWrapper;
|
||||
|
||||
public abstract class WebSocketHandler extends HandlerWrapper
|
||||
public abstract class WebSocketHandler extends HandlerWrapper implements WebSocketFactory.Acceptor
|
||||
{
|
||||
private WebSocketFactory _webSocketFactory;
|
||||
private int _bufferSize=8192;
|
||||
private int _bufferSize=64*1024;
|
||||
private int _maxIdleTime=-1;
|
||||
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/** Get the bufferSize.
|
||||
* @return the bufferSize
|
||||
|
@ -74,7 +73,7 @@ public abstract class WebSocketHandler extends HandlerWrapper
|
|||
@Override
|
||||
protected void doStart() throws Exception
|
||||
{
|
||||
_webSocketFactory=new WebSocketFactory(_bufferSize);
|
||||
_webSocketFactory=new WebSocketFactory(this,_bufferSize);
|
||||
if (_maxIdleTime>=0)
|
||||
_webSocketFactory.setMaxIdleTime(_maxIdleTime);
|
||||
super.doStart();
|
||||
|
@ -95,47 +94,17 @@ public abstract class WebSocketHandler extends HandlerWrapper
|
|||
@Override
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||
{
|
||||
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))
|
||||
{
|
||||
websocket=doWebSocketConnect(request,p);
|
||||
if (websocket!=null)
|
||||
{
|
||||
protocol=p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String host=request.getHeader("Host");
|
||||
String origin=request.getHeader("Origin");
|
||||
origin=checkOrigin(request,host,origin);
|
||||
|
||||
if (websocket!=null)
|
||||
_webSocketFactory.upgrade(request,response,websocket,origin,protocol);
|
||||
else
|
||||
response.sendError(503);
|
||||
}
|
||||
else
|
||||
{
|
||||
super.handle(target,baseRequest,request,response);
|
||||
}
|
||||
if (_webSocketFactory.acceptWebSocket(request,response) || response.isCommitted())
|
||||
return;
|
||||
super.handle(target,baseRequest,request,response);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
protected String checkOrigin(HttpServletRequest request, String host, String origin)
|
||||
public String checkOrigin(HttpServletRequest request, String host, String origin)
|
||||
{
|
||||
if (origin==null)
|
||||
origin=host;
|
||||
return origin;
|
||||
}
|
||||
/* ------------------------------------------------------------ */
|
||||
|
||||
abstract protected WebSocket doWebSocketConnect(HttpServletRequest request,String protocol);
|
||||
|
||||
}
|
||||
|
|
|
@ -35,9 +35,9 @@ import javax.servlet.http.HttpServletResponse;
|
|||
* that a websocket may be idle before closing (default 300,000).
|
||||
*
|
||||
*/
|
||||
public abstract class WebSocketServlet extends HttpServlet
|
||||
public abstract class WebSocketServlet extends HttpServlet implements WebSocketFactory.Acceptor
|
||||
{
|
||||
WebSocketFactory _websocket;
|
||||
WebSocketFactory _webSocketFactory;
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
/**
|
||||
|
@ -47,10 +47,10 @@ public abstract class WebSocketServlet extends HttpServlet
|
|||
public void init() throws ServletException
|
||||
{
|
||||
String bs=getInitParameter("bufferSize");
|
||||
_websocket = new WebSocketFactory(bs==null?8192:Integer.parseInt(bs));
|
||||
_webSocketFactory = new WebSocketFactory(this,bs==null?8192:Integer.parseInt(bs));
|
||||
String mit=getInitParameter("maxIdleTime");
|
||||
if (mit!=null)
|
||||
_websocket.setMaxIdleTime(Integer.parseInt(mit));
|
||||
_webSocketFactory.setMaxIdleTime(Integer.parseInt(mit));
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ */
|
||||
|
@ -60,50 +60,18 @@ public abstract class WebSocketServlet extends HttpServlet
|
|||
@Override
|
||||
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
if ("websocket".equalsIgnoreCase(request.getHeader("Upgrade")))
|
||||
{
|
||||
boolean hixie = request.getHeader("Sec-WebSocket-Key1")!=null;
|
||||
|
||||
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=doWebSocketConnect(request,p);
|
||||
if (websocket!=null)
|
||||
{
|
||||
protocol=p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String host=request.getHeader("Host");
|
||||
String origin=request.getHeader("Origin");
|
||||
origin=checkOrigin(request,host,origin);
|
||||
|
||||
if (websocket!=null)
|
||||
_websocket.upgrade(request,response,websocket,origin,protocol);
|
||||
else
|
||||
{
|
||||
if (hixie)
|
||||
response.setHeader("Connection","close");
|
||||
response.sendError(503);
|
||||
}
|
||||
}
|
||||
else
|
||||
super.service(request,response);
|
||||
if (_webSocketFactory.acceptWebSocket(request,response) || response.isCommitted())
|
||||
return;
|
||||
super.service(request,response);
|
||||
}
|
||||
|
||||
protected String checkOrigin(HttpServletRequest request, String host, String origin)
|
||||
public String checkOrigin(HttpServletRequest request, String host, String origin)
|
||||
{
|
||||
if (origin==null)
|
||||
origin=host;
|
||||
return origin;
|
||||
}
|
||||
|
||||
abstract protected WebSocket doWebSocketConnect(HttpServletRequest request,String protocol);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -52,8 +52,7 @@ public class WebSocketLoadTest
|
|||
|
||||
WebSocketHandler wsHandler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
return new EchoWebSocket();
|
||||
}
|
||||
|
|
|
@ -43,8 +43,7 @@ public class WebSocketMessageD00Test
|
|||
_server.addConnector(_connector);
|
||||
WebSocketHandler wsHandler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
_serverWebSocket = new TestWebSocket();
|
||||
_serverWebSocket.onConnect=("onConnect".equals(protocol));
|
||||
|
|
|
@ -39,8 +39,7 @@ public class WebSocketMessageD01Test
|
|||
_server.addConnector(_connector);
|
||||
WebSocketHandler wsHandler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
_serverWebSocket = new TestWebSocket();
|
||||
_serverWebSocket.onConnect=("onConnect".equals(protocol));
|
||||
|
|
|
@ -45,8 +45,7 @@ public class WebSocketMessageD06Test
|
|||
_server.addConnector(_connector);
|
||||
WebSocketHandler wsHandler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
_serverWebSocket = new TestWebSocket();
|
||||
_serverWebSocket.onConnect=("onConnect".equals(protocol));
|
||||
|
|
|
@ -41,8 +41,7 @@ public class WebSocketMessageW75Test
|
|||
_server.addConnector(_connector);
|
||||
WebSocketHandler wsHandler = new WebSocketHandler()
|
||||
{
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
return _serverWebSocket = new TestWebSocket();
|
||||
}
|
||||
|
|
|
@ -24,8 +24,7 @@ public class WebSocketChatServlet extends WebSocketServlet
|
|||
getServletContext().getNamedDispatcher("default").forward(request,response);
|
||||
};
|
||||
|
||||
@Override
|
||||
protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
|
||||
{
|
||||
return new ChatWebSocket();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue