Skip to content

Commit

Permalink
Merge pull request #5101 from eclipse/jetty-10.0.x-5044-WebSocketUpgr…
Browse files Browse the repository at this point in the history
…adeRequestResponse

Issue #5044 - Refactor of Jetty WebSocket Upgrade Request/Response
  • Loading branch information
lachlan-roberts authored Aug 5, 2020
2 parents 437e2be + fd5c07e commit 1e7ce3a
Show file tree
Hide file tree
Showing 39 changed files with 600 additions and 1,050 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class ClientUpgradeRequest extends HttpRequest implements Response.CompleteListener, HttpUpgrader.Factory
public abstract class CoreClientUpgradeRequest extends HttpRequest implements Response.CompleteListener, HttpUpgrader.Factory
{
public static ClientUpgradeRequest from(WebSocketCoreClient webSocketClient, URI requestURI, FrameHandler frameHandler)
public static CoreClientUpgradeRequest from(WebSocketCoreClient webSocketClient, URI requestURI, FrameHandler frameHandler)
{
return new ClientUpgradeRequest(webSocketClient, requestURI)
return new CoreClientUpgradeRequest(webSocketClient, requestURI)
{
@Override
public FrameHandler getFrameHandler()
Expand All @@ -75,15 +75,15 @@ public FrameHandler getFrameHandler()
};
}

private static final Logger LOG = LoggerFactory.getLogger(ClientUpgradeRequest.class);
private static final Logger LOG = LoggerFactory.getLogger(CoreClientUpgradeRequest.class);
protected final CompletableFuture<CoreSession> futureCoreSession;
private final WebSocketCoreClient wsClient;
private FrameHandler frameHandler;
private final Configuration.ConfigurationCustomizer customizer = new Configuration.ConfigurationCustomizer();
private final List<UpgradeListener> upgradeListeners = new ArrayList<>();
private List<ExtensionConfig> requestedExtensions = new ArrayList<>();

public ClientUpgradeRequest(WebSocketCoreClient webSocketClient, URI requestURI)
public CoreClientUpgradeRequest(WebSocketCoreClient webSocketClient, URI requestURI)
{
super(webSocketClient.getHttpClient(), new HttpConversation(), requestURI);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ public class HttpUpgraderOverHTTP implements HttpUpgrader
private static final PreEncodedHttpField WS_CONNECTION_FIELD = new PreEncodedHttpField(HttpHeader.CONNECTION, "Upgrade");
private static final PreEncodedHttpField PRAGMA_NO_CACHE_FIELD = new PreEncodedHttpField(HttpHeader.PRAGMA, "no-cache");
private static final PreEncodedHttpField CACHE_CONTROL_NO_CACHE_FIELD = new PreEncodedHttpField(HttpHeader.CACHE_CONTROL, "no-cache");
private final ClientUpgradeRequest clientUpgradeRequest;
private final CoreClientUpgradeRequest clientUpgradeRequest;

public HttpUpgraderOverHTTP(ClientUpgradeRequest clientUpgradeRequest)
public HttpUpgraderOverHTTP(CoreClientUpgradeRequest clientUpgradeRequest)
{
this.clientUpgradeRequest = clientUpgradeRequest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
public class HttpUpgraderOverHTTP2 implements HttpUpgrader
{
private static final PreEncodedHttpField WS_VERSION_FIELD = new PreEncodedHttpField(HttpHeader.SEC_WEBSOCKET_VERSION, WebSocketConstants.SPEC_VERSION_STRING);
private final ClientUpgradeRequest clientUpgradeRequest;
private final CoreClientUpgradeRequest clientUpgradeRequest;

public HttpUpgraderOverHTTP2(ClientUpgradeRequest clientUpgradeRequest)
public HttpUpgraderOverHTTP2(CoreClientUpgradeRequest clientUpgradeRequest)
{
this.clientUpgradeRequest = clientUpgradeRequest;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public WebSocketCoreClient(HttpClient httpClient, WebSocketComponents webSocketC

public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException
{
ClientUpgradeRequest request = ClientUpgradeRequest.from(this, wsUri, frameHandler);
CoreClientUpgradeRequest request = CoreClientUpgradeRequest.from(this, wsUri, frameHandler);
return connect(request);
}

public CompletableFuture<CoreSession> connect(ClientUpgradeRequest request) throws IOException
public CompletableFuture<CoreSession> connect(CoreClientUpgradeRequest request) throws IOException
{
if (!isStarted())
throw new IllegalStateException(WebSocketCoreClient.class.getSimpleName() + "@" + this.hashCode() + " is not started");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -165,7 +165,7 @@ public void testIncomingAutoFragmentToMaxFrameSize() throws Exception
public void testIncomingAutoFragmentWithPermessageDeflate() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate");
CompletableFuture<CoreSession> connect = client.connect(upgradeRequest);
connect.get(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -218,7 +218,7 @@ public void testIncomingAutoFragmentWithPermessageDeflate() throws Exception
public void testGzipBomb() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate");
CompletableFuture<CoreSession> connect = client.connect(upgradeRequest);
connect.get(5, TimeUnit.SECONDS);
Expand Down Expand Up @@ -281,7 +281,7 @@ public void testOutgoingAutoFragmentWithPermessageDeflate() throws Exception

// Connect to server with permessage-deflate enabled.
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate");
CompletableFuture<CoreSession> connect = client.connect(upgradeRequest);
connect.get(5, TimeUnit.SECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.websocket.core.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.UpgradeListener;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
import org.eclipse.jetty.websocket.core.exception.UpgradeException;
Expand Down Expand Up @@ -146,7 +146,7 @@ public void testExtensionSelection() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testExtensionSelection");
upgradeRequest.addExtensions("permessage-deflate;server_no_context_takeover", "permessage-deflate;client_no_context_takeover");

Expand Down Expand Up @@ -177,7 +177,7 @@ public void testNotOfferedParameter() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testNotOfferedParameter");
upgradeRequest.addExtensions("permessage-deflate;client_no_context_takeover");

Expand Down Expand Up @@ -207,7 +207,7 @@ public void testInvalidExtensionParameter() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testInvalidExtensionParameter");
upgradeRequest.addExtensions("permessage-deflate;invalid_parameter");

Expand All @@ -223,7 +223,7 @@ public void testNotAcceptingExtensions() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testNotAcceptingExtensions");
upgradeRequest.addExtensions("permessage-deflate;server_no_context_takeover", "permessage-deflate;client_no_context_takeover");

Expand Down Expand Up @@ -271,7 +271,7 @@ public void testAcceptTwoExtensionsOfSameName() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testAcceptTwoExtensionsOfSameName");
upgradeRequest.addExtensions("permessage-deflate;server_no_context_takeover", "permessage-deflate;client_no_context_takeover");

Expand Down Expand Up @@ -302,7 +302,7 @@ public void testSubProtocolNotOffered() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);

try (StacklessLogging stacklessLogging = new StacklessLogging(HttpChannel.class))
{
Expand All @@ -317,7 +317,7 @@ public void testSubProtocolNotOffered() throws Exception
public void testNoSubProtocolSelected() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("testNoSubProtocolSelected");
CompletableFuture<HttpFields> headers = new CompletableFuture<>();
upgradeRequest.addListener(new UpgradeListener()
Expand Down Expand Up @@ -378,7 +378,7 @@ public void testListenerExtensionSelection() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();

ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("test");

CompletableFuture<String> extensionHeader = new CompletableFuture<>();
Expand Down Expand Up @@ -409,7 +409,7 @@ public void onHandshakeResponse(HttpRequest request, HttpResponse response)
public void testListenerExtensionSelectionError() throws Exception
{
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("test");
upgradeRequest.addExtensions("permessage-deflate;server_no_context_takeover");

Expand Down Expand Up @@ -467,7 +467,7 @@ public void testClientRequestedInternalExtensions(String reqExts, String negExts

TestFrameHandler clientHandler = new TestFrameHandler();
CompletableFuture<String> extensionHeader = new CompletableFuture<>();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, server.getUri(), clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, server.getUri(), clientHandler);
upgradeRequest.setSubProtocols("test");
if (!StringUtil.isEmpty(reqExts))
upgradeRequest.addExtensions(reqExts.split(","));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.websocket.core.MessageHandler;
import org.eclipse.jetty.websocket.core.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -51,7 +51,7 @@ public ChatWebSocketClient(String hostname, int port) throws Exception

URI wsUri = baseWebsocketUri.resolve("/chat");
handler = MessageHandler.from(this::onText, null);
ClientUpgradeRequest request = ClientUpgradeRequest.from(client, wsUri, handler);
CoreClientUpgradeRequest request = CoreClientUpgradeRequest.from(client, wsUri, handler);
request.setSubProtocols("chat");
client.connect(request).get(5, TimeUnit.SECONDS);
handler.sendText("[" + name + ": has joined the room]", Callback.NOOP, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.eclipse.jetty.websocket.core.FrameHandler;
import org.eclipse.jetty.websocket.core.TestFrameHandler;
import org.eclipse.jetty.websocket.core.WebSocketServer;
import org.eclipse.jetty.websocket.core.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.core.client.UpgradeListener;
import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient;
import org.eclipse.jetty.websocket.core.server.Negotiation;
Expand Down Expand Up @@ -103,7 +103,7 @@ public void testClientDeflateBufferSize() throws Exception
{
int deflateBufferSize = 6;
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate; @deflate_buffer_size=" + deflateBufferSize);

CompletableFuture<HttpFields> futureRequestHeaders = new CompletableFuture<>();
Expand Down Expand Up @@ -157,7 +157,7 @@ public void testClientInflateBufferSize() throws Exception
{
int inflateBufferSize = 6;
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate; @inflate_buffer_size=" + inflateBufferSize);

CompletableFuture<HttpFields> futureRequestHeaders = new CompletableFuture<>();
Expand Down Expand Up @@ -212,7 +212,7 @@ public void testServerDeflateBufferSize() throws Exception
{
testNegotiator.deflateBufferSize = 6;
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate");

CompletableFuture<HttpFields> futureResponseHeaders = new CompletableFuture<>();
Expand Down Expand Up @@ -267,7 +267,7 @@ public void testServerInflateBufferSize() throws Exception
{
testNegotiator.inflateBufferSize = 6;
TestFrameHandler clientHandler = new TestFrameHandler();
ClientUpgradeRequest upgradeRequest = ClientUpgradeRequest.from(client, serverUri, clientHandler);
CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(client, serverUri, clientHandler);
upgradeRequest.addExtensions("permessage-deflate");

CompletableFuture<HttpFields> futureResponseHeaders = new CompletableFuture<>();
Expand Down
Loading

0 comments on commit 1e7ce3a

Please sign in to comment.