427699 - WebSocket upgrade response sends Sec-WebSocket-Protocol twice.

The problem was that UpgradeResponse was setting the subProtocol as a
header, and HandshakeRFC6455 was extracting it and *adding* it again.

Fixed by removing the code in HandshakeRFC6455.
This commit is contained in:
Simone Bordet 2014-02-10 14:43:22 +01:00
parent b37c68db3b
commit 51e8b83954
4 changed files with 112 additions and 19 deletions

View File

@ -49,11 +49,6 @@ public class HandshakeRFC6455 implements WebSocketHandshake
response.addHeader("Connection","Upgrade");
response.addHeader("Sec-WebSocket-Accept",AcceptHash.hashKey(key));
if (response.getAcceptedSubProtocol() != null)
{
response.addHeader("Sec-WebSocket-Protocol",response.getAcceptedSubProtocol());
}
if (response.getExtensions() != null)
{
String value = ExtensionConfig.toHeaderValue(response.getExtensions());

View File

@ -0,0 +1,91 @@
//
// ========================================================================
// Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.websocket.server;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.util.B64Code;
import org.eclipse.jetty.websocket.server.browser.BrowserDebugTool;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class WebSocketProtocolTest
{
private BrowserDebugTool server;
@Before
public void startServer() throws Exception
{
server = new BrowserDebugTool();
server.prepare(0);
server.start();
}
@After
public void stopServer() throws Exception
{
server.stop();
}
@Test
public void testWebSocketProtocolResponse() throws Exception
{
try (Socket client = new Socket("localhost", server.getPort()))
{
byte[] key = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
StringBuilder request = new StringBuilder();
request.append("GET / HTTP/1.1\r\n")
.append("Host: localhost\r\n")
.append("Connection: Upgrade\r\n")
.append("Upgrade: websocket\r\n")
.append("Sec-WebSocket-version: 13\r\n")
.append("Sec-WebSocket-Key:").append(B64Code.encode(key)).append("\r\n")
.append("Sec-WebSocket-Protocol: echo\r\n")
.append("\r\n");
OutputStream output = client.getOutputStream();
output.write(request.toString().getBytes(StandardCharsets.UTF_8));
output.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = input.readLine();
Assert.assertTrue(line.contains(" 101 "));
HttpFields fields = new HttpFields();
while ((line = input.readLine()) != null)
{
if (line.isEmpty())
break;
int colon = line.indexOf(':');
Assert.assertTrue(colon > 0);
String name = line.substring(0, colon).trim();
String value = line.substring(colon + 1).trim();
fields.add(name, value);
}
Assert.assertEquals(1, fields.getValuesList("Sec-WebSocket-Protocol").size());
}
}
}

View File

@ -55,8 +55,8 @@ public class BrowserDebugTool implements WebSocketCreator
try
{
BrowserDebugTool tool = new BrowserDebugTool();
tool.setupServer(port);
tool.runForever();
tool.prepare(port);
tool.start();
}
catch (Throwable t)
{
@ -65,6 +65,7 @@ public class BrowserDebugTool implements WebSocketCreator
}
private Server server;
private ServerConnector connector;
@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp)
@ -84,21 +85,24 @@ public class BrowserDebugTool implements WebSocketCreator
String rexts = req.getHeader("Sec-WebSocket-Extensions");
LOG.debug("User-Agent: {}", ua);
LOG.debug("Sec-WebSocket-Extensions: {}", rexts);
BrowserSocket socket = new BrowserSocket(ua,rexts);
return socket;
return new BrowserSocket(ua,rexts);
}
private void runForever() throws Exception
public void start() throws Exception
{
server.start();
LOG.info("Server available.");
server.join();
LOG.info("Server available on port {}", getPort());
}
private void setupServer(int port)
public void stop() throws Exception
{
server.stop();
}
public void prepare(int port)
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector = new ServerConnector(server);
connector.setPort(port);
server.addConnector(connector);
@ -128,4 +132,9 @@ public class BrowserDebugTool implements WebSocketCreator
LOG.info("{} setup on port {}",this.getClass().getName(),port);
}
public int getPort()
{
return connector.getLocalPort();
}
}

View File

@ -76,7 +76,6 @@ public class BrowserSocket
private static final Logger LOG = Log.getLogger(BrowserSocket.class);
private Session session;
private RemoteEndpoint remote;
private final String userAgent;
private final String requestedExtensions;
@ -91,7 +90,6 @@ public class BrowserSocket
{
LOG.info("Connect [{}]",session);
this.session = session;
this.remote = session.getRemote();
}
@OnWebSocketClose
@ -162,7 +160,7 @@ public class BrowserSocket
// Setup threads
for (int n = 0; n < threadCount; n++)
{
threads[n] = new Thread(new WriteMany(remote,size,count),"WriteMany[" + n + "]");
threads[n] = new Thread(new WriteMany(session.getRemote(),size,count),"WriteMany[" + n + "]");
}
// Execute threads
@ -220,14 +218,14 @@ public class BrowserSocket
return;
}
if (session.isOpen() == false)
if (!session.isOpen())
{
LOG.debug("Not open");
return;
}
// Async write
remote.sendString(message,null);
session.getRemote().sendString(message, null);
}
private void writeMessage(String format, Object... args)