Merge remote-tracking branch 'origin/jetty-10.0.x' into jetty-11.0.x

This commit is contained in:
Lachlan Roberts 2021-01-13 15:04:45 +11:00
commit 17177abfd7
7 changed files with 236 additions and 18 deletions

View File

@ -35,7 +35,7 @@ import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation;
public final class RFC6455Handshaker extends AbstractHandshaker
{
private static final HttpField UPGRADE_WEBSOCKET = new PreEncodedHttpField(HttpHeader.UPGRADE, "WebSocket");
private static final HttpField UPGRADE_WEBSOCKET = new PreEncodedHttpField(HttpHeader.UPGRADE, "websocket");
private static final HttpField CONNECTION_UPGRADE = new PreEncodedHttpField(HttpHeader.CONNECTION, HttpHeader.UPGRADE.asString());
@Override

View File

@ -332,7 +332,7 @@ public class WebSocketNegotiationTest extends WebSocketTester
// RFC6455: If the server does not agree to any of the client's requested subprotocols, the only acceptable
// value is null. It MUST NOT send back a |Sec-WebSocket-Protocol| header field in its response.
HttpFields httpFields = headers.get();
assertThat(httpFields.get(HttpHeader.UPGRADE), is("WebSocket"));
assertThat(httpFields.get(HttpHeader.UPGRADE), is("websocket"));
assertNull(httpFields.get(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL));
}

View File

@ -16,8 +16,6 @@ package org.eclipse.jetty.websocket.jakarta.client;
import java.net.URI;
import java.security.Principal;
import org.eclipse.jetty.client.HttpResponse;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
@ -34,13 +32,6 @@ public class JakartaClientUpgradeRequest extends CoreClientUpgradeRequest implem
frameHandler = clientContainer.newFrameHandler(websocketPojo, this);
}
@Override
public void upgrade(HttpResponse response, EndPoint endPoint)
{
frameHandler.setUpgradeRequest(this);
super.upgrade(response, endPoint);
}
@Override
public FrameHandler getFrameHandler()
{

View File

@ -68,7 +68,7 @@ public class JakartaWebSocketFrameHandler implements FrameHandler
private MethodHandle pongHandle;
private JakartaWebSocketMessageMetadata textMetadata;
private JakartaWebSocketMessageMetadata binaryMetadata;
private UpgradeRequest upgradeRequest;
private final UpgradeRequest upgradeRequest;
private EndpointConfig endpointConfig;
private final Map<Byte, RegisteredMessageHandler> messageHandlerMap = new HashMap<>();
private MessageSink textSink;
@ -79,6 +79,7 @@ public class JakartaWebSocketFrameHandler implements FrameHandler
protected byte dataType = OpCode.UNDEFINED;
public JakartaWebSocketFrameHandler(JakartaWebSocketContainer container,
UpgradeRequest upgradeRequest,
Object endpointInstance,
MethodHandle openHandle, MethodHandle closeHandle, MethodHandle errorHandle,
JakartaWebSocketMessageMetadata textMetadata,
@ -89,6 +90,7 @@ public class JakartaWebSocketFrameHandler implements FrameHandler
this.logger = LoggerFactory.getLogger(endpointInstance.getClass());
this.container = container;
this.upgradeRequest = upgradeRequest;
if (endpointInstance instanceof ConfiguredEndpoint)
{
RuntimeException oops = new RuntimeException("ConfiguredEndpoint needs to be unwrapped");
@ -96,7 +98,6 @@ public class JakartaWebSocketFrameHandler implements FrameHandler
throw oops;
}
this.endpointInstance = endpointInstance;
this.openHandle = openHandle;
this.closeHandle = closeHandle;
this.errorHandle = errorHandle;
@ -636,11 +637,6 @@ public class JakartaWebSocketFrameHandler implements FrameHandler
}
}
public void setUpgradeRequest(UpgradeRequest upgradeRequest)
{
this.upgradeRequest = upgradeRequest;
}
public UpgradeRequest getUpgradeRequest()
{
return upgradeRequest;

View File

@ -167,6 +167,7 @@ public abstract class JakartaWebSocketFrameHandlerFactory
return new JakartaWebSocketFrameHandler(
container,
upgradeRequest,
endpoint,
openHandle, closeHandle, errorHandle,
textMetadata, binaryMetadata,

View File

@ -0,0 +1,104 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.jakarta.tests;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import jakarta.websocket.ClientEndpoint;
import jakarta.websocket.ClientEndpointConfig;
import jakarta.websocket.Session;
import jakarta.websocket.server.ServerEndpoint;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.jakarta.client.JakartaWebSocketClientContainer;
import org.eclipse.jetty.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UpgradeRequestResponseTest
{
private ServerConnector connector;
private JakartaWebSocketClientContainer client;
private static CompletableFuture<EventSocket> serverSocketFuture;
@ServerEndpoint("/")
public static class ServerSocket extends EchoSocket
{
public ServerSocket()
{
serverSocketFuture.complete(this);
}
}
public static class PermessageDeflateConfig extends ClientEndpointConfig.Configurator
{
@Override
public void beforeRequest(Map<String, List<String>> headers)
{
headers.put(HttpHeader.SEC_WEBSOCKET_EXTENSIONS.asString(), Collections.singletonList("permessage-deflate"));
}
}
@ClientEndpoint(configurator = PermessageDeflateConfig.class)
public static class ClientSocket extends EventSocket
{
}
@BeforeEach
public void start() throws Exception
{
Server server = new Server();
connector = new ServerConnector(server);
server.addConnector(connector);
serverSocketFuture = new CompletableFuture<>();
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
server.setHandler(contextHandler);
contextHandler.setContextPath("/");
JakartaWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
container.addEndpoint(ServerSocket.class));
client = new JakartaWebSocketClientContainer();
server.start();
client.start();
}
@Test
public void testUpgradeRequestResponse() throws Exception
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
EventSocket socket = new ClientSocket();
Session clientSession = client.connectToServer(socket, uri);
EventSocket serverSocket = serverSocketFuture.get(5, TimeUnit.SECONDS);
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
// The user principal is found on the base UpgradeRequest.
assertDoesNotThrow(clientSession::getUserPrincipal);
assertDoesNotThrow(serverSocket.session::getUserPrincipal);
clientSession.close();
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
}
}

