Return-Path: X-Original-To: apmail-tomcat-dev-archive@www.apache.org Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 78D2A9019 for ; Thu, 23 Feb 2012 00:42:30 +0000 (UTC) Received: (qmail 3231 invoked by uid 500); 23 Feb 2012 00:42:29 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 3172 invoked by uid 500); 23 Feb 2012 00:42:29 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 3163 invoked by uid 99); 23 Feb 2012 00:42:29 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Feb 2012 00:42:29 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 23 Feb 2012 00:42:26 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B51F823888E7 for ; Thu, 23 Feb 2012 00:42:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1292602 - in /tomcat/trunk/java/org/apache/catalina/websocket: MessageInbound.java StreamInbound.java WsOutbound.java Date: Thu, 23 Feb 2012 00:42:05 -0000 To: dev@tomcat.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120223004205.B51F823888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: markt Date: Thu Feb 23 00:42:05 2012 New Revision: 1292602 URL: http://svn.apache.org/viewvc?rev=1292602&view=rev Log: Tighten up UTF-8 handling Modified: tomcat/trunk/java/org/apache/catalina/websocket/MessageInbound.java tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java Modified: tomcat/trunk/java/org/apache/catalina/websocket/MessageInbound.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/MessageInbound.java?rev=1292602&r1=1292601&r2=1292602&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/MessageInbound.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/MessageInbound.java Thu Feb 23 00:42:05 2012 @@ -54,6 +54,7 @@ public abstract class MessageInbound ext if (cb.remaining() == 0) { resizeCharBuffer(); } + // TODO This should fail on invalid UTF-8 input but doesn't read = r.read(cb.array(), cb.position(), cb.remaining()); } cb.flip(); Modified: tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java?rev=1292602&r1=1292601&r2=1292602&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/StreamInbound.java Thu Feb 23 00:42:05 2012 @@ -20,6 +20,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.nio.charset.CodingErrorAction; +import java.nio.charset.MalformedInputException; +import java.nio.charset.UnmappableCharacterException; import org.apache.coyote.http11.upgrade.UpgradeInbound; import org.apache.coyote.http11.upgrade.UpgradeOutbound; @@ -68,8 +71,10 @@ public abstract class StreamInbound impl onBinaryData(wsIs); return SocketState.UPGRADED; } else if (opCode == Constants.OPCODE_TEXT) { - InputStreamReader r = - new InputStreamReader(wsIs, B2CConverter.UTF_8); + InputStreamReader r = new InputStreamReader(wsIs, + B2CConverter.UTF_8.newDecoder() + .onMalformedInput(CodingErrorAction.REPORT) + .onUnmappableCharacter(CodingErrorAction.REPORT)); onTextData(r); return SocketState.UPGRADED; } @@ -88,6 +93,14 @@ public abstract class StreamInbound impl // Unknown OpCode getOutbound().close(1002, null); return SocketState.CLOSED; + } catch (MalformedInputException mie) { + // Invalid UTF-8 + getOutbound().close(1007, null); + return SocketState.CLOSED; + } catch (UnmappableCharacterException uce) { + // Invalid UTF-8 + getOutbound().close(1007, null); + return SocketState.CLOSED; } catch (IOException ioe) { // Given something must have gone to reach this point, this might // not work but try it anyway. Modified: tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java?rev=1292602&r1=1292601&r2=1292602&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java (original) +++ tomcat/trunk/java/org/apache/catalina/websocket/WsOutbound.java Thu Feb 23 00:42:05 2012 @@ -19,6 +19,8 @@ package org.apache.catalina.websocket; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.CharBuffer; +import java.nio.charset.CharsetEncoder; +import java.nio.charset.CoderResult; import org.apache.coyote.http11.upgrade.UpgradeOutbound; import org.apache.tomcat.util.buf.B2CConverter; @@ -121,8 +123,6 @@ public class WsOutbound { } closed = true; - doFlush(true); - upgradeOutbound.write(0x88); if (status == 0) { upgradeOutbound.write(0); @@ -174,6 +174,10 @@ public class WsOutbound { private void doWriteBytes(ByteBuffer buffer, boolean finalFragment) throws IOException { + if (closed) { + throw new IOException("Closed"); + } + // Work out the first byte int first = 0x00; if (finalFragment) { @@ -226,8 +230,12 @@ public class WsOutbound { private void doWriteText(CharBuffer buffer, boolean finalFragment) throws IOException { + CharsetEncoder encoder = B2CConverter.UTF_8.newEncoder(); do { - B2CConverter.UTF_8.newEncoder().encode(buffer, bb, true); + CoderResult cr = encoder.encode(buffer, bb, true); + if (cr.isError()) { + cr.throwException(); + } bb.flip(); if (buffer.hasRemaining()) { doWriteBytes(bb, false); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org For additional commands, e-mail: dev-help@tomcat.apache.org