357178 websockets draft 14 support

This commit is contained in:
Greg Wilkins 2011-09-09 12:10:01 +10:00
parent 675464e82d
commit 4c885ca3ad
4 changed files with 73 additions and 32 deletions

View File

@ -42,7 +42,8 @@ import org.eclipse.jetty.websocket.WebSocket.OnTextMessage;
public class WebSocketConnectionD13 extends AbstractConnection implements WebSocketConnection
{
private static final Logger LOG = Log.getLogger(WebSocketConnectionD13.class);
private static final boolean STRICT=true;
private static final boolean STRICT=Boolean.getBoolean("org.eclipse.jetty.websocket.STRICT");
private static final boolean BRUTAL=Boolean.getBoolean("org.eclipse.jetty.websocket.BRUTAL");
final static byte OP_CONTINUATION = 0x00;
final static byte OP_TEXT = 0x01;
@ -62,7 +63,7 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
final static int CLOSE_UNDEFINED=1004;
final static int CLOSE_NO_CODE=1005;
final static int CLOSE_NO_CLOSE=1006;
final static int CLOSE_NOT_UTF8=1007;
final static int CLOSE_BAD_PAYLOAD=1007;
final static int CLOSE_POLICY_VIOLATION=1008;
final static int CLOSE_MESSAGE_TOO_LARGE=1009;
final static int CLOSE_REQUIRED_EXTENSION=1010;
@ -641,19 +642,13 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
{
if (isControlFrame(opcode) && buffer.length()>125)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Control frame too large");
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"Control frame too large");
return;
}
if ((flags&0x7)!=0)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"RSV bits set 0x"+Integer.toHexString(flags));
return;
}
if (_opcode!=-1 && opcode!=WebSocketConnectionD13.OP_CONTINUATION)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad continuation"+Integer.toHexString(opcode));
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"RSV bits set 0x"+Integer.toHexString(flags));
return;
}
@ -681,7 +676,7 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
{
if (_opcode==-1)
{
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad Continuation");
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad Continuation");
return;
}
@ -756,6 +751,12 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
case WebSocketConnectionD13.OP_TEXT:
{
if (STRICT && _opcode!=-1)
{
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"Expected Continuation"+Integer.toHexString(opcode));
return;
}
if(_onTextMessage!=null)
{
if (_connection.getMaxTextMessageSize()<=0)
@ -766,7 +767,7 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
else
{
LOG.warn("Frame discarded. Text aggregation disabled for {}",_endp);
_connection.close(WebSocketConnectionD13.CLOSE_POLICY_VIOLATION,"Text frame aggregation disabled");
errorClose(WebSocketConnectionD13.CLOSE_POLICY_VIOLATION,"Text frame aggregation disabled");
}
}
// append bytes to message buffer (if they fit)
@ -788,9 +789,15 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
}
break;
}
case WebSocketConnectionD13.OP_BINARY:
{
if (STRICT && _opcode!=-1)
{
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"Expected Continuation"+Integer.toHexString(opcode));
return;
}
if (_onBinaryMessage!=null && checkBinaryMessageSize(0,buffer.length()))
{
if (lastFrame)
@ -808,25 +815,23 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
else
{
LOG.warn("Frame discarded. Binary aggregation disabed for {}",_endp);
_connection.close(WebSocketConnectionD13.CLOSE_POLICY_VIOLATION,"Binary frame aggregation disabled");
errorClose(WebSocketConnectionD13.CLOSE_POLICY_VIOLATION,"Binary frame aggregation disabled");
}
}
break;
}
default:
if (STRICT)
_connection.close(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad opcode 0x"+Integer.toHexString(opcode));
errorClose(WebSocketConnectionD13.CLOSE_PROTOCOL,"Bad opcode 0x"+Integer.toHexString(opcode));
return;
}
}
catch(Utf8Appendable.NotUtf8Exception notUtf8)
{
LOG.warn(notUtf8);
LOG.warn("{} for {}",notUtf8,_endp);
LOG.debug(notUtf8);
_connection.close(WebSocketConnectionD13.CLOSE_NOT_UTF8,"Invalid UTF-8");
errorClose(WebSocketConnectionD13.CLOSE_BAD_PAYLOAD,"Invalid UTF-8");
}
catch(ThreadDeath th)
{
@ -838,6 +843,23 @@ public class WebSocketConnectionD13 extends AbstractConnection implements WebSoc
}
}
private void errorClose(int code, String message)
{
_connection.close(code,message);
if (BRUTAL)
{
try
{
_endp.close();
}
catch (IOException e)
{
LOG.warn(e.toString());
LOG.debug(e);
}
}
}
private boolean checkBinaryMessageSize(int bufferLen, int length)
{
int max = _connection.getMaxBinaryMessageSize();

View File

@ -227,11 +227,13 @@ public class WebSocketFactory
connection = new WebSocketConnectionD12(websocket, endp, _buffers, http.getTimeStamp(), _maxIdleTime, protocol,extensions,draft);
break;
case 13:
case 14:
extensions= initExtensions(extensions_requested,8-WebSocketConnectionD13.OP_EXT_DATA, 16-WebSocketConnectionD13.OP_EXT_CTRL,3);
connection = new WebSocketConnectionD13(websocket, endp, _buffers, http.getTimeStamp(), _maxIdleTime, protocol,extensions,draft);
break;
default:
LOG.warn("Unsupported Websocket version: "+draft);
response.setHeader("Sec-WebSocket-Version","0,6,12,13,14");
throw new HttpException(400, "Unsupported draft specification: " + draft);
}
@ -251,6 +253,8 @@ public class WebSocketFactory
request.setAttribute("org.eclipse.jetty.io.Connection", connection);
}
/**
*/
protected String[] parseProtocols(String protocol)
{
if (protocol == null)
@ -264,6 +268,8 @@ public class WebSocketFactory
return protocols;
}
/**
*/
public boolean acceptWebSocket(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
@ -280,24 +286,34 @@ public class WebSocketFactory
// Try each requested protocol
WebSocket websocket = null;
String protocol = request.getHeader("Sec-WebSocket-Protocol");
if (protocol == null) // TODO remove once draft period is over
protocol = request.getHeader("WebSocket-Protocol");
for (String p : parseProtocols(protocol))
Enumeration<String> protocols = request.getHeaders("Sec-WebSocket-Protocol");
String protocol=null;
while (protocol==null && protocols!=null && protocols.hasMoreElements())
{
websocket = _acceptor.doWebSocketConnect(request, p);
if (websocket != null)
String candidate = protocols.nextElement();
for (String p : parseProtocols(candidate))
{
protocol = p;
break;
websocket = _acceptor.doWebSocketConnect(request, p);
if (websocket != null)
{
protocol = p;
break;
}
}
}
// Did we get a websocket?
if (websocket == null)
{
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return false;
// Try with no protocol
websocket = _acceptor.doWebSocketConnect(request, null);
if (websocket==null)
{
response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
return false;
}
}
// Send the upgrade
@ -308,6 +324,8 @@ public class WebSocketFactory
return false;
}
/**
*/
public List<Extension> initExtensions(List<String> requested,int maxDataOpcodes,int maxControlOpcodes,int maxReservedBits)
{
List<Extension> extensions = new ArrayList<Extension>();
@ -339,6 +357,8 @@ public class WebSocketFactory
return extensions;
}
/**
*/
private Extension newExtension(String name)
{
try
@ -354,6 +374,4 @@ public class WebSocketFactory
return null;
}
}

View File

@ -84,6 +84,7 @@ public class WebSocketMessageD00Test
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input, "ISO-8859-1"));
String responseLine = reader.readLine();
System.err.println(responseLine);
assertTrue(responseLine.startsWith("HTTP/1.1 101 WebSocket Protocol Handshake"));
// Read until we find an empty line, which signals the end of the http response
String line;

View File

@ -998,7 +998,7 @@ public class WebSocketMessageD13Test
assertEquals(0x80|WebSocketConnectionD13.OP_CLOSE,input.read());
assertEquals(15,input.read());
int code=(0xff&input.read())*0x100+(0xff&input.read());
assertEquals(WebSocketConnectionD13.CLOSE_NOT_UTF8,code);
assertEquals(WebSocketConnectionD13.CLOSE_BAD_PAYLOAD,code);
lookFor("Invalid UTF-8",input);
}