325105 websocket ondisconnect fixed

git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@2273 7e9141cc-0065-0410-87d8-b60c137991c4
This commit is contained in:
Greg Wilkins 2010-09-14 11:47:07 +00:00
parent 55f10af59e
commit 837b477f12
9 changed files with 102 additions and 8 deletions

View File

@ -32,6 +32,7 @@ jetty-7.2-SNAPSHOT
+ 324811 NPE in Server.dump
+ 324812 restore WebAppContext constructor used by geronimo integration
+ 325072 include to DefaultServlet of missing file throws FileNotFoundException
+ 325105 websocket ondisconnect fixed
+ 325128 websocket send during onConnect
+ JETTY-912 added per exchange timeout api
+ JETTY-1245 Do not use direct buffers with NIO SSL

View File

@ -431,6 +431,10 @@ public class HttpConnection implements Connection
return false;
}
public void closed()
{
}
public EndPoint getEndPoint()
{
return _endp;

View File

@ -42,4 +42,6 @@ public interface Connection
boolean isIdle();
boolean isSuspended();
void closed();
}

View File

@ -1026,12 +1026,13 @@ public abstract class AbstractConnector extends HttpBuffers implements Connector
/* ------------------------------------------------------------ */
protected void connectionClosed(Connection connection)
{
connection.closed();
if (_statsStartedAt.get() == -1)
return;
long duration = System.currentTimeMillis() - connection.getTimeStamp();
int requests = (connection instanceof HttpConnection)?((HttpConnection)connection).getRequests():0;
_requestStats.set(requests);
_connectionStats.decrement();
_connectionDurationStats.set(duration);

View File

@ -805,6 +805,11 @@ public class HttpConnection implements Connection
return _request.getAsyncContinuation().isSuspended();
}
/* ------------------------------------------------------------ */
public void closed()
{
}
/* ------------------------------------------------------------ */
public boolean isExpecting100Continues()
{

View File

@ -548,6 +548,10 @@ public class ProxyHandler extends HandlerWrapper
{
return false;
}
public void closed()
{
}
public void ready()
{
@ -687,6 +691,10 @@ public class ProxyHandler extends HandlerWrapper
{
return false;
}
public void closed()
{
}
public void setConnection(ProxyToServerConnection connection)
{

View File

@ -228,9 +228,6 @@ public class WebSocketConnection implements Connection, WebSocket.Outbound
_idle.access(_endp);
checkWriteable();
}
else
// TODO - not really the best way
_websocket.onDisconnect();
}
return this;
}
@ -260,6 +257,11 @@ public class WebSocketConnection implements Connection, WebSocket.Outbound
return false;
}
public void closed()
{
_websocket.onDisconnect();
}
public long getTimeStamp()
{
return _timestamp;

View File

@ -13,6 +13,7 @@ public abstract class WebSocketHandler extends HandlerWrapper
{
private WebSocketFactory _websocket;
private int _bufferSize=8192;
private int _maxIdleTime=-1;
/* ------------------------------------------------------------ */
@ -33,6 +34,26 @@ public abstract class WebSocketHandler extends HandlerWrapper
_bufferSize = bufferSize;
}
/* ------------------------------------------------------------ */
/** Get the maxIdleTime.
* @return the maxIdleTime
*/
public int getMaxIdleTime()
{
return (int)(_websocket==null?_maxIdleTime:_websocket.getMaxIdleTime());
}
/* ------------------------------------------------------------ */
/** Set the maxIdleTime.
* @param maxIdleTime the maxIdleTime to set
*/
public void setMaxIdleTime(int maxIdleTime)
{
_maxIdleTime = maxIdleTime;
if (_websocket!=null)
_websocket.setMaxIdleTime(maxIdleTime);
}
/* ------------------------------------------------------------ */
/**
* @see org.eclipse.jetty.server.handler.HandlerWrapper#doStart()
@ -41,6 +62,8 @@ public abstract class WebSocketHandler extends HandlerWrapper
protected void doStart() throws Exception
{
_websocket=new WebSocketFactory(_bufferSize);
if (_maxIdleTime>=0)
_websocket.setMaxIdleTime(_maxIdleTime);
super.doStart();
}
@ -78,13 +101,15 @@ public abstract class WebSocketHandler extends HandlerWrapper
super.handle(target,baseRequest,request,response);
}
}
/* ------------------------------------------------------------ */
protected String checkOrigin(HttpServletRequest request, String host, String origin)
{
if (origin==null)
origin=host;
return origin;
}
/* ------------------------------------------------------------ */
abstract protected WebSocket doWebSocketConnect(HttpServletRequest request,String protocol);

View File

@ -51,6 +51,7 @@ public class WebSocketMessageD01Test
return _serverWebSocket;
}
};
wsHandler.setMaxIdleTime(1000);
wsHandler.setHandler(new DefaultHandler());
_server.setHandler(wsHandler);
_server.start();
@ -146,6 +147,44 @@ public class WebSocketMessageD01Test
lookFor("sent on connect",input);
}
@Test
public void testIdle() throws Exception
{
Socket socket = new Socket("localhost", _connector.getLocalPort());
OutputStream output = socket.getOutputStream();
output.write(
("GET /test HTTP/1.1\r\n" +
"Host: localhost\r\n" +
"Upgrade: WebSocket\r\n" +
"Connection: Upgrade\r\n" +
"Sec-WebSocket-Draft: 1\r\n" +
"Sec-WebSocket-Protocol: onConnect\r\n" +
"Sec-WebSocket-Key1: 4 @1 46546xW%0l 1 5\r\n" +
"Sec-WebSocket-Key2: 12998 5 Y3 1 .P00\r\n" +
"\r\n"+
"^n:ds[4U").getBytes("ISO-8859-1"));
output.flush();
// Make sure the read times out if there are problems with the implementation
socket.setSoTimeout(1000);
InputStream input = socket.getInputStream();
lookFor("HTTP/1.1 101 WebSocket Protocol Handshake\r\n",input);
skipTo("\r\n\r\n",input);
assertTrue(_serverWebSocket.awaitConnected(1000));
assertNotNull(_serverWebSocket.outbound);
lookFor("8jKS'y:G*Co,Wxa-",input);
assertEquals(0x00,input.read());
assertEquals(0x0f,input.read());
lookFor("sent on connect",input);
assertTrue(_serverWebSocket.awaitDisconnected(5000));
}
private void lookFor(String string,InputStream in)
throws IOException
{
@ -188,7 +227,8 @@ public class WebSocketMessageD01Test
private static class TestWebSocket implements WebSocket
{
boolean onConnect=false;
private final CountDownLatch latch = new CountDownLatch(1);
private final CountDownLatch connected = new CountDownLatch(1);
private final CountDownLatch disconnected = new CountDownLatch(1);
private volatile Outbound outbound;
public void onConnect(Outbound outbound)
@ -205,12 +245,17 @@ public class WebSocketMessageD01Test
e.printStackTrace();
}
}
latch.countDown();
connected.countDown();
}
private boolean awaitConnected(long time) throws InterruptedException
{
return latch.await(time, TimeUnit.MILLISECONDS);
return connected.await(time, TimeUnit.MILLISECONDS);
}
private boolean awaitDisconnected(long time) throws InterruptedException
{
return disconnected.await(time, TimeUnit.MILLISECONDS);
}
public void onMessage(byte frame, String data)
@ -223,6 +268,7 @@ public class WebSocketMessageD01Test
public void onDisconnect()
{
disconnected.countDown();
}
public void onFragment(boolean more, byte opcode, byte[] data, int offset, int length)