tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ma...@apache.org
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 GMT
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


Mime
View raw message