You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2015/03/26 16:28:56 UTC

svn commit: r1669353 - in /tomcat/trunk: java/org/apache/tomcat/websocket/WsWebSocketContainer.java test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java test/org/apache/tomcat/websocket/TesterEchoServer.java

Author: remm
Date: Thu Mar 26 15:28:55 2015
New Revision: 1669353

URL: http://svn.apache.org/r1669353
Log:
57761: Avoid using an empty path in the HTTP request when the websocket client connects to a server root.

Modified:
    tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
    tomcat/trunk/test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java
    tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java

Modified: tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java?rev=1669353&r1=1669352&r2=1669353&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/websocket/WsWebSocketContainer.java Thu Mar 26 15:28:55 2015
@@ -501,7 +501,10 @@ public class WsWebSocketContainer implem
 
         // Request line
         result.put("GET ".getBytes(StandardCharsets.ISO_8859_1));
-        result.put(uri.getRawPath().getBytes(StandardCharsets.ISO_8859_1));
+        byte[] path = (null == uri.getPath() || "".equals(uri.getPath()))
+                ? "/".getBytes(StandardCharsets.ISO_8859_1)
+                : uri.getRawPath().getBytes(StandardCharsets.ISO_8859_1);
+        result.put(path);
         String query = uri.getRawQuery();
         if (query != null) {
             result.put((byte) '?');

Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java?rev=1669353&r1=1669352&r2=1669353&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TestWebSocketFrameClient.java Thu Mar 26 15:28:55 2015
@@ -80,4 +80,55 @@ public class TestWebSocketFrameClient ex
         }
     }
 
+    @Test
+    public void testConnectToRootEndpoint() throws Exception {
+
+        Tomcat tomcat = getTomcatInstance();
+        // No file system docBase required
+        Context ctx = tomcat.addContext("", null);
+        ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
+        Tomcat.addServlet(ctx, "default", new DefaultServlet());
+        ctx.addServletMapping("/", "default");
+        Context ctx2 = tomcat.addContext("/foo", null);
+        ctx2.addApplicationListener(TesterEchoServer.Config.class.getName());
+        Tomcat.addServlet(ctx2, "default", new DefaultServlet());
+        ctx2.addServletMapping("/", "default");
+
+        tomcat.start();
+
+        echoTester("");
+        echoTester("/");
+        // FIXME: The ws client doesn't handle any response other than the upgrade,
+        // which may or may not be allowed. In that case, the server will return
+        // a redirect to the root of the webapp to avoid possible broken relative
+        // paths.
+        // echoTester("/foo");
+        echoTester("/foo/");
+    }
+
+    public void echoTester(String path) throws Exception {
+        WebSocketContainer wsContainer =
+                ContainerProvider.getWebSocketContainer();
+        ClientEndpointConfig clientEndpointConfig =
+                ClientEndpointConfig.Builder.create().build();
+        Session wsSession = wsContainer.connectToServer(
+                TesterProgrammaticEndpoint.class,
+                clientEndpointConfig,
+                new URI("ws://localhost:" + getPort() + path));
+        CountDownLatch latch =
+                new CountDownLatch(1);
+        BasicText handler = new BasicText(latch);
+        wsSession.addMessageHandler(handler);
+        wsSession.getBasicRemote().sendText("Hello");
+
+        handler.getLatch().await(100, TimeUnit.MILLISECONDS);
+
+        Queue<String> messages = handler.getMessages();
+        Assert.assertEquals(1, messages.size());
+        for (String message : messages) {
+            Assert.assertEquals("Hello", message);
+        }
+        wsSession.close();
+    }
+
 }

Modified: tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java?rev=1669353&r1=1669352&r2=1669353&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java (original)
+++ tomcat/trunk/test/org/apache/tomcat/websocket/TesterEchoServer.java Thu Mar 26 15:28:55 2015
@@ -51,6 +51,7 @@ public class TesterEchoServer {
                 sc.addEndpoint(BasicLimitLow.class);
                 sc.addEndpoint(BasicLimitHigh.class);
                 sc.addEndpoint(WriterError.class);
+                sc.addEndpoint(RootEcho.class);
             } catch (DeploymentException e) {
                 throw new IllegalStateException(e);
             }
@@ -204,6 +205,23 @@ public class TesterEchoServer {
                 try {
                     session.close();
                 } catch (IOException e1) {
+                    // Ignore
+                }
+            }
+        }
+    }
+
+    @ServerEndpoint("/")
+    public static class RootEcho {
+
+        @OnMessage
+        public void echoTextMessage(Session session, @SuppressWarnings("unused") String msg) {
+            try {
+                session.getBasicRemote().sendText(msg);
+            } catch (IOException e) {
+                try {
+                    session.close();
+                } catch (IOException e1) {
                     // Ignore
                 }
             }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org