View File

@ -0,0 +1,126 @@
//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests;
import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.UpgradeRequest;
import org.eclipse.jetty.websocket.api.UpgradeResponse;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketServletContainerInitializer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class UpgradeRequestResponseTest
{
private ServerConnector connector;
private WebSocketClient client;
private EventSocket serverSocket;
@BeforeEach
public void start() throws Exception
{
Server server = new Server();
connector = new ServerConnector(server);
server.addConnector(connector);
serverSocket = new EchoSocket();
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
server.setHandler(contextHandler);
contextHandler.setContextPath("/");
JettyWebSocketServletContainerInitializer.configure(contextHandler, (context, container) ->
container.addMapping("/", (req, resp) -> serverSocket));
client = new WebSocketClient();
server.start();
client.start();
}
@Test
public void testClientUpgradeRequestResponse() throws Exception
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
EventSocket socket = new EventSocket();
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.addExtensions("permessage-deflate");
CompletableFuture<Session> connect = client.connect(socket, uri, request);
Session session = connect.get(5, TimeUnit.SECONDS);
UpgradeRequest upgradeRequest = session.getUpgradeRequest();
UpgradeResponse upgradeResponse = session.getUpgradeResponse();
assertNotNull(upgradeRequest);
assertThat(upgradeRequest.getHeader(HttpHeader.UPGRADE.asString()), is("websocket"));
assertThat(upgradeRequest.getHeader(HttpHeader.SEC_WEBSOCKET_EXTENSIONS.asString()), is("permessage-deflate"));
assertThat(upgradeRequest.getExtensions().size(), is(1));
assertThat(upgradeRequest.getExtensions().get(0).getName(), is("permessage-deflate"));
assertNotNull(upgradeResponse);
assertThat(upgradeResponse.getStatusCode(), is(HttpStatus.SWITCHING_PROTOCOLS_101));
assertThat(upgradeResponse.getHeader(HttpHeader.UPGRADE.asString()), is("websocket"));
assertThat(upgradeResponse.getHeader(HttpHeader.SEC_WEBSOCKET_EXTENSIONS.asString()), is("permessage-deflate"));
assertThat(upgradeResponse.getExtensions().size(), is(1));
assertThat(upgradeResponse.getExtensions().get(0).getName(), is("permessage-deflate"));
session.close();
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
}
@Test
public void testServerUpgradeRequestResponse() throws Exception
{
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
EventSocket socket = new EventSocket();
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.addExtensions("permessage-deflate");
CompletableFuture<Session> connect = client.connect(socket, uri, request);
Session session = connect.get(5, TimeUnit.SECONDS);
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));
UpgradeRequest upgradeRequest = serverSocket.session.getUpgradeRequest();
UpgradeResponse upgradeResponse = serverSocket.session.getUpgradeResponse();
assertNotNull(upgradeRequest);
assertThat(upgradeRequest.getHeader(HttpHeader.UPGRADE.asString()), is("websocket"));
assertThat(upgradeRequest.getHeader(HttpHeader.SEC_WEBSOCKET_EXTENSIONS.asString()), is("permessage-deflate"));
assertThat(upgradeRequest.getExtensions().size(), is(1));
assertThat(upgradeRequest.getExtensions().get(0).getName(), is("permessage-deflate"));
assertNotNull(upgradeResponse);
/* TODO: The HttpServletResponse is eventually recycled so we lose this information.
assertThat(upgradeResponse.getStatusCode(), is(HttpStatus.SWITCHING_PROTOCOLS_101));
assertThat(upgradeResponse.getHeader(HttpHeader.UPGRADE.asString()), is("websocket"));
assertThat(upgradeResponse.getHeader(HttpHeader.SEC_WEBSOCKET_EXTENSIONS.asString()), is("permessage-deflate"));
assertThat(upgradeResponse.getExtensions().size(), is(1));
assertThat(upgradeResponse.getExtensions().get(0).getName(), is("permessage-deflate"));
*/
session.close();
assertTrue(socket.closeLatch.await(5, TimeUnit.SECONDS));
}
}