Author: markt Date: Mon Dec 16 19:20:17 2013 New Revision: 1551320 URL: http://svn.apache.org/r1551320 Log: Better handling if no message handler is configured. Message is just ignored.
Modified: tomcat/tc7.0.x/trunk/ (props changed) tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Propchange: tomcat/tc7.0.x/trunk/ ------------------------------------------------------------------------------ Merged /tomcat/trunk:r1551298 Modified: tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java?rev=1551320&r1=1551319&r2=1551320&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/tomcat/websocket/WsFrameBase.java Mon Dec 16 19:20:17 2013 @@ -269,9 +269,17 @@ public abstract class WsFrameBase { if (Util.isControl(opCode)) { result = processDataControl(); } else if (textMessage) { - result = processDataText(); + if (textMsgHandler == null) { + result = swallowInput(); + } else { + result = processDataText(); + } } else { - result = processDataBinary(); + if (binaryMsgHandler == null) { + result = swallowInput(); + } else { + result = processDataBinary(); + } } checkRoomPayload(); return result; @@ -347,34 +355,32 @@ public abstract class WsFrameBase { @SuppressWarnings("unchecked") private void sendMessageText(boolean last) throws WsIOException { - if (textMsgHandler != null) { - if (textMsgHandler instanceof WrappedMessageHandler) { - long maxMessageSize = - ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize(); - if (maxMessageSize > -1 && - messageBufferText.remaining() > maxMessageSize) { - throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, - sm.getString("wsFrame.messageTooBig", - Long.valueOf(messageBufferText.remaining()), - Long.valueOf(maxMessageSize)))); - } + if (textMsgHandler instanceof WrappedMessageHandler) { + long maxMessageSize = + ((WrappedMessageHandler) textMsgHandler).getMaxMessageSize(); + if (maxMessageSize > -1 && + messageBufferText.remaining() > maxMessageSize) { + throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, + sm.getString("wsFrame.messageTooBig", + Long.valueOf(messageBufferText.remaining()), + Long.valueOf(maxMessageSize)))); } + } - try { - if (textMsgHandler instanceof MessageHandler.Partial<?>) { - ((MessageHandler.Partial<String>) textMsgHandler).onMessage( - messageBufferText.toString(), last); - } else { - // Caller ensures last == true if this branch is used - ((MessageHandler.Whole<String>) textMsgHandler).onMessage( - messageBufferText.toString()); - } - } catch (Throwable t) { - ExceptionUtils.handleThrowable(t); - wsSession.getLocal().onError(wsSession, t); - } finally { - messageBufferText.clear(); - } + try { + if (textMsgHandler instanceof MessageHandler.Partial<?>) { + ((MessageHandler.Partial<String>) textMsgHandler).onMessage( + messageBufferText.toString(), last); + } else { + // Caller ensures last == true if this branch is used + ((MessageHandler.Whole<String>) textMsgHandler).onMessage( + messageBufferText.toString()); + } + } catch (Throwable t) { + ExceptionUtils.handleThrowable(t); + wsSession.getLocal().onError(wsSession, t); + } finally { + messageBufferText.clear(); } } @@ -530,28 +536,26 @@ public abstract class WsFrameBase { @SuppressWarnings("unchecked") private void sendMessageBinary(ByteBuffer msg, boolean last) throws WsIOException { - if (binaryMsgHandler != null) { - if (binaryMsgHandler instanceof WrappedMessageHandler) { - long maxMessageSize = - ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize(); - if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) { - throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, - sm.getString("wsFrame.messageTooBig", - Long.valueOf(msg.remaining()), - Long.valueOf(maxMessageSize)))); - } + if (binaryMsgHandler instanceof WrappedMessageHandler) { + long maxMessageSize = + ((WrappedMessageHandler) binaryMsgHandler).getMaxMessageSize(); + if (maxMessageSize > -1 && msg.remaining() > maxMessageSize) { + throw new WsIOException(new CloseReason(CloseCodes.TOO_BIG, + sm.getString("wsFrame.messageTooBig", + Long.valueOf(msg.remaining()), + Long.valueOf(maxMessageSize)))); } - try { - if (binaryMsgHandler instanceof MessageHandler.Partial<?>) { - ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last); - } else { - // Caller ensures last == true if this branch is used - ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); - } - } catch(Throwable t) { - ExceptionUtils.handleThrowable(t); - wsSession.getLocal().onError(wsSession, t); + } + try { + if (binaryMsgHandler instanceof MessageHandler.Partial<?>) { + ((MessageHandler.Partial<ByteBuffer>) binaryMsgHandler).onMessage(msg, last); + } else { + // Caller ensures last == true if this branch is used + ((MessageHandler.Whole<ByteBuffer>) binaryMsgHandler).onMessage(msg); } + } catch(Throwable t) { + ExceptionUtils.handleThrowable(t); + wsSession.getLocal().onError(wsSession, t); } } @@ -611,16 +615,10 @@ public abstract class WsFrameBase { if (Util.isControl(opCode)) { return false; } else if (textMessage) { - if (textMsgHandler != null) { - return textMsgHandler instanceof MessageHandler.Partial<?>; - } - return false; + return textMsgHandler instanceof MessageHandler.Partial<?>; } else { // Must be binary - if (binaryMsgHandler != null) { - return binaryMsgHandler instanceof MessageHandler.Partial<?>; - } - return false; + return binaryMsgHandler instanceof MessageHandler.Partial<?>; } } @@ -653,6 +651,23 @@ public abstract class WsFrameBase { } + private boolean swallowInput() { + long toSkip = Math.min(payloadLength - payloadWritten, writePos - readPos); + readPos += toSkip; + payloadWritten += toSkip; + if (payloadWritten == payloadLength) { + if (continuationExpected) { + newFrame(); + } else { + newMessage(); + } + return true; + } else { + return false; + } + } + + protected static long byteArrayToLong(byte[] b, int start, int len) throws IOException { if (len > 8) { Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1551320&r1=1551319&r2=1551320&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Mon Dec 16 19:20:17 2013 @@ -68,6 +68,10 @@ Fix string comparison in <code>HostConfig.setContextClass()</code>. (kkolinko) </fix> + <scode> + Streamline handling of WebSocket messages whe no handler is configured + for the message currently being received. (markt) + </scode> </changelog> </subsection> <subsection name="Cluster"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org