Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion java/org/apache/tomcat/websocket/server/UpgradeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ public static void doUpgrade(WsServerContainer sc, HttpServletRequest req, HttpS
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
if (!headerContainsToken(req, Constants.WS_VERSION_HEADER_NAME, Constants.WS_VERSION_HEADER_VALUE)) {
// RFC 6455, sections 11.3.1 and 11.3.5: these header fields must not appear more than once in a request
if (countHeaders(req, Constants.WS_VERSION_HEADER_NAME) > 1 ||
countHeaders(req, Constants.WS_KEY_HEADER_NAME) > 1) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
String version = req.getHeader(Constants.WS_VERSION_HEADER_NAME);
if (version == null || !Constants.WS_VERSION_HEADER_VALUE.equals(version.trim())) {
resp.setStatus(HttpServletResponse.SC_UPGRADE_REQUIRED);
resp.setHeader(Constants.WS_VERSION_HEADER_NAME, Constants.WS_VERSION_HEADER_VALUE);
return;
Expand Down Expand Up @@ -323,6 +330,11 @@ private static void append(StringBuilder sb, Extension extension) {
}


private static int countHeaders(HttpServletRequest req, String headerName) {
return Collections.list(req.getHeaders(headerName)).size();
}


/*
* This only works for tokens. Quoted strings need more sophisticated parsing.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.websocket.server;

import java.nio.charset.StandardCharsets;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.websocket.CloseReason.CloseCodes;

import org.junit.Assert;
import org.junit.Test;

import org.apache.tomcat.websocket.TesterEchoServer;
import org.apache.tomcat.websocket.WebSocketBaseTest;

/**
* RFC 6455, sections 4.2.1, 11.3.1 and 11.3.5: the Sec-WebSocket-Key and Sec-WebSocket-Version header fields must
* appear once in the opening handshake request and the version must be 13.
*/
public class TestUpgradeSingleValueHeaders extends WebSocketBaseTest {

private static final String KEY = "Sec-WebSocket-Key: MDEyMzQ1Njc4OTAxMjM0NQ==\r\n";
private static final String VERSION = "Sec-WebSocket-Version: 13\r\n";


@Test
public void testValid() throws Exception {
doTest(KEY + VERSION, HttpServletResponse.SC_SWITCHING_PROTOCOLS);
}


@Test
public void testDuplicateKey() throws Exception {
doTest(KEY + KEY + VERSION, HttpServletResponse.SC_BAD_REQUEST);
}


@Test
public void testDuplicateVersion() throws Exception {
doTest(KEY + VERSION + VERSION, HttpServletResponse.SC_BAD_REQUEST);
}


@Test
public void testVersionList() throws Exception {
doTest(KEY + "Sec-WebSocket-Version: 8, 13\r\n", HttpServletResponse.SC_UPGRADE_REQUIRED);
}


@Test
public void testUnsupportedVersion() throws Exception {
doTest(KEY + "Sec-WebSocket-Version: 8\r\n", HttpServletResponse.SC_UPGRADE_REQUIRED);
}


private void doTest(String keyAndVersionHeaders, int expectedStatusCode) throws Exception {
startServer(TesterEchoServer.Config.class);

TesterWsClient client = new TesterWsClient("localhost", getPort());
// Replace the key and version header lines of the default request
String req = client.createUpgradeRequest(TesterEchoServer.Config.PATH_BASIC)
.replaceAll("Sec-WebSocket-(Key|Version): [^\r]*\r\n", "")
.replace("Upgrade: websocket\r\n", "Upgrade: websocket\r\n" + keyAndVersionHeaders);
client.write(req.getBytes(StandardCharsets.UTF_8));
int rc = client.readUpgradeResponse();

Assert.assertEquals(expectedStatusCode, rc);

if (expectedStatusCode == HttpServletResponse.SC_SWITCHING_PROTOCOLS) {
client.sendCloseFrame(CloseCodes.NORMAL_CLOSURE);
// Read (and ignore) the response
byte[] buf = new byte[256];
while (client.read(buf) > 0) {
// Ignore
}
}
client.closeSocket();
}
}
Loading