From a391dc4aa9eee1933313feb524c53a1dbb87b059 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 10:10:59 +1000 Subject: [PATCH 01/15] Use wrapped HttpServlet req/resp instead of base ones from ServletContextRequest Signed-off-by: Lachlan Roberts --- .../ee10/servlet/ServletContextRequest.java | 17 +++- .../ee10/servlet/ServletContextResponse.java | 19 +++- .../server/JettyWebSocketServlet.java | 32 ++++--- .../DelegatedServerUpgradeRequest.java | 14 +-- .../DelegatedServerUpgradeResponse.java | 29 +++--- .../JettyServerFrameHandlerFactory.java | 8 +- .../ee10/websocket/tests/SimpleEchoTest.java | 90 +++++++++++++++++++ .../servlet/WebSocketUpgradeFilter.java | 27 ++++-- 8 files changed, 188 insertions(+), 48 deletions(-) create mode 100644 jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java index 8601610f066..55d73badb5b 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java @@ -31,6 +31,7 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicReference; import jakarta.servlet.AsyncContext; import jakarta.servlet.AsyncListener; @@ -74,6 +75,7 @@ import org.eclipse.jetty.server.handler.ContextRequest; import org.eclipse.jetty.server.handler.ContextResponse; import org.eclipse.jetty.session.Session; import org.eclipse.jetty.session.SessionManager; +import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Fields; import org.eclipse.jetty.util.HostPort; @@ -82,7 +84,7 @@ import org.eclipse.jetty.util.URIUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ServletContextRequest extends ContextRequest implements Runnable +public class ServletContextRequest extends ContextRequest implements Runnable, Attachable { public static final String __MULTIPART_CONFIG_ELEMENT = "org.eclipse.jetty.multipartConfig"; @@ -122,6 +124,7 @@ public class ServletContextRequest extends ContextRequest implements Runnable final HttpInput _httpInput; final String _pathInContext; Charset _queryEncoding; + final AtomicReference _attachment = new AtomicReference<>(); final List _requestAttributeListeners = new ArrayList<>(); @@ -140,6 +143,18 @@ public class ServletContextRequest extends ContextRequest implements Runnable _pathInContext = pathInContext; } + @Override + public Object getAttachment() + { + return _attachment.get(); + } + + @Override + public void setAttachment(Object attachment) + { + _attachment.set(attachment); + } + @Override public void process(Request request, Response response, Callback callback) throws Exception { diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java index a9f162a6048..f8d12cb3923 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java @@ -20,6 +20,7 @@ import java.util.Collection; import java.util.EnumSet; import java.util.Iterator; import java.util.Locale; +import java.util.concurrent.atomic.AtomicReference; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletOutputStream; @@ -53,6 +54,7 @@ import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.handler.ContextResponse; import org.eclipse.jetty.session.Session; import org.eclipse.jetty.session.SessionManager; +import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; @@ -60,7 +62,7 @@ import org.eclipse.jetty.util.SharedBlockingCallback; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.URIUtil; -public class ServletContextResponse extends ContextResponse +public class ServletContextResponse extends ContextResponse implements Attachable { private static final int __MIN_BUFFER_SIZE = 1; private static final HttpField __EXPIRES_01JAN1970 = new PreEncodedHttpField(HttpHeader.EXPIRES, DateGenerator.__01Jan1970); @@ -85,7 +87,8 @@ public class ServletContextResponse extends ContextResponse private ResponseWriter _writer; private long _contentLength = -1; - + final AtomicReference _attachment = new AtomicReference<>(); + public static ServletContextResponse getBaseResponse(ServletResponse response) { if (response instanceof ServletApiResponse) @@ -112,6 +115,18 @@ public class ServletContextResponse extends ContextResponse _httpServletResponse = new ServletApiResponse(response); } + @Override + public Object getAttachment() + { + return _attachment.get(); + } + + @Override + public void setAttachment(Object attachment) + { + _attachment.set(attachment); + } + public HttpOutput getHttpOutput() { return _httpOutput; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java index 4c490fd0bba..a449654162b 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java @@ -24,6 +24,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletContextRequest; +import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.server.internal.DelegatedServerUpgradeRequest; import org.eclipse.jetty.ee10.websocket.server.internal.DelegatedServerUpgradeResponse; import org.eclipse.jetty.ee10.websocket.server.internal.JettyServerFrameHandlerFactory; @@ -182,23 +183,32 @@ public abstract class JettyWebSocketServlet extends HttpServlet ServletContextRequest request = ServletContextRequest.getBaseRequest(req); if (request == null) throw new IllegalStateException("Base Request not available"); + ServletContextResponse response = request.getResponse(); - // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + // Do preliminary check before proceeding to attempt an upgrade. + if (mapping.getHandshaker().isWebSocketUpgradeRequest(request)) { - if (mapping.upgrade(request, request.getResponse(), callback, null)) + // provide a null default customizer the customizer will be on the negotiator in the mapping + try (Blocker.Callback callback = Blocker.callback()) { - callback.block(); - return; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + request.setAttachment(req); + response.setAttachment(resp); + + if (mapping.upgrade(request, response, callback, null)) + { + callback.block(); + return; + } + } + finally + { + request.setAttachment(null); + response.setAttachment(null); } } - // If we reach this point, it means we had an incoming request to upgrade - // but it was either not a proper websocket upgrade, or it was possibly rejected - // due to incoming request constraints (controlled by WebSocketCreator) - if (resp.isCommitted()) - return; - // Handle normally super.service(req, resp); } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java index f99faa13fdf..c631b35738f 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java @@ -52,13 +52,13 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest public DelegatedServerUpgradeRequest(ServerUpgradeRequest request) { - this(request, Request.as(request, ServletContextRequest.class).getHttpServletRequest()); - } + ServletContextRequest servletContextRequest = Request.as(request, ServletContextRequest.class); + Object attachment = servletContextRequest.getAttachment(); + if (!(attachment instanceof HttpServletRequest)) + throw new IllegalArgumentException("correct attachment not set on ServletContextRequest"); - public DelegatedServerUpgradeRequest(ServerUpgradeRequest request, HttpServletRequest servletRequest) - { this.upgradeRequest = request; - this.httpServletRequest = servletRequest; + this.httpServletRequest = (HttpServletRequest)attachment; this.queryString = httpServletRequest.getQueryString(); try @@ -145,13 +145,13 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest @Override public String getMethod() { - return upgradeRequest.getMethod(); + return httpServletRequest.getMethod(); } @Override public String getOrigin() { - return upgradeRequest.getHeaders().get(HttpHeader.ORIGIN); + return httpServletRequest.getHeader(HttpHeader.ORIGIN.asString()); } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java index f81ac14a5ed..bbd16f0fed6 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java @@ -21,26 +21,35 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee10.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeResponse; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; public class DelegatedServerUpgradeResponse implements JettyServerUpgradeResponse { private final ServerUpgradeResponse upgradeResponse; + private final HttpServletResponse httpServletResponse; public DelegatedServerUpgradeResponse(ServerUpgradeResponse response) { upgradeResponse = response; + ServletContextResponse servletContextResponse = Response.as(response, ServletContextResponse.class); + Object attachment = servletContextResponse.getAttachment(); + if (!(attachment instanceof HttpServletResponse)) + throw new IllegalArgumentException("correct attachment not set on ServletContextResponse"); + this.httpServletResponse = (HttpServletResponse)attachment; } @Override public void addHeader(String name, String value) { + // TODO: This should go to the httpServletResponse for headers but then it won't do interception of the websocket headers + // which are done through the jetty-core Response wrapping ServerUpgradeResponse done by websocket-core. upgradeResponse.getHeaders().add(name, value); } @@ -97,17 +106,13 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons @Override public int getStatusCode() { - return upgradeResponse.getStatus(); + return httpServletResponse.getStatus(); } @Override public void sendForbidden(String message) throws IOException { - try (Blocker.Callback callback = Blocker.callback()) - { - Response.writeError(upgradeResponse.getRequest(), upgradeResponse, callback, HttpStatus.FORBIDDEN_403, message); - callback.block(); - } + httpServletResponse.sendError(HttpStatus.FORBIDDEN_403, message); } @Override @@ -127,22 +132,18 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons @Override public void setStatusCode(int statusCode) { - upgradeResponse.setStatus(statusCode); + httpServletResponse.setStatus(statusCode); } @Override public boolean isCommitted() { - return upgradeResponse.isCommitted(); + return httpServletResponse.isCommitted(); } @Override public void sendError(int statusCode, String message) throws IOException { - try (Blocker.Callback callback = Blocker.callback()) - { - Response.writeError(upgradeResponse.getRequest(), upgradeResponse, callback, statusCode, message); - callback.block(); - } + httpServletResponse.sendError(statusCode, message); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/JettyServerFrameHandlerFactory.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/JettyServerFrameHandlerFactory.java index f64bc7acb39..53c29b40d93 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/JettyServerFrameHandlerFactory.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/JettyServerFrameHandlerFactory.java @@ -14,12 +14,9 @@ package org.eclipse.jetty.ee10.websocket.server.internal; import jakarta.servlet.ServletContext; -import jakarta.servlet.http.HttpServletRequest; -import org.eclipse.jetty.ee10.servlet.ServletContextRequest; import org.eclipse.jetty.ee10.websocket.common.JettyWebSocketFrameHandler; import org.eclipse.jetty.ee10.websocket.common.JettyWebSocketFrameHandlerFactory; import org.eclipse.jetty.ee10.websocket.server.JettyWebSocketServerContainer; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; @@ -42,11 +39,8 @@ public class JettyServerFrameHandlerFactory extends JettyWebSocketFrameHandlerFa @Override public FrameHandler newFrameHandler(Object websocketPojo, ServerUpgradeRequest upgradeRequest, ServerUpgradeResponse upgradeResponse) { - ServletContextRequest servletContextRequest = Request.as(upgradeRequest, ServletContextRequest.class); - HttpServletRequest httpServletRequest = servletContextRequest.getHttpServletRequest(); - JettyWebSocketFrameHandler frameHandler = super.newJettyFrameHandler(websocketPojo); - frameHandler.setUpgradeRequest(new DelegatedServerUpgradeRequest(upgradeRequest, httpServletRequest)); + frameHandler.setUpgradeRequest(new DelegatedServerUpgradeRequest(upgradeRequest)); frameHandler.setUpgradeResponse(new DelegatedServerUpgradeResponse(upgradeResponse)); return frameHandler; } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java new file mode 100644 index 00000000000..2564a0ef1bb --- /dev/null +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java @@ -0,0 +1,90 @@ +// +// ======================================================================== +// Copyright (c) 1995-2022 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.ee10.websocket.tests; + +import java.net.URI; +import java.time.Duration; +import java.util.concurrent.TimeUnit; + +import org.eclipse.jetty.ee10.servlet.ServletContextHandler; +import org.eclipse.jetty.ee10.websocket.api.Session; +import org.eclipse.jetty.ee10.websocket.api.StatusCode; +import org.eclipse.jetty.ee10.websocket.client.WebSocketClient; +import org.eclipse.jetty.ee10.websocket.server.config.JettyWebSocketServletContainerInitializer; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class SimpleEchoTest +{ + private Server _server; + private WebSocketClient _client; + private ServerConnector _connector; + + @BeforeEach + public void start() throws Exception + { + _server = new Server(); + _connector = new ServerConnector(_server); + _server.addConnector(_connector); + + ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + contextHandler.setContextPath("/"); + JettyWebSocketServletContainerInitializer.configure(contextHandler, ((servletContext, container) -> + { + container.setIdleTimeout(Duration.ZERO); + container.addMapping("/", EchoSocket.class); + })); + _server.setHandler(contextHandler); + _server.start(); + + _client = new WebSocketClient(); + _client.start(); + } + + @AfterEach + public void stop() throws Exception + { + _client.stop(); + _server.stop(); + } + + @Test + public void testEcho() throws Exception + { + int timeout = 3; + _client.setIdleTimeout(Duration.ofSeconds(timeout)); + _client.setConnectTimeout(Duration.ofSeconds(timeout).toMillis()); + + URI uri = new URI("ws://localhost:" + _connector.getLocalPort()); + EventSocket clientEndpoint = new EventSocket(); + Session session = _client.connect(clientEndpoint, uri).get(timeout, TimeUnit.SECONDS); + session.setIdleTimeout(Duration.ofSeconds(timeout)); + + String message = "hello world 1234"; + session.getRemote().sendString(message); + String received = clientEndpoint.textMessages.poll(timeout, TimeUnit.SECONDS); + assertThat(received, equalTo(message)); + + session.close(); + assertTrue(clientEndpoint.closeLatch.await(timeout, TimeUnit.SECONDS)); + assertThat(clientEndpoint.closeCode, equalTo(StatusCode.NORMAL)); + } +} diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java index 612563dc00e..391d964ad70 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java @@ -30,6 +30,7 @@ import org.eclipse.jetty.ee10.servlet.FilterHolder; import org.eclipse.jetty.ee10.servlet.FilterMapping; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletContextRequest; +import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.servlet.ServletHandler; import org.eclipse.jetty.server.handler.ContextHandler; import org.eclipse.jetty.util.Blocker; @@ -158,16 +159,30 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable ServletContextRequest baseRequest = ServletContextRequest.getBaseRequest(request); if (baseRequest == null) throw new IllegalStateException("Base Request not available"); + ServletContextResponse baseResponse = baseRequest.getResponse(); - // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + // Do preliminary check before proceeding to attempt an upgrade. + if (mappings.getHandshaker().isWebSocketUpgradeRequest(baseRequest)) { - if (mappings.upgrade(baseRequest, baseRequest.getResponse(), callback, defaultCustomizer)) + // provide a null default customizer the customizer will be on the negotiator in the mapping + try (Blocker.Callback callback = Blocker.callback()) { - callback.block(); - return; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (mappings.upgrade(baseRequest, baseResponse, callback, null)) + { + callback.block(); + return; + } + } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); } - callback.succeeded(); // TODO this is wasteful making a blocker on every request, even if it is not used. At leasts should be shared... but better to detect if we might need to upgrade first? } // If we reach this point, it means we had an incoming request to upgrade From a8cf747754071b6330a3d2100832241c7f0ef533 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 10:16:39 +1000 Subject: [PATCH 02/15] extract isWebSocketUpgradeRequest into Handshaker interface Signed-off-by: Lachlan Roberts --- .../websocket/core/server/Handshaker.java | 2 ++ .../server/internal/AbstractHandshaker.java | 25 +++++++++++-------- .../server/internal/HandshakerSelector.java | 6 +++++ .../server/internal/RFC6455Handshaker.java | 4 +-- .../server/internal/RFC8441Handshaker.java | 4 +-- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java index 8d57d4bd8de..fb590c6b568 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java @@ -29,5 +29,7 @@ public interface Handshaker return new HandshakerSelector(); } + boolean isWebSocketUpgradeRequest(Request request); + boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException; } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java index b3420eaeb4e..1bed5b69964 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java @@ -56,7 +56,7 @@ public abstract class AbstractHandshaker implements Handshaker @Override public boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException { - if (!validateRequest(request)) + if (!isWebSocketUpgradeRequest(request)) return false; WebSocketNegotiation negotiation = newNegotiation(request, response, callback, components); @@ -156,10 +156,22 @@ public abstract class AbstractHandshaker implements Handshaker return true; } - protected abstract boolean validateRequest(Request request); - protected abstract WebSocketNegotiation newNegotiation(Request request, Response response, Callback callback, WebSocketComponents webSocketComponents); + @Override + public boolean isWebSocketUpgradeRequest(Request request) + { + String wsVersionHeader = request.getHeaders().get(HttpHeader.SEC_WEBSOCKET_VERSION); + if (!WebSocketConstants.SPEC_VERSION_STRING.equals(wsVersionHeader)) + { + if (LOG.isDebugEnabled()) + LOG.debug("not upgraded: unsupported version {} {}", wsVersionHeader, request); + return false; + } + + return true; + } + protected boolean validateNegotiation(WebSocketNegotiation negotiation) { if (!negotiation.validateHeaders()) @@ -169,13 +181,6 @@ public abstract class AbstractHandshaker implements Handshaker return false; } - if (!WebSocketConstants.SPEC_VERSION_STRING.equals(negotiation.getVersion())) - { - if (LOG.isDebugEnabled()) - LOG.debug("not upgraded: unsupported version {} {}", negotiation.getVersion(), negotiation.getRequest()); - return false; - } - return true; } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/HandshakerSelector.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/HandshakerSelector.java index 638656b1d1e..68e4f2c5adb 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/HandshakerSelector.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/HandshakerSelector.java @@ -33,6 +33,12 @@ public class HandshakerSelector implements Handshaker private final RFC6455Handshaker rfc6455 = new RFC6455Handshaker(); private final RFC8441Handshaker rfc8441 = new RFC8441Handshaker(); + @Override + public boolean isWebSocketUpgradeRequest(Request request) + { + return rfc6455.isWebSocketUpgradeRequest(request) || rfc8441.isWebSocketUpgradeRequest(request); + } + @Override public boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java index 6d1432e99bf..14a56c6eafd 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java @@ -40,7 +40,7 @@ public final class RFC6455Handshaker extends AbstractHandshaker private static final HttpField CONNECTION_UPGRADE = new PreEncodedHttpField(HttpHeader.CONNECTION, HttpHeader.UPGRADE.asString()); @Override - protected boolean validateRequest(Request request) + public boolean isWebSocketUpgradeRequest(Request request) { if (!HttpMethod.GET.is(request.getMethod())) { @@ -56,7 +56,7 @@ public final class RFC6455Handshaker extends AbstractHandshaker return false; } - return true; + return super.isWebSocketUpgradeRequest(request); } @Override diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java index 9a399a42cf6..5adba65168f 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java @@ -32,7 +32,7 @@ import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public class RFC8441Handshaker extends AbstractHandshaker { @Override - protected boolean validateRequest(Request request) + public boolean isWebSocketUpgradeRequest(Request request) { if (!HttpMethod.CONNECT.is(request.getMethod())) { @@ -48,7 +48,7 @@ public class RFC8441Handshaker extends AbstractHandshaker return false; } - return true; + return super.isWebSocketUpgradeRequest(request); } @Override From 297e717ca1408cc81feaf2181e700012ea15cf05 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 11:23:54 +1000 Subject: [PATCH 03/15] improve javadoc to strengthen contract for websocket upgrade Signed-off-by: Lachlan Roberts --- .../websocket/core/server/Handshaker.java | 15 +++++++++ .../core/server/WebSocketCreator.java | 13 +++++--- .../core/server/WebSocketMappings.java | 33 ++++++++++++++++--- .../server/JettyWebSocketCreator.java | 4 +++ .../server/JettyWebSocketServerContainer.java | 3 +- 5 files changed, 57 insertions(+), 11 deletions(-) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java index fb590c6b568..32a7155bb8b 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java @@ -31,5 +31,20 @@ public interface Handshaker boolean isWebSocketUpgradeRequest(Request request); + /** + * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the + * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be + * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing + * the callback. + * + * @param negotiator the negotiator + * @param request the request + * @param response the response + * @param callback the callback + * @param components the WebSocket components + * @param defaultCustomizer the customizer + * @return true if the WebSocket upgrade was accepted + * @throws IOException there is an error during the upgrade + */ boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException; } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java index d7c07557f81..f33b75360da 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java @@ -15,12 +15,11 @@ package org.eclipse.jetty.websocket.core.server; import org.eclipse.jetty.util.Callback; -// TODO: improve javadoc. /** * Abstract WebSocket creator interface. *

- * Should you desire filtering of the WebSocket object creation due to criteria such as origin or sub-protocol, - * then you will be required to implement a custom WebSocketCreator implementation. + * This can be used for filtering of the WebSocket object creation due to criteria such as origin or sub-protocol, + * or for choosing a specific WebSocket object based on the upgrade request. *

*/ public interface WebSocketCreator @@ -28,10 +27,14 @@ public interface WebSocketCreator /** * Create a websocket from the incoming request. * + *

If the creator returns null it is responsible for completing the {@link Callback} and sending a response. + * If the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or + * complete the {@link Callback}.

+ * * @param req the request details * @param resp the response details - * @param callback callback - * @return a websocket object to use, or null if no websocket should be created from this request. + * @param callback the callback, should only be completed by the creator if a null WebSocket object is returned. + * @return the WebSocket object, or null to take responsibility to send error response if no WebSocket is to be created. */ Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback); } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java index 9e0f56016c4..e92d38b4c95 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java @@ -229,6 +229,19 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener return negotiator; } + /** + * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the + * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be + * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing + * the callback. + * + * @param request the request + * @param response the response + * @param callback the callback + * @param defaultCustomizer the customizer + * @return true if the WebSocket upgrade was accepted + * @throws IOException there is an error during the upgrade + */ public boolean upgrade(Request request, Response response, Callback callback, Configuration.Customizer defaultCustomizer) throws IOException { String target = request.getPathInContext(); @@ -239,13 +252,23 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener request.setAttribute(PathSpec.class.getName(), pathSpec); }); - if (negotiator == null) - return false; - - // We have an upgrade request - return handshaker.upgradeRequest(negotiator, request, response, callback, components, defaultCustomizer); + return upgrade(negotiator, request, response, callback, defaultCustomizer); } + /** + * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the + * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be + * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing + * the callback. + * + * @param negotiator the negotiator + * @param request the request + * @param response the response + * @param callback the callback + * @param defaultCustomizer the customizer + * @return true if the WebSocket upgrade was accepted + * @throws IOException there is an error during the upgrade + */ public boolean upgrade(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, Configuration.Customizer defaultCustomizer) throws IOException { if (negotiator == null) diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java index 1a16e5a181d..6b899401f64 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java @@ -25,6 +25,10 @@ public interface JettyWebSocketCreator /** * Create a websocket from the incoming request. * + *

If no websocket is to be created (return value of null), the {@link JettyWebSocketCreator} + * is responsible for sending a response with {@link JettyServerUpgradeResponse#sendError(int, String)}, + * {@link JettyServerUpgradeResponse#sendForbidden(String)} or {@link JettyServerUpgradeResponse#setStatusCode(int)}.

+ * * @param req the request details * @param resp the response details * @return a websocket object to use, or null if no websocket should be created from this request. diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index e010bc6b6ae..9d2b2f124a7 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -152,7 +152,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements try { Object webSocket = creator.createWebSocket(new DelegatedServerUpgradeRequest(req), new DelegatedServerUpgradeResponse(resp)); - cb.succeeded(); + if (webSocket == null) + cb.succeeded(); return webSocket; } catch (Throwable t) From 43d4e47056e36cd737dbc94e80be37ea0a0db1e8 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 13:42:39 +1000 Subject: [PATCH 04/15] Simplify Core API by making WebSocketNegotiation internal Signed-off-by: Lachlan Roberts --- .../src/main/java/module-info.java | 1 + .../{internal => }/CreatorNegotiator.java | 23 +++------- .../core/server/ServerUpgradeRequest.java | 8 +++- .../core/server/ServerUpgradeResponse.java | 1 + .../core/server/WebSocketCreator.java | 6 +-- .../core/server/WebSocketMappings.java | 3 +- .../core/server/WebSocketNegotiator.java | 46 ++++++------------- .../server/internal/AbstractHandshaker.java | 3 +- .../server/internal/RFC6455Handshaker.java | 1 - .../server/internal/RFC6455Negotiation.java | 1 - .../server/internal/RFC8441Handshaker.java | 1 - .../server/internal/RFC8441Negotiation.java | 1 - .../internal/WebSocketHttpFieldsWrapper.java | 1 - .../{ => internal}/WebSocketNegotiation.java | 9 +++- .../jetty/websocket/core/DemandTest.java | 3 +- .../core/TestWebSocketNegotiator.java | 12 ++--- .../websocket/core/WebSocketEchoTest.java | 3 +- .../core/WebSocketNegotiationTest.java | 22 ++++----- .../jetty/websocket/core/WebSocketServer.java | 12 ++--- .../core/chat/ChatWebSocketServer.java | 12 ++--- .../PerMessageDeflaterBufferSizeTest.java | 10 ++-- .../PermessageDeflateDemandTest.java | 3 +- .../extensions/ValidationExtensionTest.java | 11 +++-- .../core/proxy/WebSocketProxyTest.java | 4 +- .../internal/JakartaWebSocketCreator.java | 26 +++++------ .../JakartaWebSocketServerContainer.java | 3 +- .../websocket/jakarta/tests/CoreServer.java | 29 ++++-------- .../jakarta/tests/client/CookiesTest.java | 21 ++++----- .../client/DecoderReaderManySmallTest.java | 9 ++-- .../tests/client/MessageReceivingTest.java | 20 ++++---- .../server/JettyWebSocketServerContainer.java | 5 +- .../server/JettyWebSocketServlet.java | 4 +- .../ee10/websocket/tests/SimpleEchoTest.java | 2 +- .../internal/JakartaWebSocketCreator.java | 26 +++++------ .../JakartaWebSocketServerContainer.java | 3 +- .../websocket/jakarta/tests/CoreServer.java | 29 ++++-------- .../jakarta/tests/client/CookiesTest.java | 20 ++++---- .../client/DecoderReaderManySmallTest.java | 10 ++-- .../tests/client/MessageReceivingTest.java | 20 ++++---- .../server/JettyWebSocketServerContainer.java | 5 +- .../server/JettyWebSocketServlet.java | 4 +- 41 files changed, 189 insertions(+), 244 deletions(-) rename jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/{internal => }/CreatorNegotiator.java (65%) rename jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/{ => internal}/WebSocketNegotiation.java (95%) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java index 2cfd2d33936..c82c3b0f886 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java @@ -19,4 +19,5 @@ module org.eclipse.jetty.websocket.core.server requires transitive org.eclipse.jetty.websocket.core.common; exports org.eclipse.jetty.websocket.core.server; + exports org.eclipse.jetty.websocket.core.server.internal; } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java similarity index 65% rename from jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java rename to jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java index 0dd960cf52f..1e136cf3364 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java @@ -11,19 +11,13 @@ // ======================================================================== // -package org.eclipse.jetty.websocket.core.server.internal; +package org.eclipse.jetty.websocket.core.server; -import java.io.IOException; import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.server.Context; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; -import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; -import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; -import org.eclipse.jetty.websocket.core.server.WebSocketCreator; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; public class CreatorNegotiator extends WebSocketNegotiator.AbstractNegotiator { @@ -48,28 +42,25 @@ public class CreatorNegotiator extends WebSocketNegotiator.AbstractNegotiator } @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - Context context = negotiation.getRequest().getContext(); - ServerUpgradeRequest upgradeRequest = negotiation.getRequest(); - ServerUpgradeResponse upgradeResponse = negotiation.getResponse(); - + Context context = request.getContext(); Object websocketPojo; try { AtomicReference result = new AtomicReference<>(); - context.run(() -> result.set(creator.createWebSocket(upgradeRequest, upgradeResponse, negotiation.getCallback()))); + context.run(() -> result.set(creator.createWebSocket(request, response, callback))); websocketPojo = result.get(); } catch (Throwable t) { - negotiation.getCallback().failed(t); + callback.failed(t); return null; } if (websocketPojo == null) return null; - return factory.newFrameHandler(websocketPojo, upgradeRequest, upgradeResponse); + return factory.newFrameHandler(websocketPojo, request, response); } @Override diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeRequest.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeRequest.java index 0a39b731b12..224072aef7c 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeRequest.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeRequest.java @@ -21,7 +21,9 @@ import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.Attributes; import org.eclipse.jetty.websocket.core.ExtensionConfig; +import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.WebSocketConstants; +import org.eclipse.jetty.websocket.core.server.internal.WebSocketNegotiation; /** * Upgrade request used for websocket negotiation. @@ -41,6 +43,11 @@ public class ServerUpgradeRequest extends Request.Wrapper this.request = baseRequest; } + public WebSocketComponents getWebSocketComponents() + { + return negotiation.getWebSocketComponents(); + } + public void upgrade(Attributes attributes) { this.attributes.clearAttributes(); @@ -94,7 +101,6 @@ public class ServerUpgradeRequest extends Request.Wrapper /** * @return The extensions offered - * @see WebSocketNegotiation#getOfferedExtensions() */ public List getExtensions() { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeResponse.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeResponse.java index 7b41d061bf8..856fc1714ba 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeResponse.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/ServerUpgradeResponse.java @@ -20,6 +20,7 @@ import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.server.internal.WebSocketHttpFieldsWrapper; +import org.eclipse.jetty.websocket.core.server.internal.WebSocketNegotiation; /** * Upgrade response used for websocket negotiation. diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java index f33b75360da..5a679abbeab 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java @@ -31,10 +31,10 @@ public interface WebSocketCreator * If the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or * complete the {@link Callback}.

* - * @param req the request details - * @param resp the response details + * @param request the request details + * @param response the response details * @param callback the callback, should only be completed by the creator if a null WebSocket object is returned. * @return the WebSocket object, or null to take responsibility to send error response if no WebSocket is to be created. */ - Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback); + Object createWebSocket(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback); } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java index e92d38b4c95..5f15af9103f 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java @@ -34,7 +34,6 @@ import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; -import org.eclipse.jetty.websocket.core.server.internal.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.internal.HandshakerSelector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -184,7 +183,7 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener */ public void addMapping(PathSpec pathSpec, WebSocketCreator creator, FrameHandlerFactory factory, Configuration.Customizer customizer) throws WebSocketException { - mappings.put(pathSpec, WebSocketNegotiator.from(creator, factory, customizer)); + mappings.put(pathSpec, new CreatorNegotiator(creator, factory, customizer)); } /** diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java index 2dc3628996d..305e6c3fce4 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java @@ -13,49 +13,31 @@ package org.eclipse.jetty.websocket.core.server; -import java.io.IOException; -import java.util.function.Function; - +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.internal.CreatorNegotiator; public interface WebSocketNegotiator extends Configuration.Customizer { - FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException; + /** + * Create a {@link FrameHandler} from the incoming request. + * + *

If the negotiator returns null it is responsible for completing the {@link Callback} and sending a response. + * If the negotiator intends to return non-null {@link FrameHandler}, it MUST NOT write content to the response or + * complete the {@link Callback}.

+ * + * @param request the request details + * @param response the response details + * @param callback the callback, should only be completed by the creator if a null WebSocket object is returned. + * @return the FrameHandler, or null to take responsibility to send error response if no WebSocket is to be created. + */ + FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback); @Override default void customize(Configuration configurable) { } - static WebSocketNegotiator from(Function negotiate) - { - return from(negotiate, null); - } - - static WebSocketNegotiator from(Function negotiate, Configuration.Customizer customizer) - { - return new AbstractNegotiator(customizer) - { - @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) - { - return negotiate.apply(negotiation); - } - }; - } - - static WebSocketNegotiator from(WebSocketCreator creator, FrameHandlerFactory factory) - { - return from(creator, factory, null); - } - - static WebSocketNegotiator from(WebSocketCreator creator, FrameHandlerFactory factory, Configuration.Customizer customizer) - { - return new CreatorNegotiator(creator, factory, customizer); - } - abstract class AbstractNegotiator extends Configuration.ConfigurationCustomizer implements WebSocketNegotiator { final Configuration.Customizer customizer; diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java index 1bed5b69964..23940f0eab2 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/AbstractHandshaker.java @@ -43,7 +43,6 @@ import org.eclipse.jetty.websocket.core.internal.Negotiated; import org.eclipse.jetty.websocket.core.internal.WebSocketConnection; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; import org.eclipse.jetty.websocket.core.server.Handshaker; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -68,7 +67,7 @@ public abstract class AbstractHandshaker implements Handshaker return false; // Negotiate the FrameHandler - FrameHandler handler = negotiator.negotiate(negotiation); + FrameHandler handler = negotiator.negotiate(negotiation.getRequest(), negotiation.getResponse(), negotiation.getCallback()); if (handler == null) return true; diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java index 14a56c6eafd..ee2dd2a546d 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java @@ -32,7 +32,6 @@ import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.internal.WebSocketConnection; import org.eclipse.jetty.websocket.core.internal.WebSocketCore; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public final class RFC6455Handshaker extends AbstractHandshaker { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Negotiation.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Negotiation.java index fedbeb034fa..a3da2e78355 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Negotiation.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Negotiation.java @@ -21,7 +21,6 @@ import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.WebSocketComponents; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public class RFC6455Negotiation extends WebSocketNegotiation { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java index 5adba65168f..9c7c598ded5 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Handshaker.java @@ -27,7 +27,6 @@ import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.internal.WebSocketConnection; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public class RFC8441Handshaker extends AbstractHandshaker { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Negotiation.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Negotiation.java index bc0a89de790..70be990d006 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Negotiation.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC8441Negotiation.java @@ -19,7 +19,6 @@ import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.WebSocketComponents; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public class RFC8441Negotiation extends WebSocketNegotiation { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketHttpFieldsWrapper.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketHttpFieldsWrapper.java index c169e9891e7..8714fda628a 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketHttpFieldsWrapper.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketHttpFieldsWrapper.java @@ -18,7 +18,6 @@ import java.util.Collections; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; public class WebSocketHttpFieldsWrapper extends HttpFieldsWrapper { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiation.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketNegotiation.java similarity index 95% rename from jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiation.java rename to jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketNegotiation.java index 8fd222b7477..5a2b516cc99 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiation.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/WebSocketNegotiation.java @@ -11,7 +11,7 @@ // ======================================================================== // -package org.eclipse.jetty.websocket.core.server; +package org.eclipse.jetty.websocket.core.server.internal; import java.util.ArrayList; import java.util.Collections; @@ -28,6 +28,8 @@ import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; public abstract class WebSocketNegotiation { @@ -64,6 +66,11 @@ public abstract class WebSocketNegotiation return callback; } + public WebSocketComponents getWebSocketComponents() + { + return components; + } + public void negotiate() throws BadMessageException { try diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/DemandTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/DemandTest.java index fc415af7af7..9eb0acd8976 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/DemandTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/DemandTest.java @@ -23,7 +23,6 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -47,7 +46,7 @@ public class DemandTest WebSocketUpgradeHandler upgradeHandler = new WebSocketUpgradeHandler(); _server.setHandler(upgradeHandler); - upgradeHandler.addMapping("/", WebSocketNegotiator.from((neg) -> new EchoFrameHandler())); + upgradeHandler.addMapping("/", (req, resp, cb) -> new EchoFrameHandler()); _server.start(); _client = new WebSocketCoreClient(); diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/TestWebSocketNegotiator.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/TestWebSocketNegotiator.java index 49434aebdaf..2d2dec2471c 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/TestWebSocketNegotiator.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/TestWebSocketNegotiator.java @@ -13,10 +13,11 @@ package org.eclipse.jetty.websocket.core; -import java.io.IOException; import java.util.List; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; public class TestWebSocketNegotiator extends WebSocketNegotiator.AbstractNegotiator @@ -35,12 +36,11 @@ public class TestWebSocketNegotiator extends WebSocketNegotiator.AbstractNegotia } @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubprotocols = negotiation.getOfferedSubprotocols(); + List offeredSubprotocols = request.getSubProtocols(); if (!offeredSubprotocols.isEmpty()) - negotiation.setSubprotocol(offeredSubprotocols.get(0)); - + response.setAcceptedSubProtocol(offeredSubprotocols.get(0)); return frameHandler; } } diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketEchoTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketEchoTest.java index a8bf36a7add..58a00a71a69 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketEchoTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketEchoTest.java @@ -22,7 +22,6 @@ import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -64,7 +63,7 @@ public class WebSocketEchoTest @Test public void test() throws Exception { - _upgradeHandler.addMapping("/", WebSocketNegotiator.from(n -> new EchoFrameHandler())); + _upgradeHandler.addMapping("/", (req, resp, cb) -> new EchoFrameHandler()); TestMessageHandler clientHandler = new TestMessageHandler(); URI uri = URI.create("ws://localhost:" + _serverConnector.getLocalPort()); CoreClientUpgradeRequest upgradeRequest = CoreClientUpgradeRequest.from(_client, uri, clientHandler); diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java index c4a273f70bd..eda43973664 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.websocket.core; -import java.io.IOException; import java.net.InetSocketAddress; import java.net.Socket; import java.nio.charset.StandardCharsets; @@ -37,7 +36,8 @@ import org.eclipse.jetty.websocket.core.client.UpgradeListener; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.UpgradeException; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -79,32 +79,32 @@ public class WebSocketNegotiationTest extends WebSocketTester WebSocketNegotiator negotiator = new WebSocketNegotiator.AbstractNegotiator() { @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - if (negotiation.getOfferedSubprotocols().isEmpty()) + if (request.getSubProtocols().isEmpty()) { - negotiation.setSubprotocol("NotOffered"); + response.setAcceptedSubProtocol("NotOffered"); return new EchoFrameHandler(); } - String subprotocol = negotiation.getOfferedSubprotocols().get(0); - negotiation.setSubprotocol(subprotocol); + String subprotocol = request.getSubProtocols().get(0); + response.setAcceptedSubProtocol(subprotocol); switch (subprotocol) { case "testExtensionSelection": - negotiation.setNegotiatedExtensions(List.of(ExtensionConfig.parse("permessage-deflate;client_no_context_takeover"))); + response.setExtensions(List.of(ExtensionConfig.parse("permessage-deflate;client_no_context_takeover"))); break; case "testNotOfferedParameter": - negotiation.setNegotiatedExtensions(List.of(ExtensionConfig.parse("permessage-deflate;server_no_context_takeover"))); + response.setExtensions(List.of(ExtensionConfig.parse("permessage-deflate;server_no_context_takeover"))); break; case "testNotAcceptingExtensions": - negotiation.setNegotiatedExtensions(Collections.emptyList()); + response.setExtensions(Collections.emptyList()); break; case "testNoSubProtocolSelected": - negotiation.setSubprotocol(null); + response.setAcceptedSubProtocol(null); break; case "test": diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketServer.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketServer.java index 6cd77b04792..bdf7cfb1262 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketServer.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketServer.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.websocket.core; -import java.io.IOException; import java.net.URI; import java.util.List; @@ -21,8 +20,10 @@ import org.eclipse.jetty.server.NetworkConnector; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.ssl.SslContextFactory; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; @@ -112,12 +113,11 @@ public class WebSocketServer } @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubprotocols = negotiation.getOfferedSubprotocols(); + List offeredSubprotocols = request.getSubProtocols(); if (!offeredSubprotocols.isEmpty()) - negotiation.setSubprotocol(offeredSubprotocols.get(0)); - + response.setAcceptedSubProtocol(offeredSubprotocols.get(0)); return frameHandler; } } diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/chat/ChatWebSocketServer.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/chat/ChatWebSocketServer.java index 4f3597c42a2..6dc641cafdf 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/chat/ChatWebSocketServer.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/chat/ChatWebSocketServer.java @@ -33,8 +33,8 @@ import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.internal.MessageHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -47,7 +47,7 @@ public class ChatWebSocketServer private final Set members = new HashSet<>(); - private FrameHandler negotiate(WebSocketNegotiation negotiation) + private FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { // Finalize negotiations in API layer involves: // + MAY mutate the policy @@ -56,10 +56,10 @@ public class ChatWebSocketServer // + MAY reject with sendError semantics // + MAY change/add/remove offered extensions // + MUST pick subprotocol - List subprotocols = negotiation.getOfferedSubprotocols(); + List subprotocols = request.getSubProtocols(); if (!subprotocols.contains("chat")) return null; - negotiation.setSubprotocol("chat"); + response.setAcceptedSubProtocol("chat"); // + MUST return the FrameHandler or null or exception? return new MessageHandler() @@ -115,7 +115,7 @@ public class ChatWebSocketServer ChatWebSocketServer chat = new ChatWebSocketServer(); WebSocketComponents components = new WebSocketComponents(); WebSocketUpgradeHandler upgradeHandler = new WebSocketUpgradeHandler(components); - upgradeHandler.addMapping(new ServletPathSpec("/*"), WebSocketNegotiator.from(chat::negotiate)); + upgradeHandler.addMapping(new ServletPathSpec("/*"), chat::negotiate); context.setHandler(upgradeHandler); upgradeHandler.setHandler(new Handler.Processor() diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflaterBufferSizeTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflaterBufferSizeTest.java index 183bf013141..12c31df9e6c 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflaterBufferSizeTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PerMessageDeflaterBufferSizeTest.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.websocket.core.extensions; -import java.io.IOException; import java.net.URI; import java.nio.ByteBuffer; import java.util.Objects; @@ -36,7 +35,8 @@ import org.eclipse.jetty.websocket.core.WebSocketServer; 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.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; @@ -64,14 +64,14 @@ public class PerMessageDeflaterBufferSizeTest int inflateBufferSize = -1; @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - for (ExtensionConfig extensionConfig : negotiation.getOfferedExtensions()) + for (ExtensionConfig extensionConfig : request.getExtensions()) { assertFalse(extensionConfig.getName().startsWith("@")); } - for (ExtensionConfig extensionConfig : negotiation.getNegotiatedExtensions()) + for (ExtensionConfig extensionConfig : response.getExtensions()) { if ("permessage-deflate".equals(extensionConfig.getName())) { diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PermessageDeflateDemandTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PermessageDeflateDemandTest.java index 29b1ed52b7e..d7a975dca62 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PermessageDeflateDemandTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/PermessageDeflateDemandTest.java @@ -33,7 +33,6 @@ import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.TestFrameHandler; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -77,7 +76,7 @@ public class PermessageDeflateDemandTest public void test() throws Exception { ServerHandler serverHandler = new ServerHandler(); - _upgradeHandler.addMapping("/", WebSocketNegotiator.from(n -> serverHandler)); + _upgradeHandler.addMapping("/", (req, resp, cb) -> serverHandler); TestFrameHandler clientHandler = new TestFrameHandler(); URI uri = URI.create("ws://localhost:" + _connector.getLocalPort()); diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ValidationExtensionTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ValidationExtensionTest.java index 19357f3005e..1286c1e4bf6 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ValidationExtensionTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/extensions/ValidationExtensionTest.java @@ -13,13 +13,13 @@ package org.eclipse.jetty.websocket.core.extensions; -import java.io.IOException; import java.net.Socket; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.util.BufferUtil; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.CloseStatus; import org.eclipse.jetty.websocket.core.ExtensionConfig; import org.eclipse.jetty.websocket.core.Frame; @@ -30,7 +30,8 @@ import org.eclipse.jetty.websocket.core.TestFrameHandler; import org.eclipse.jetty.websocket.core.TestWebSocketNegotiator; import org.eclipse.jetty.websocket.core.WebSocketServer; import org.eclipse.jetty.websocket.core.WebSocketTester; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -53,14 +54,14 @@ public class ValidationExtensionTest extends WebSocketTester WebSocketNegotiator negotiator = new TestWebSocketNegotiator(serverHandler) { @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { List negotiatedExtensions = new ArrayList<>(); negotiatedExtensions.add(ExtensionConfig.parse( "@validation; outgoing-sequence; incoming-sequence; outgoing-frame; incoming-frame; incoming-utf8; outgoing-utf8")); - negotiation.setNegotiatedExtensions(negotiatedExtensions); + response.setExtensions(negotiatedExtensions); - return super.negotiate(negotiation); + return super.negotiate(request, response, callback); } }; server = new WebSocketServer(negotiator); diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java index 11a64145217..b1610d6a001 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java @@ -41,6 +41,8 @@ import org.eclipse.jetty.websocket.core.TestWebSocketNegotiator; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; +import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; +import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.junit.jupiter.api.AfterEach; @@ -113,7 +115,7 @@ public class WebSocketProxyTest handlers.addHandler(serverContext); ContextHandler proxyContext = new ContextHandler("/proxy"); - negotiator = WebSocketNegotiator.from(negotiation -> proxy.client2Proxy, defaultCustomizer); + negotiator = new CreatorNegotiator((req, resp, cb) -> proxy.client2Proxy, (FrameHandlerFactory)defaultCustomizer); upgradeHandler = new WebSocketUpgradeHandler(); upgradeHandler.addMapping("/*", negotiator); proxyContext.setHandler(upgradeHandler); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketCreator.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketCreator.java index 135c8db5a0b..3d1359e05a8 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketCreator.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketCreator.java @@ -60,10 +60,10 @@ public class JakartaWebSocketCreator implements WebSocketCreator } @Override - public Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback) + public Object createWebSocket(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - final JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(req); - final JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(resp); + final JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(request); + final JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(response); // Establish a copy of the config, so that the UserProperties are unique // per upgrade request. @@ -83,27 +83,27 @@ public class JakartaWebSocketCreator implements WebSocketCreator // it is not JSR api breaking. A few users on #jetty and a few from cometd // have asked for access to this information. Map userProperties = config.getUserProperties(); - userProperties.put(PROP_LOCAL_ADDRESS, req.getConnectionMetaData().getLocalSocketAddress()); - userProperties.put(PROP_REMOTE_ADDRESS, req.getConnectionMetaData().getRemoteSocketAddress()); - userProperties.put(PROP_LOCALES, Request.getLocales(req)); + userProperties.put(PROP_LOCAL_ADDRESS, request.getConnectionMetaData().getLocalSocketAddress()); + userProperties.put(PROP_REMOTE_ADDRESS, request.getConnectionMetaData().getRemoteSocketAddress()); + userProperties.put(PROP_LOCALES, Request.getLocales(request)); // Get Configurator from config object (not guaranteed to be unique per endpoint upgrade) ServerEndpointConfig.Configurator configurator = config.getConfigurator(); // [JSR] Step 1: check origin - if (!configurator.checkOrigin(req.getHeaders().get(HttpHeader.ORIGIN))) + if (!configurator.checkOrigin(request.getHeaders().get(HttpHeader.ORIGIN))) { - Response.writeError(req, resp, callback, HttpStatus.FORBIDDEN_403, "Origin mismatch"); + Response.writeError(request, response, callback, HttpStatus.FORBIDDEN_403, "Origin mismatch"); return null; } // [JSR] Step 2: deal with sub protocols List supported = config.getSubprotocols(); - List requested = req.getSubProtocols(); + List requested = request.getSubProtocols(); String subprotocol = configurator.getNegotiatedSubprotocol(supported, requested); if (StringUtil.isNotBlank(subprotocol)) { - resp.setAcceptedSubProtocol(subprotocol); + response.setAcceptedSubProtocol(subprotocol); } // [JSR] Step 3: deal with extensions @@ -113,7 +113,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator installedExtensions.add(new JakartaWebSocketExtension(extName)); } List requestedExts = new ArrayList<>(); - for (ExtensionConfig reqCfg : req.getExtensions()) + for (ExtensionConfig reqCfg : request.getExtensions()) { requestedExts.add(new JakartaWebSocketExtension(reqCfg)); } @@ -131,7 +131,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator configs.add(ecfg); } } - resp.setExtensions(configs); + response.setExtensions(configs); // [JSR] Step 4: build out new ServerEndpointConfig Object pathSpecObject = jsrHandshakeRequest.getRequestPathSpec(); @@ -139,7 +139,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator { // We can get path params from PathSpec and Request Path. UriTemplatePathSpec pathSpec = (UriTemplatePathSpec)pathSpecObject; - Map pathParams = pathSpec.getPathParams(req.getPathInContext()); + Map pathParams = pathSpec.getPathParams(request.getPathInContext()); // Wrap the config with the path spec information. config = new PathParamServerEndpointConfig(config, pathParams); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 4ef35b24478..9a26f3cc94b 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -43,6 +43,7 @@ import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; +import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; @@ -294,7 +295,7 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta // Perform the upgrade. JakartaWebSocketCreator creator = new JakartaWebSocketCreator(this, config, getExtensionRegistry()); - WebSocketNegotiator negotiator = WebSocketNegotiator.from(creator, frameHandlerFactory); + WebSocketNegotiator negotiator = new CreatorNegotiator(creator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); ServletContextRequest baseRequest = ServletContextRequest.getBaseRequest(request); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/CoreServer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/CoreServer.java index f816b592a12..6da2d3e30a8 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/CoreServer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/CoreServer.java @@ -13,10 +13,8 @@ package org.eclipse.jetty.ee10.websocket.jakarta.tests; -import java.io.IOException; import java.net.URI; import java.util.List; -import java.util.function.Function; import org.eclipse.jetty.ee10.websocket.jakarta.tests.framehandlers.FrameEcho; import org.eclipse.jetty.ee10.websocket.jakarta.tests.framehandlers.WholeMessageEcho; @@ -24,10 +22,12 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; @@ -39,18 +39,6 @@ public class CoreServer extends ContainerLifeCycle private URI serverUri; private URI wsUri; - public CoreServer(Function negotiationFunction) - { - this(new WebSocketNegotiator.AbstractNegotiator() - { - @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException - { - return negotiationFunction.apply(negotiation); - } - }); - } - public CoreServer(WebSocketNegotiator negotiator) { this.negotiator = negotiator; @@ -99,27 +87,26 @@ public class CoreServer extends ContainerLifeCycle public static class EchoNegotiator extends WebSocketNegotiator.AbstractNegotiator { @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); - + List offeredSubProtocols = request.getSubProtocols(); if (offeredSubProtocols.isEmpty()) { return new WholeMessageEcho(); } else { - for (String offeredSubProtocol : negotiation.getOfferedSubprotocols()) + for (String offeredSubProtocol : offeredSubProtocols) { if ("echo-whole".equalsIgnoreCase(offeredSubProtocol)) { - negotiation.setSubprotocol("echo-whole"); + response.setAcceptedSubProtocol("echo-whole"); return new WholeMessageEcho(); } if ("echo-frames".equalsIgnoreCase(offeredSubProtocol)) { - negotiation.setSubprotocol("echo-frames"); + response.setAcceptedSubProtocol("echo-frames"); return new FrameEcho(); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/CookiesTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/CookiesTest.java index edc4e0f1f9b..17035d0b897 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/CookiesTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/CookiesTest.java @@ -18,7 +18,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import java.util.function.Function; import jakarta.websocket.ClientEndpointConfig; import jakarta.websocket.ContainerProvider; @@ -33,8 +32,7 @@ import org.eclipse.jetty.ee10.websocket.jakarta.tests.framehandlers.WholeMessage import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.FuturePromise; -import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -49,9 +47,9 @@ public class CookiesTest { private CoreServer server; - protected void startServer(Function negotiationFunction) throws Exception + protected void startServer(WebSocketNegotiator negotiator) throws Exception { - server = new CoreServer(negotiationFunction); + server = new CoreServer(negotiator); server.start(); } @@ -68,10 +66,9 @@ public class CookiesTest final String cookieValue = "value"; final String cookieString = cookieName + "=" + cookieValue; - startServer(negotiation -> + startServer((req, resp, cb) -> { - Request request = negotiation.getRequest(); - List cookies = Request.getCookies(request); + List cookies = Request.getCookies(req); assertThat("Cookies", cookies, notNullValue()); assertThat("Cookies", cookies.size(), is(1)); org.eclipse.jetty.http.HttpCookie cookie = cookies.get(0); @@ -79,8 +76,8 @@ public class CookiesTest assertEquals(cookieValue, cookie.getValue()); StringBuilder requestHeaders = new StringBuilder(); - request.getHeaders().getFieldNamesCollection() - .forEach(name -> requestHeaders.append(name).append(": ").append(request.getHeaders().get(name)).append("\n")); + req.getHeaders().getFieldNamesCollection() + .forEach(name -> requestHeaders.append(name).append(": ").append(req.getHeaders().get(name)).append("\n")); return new StaticText(requestHeaders.toString()); }); @@ -112,10 +109,10 @@ public class CookiesTest final String cookieValue = "value"; final String cookieDomain = "domain"; final String cookiePath = "/path"; - startServer(negotiation -> + startServer((req, resp, cb) -> { org.eclipse.jetty.http.HttpCookie cookie = new org.eclipse.jetty.http.HttpCookie(cookieName, cookieValue, cookieDomain, cookiePath); - Response.addCookie(negotiation.getResponse(), cookie); + Response.addCookie(resp, cookie); return new WholeMessageEcho(); }); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java index 38b973cee12..d11cd1ce452 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java @@ -34,7 +34,6 @@ import org.eclipse.jetty.ee10.websocket.jakarta.tests.CoreServer; import org.eclipse.jetty.ee10.websocket.jakarta.tests.WSEventTracker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.internal.MessageHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,14 +49,14 @@ public class DecoderReaderManySmallTest @BeforeEach public void setUp() throws Exception { - server = new CoreServer(WebSocketNegotiator.from((negotiation) -> + server = new CoreServer((req, resp, cb) -> { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); + List offeredSubProtocols = req.getSubProtocols(); if (!offeredSubProtocols.isEmpty()) - negotiation.setSubprotocol(offeredSubProtocols.get(0)); + resp.setAcceptedSubProtocol(offeredSubProtocols.get(0)); return new EventIdFrameHandler(); - })); + }); server.start(); client = ContainerProvider.getWebSocketContainer(); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/MessageReceivingTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/MessageReceivingTest.java index 4e96fdc4fe8..c9d2d2ca86b 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/MessageReceivingTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee10/websocket/jakarta/tests/client/MessageReceivingTest.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.ee10.websocket.jakarta.tests.client; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -41,7 +40,8 @@ import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.internal.MessageHandler; import org.eclipse.jetty.websocket.core.internal.util.TextUtils; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -348,28 +348,26 @@ public class MessageReceivingTest } @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); + List offeredSubProtocols = request.getSubProtocols(); if (offeredSubProtocols.contains("partial-text")) { - negotiation.setSubprotocol("partial-text"); + response.setAcceptedSubProtocol("partial-text"); return new SendPartialTextFrameHandler(); } if (offeredSubProtocols.contains("partial-binary")) { - negotiation.setSubprotocol("partial-binary"); - SendPartialBinaryFrameHandler frameHandler = new SendPartialBinaryFrameHandler(); - return frameHandler; + response.setAcceptedSubProtocol("partial-binary"); + return new SendPartialBinaryFrameHandler(); } if (offeredSubProtocols.contains("echo")) { - negotiation.setSubprotocol("echo"); - EchoWholeMessageFrameHandler frameHandler = new EchoWholeMessageFrameHandler(); - return frameHandler; + response.setAcceptedSubProtocol("echo"); + return new EchoWholeMessageFrameHandler(); } return null; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index 9d2b2f124a7..e988653d7d4 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -46,6 +46,7 @@ import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; +import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketCreator; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; @@ -212,12 +213,12 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements if (baseRequest == null) throw new IllegalStateException("Base Request not available"); - WebSocketNegotiator negotiator = WebSocketNegotiator.from(coreCreator, frameHandlerFactory, customizer); + WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); try (Blocker.Callback callback = Blocker.callback()) { - boolean upgraded = handshaker.upgradeRequest(negotiator, baseRequest, baseRequest.getResponse(), callback, components, null); + boolean upgraded = handshaker.upgradeRequest(negotiator, baseRequest, baseRequest.getResponse(), callback, components, customizer); callback.block(); return upgraded; } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java index a449654162b..451b18a2ac3 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java @@ -293,11 +293,11 @@ public abstract class JettyWebSocketServlet extends HttpServlet } @Override - public Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback) + public Object createWebSocket(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { try { - Object webSocket = creator.createWebSocket(new DelegatedServerUpgradeRequest(req), new DelegatedServerUpgradeResponse(resp)); + Object webSocket = creator.createWebSocket(new DelegatedServerUpgradeRequest(request), new DelegatedServerUpgradeResponse(response)); callback.succeeded(); return webSocket; } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java index 2564a0ef1bb..36af01e772d 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-tests/src/test/java/org/eclipse/jetty/ee10/websocket/tests/SimpleEchoTest.java @@ -69,7 +69,7 @@ public class SimpleEchoTest @Test public void testEcho() throws Exception { - int timeout = 3; + int timeout = 10000; _client.setIdleTimeout(Duration.ofSeconds(timeout)); _client.setConnectTimeout(Duration.ofSeconds(timeout).toMillis()); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketCreator.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketCreator.java index 3bc5bb5171f..58d140f0b8d 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketCreator.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketCreator.java @@ -60,10 +60,10 @@ public class JakartaWebSocketCreator implements WebSocketCreator } @Override - public Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback) + public Object createWebSocket(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - final JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(req); - final JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(resp); + final JsrHandshakeRequest jsrHandshakeRequest = new JsrHandshakeRequest(request); + final JsrHandshakeResponse jsrHandshakeResponse = new JsrHandshakeResponse(response); // Establish a copy of the config, so that the UserProperties are unique // per upgrade request. @@ -83,27 +83,27 @@ public class JakartaWebSocketCreator implements WebSocketCreator // it is not JSR api breaking. A few users on #jetty and a few from cometd // have asked for access to this information. Map userProperties = config.getUserProperties(); - userProperties.put(PROP_LOCAL_ADDRESS, req.getConnectionMetaData().getLocalSocketAddress()); - userProperties.put(PROP_REMOTE_ADDRESS, req.getConnectionMetaData().getRemoteSocketAddress()); - userProperties.put(PROP_LOCALES, Request.getLocales(req)); + userProperties.put(PROP_LOCAL_ADDRESS, request.getConnectionMetaData().getLocalSocketAddress()); + userProperties.put(PROP_REMOTE_ADDRESS, request.getConnectionMetaData().getRemoteSocketAddress()); + userProperties.put(PROP_LOCALES, Request.getLocales(request)); // Get Configurator from config object (not guaranteed to be unique per endpoint upgrade) ServerEndpointConfig.Configurator configurator = config.getConfigurator(); // [JSR] Step 1: check origin - if (!configurator.checkOrigin(req.getHeaders().get(HttpHeader.ORIGIN))) + if (!configurator.checkOrigin(request.getHeaders().get(HttpHeader.ORIGIN))) { - Response.writeError(req, resp, callback, HttpStatus.FORBIDDEN_403, "Origin mismatch"); + Response.writeError(request, response, callback, HttpStatus.FORBIDDEN_403, "Origin mismatch"); return null; } // [JSR] Step 2: deal with sub protocols List supported = config.getSubprotocols(); - List requested = req.getSubProtocols(); + List requested = request.getSubProtocols(); String subprotocol = configurator.getNegotiatedSubprotocol(supported, requested); if (StringUtil.isNotBlank(subprotocol)) { - resp.setAcceptedSubProtocol(subprotocol); + response.setAcceptedSubProtocol(subprotocol); } // [JSR] Step 3: deal with extensions @@ -113,7 +113,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator installedExtensions.add(new JakartaWebSocketExtension(extName)); } List requestedExts = new ArrayList<>(); - for (ExtensionConfig reqCfg : req.getExtensions()) + for (ExtensionConfig reqCfg : request.getExtensions()) { requestedExts.add(new JakartaWebSocketExtension(reqCfg)); } @@ -131,7 +131,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator configs.add(ecfg); } } - resp.setExtensions(configs); + response.setExtensions(configs); // [JSR] Step 4: build out new ServerEndpointConfig Object pathSpecObject = jsrHandshakeRequest.getRequestPathSpec(); @@ -139,7 +139,7 @@ public class JakartaWebSocketCreator implements WebSocketCreator { // We can get path params from PathSpec and Request Path. UriTemplatePathSpec pathSpec = (UriTemplatePathSpec)pathSpecObject; - Map pathParams = pathSpec.getPathParams(req.getPathInContext()); + Map pathParams = pathSpec.getPathParams(request.getPathInContext()); // Wrap the config with the path spec information. config = new PathParamServerEndpointConfig(config, pathParams); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index bbbd8dab4ff..c5e61c96d8f 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -43,6 +43,7 @@ import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; +import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; @@ -294,7 +295,7 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta // Perform the upgrade. JakartaWebSocketCreator creator = new JakartaWebSocketCreator(this, config, getExtensionRegistry()); - WebSocketNegotiator negotiator = WebSocketNegotiator.from(creator, frameHandlerFactory); + WebSocketNegotiator negotiator = new CreatorNegotiator(creator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/CoreServer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/CoreServer.java index 5d52aad7460..ee969bef5b9 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/CoreServer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/CoreServer.java @@ -13,10 +13,8 @@ package org.eclipse.jetty.ee9.websocket.jakarta.tests; -import java.io.IOException; import java.net.URI; import java.util.List; -import java.util.function.Function; import org.eclipse.jetty.ee9.websocket.jakarta.tests.framehandlers.FrameEcho; import org.eclipse.jetty.ee9.websocket.jakarta.tests.framehandlers.WholeMessageEcho; @@ -24,10 +22,12 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.DefaultHandler; import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; @@ -39,18 +39,6 @@ public class CoreServer extends ContainerLifeCycle private URI serverUri; private URI wsUri; - public CoreServer(Function negotiationFunction) - { - this(new WebSocketNegotiator.AbstractNegotiator() - { - @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException - { - return negotiationFunction.apply(negotiation); - } - }); - } - public CoreServer(WebSocketNegotiator negotiator) { this.negotiator = negotiator; @@ -99,27 +87,26 @@ public class CoreServer extends ContainerLifeCycle public static class EchoNegotiator extends WebSocketNegotiator.AbstractNegotiator { @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); - + List offeredSubProtocols = request.getSubProtocols(); if (offeredSubProtocols.isEmpty()) { return new WholeMessageEcho(); } else { - for (String offeredSubProtocol : negotiation.getOfferedSubprotocols()) + for (String offeredSubProtocol : offeredSubProtocols) { if ("echo-whole".equalsIgnoreCase(offeredSubProtocol)) { - negotiation.setSubprotocol("echo-whole"); + response.setAcceptedSubProtocol("echo-whole"); return new WholeMessageEcho(); } if ("echo-frames".equalsIgnoreCase(offeredSubProtocol)) { - negotiation.setSubprotocol("echo-frames"); + response.setAcceptedSubProtocol("echo-frames"); return new FrameEcho(); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/CookiesTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/CookiesTest.java index 53eb3099ef4..da6b08cf497 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/CookiesTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/CookiesTest.java @@ -18,7 +18,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import java.util.function.Function; import jakarta.websocket.ClientEndpointConfig; import jakarta.websocket.ContainerProvider; @@ -33,9 +32,7 @@ import org.eclipse.jetty.ee9.websocket.jakarta.tests.framehandlers.WholeMessageE import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.FuturePromise; -import org.eclipse.jetty.websocket.core.FrameHandler; -import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; @@ -50,9 +47,9 @@ public class CookiesTest { private CoreServer server; - protected void startServer(Function negotiationFunction) throws Exception + protected void startServer(WebSocketNegotiator negotiator) throws Exception { - server = new CoreServer(negotiationFunction); + server = new CoreServer(negotiator); server.start(); } @@ -69,10 +66,9 @@ public class CookiesTest final String cookieValue = "value"; final String cookieString = cookieName + "=" + cookieValue; - startServer(negotiation -> + startServer((req, resp, cb) -> { - ServerUpgradeRequest request = negotiation.getRequest(); - List cookies = Request.getCookies(request); + List cookies = Request.getCookies(req); assertThat("Cookies", cookies, notNullValue()); assertThat("Cookies", cookies.size(), is(1)); org.eclipse.jetty.http.HttpCookie cookie = cookies.get(0); @@ -80,7 +76,7 @@ public class CookiesTest assertEquals(cookieValue, cookie.getValue()); StringBuilder requestHeaders = new StringBuilder(); - request.getHeaders() + req.getHeaders() .forEach(field -> requestHeaders.append(field.getName()).append(": ").append(field.getValue()).append("\n")); return new StaticText(requestHeaders.toString()); @@ -113,10 +109,10 @@ public class CookiesTest final String cookieValue = "value"; final String cookieDomain = "domain"; final String cookiePath = "/path"; - startServer(negotiation -> + startServer((req, resp, cb) -> { org.eclipse.jetty.http.HttpCookie cookie = new org.eclipse.jetty.http.HttpCookie(cookieName, cookieValue, cookieDomain, cookiePath); - Response.addCookie(negotiation.getResponse(), cookie); + Response.addCookie(resp, cookie); return new WholeMessageEcho(); }); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java index 9e9b9c2bca2..d5297b92056 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/DecoderReaderManySmallTest.java @@ -34,7 +34,6 @@ import org.eclipse.jetty.ee9.websocket.jakarta.tests.CoreServer; import org.eclipse.jetty.ee9.websocket.jakarta.tests.WSEventTracker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.internal.MessageHandler; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -50,14 +49,13 @@ public class DecoderReaderManySmallTest @BeforeEach public void setUp() throws Exception { - server = new CoreServer(WebSocketNegotiator.from((negotiation) -> + server = new CoreServer((req, resp, cb) -> { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); + List offeredSubProtocols = req.getSubProtocols(); if (!offeredSubProtocols.isEmpty()) - negotiation.setSubprotocol(offeredSubProtocols.get(0)); - + resp.setAcceptedSubProtocol(offeredSubProtocols.get(0)); return new EventIdFrameHandler(); - })); + }); server.start(); client = ContainerProvider.getWebSocketContainer(); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/MessageReceivingTest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/MessageReceivingTest.java index 7f0436eea71..f3969f4d5ec 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/MessageReceivingTest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-tests/src/test/java/org/eclipse/jetty/ee9/websocket/jakarta/tests/client/MessageReceivingTest.java @@ -13,7 +13,6 @@ package org.eclipse.jetty.ee9.websocket.jakarta.tests.client; -import java.io.IOException; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; @@ -41,7 +40,8 @@ import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.internal.MessageHandler; import org.eclipse.jetty.websocket.core.internal.util.TextUtils; -import org.eclipse.jetty.websocket.core.server.WebSocketNegotiation; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -348,28 +348,26 @@ public class MessageReceivingTest } @Override - public FrameHandler negotiate(WebSocketNegotiation negotiation) throws IOException + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { - List offeredSubProtocols = negotiation.getOfferedSubprotocols(); + List offeredSubProtocols = request.getSubProtocols(); if (offeredSubProtocols.contains("partial-text")) { - negotiation.setSubprotocol("partial-text"); + response.setAcceptedSubProtocol("partial-text"); return new SendPartialTextFrameHandler(); } if (offeredSubProtocols.contains("partial-binary")) { - negotiation.setSubprotocol("partial-binary"); - SendPartialBinaryFrameHandler frameHandler = new SendPartialBinaryFrameHandler(); - return frameHandler; + response.setAcceptedSubProtocol("partial-binary"); + return new SendPartialBinaryFrameHandler(); } if (offeredSubProtocols.contains("echo")) { - negotiation.setSubprotocol("echo"); - EchoWholeMessageFrameHandler frameHandler = new EchoWholeMessageFrameHandler(); - return frameHandler; + response.setAcceptedSubProtocol("echo"); + return new EchoWholeMessageFrameHandler(); } return null; diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index c9e9d77bee9..f72e8c88c31 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -46,6 +46,7 @@ import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; +import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketCreator; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; @@ -208,12 +209,12 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements }; HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); - WebSocketNegotiator negotiator = WebSocketNegotiator.from(coreCreator, frameHandlerFactory, customizer); + WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); try (Blocker.Callback callback = Blocker.callback()) { - boolean upgraded = handshaker.upgradeRequest(negotiator, httpChannel.getCoreRequest(), httpChannel.getCoreResponse(), callback, components, null); + boolean upgraded = handshaker.upgradeRequest(negotiator, httpChannel.getCoreRequest(), httpChannel.getCoreResponse(), callback, components, customizer); callback.block(); return upgraded; } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java index ae5f693b9c3..717ce763bc8 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java @@ -282,11 +282,11 @@ public abstract class JettyWebSocketServlet extends HttpServlet } @Override - public Object createWebSocket(ServerUpgradeRequest req, ServerUpgradeResponse resp, Callback callback) + public Object createWebSocket(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) { try { - Object webSocket = creator.createWebSocket(new DelegatedServerUpgradeRequest(req), new DelegatedServerUpgradeResponse(resp)); + Object webSocket = creator.createWebSocket(new DelegatedServerUpgradeRequest(request), new DelegatedServerUpgradeResponse(response)); callback.succeeded(); return webSocket; } From c1cf35d7d580862bfdfccb06a48a4d78f46ec936 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 16:20:48 +1000 Subject: [PATCH 05/15] fix websocket test failures Signed-off-by: Lachlan Roberts --- .../JakartaWebSocketServerContainer.java | 18 +++++++++++++-- .../server/JettyWebSocketServerContainer.java | 22 ++++++++++++++++--- .../servlet/WebSocketUpgradeFilter.java | 2 +- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 9a26f3cc94b..8b7d74f877a 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -30,6 +30,7 @@ import jakarta.websocket.server.ServerEndpointConfig; import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletContextRequest; +import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.jakarta.client.internal.JakartaWebSocketClientContainer; import org.eclipse.jetty.ee10.websocket.jakarta.server.config.ContainerDefaultConfigurator; import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; @@ -301,11 +302,24 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta ServletContextRequest baseRequest = ServletContextRequest.getBaseRequest(request); if (baseRequest == null) throw new IllegalStateException(); + ServletContextResponse baseResponse = baseRequest.getResponse(); try (Blocker.Callback callback = Blocker.callback()) { - handshaker.upgradeRequest(negotiator, baseRequest, baseRequest.getResponse(), callback, components, defaultCustomizer); - callback.block(); + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) + { + callback.block(); + } + } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index e988653d7d4..bf74a8d1fe8 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -26,6 +26,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jetty.ee10.servlet.ServletContextHandler; import org.eclipse.jetty.ee10.servlet.ServletContextRequest; +import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.api.Session; import org.eclipse.jetty.ee10.websocket.api.WebSocketBehavior; import org.eclipse.jetty.ee10.websocket.api.WebSocketContainer; @@ -212,16 +213,31 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements ServletContextRequest baseRequest = ServletContextRequest.getBaseRequest(request); if (baseRequest == null) throw new IllegalStateException("Base Request not available"); + ServletContextResponse baseResponse = baseRequest.getResponse(); WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); try (Blocker.Callback callback = Blocker.callback()) { - boolean upgraded = handshaker.upgradeRequest(negotiator, baseRequest, baseRequest.getResponse(), callback, components, customizer); - callback.block(); - return upgraded; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) + { + callback.block(); + return !baseResponse.isCommitted(); + } } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); + } + + return false; } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java index 391d964ad70..267ae1acf56 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java @@ -172,7 +172,7 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable baseRequest.setAttachment(request); baseResponse.setAttachment(response); - if (mappings.upgrade(baseRequest, baseResponse, callback, null)) + if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) { callback.block(); return; From f67f605acb42e3d7e339a1c20d71d46b5cf42650 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 18:58:58 +1000 Subject: [PATCH 06/15] update javadoc and fix ee9 implementations Signed-off-by: Lachlan Roberts --- .../websocket/core/server/Handshaker.java | 12 +++-- .../core/server/WebSocketCreator.java | 2 +- .../core/server/WebSocketMappings.java | 20 ++++---- .../server/JettyWebSocketServerContainer.java | 9 +++- .../jetty/ee9/nested/ContextHandler.java | 48 ++++++++++++++++++- .../JakartaWebSocketServerContainer.java | 20 +++++++- .../server/JettyWebSocketCreator.java | 4 ++ .../server/JettyWebSocketServerContainer.java | 33 +++++++++++-- .../server/JettyWebSocketServlet.java | 30 ++++++++++-- .../servlet/WebSocketUpgradeFilter.java | 33 +++++++++---- 10 files changed, 173 insertions(+), 38 deletions(-) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java index 32a7155bb8b..3fe2c137f51 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java @@ -32,10 +32,12 @@ public interface Handshaker boolean isWebSocketUpgradeRequest(Request request); /** - * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the - * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be - * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing - * the callback. + * This will attempt to upgrade a request to WebSocket. + * + *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for + * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response + * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible + * for completing the callback and generating a response.

* * @param negotiator the negotiator * @param request the request @@ -43,7 +45,7 @@ public interface Handshaker * @param callback the callback * @param components the WebSocket components * @param defaultCustomizer the customizer - * @return true if the WebSocket upgrade was accepted + * @return true if the WebSocket upgrade was attempted * @throws IOException there is an error during the upgrade */ boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException; diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java index 5a679abbeab..81897008229 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java @@ -28,7 +28,7 @@ public interface WebSocketCreator * Create a websocket from the incoming request. * *

If the creator returns null it is responsible for completing the {@link Callback} and sending a response. - * If the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or + * But if the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or * complete the {@link Callback}.

* * @param request the request details diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java index 5f15af9103f..729e8d1643d 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java @@ -229,10 +229,12 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener } /** - * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the - * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be - * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing - * the callback. + * This will attempt to find a WebSocket mapping and upgrade a request to WebSocket. + * + *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for + * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response + * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible + * for completing the callback and generating a response.

* * @param request the request * @param response the response @@ -255,10 +257,12 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener } /** - * This method returns true the WebSocket upgrade was accepted. A return value of true means this method has taken the - * responsibility for completing the callback, the request will be upgraded to WebSocket or a response will be - * sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible for completing - * the callback. + * This will attempt to upgrade a request to WebSocket. + * + *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for + * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response + * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible + * for completing the callback and generating a response.

* * @param negotiator the negotiator * @param request the request diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index bf74a8d1fe8..c9897695aae 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -187,10 +187,15 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements /** * An immediate programmatic WebSocket upgrade that does not register a mapping or create a {@link WebSocketUpgradeFilter}. + * + *

A return value of true means the connection was Upgraded to WebSocket or an error response is being generated. + * A return value of false means that it was a bad upgrade request and couldn't be upgraded to WebSocket and the + * caller is responsible for generating the response.

+ * * @param creator the WebSocketCreator to use. * @param request the HttpServletRequest. * @param response the HttpServletResponse. - * @return true if the connection was successfully upgraded to WebSocket. + * @return true if the connection could be upgraded or an error was sent. * @throws IOException if an I/O error occurs. */ public boolean upgrade(JettyWebSocketCreator creator, HttpServletRequest request, HttpServletResponse response) throws IOException @@ -228,7 +233,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) { callback.block(); - return !baseResponse.isCommitted(); + return true; } } finally diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java index e846b277dee..c005387038a 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java @@ -35,6 +35,7 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import jakarta.servlet.DispatcherType; @@ -72,6 +73,8 @@ import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.ContextRequest; +import org.eclipse.jetty.server.handler.ContextResponse; +import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Attributes; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.ExceptionUtil; @@ -2450,9 +2453,11 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu } } - public static class CoreContextRequest extends ContextRequest + public static class CoreContextRequest extends ContextRequest implements Attachable { private final HttpChannel _httpChannel; + private final org.eclipse.jetty.server.handler.ContextHandler _contextHandler; + private final AtomicReference _attachment = new AtomicReference<>(); protected CoreContextRequest(org.eclipse.jetty.server.handler.ContextHandler contextHandler, org.eclipse.jetty.server.handler.ContextHandler.Context context, @@ -2462,12 +2467,53 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu { super(contextHandler, context, wrapped, pathInContext); _httpChannel = httpChannel; + _contextHandler = contextHandler; } public HttpChannel getHttpChannel() { return _httpChannel; } + + @Override + protected ContextResponse newContextResponse(org.eclipse.jetty.server.Request request, Response response) + { + return new CoreContextResponse(_contextHandler.getContext(), this, response); + } + + @Override + public Object getAttachment() + { + return _attachment.get(); + } + + @Override + public void setAttachment(Object attachment) + { + _attachment.set(attachment); + } + } + + public static class CoreContextResponse extends ContextResponse implements Attachable + { + private final AtomicReference _attachment = new AtomicReference<>(); + + public CoreContextResponse(org.eclipse.jetty.server.handler.ContextHandler.Context context, org.eclipse.jetty.server.Request request, Response response) + { + super(context, request, response); + } + + @Override + public Object getAttachment() + { + return _attachment.get(); + } + + @Override + public void setAttachment(Object attachment) + { + _attachment.set(attachment); + } } public class CoreContextHandler extends org.eclipse.jetty.server.handler.ContextHandler implements org.eclipse.jetty.server.Request.Processor diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index c5e61c96d8f..8ca68e467a9 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -36,6 +36,8 @@ import org.eclipse.jetty.ee9.websocket.jakarta.server.config.ContainerDefaultCon import org.eclipse.jetty.ee9.websocket.jakarta.server.config.JakartaWebSocketServletContainerInitializer; import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.http.pathmap.UriTemplatePathSpec; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; @@ -299,10 +301,24 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); + ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); + ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); try (Blocker.Callback callback = Blocker.callback()) { - handshaker.upgradeRequest(negotiator, httpChannel.getCoreRequest(), httpChannel.getCoreResponse(), callback, components, defaultCustomizer); - callback.block(); + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) + { + callback.block(); + } + } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketCreator.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketCreator.java index eb48fa57bc1..6686e2b2ed5 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketCreator.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketCreator.java @@ -25,6 +25,10 @@ public interface JettyWebSocketCreator /** * Create a websocket from the incoming request. * + *

If no websocket is to be created (return value of null), the {@link JettyWebSocketCreator} + * is responsible for sending a response with {@link JettyServerUpgradeResponse#sendError(int, String)}, + * {@link JettyServerUpgradeResponse#sendForbidden(String)} or {@link JettyServerUpgradeResponse#setStatusCode(int)}.

+ * * @param req the request details * @param resp the response details * @return a websocket object to use, or null if no websocket should be created from this request. diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index f72e8c88c31..a77436cdcde 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -24,6 +24,7 @@ import java.util.function.Consumer; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.nested.HttpChannel; import org.eclipse.jetty.ee9.servlet.ServletContextHandler; import org.eclipse.jetty.ee9.websocket.api.Session; @@ -38,6 +39,8 @@ import org.eclipse.jetty.ee9.websocket.server.internal.DelegatedServerUpgradeRes import org.eclipse.jetty.ee9.websocket.server.internal.JettyServerFrameHandlerFactory; import org.eclipse.jetty.ee9.websocket.servlet.WebSocketUpgradeFilter; import org.eclipse.jetty.http.pathmap.PathSpec; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; @@ -185,10 +188,15 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements /** * An immediate programmatic WebSocket upgrade that does not register a mapping or create a {@link WebSocketUpgradeFilter}. + * + *

A return value of true means the connection was Upgraded to WebSocket or an error response is being generated. + * A return value of false means that it was a bad upgrade request and couldn't be upgraded to WebSocket and the + * caller is responsible for generating the response.

+ * * @param creator the WebSocketCreator to use. * @param request the HttpServletRequest. * @param response the HttpServletResponse. - * @return true if the connection was successfully upgraded to WebSocket. + * @return true if the connection could be upgraded or an error was sent. * @throws IOException if an I/O error occurs. */ public boolean upgrade(JettyWebSocketCreator creator, HttpServletRequest request, HttpServletResponse response) throws IOException @@ -208,16 +216,31 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } }; - HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); + HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); + ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); + ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); try (Blocker.Callback callback = Blocker.callback()) { - boolean upgraded = handshaker.upgradeRequest(negotiator, httpChannel.getCoreRequest(), httpChannel.getCoreResponse(), callback, components, customizer); - callback.block(); - return upgraded; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) + { + callback.block(); + return true; + } } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); + } + return false; } @Override diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java index 717ce763bc8..85305abde9f 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java @@ -30,6 +30,8 @@ import org.eclipse.jetty.ee9.websocket.server.internal.DelegatedServerUpgradeReq import org.eclipse.jetty.ee9.websocket.server.internal.DelegatedServerUpgradeResponse; import org.eclipse.jetty.ee9.websocket.server.internal.JettyServerFrameHandlerFactory; import org.eclipse.jetty.ee9.websocket.servlet.WebSocketUpgradeFilter; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.Configuration; @@ -182,13 +184,31 @@ public abstract class JettyWebSocketServlet extends HttpServlet throws ServletException, IOException { // provide a null default customizer the customizer will be on the negotiator in the mapping - HttpChannel channel = (HttpChannel)req.getAttribute(HttpChannel.class.getName()); - try (Blocker.Callback callback = Blocker.callback()) + HttpChannel httpChannel = (HttpChannel)req.getAttribute(HttpChannel.class.getName()); + ContextHandler.CoreContextRequest request = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); + ContextHandler.CoreContextResponse response = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + + // Do preliminary check before proceeding to attempt an upgrade. + if (mapping.getHandshaker().isWebSocketUpgradeRequest(request)) { - if (mapping.upgrade(channel.getCoreRequest(), channel.getCoreResponse(), callback, null)) + // provide a null default customizer the customizer will be on the negotiator in the mapping + try (Blocker.Callback callback = Blocker.callback()) { - callback.block(); - return; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + request.setAttachment(req); + response.setAttachment(resp); + + if (mapping.upgrade(request, response, callback, null)) + { + callback.block(); + return; + } + } + finally + { + request.setAttachment(null); + response.setAttachment(null); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java index 5383ea3911d..74f376eee41 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java @@ -26,13 +26,13 @@ import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.nested.HttpChannel; import org.eclipse.jetty.ee9.servlet.FilterHolder; import org.eclipse.jetty.ee9.servlet.FilterMapping; import org.eclipse.jetty.ee9.servlet.ServletHandler; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.Response; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.Dumpable; @@ -156,16 +156,31 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - HttpServletRequest httpreq = (HttpServletRequest)request; - HttpServletResponse httpresp = (HttpServletResponse)response; - HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); - try (Blocker.Callback callback = Blocker.callback()) + ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); + ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + + // Do preliminary check before proceeding to attempt an upgrade. + if (mappings.getHandshaker().isWebSocketUpgradeRequest(baseRequest)) { - if (mappings.upgrade(httpChannel.getCoreRequest(), httpChannel.getCoreResponse(), callback, defaultCustomizer)) + // provide a null default customizer the customizer will be on the negotiator in the mapping + try (Blocker.Callback callback = Blocker.callback()) { - callback.block(); - return; + // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // are accessible when websocket-core calls back the Jetty WebSocket creator. + baseRequest.setAttachment(request); + baseResponse.setAttachment(response); + + if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) + { + callback.block(); + return; + } + } + finally + { + baseRequest.setAttachment(null); + baseResponse.setAttachment(null); } } From d0a731f84f42de68f3e31cb72527c0da16c1b16d Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 4 Aug 2022 20:46:58 +1000 Subject: [PATCH 07/15] fix websocket-core WebSocketProxyTest Signed-off-by: Lachlan Roberts --- .../websocket/core/proxy/WebSocketProxyTest.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java index b1610d6a001..e243375cd58 100644 --- a/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java +++ b/jetty-core/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/proxy/WebSocketProxyTest.java @@ -35,14 +35,15 @@ import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.EchoFrameHandler; import org.eclipse.jetty.websocket.core.Frame; +import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.OpCode; import org.eclipse.jetty.websocket.core.TestAsyncFrameHandler; import org.eclipse.jetty.websocket.core.TestWebSocketNegotiator; import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.internal.WebSocketCoreSession; -import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; -import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; import org.eclipse.jetty.websocket.core.server.WebSocketUpgradeHandler; import org.junit.jupiter.api.AfterEach; @@ -115,7 +116,14 @@ public class WebSocketProxyTest handlers.addHandler(serverContext); ContextHandler proxyContext = new ContextHandler("/proxy"); - negotiator = new CreatorNegotiator((req, resp, cb) -> proxy.client2Proxy, (FrameHandlerFactory)defaultCustomizer); + negotiator = new WebSocketNegotiator.AbstractNegotiator(defaultCustomizer) + { + @Override + public FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback) + { + return proxy.client2Proxy; + } + }; upgradeHandler = new WebSocketUpgradeHandler(); upgradeHandler.addMapping("/*", negotiator); proxyContext.setHandler(upgradeHandler); From c5f5bb9bba49a97f82ac6728df36511b71aa81ac Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Mon, 8 Aug 2022 15:31:58 +1000 Subject: [PATCH 08/15] switch ordering of checks in RFC6455Handshaker.isWebSocketUpgradeRequest Signed-off-by: Lachlan Roberts --- .../server/internal/RFC6455Handshaker.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java index ee2dd2a546d..51e204a6791 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java @@ -41,13 +41,6 @@ public final class RFC6455Handshaker extends AbstractHandshaker @Override public boolean isWebSocketUpgradeRequest(Request request) { - if (!HttpMethod.GET.is(request.getMethod())) - { - if (LOG.isDebugEnabled()) - LOG.debug("not upgraded method!=GET {}", request); - return false; - } - if (!HttpVersion.HTTP_1_1.is(request.getConnectionMetaData().getProtocol())) { if (LOG.isDebugEnabled()) @@ -55,7 +48,17 @@ public final class RFC6455Handshaker extends AbstractHandshaker return false; } - return super.isWebSocketUpgradeRequest(request); + if (!super.isWebSocketUpgradeRequest(request)) + return false; + + if (!HttpMethod.GET.is(request.getMethod())) + { + if (LOG.isDebugEnabled()) + LOG.debug("not upgraded method!=GET {}", request); + return false; + } + + return true; } @Override From 6101384cf65c958cec0ff160f5d5dc8472358067 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Tue, 9 Aug 2022 15:46:14 +1000 Subject: [PATCH 09/15] use attributes instead of attachable for wrapped request/response Signed-off-by: Lachlan Roberts --- .../ee10/servlet/ServletContextRequest.java | 18 ++----- .../ee10/servlet/ServletContextResponse.java | 15 +----- .../JakartaWebSocketServerContainer.java | 10 ++-- .../server/JettyWebSocketServerContainer.java | 10 ++-- .../server/JettyWebSocketServlet.java | 10 ++-- .../DelegatedServerUpgradeRequest.java | 9 +--- .../DelegatedServerUpgradeResponse.java | 7 ++- .../servlet/WebSocketUpgradeFilter.java | 10 ++-- .../jetty/ee9/nested/ContextHandler.java | 51 ++----------------- .../JakartaWebSocketServerContainer.java | 13 ++--- .../server/JettyWebSocketServerContainer.java | 13 ++--- .../server/JettyWebSocketServlet.java | 14 ++--- .../DelegatedServerUpgradeRequest.java | 13 ++--- .../DelegatedServerUpgradeResponse.java | 29 +++++------ .../servlet/WebSocketUpgradeFilter.java | 14 ++--- 15 files changed, 78 insertions(+), 158 deletions(-) diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java index 55d73badb5b..de07a56c167 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java @@ -75,7 +75,6 @@ import org.eclipse.jetty.server.handler.ContextRequest; import org.eclipse.jetty.server.handler.ContextResponse; import org.eclipse.jetty.session.Session; import org.eclipse.jetty.session.SessionManager; -import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.Fields; import org.eclipse.jetty.util.HostPort; @@ -84,10 +83,11 @@ import org.eclipse.jetty.util.URIUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class ServletContextRequest extends ContextRequest implements Runnable, Attachable +public class ServletContextRequest extends ContextRequest implements Runnable { public static final String __MULTIPART_CONFIG_ELEMENT = "org.eclipse.jetty.multipartConfig"; - + public static final String WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedRequest"; + public static final String WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedResponse"; private static final Logger LOG = LoggerFactory.getLogger(ServletContextRequest.class); private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault()); private static final int INPUT_NONE = 0; @@ -143,18 +143,6 @@ public class ServletContextRequest extends ContextRequest implements Runnable, A _pathInContext = pathInContext; } - @Override - public Object getAttachment() - { - return _attachment.get(); - } - - @Override - public void setAttachment(Object attachment) - { - _attachment.set(attachment); - } - @Override public void process(Request request, Response response, Callback callback) throws Exception { diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java index f8d12cb3923..e3464d15d0c 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java @@ -54,7 +54,6 @@ import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.handler.ContextResponse; import org.eclipse.jetty.session.Session; import org.eclipse.jetty.session.SessionManager; -import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.FutureCallback; @@ -62,7 +61,7 @@ import org.eclipse.jetty.util.SharedBlockingCallback; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.URIUtil; -public class ServletContextResponse extends ContextResponse implements Attachable +public class ServletContextResponse extends ContextResponse { private static final int __MIN_BUFFER_SIZE = 1; private static final HttpField __EXPIRES_01JAN1970 = new PreEncodedHttpField(HttpHeader.EXPIRES, DateGenerator.__01Jan1970); @@ -115,18 +114,6 @@ public class ServletContextResponse extends ContextResponse implements Attachabl _httpServletResponse = new ServletApiResponse(response); } - @Override - public Object getAttachment() - { - return _attachment.get(); - } - - @Override - public void setAttachment(Object attachment) - { - _attachment.set(attachment); - } - public HttpOutput getHttpOutput() { return _httpOutput; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 8b7d74f877a..a0da966ed72 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -306,10 +306,10 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) { @@ -318,8 +318,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index c9897695aae..4c9589fdb70 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -225,10 +225,10 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) { @@ -238,8 +238,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } return false; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java index 451b18a2ac3..2b410307655 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java @@ -191,10 +191,10 @@ public abstract class JettyWebSocketServlet extends HttpServlet // provide a null default customizer the customizer will be on the negotiator in the mapping try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - request.setAttachment(req); - response.setAttachment(resp); + request.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); + request.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); if (mapping.upgrade(request, response, callback, null)) { @@ -204,8 +204,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet } finally { - request.setAttachment(null); - response.setAttachment(null); + request.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java index c631b35738f..7855abdab3e 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java @@ -37,7 +37,6 @@ import org.eclipse.jetty.ee10.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; @@ -52,13 +51,9 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest public DelegatedServerUpgradeRequest(ServerUpgradeRequest request) { - ServletContextRequest servletContextRequest = Request.as(request, ServletContextRequest.class); - Object attachment = servletContextRequest.getAttachment(); - if (!(attachment instanceof HttpServletRequest)) - throw new IllegalArgumentException("correct attachment not set on ServletContextRequest"); - + this.httpServletRequest = (HttpServletRequest)request + .getAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); this.upgradeRequest = request; - this.httpServletRequest = (HttpServletRequest)attachment; this.queryString = httpServletRequest.getQueryString(); try diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java index bbd16f0fed6..6261753b0c1 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java @@ -22,6 +22,7 @@ import java.util.Set; import java.util.stream.Collectors; import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee10.servlet.ServletContextRequest; import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee10.websocket.common.JettyExtensionConfig; @@ -39,10 +40,8 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons { upgradeResponse = response; ServletContextResponse servletContextResponse = Response.as(response, ServletContextResponse.class); - Object attachment = servletContextResponse.getAttachment(); - if (!(attachment instanceof HttpServletResponse)) - throw new IllegalArgumentException("correct attachment not set on ServletContextResponse"); - this.httpServletResponse = (HttpServletResponse)attachment; + this.httpServletResponse = (HttpServletResponse)servletContextResponse.getRequest() + .getAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java index 267ae1acf56..6b474ef7075 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java @@ -167,10 +167,10 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable // provide a null default customizer the customizer will be on the negotiator in the mapping try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) { @@ -180,8 +180,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java index c005387038a..09af8293b3d 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java @@ -35,7 +35,6 @@ import java.util.Objects; import java.util.Set; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import jakarta.servlet.DispatcherType; @@ -73,8 +72,6 @@ import org.eclipse.jetty.server.Response; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.handler.ContextHandlerCollection; import org.eclipse.jetty.server.handler.ContextRequest; -import org.eclipse.jetty.server.handler.ContextResponse; -import org.eclipse.jetty.util.Attachable; import org.eclipse.jetty.util.Attributes; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.util.ExceptionUtil; @@ -2453,11 +2450,12 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu } } - public static class CoreContextRequest extends ContextRequest implements Attachable + public static class CoreContextRequest extends ContextRequest { + public static final String WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedRequest"; + public static final String WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedResponse"; + private final HttpChannel _httpChannel; - private final org.eclipse.jetty.server.handler.ContextHandler _contextHandler; - private final AtomicReference _attachment = new AtomicReference<>(); protected CoreContextRequest(org.eclipse.jetty.server.handler.ContextHandler contextHandler, org.eclipse.jetty.server.handler.ContextHandler.Context context, @@ -2467,53 +2465,12 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu { super(contextHandler, context, wrapped, pathInContext); _httpChannel = httpChannel; - _contextHandler = contextHandler; } public HttpChannel getHttpChannel() { return _httpChannel; } - - @Override - protected ContextResponse newContextResponse(org.eclipse.jetty.server.Request request, Response response) - { - return new CoreContextResponse(_contextHandler.getContext(), this, response); - } - - @Override - public Object getAttachment() - { - return _attachment.get(); - } - - @Override - public void setAttachment(Object attachment) - { - _attachment.set(attachment); - } - } - - public static class CoreContextResponse extends ContextResponse implements Attachable - { - private final AtomicReference _attachment = new AtomicReference<>(); - - public CoreContextResponse(org.eclipse.jetty.server.handler.ContextHandler.Context context, org.eclipse.jetty.server.Request request, Response response) - { - super(context, request, response); - } - - @Override - public Object getAttachment() - { - return _attachment.get(); - } - - @Override - public void setAttachment(Object attachment) - { - _attachment.set(attachment); - } } public class CoreContextHandler extends org.eclipse.jetty.server.handler.ContextHandler implements org.eclipse.jetty.server.Request.Processor diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 8ca68e467a9..74f6c7d1d37 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -301,14 +301,15 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); - ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); - ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + Request baseRequest = httpChannel.getCoreRequest(); + Response baseResponse = httpChannel.getCoreResponse(); + try (Blocker.Callback callback = Blocker.callback()) { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) { @@ -317,8 +318,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index a77436cdcde..fc1a2ad91ab 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -220,14 +220,15 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); - ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); - ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + Request baseRequest = httpChannel.getCoreRequest(); + Response baseResponse = httpChannel.getCoreResponse(); + try (Blocker.Callback callback = Blocker.callback()) { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) { @@ -237,8 +238,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } return false; } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java index 85305abde9f..6afa02695fe 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java @@ -185,8 +185,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet { // provide a null default customizer the customizer will be on the negotiator in the mapping HttpChannel httpChannel = (HttpChannel)req.getAttribute(HttpChannel.class.getName()); - ContextHandler.CoreContextRequest request = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); - ContextHandler.CoreContextResponse response = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + Request request = httpChannel.getCoreRequest(); + Response response = httpChannel.getCoreResponse(); // Do preliminary check before proceeding to attempt an upgrade. if (mapping.getHandshaker().isWebSocketUpgradeRequest(request)) @@ -194,10 +194,10 @@ public abstract class JettyWebSocketServlet extends HttpServlet // provide a null default customizer the customizer will be on the negotiator in the mapping try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - request.setAttachment(req); - response.setAttachment(resp); + request.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); + request.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); if (mapping.upgrade(request, response, callback, null)) { @@ -207,8 +207,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet } finally { - request.setAttachment(null); - response.setAttachment(null); + request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java index fd2fca6f073..aa67e6d9588 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java @@ -37,7 +37,6 @@ import org.eclipse.jetty.ee9.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeRequest; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpHeader; -import org.eclipse.jetty.server.Request; import org.eclipse.jetty.util.URIUtil; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; @@ -52,13 +51,9 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest public DelegatedServerUpgradeRequest(ServerUpgradeRequest request) { - this(request, Request.as(request, ContextHandler.CoreContextRequest.class).getHttpChannel().getRequest()); - } - - public DelegatedServerUpgradeRequest(ServerUpgradeRequest request, HttpServletRequest servletRequest) - { + this.httpServletRequest = (HttpServletRequest)request + .getAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); this.upgradeRequest = request; - this.httpServletRequest = servletRequest; this.queryString = httpServletRequest.getQueryString(); try @@ -145,13 +140,13 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest @Override public String getMethod() { - return upgradeRequest.getMethod(); + return httpServletRequest.getMethod(); } @Override public String getOrigin() { - return upgradeRequest.getHeaders().get(HttpHeader.ORIGIN); + return httpServletRequest.getHeader(HttpHeader.ORIGIN.asString()); } @Override diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java index d6d024ed48c..e56e39adcd7 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java @@ -21,26 +21,31 @@ import java.util.Map; import java.util.Set; import java.util.stream.Collectors; +import jakarta.servlet.http.HttpServletResponse; +import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee9.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeResponse; import org.eclipse.jetty.http.HttpStatus; -import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; public class DelegatedServerUpgradeResponse implements JettyServerUpgradeResponse { private final ServerUpgradeResponse upgradeResponse; + private final HttpServletResponse httpServletResponse; public DelegatedServerUpgradeResponse(ServerUpgradeResponse response) { - upgradeResponse = response; + this.upgradeResponse = response; + this.httpServletResponse = (HttpServletResponse)response.getRequest() + .getAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } @Override public void addHeader(String name, String value) { + // TODO: This should go to the httpServletResponse for headers but then it won't do interception of the websocket headers + // which are done through the jetty-core Response wrapping ServerUpgradeResponse done by websocket-core. upgradeResponse.getHeaders().add(name, value); } @@ -97,17 +102,13 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons @Override public int getStatusCode() { - return upgradeResponse.getStatus(); + return httpServletResponse.getStatus(); } @Override public void sendForbidden(String message) throws IOException { - try (Blocker.Callback callback = Blocker.callback()) - { - Response.writeError(upgradeResponse.getRequest(), upgradeResponse, callback, HttpStatus.FORBIDDEN_403, message); - callback.block(); - } + httpServletResponse.sendError(HttpStatus.FORBIDDEN_403, message); } @Override @@ -127,22 +128,18 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons @Override public void setStatusCode(int statusCode) { - upgradeResponse.setStatus(statusCode); + httpServletResponse.setStatus(statusCode); } @Override public boolean isCommitted() { - return upgradeResponse.isCommitted(); + return httpServletResponse.isCommitted(); } @Override public void sendError(int statusCode, String message) throws IOException { - try (Blocker.Callback callback = Blocker.callback()) - { - Response.writeError(upgradeResponse.getRequest(), upgradeResponse, callback, statusCode, message); - callback.block(); - } + httpServletResponse.sendError(statusCode, message); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java index 74f376eee41..d2b6a1c7b51 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java @@ -157,8 +157,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); - ContextHandler.CoreContextRequest baseRequest = Request.as(httpChannel.getCoreRequest(), ContextHandler.CoreContextRequest.class); - ContextHandler.CoreContextResponse baseResponse = Response.as(httpChannel.getCoreResponse(), ContextHandler.CoreContextResponse.class); + Request baseRequest = httpChannel.getCoreRequest(); + Response baseResponse = httpChannel.getCoreResponse(); // Do preliminary check before proceeding to attempt an upgrade. if (mappings.getHandshaker().isWebSocketUpgradeRequest(baseRequest)) @@ -166,10 +166,10 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable // provide a null default customizer the customizer will be on the negotiator in the mapping try (Blocker.Callback callback = Blocker.callback()) { - // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they + // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttachment(request); - baseResponse.setAttachment(response); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) { @@ -179,8 +179,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable } finally { - baseRequest.setAttachment(null); - baseResponse.setAttachment(null); + baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } From bf83e55999e15425de08a689f7c518a115b4c91c Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 17 Aug 2022 15:54:09 +1000 Subject: [PATCH 10/15] keep CreatorNegotiator internal Signed-off-by: Lachlan Roberts --- .../src/main/java/module-info.java | 1 - .../websocket/core/server/WebSocketMappings.java | 3 ++- .../websocket/core/server/WebSocketNegotiator.java | 11 +++++++++++ .../core/server/{ => internal}/CreatorNegotiator.java | 7 ++++++- .../internal/JakartaWebSocketServerContainer.java | 3 +-- .../server/JettyWebSocketServerContainer.java | 3 +-- .../internal/JakartaWebSocketServerContainer.java | 3 +-- .../server/JettyWebSocketServerContainer.java | 3 +-- 8 files changed, 23 insertions(+), 11 deletions(-) rename jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/{ => internal}/CreatorNegotiator.java (84%) diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java index c82c3b0f886..2cfd2d33936 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/module-info.java @@ -19,5 +19,4 @@ module org.eclipse.jetty.websocket.core.server requires transitive org.eclipse.jetty.websocket.core.common; exports org.eclipse.jetty.websocket.core.server; - exports org.eclipse.jetty.websocket.core.server.internal; } diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java index 729e8d1643d..e56ccdecd70 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java @@ -34,6 +34,7 @@ import org.eclipse.jetty.websocket.core.CoreSession; import org.eclipse.jetty.websocket.core.FrameHandler; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; +import org.eclipse.jetty.websocket.core.server.internal.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.internal.HandshakerSelector; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -183,7 +184,7 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener */ public void addMapping(PathSpec pathSpec, WebSocketCreator creator, FrameHandlerFactory factory, Configuration.Customizer customizer) throws WebSocketException { - mappings.put(pathSpec, new CreatorNegotiator(creator, factory, customizer)); + mappings.put(pathSpec, WebSocketNegotiator.from(creator, factory, customizer)); } /** diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java index 305e6c3fce4..ab128229348 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java @@ -16,6 +16,7 @@ package org.eclipse.jetty.websocket.core.server; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.FrameHandler; +import org.eclipse.jetty.websocket.core.server.internal.CreatorNegotiator; public interface WebSocketNegotiator extends Configuration.Customizer { @@ -33,6 +34,16 @@ public interface WebSocketNegotiator extends Configuration.Customizer */ FrameHandler negotiate(ServerUpgradeRequest request, ServerUpgradeResponse response, Callback callback); + static WebSocketNegotiator from(WebSocketCreator creator, FrameHandlerFactory factory) + { + return from(creator, factory, null); + } + + static WebSocketNegotiator from(WebSocketCreator creator, FrameHandlerFactory factory, Configuration.Customizer customizer) + { + return new CreatorNegotiator(creator, factory, customizer); + } + @Override default void customize(Configuration configurable) { diff --git a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java similarity index 84% rename from jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java rename to jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java index 1e136cf3364..4f1e7120e14 100644 --- a/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/CreatorNegotiator.java +++ b/jetty-core/jetty-websocket/websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/CreatorNegotiator.java @@ -11,13 +11,18 @@ // ======================================================================== // -package org.eclipse.jetty.websocket.core.server; +package org.eclipse.jetty.websocket.core.server.internal; import java.util.concurrent.atomic.AtomicReference; import org.eclipse.jetty.server.Context; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.FrameHandler; +import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; +import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; +import org.eclipse.jetty.websocket.core.server.WebSocketCreator; +import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; public class CreatorNegotiator extends WebSocketNegotiator.AbstractNegotiator { diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index a0da966ed72..96a43996e6b 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -44,7 +44,6 @@ import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; -import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; @@ -296,7 +295,7 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta // Perform the upgrade. JakartaWebSocketCreator creator = new JakartaWebSocketCreator(this, config, getExtensionRegistry()); - WebSocketNegotiator negotiator = new CreatorNegotiator(creator, frameHandlerFactory); + WebSocketNegotiator negotiator = WebSocketNegotiator.from(creator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); ServletContextRequest baseRequest = ServletContextRequest.getBaseRequest(request); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index 4c9589fdb70..44f3d2d7aaf 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -47,7 +47,6 @@ import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; -import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketCreator; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; @@ -220,7 +219,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements throw new IllegalStateException("Base Request not available"); ServletContextResponse baseResponse = baseRequest.getResponse(); - WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); + WebSocketNegotiator negotiator = WebSocketNegotiator.from(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); try (Blocker.Callback callback = Blocker.callback()) diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 74f6c7d1d37..2311677b56d 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -45,7 +45,6 @@ import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; -import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.eclipse.jetty.websocket.core.server.WebSocketNegotiator; @@ -297,7 +296,7 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta // Perform the upgrade. JakartaWebSocketCreator creator = new JakartaWebSocketCreator(this, config, getExtensionRegistry()); - WebSocketNegotiator negotiator = new CreatorNegotiator(creator, frameHandlerFactory); + WebSocketNegotiator negotiator = WebSocketNegotiator.from(creator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index fc1a2ad91ab..5f2729e9756 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -49,7 +49,6 @@ import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; -import org.eclipse.jetty.websocket.core.server.CreatorNegotiator; import org.eclipse.jetty.websocket.core.server.Handshaker; import org.eclipse.jetty.websocket.core.server.WebSocketCreator; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; @@ -216,7 +215,7 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } }; - WebSocketNegotiator negotiator = new CreatorNegotiator(coreCreator, frameHandlerFactory); + WebSocketNegotiator negotiator = WebSocketNegotiator.from(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); HttpChannel httpChannel = (HttpChannel)request.getAttribute(HttpChannel.class.getName()); From 9f92b6b7be410cf1abdee03abd6acdd0a3da95f6 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 17 Aug 2022 16:34:48 +1000 Subject: [PATCH 11/15] PR #8413 - changes from review Signed-off-by: Lachlan Roberts --- .../websocket/core/server/Handshaker.java | 12 +++++------ .../core/server/WebSocketCreator.java | 2 +- .../core/server/WebSocketMappings.java | 20 +++++++++---------- .../core/server/WebSocketNegotiator.java | 4 ++-- .../server/internal/RFC6455Handshaker.java | 19 ++++++++---------- .../ee10/servlet/ServletContextRequest.java | 2 -- .../ee10/servlet/ServletContextResponse.java | 2 -- .../server/JettyWebSocketCreator.java | 2 +- 8 files changed, 28 insertions(+), 35 deletions(-) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java index 3fe2c137f51..93b1ba42b35 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java @@ -32,12 +32,12 @@ public interface Handshaker boolean isWebSocketUpgradeRequest(Request request); /** - * This will attempt to upgrade a request to WebSocket. + *

Attempts to upgrade a request to WebSocket.

* - *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for - * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response - * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible - * for completing the callback and generating a response.

+ *

Returns {@code true} if the WebSocket upgrade is successful and a successful response is generated and the callback + * eventually completed, or if the WebSocket upgrade failed and a failure response is generated and the callback eventually + * completed. Returns {@code false} if a response is not generated and the caller is responsible for generating a response + * and completing the callback.

* * @param negotiator the negotiator * @param request the request @@ -45,7 +45,7 @@ public interface Handshaker * @param callback the callback * @param components the WebSocket components * @param defaultCustomizer the customizer - * @return true if the WebSocket upgrade was attempted + * @return true if a response was generated, false if a response is not generated * @throws IOException there is an error during the upgrade */ boolean upgradeRequest(WebSocketNegotiator negotiator, Request request, Response response, Callback callback, WebSocketComponents components, Configuration.Customizer defaultCustomizer) throws IOException; diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java index 81897008229..528d72ffcef 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketCreator.java @@ -29,7 +29,7 @@ public interface WebSocketCreator * *

If the creator returns null it is responsible for completing the {@link Callback} and sending a response. * But if the creator intends to return non-null WebSocket object, it MUST NOT write content to the response or - * complete the {@link Callback}.

+ * complete the {@link Callback}, but it may modify the response headers.

* * @param request the request details * @param response the response details diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java index e56ccdecd70..7f86efd370e 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketMappings.java @@ -230,12 +230,12 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener } /** - * This will attempt to find a WebSocket mapping and upgrade a request to WebSocket. + *

Attempts to find a WebSocket mapping and upgrade a request to WebSocket.

* - *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for - * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response - * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible - * for completing the callback and generating a response.

+ *

Returns {@code true} if the WebSocket upgrade is successful and a successful response is generated and the callback + * eventually completed, or if the WebSocket upgrade failed and a failure response is generated and the callback eventually + * completed. Returns {@code false} if a response is not generated and the caller is responsible for generating a response + * and completing the callback.

* * @param request the request * @param response the response @@ -258,12 +258,12 @@ public class WebSocketMappings implements Dumpable, LifeCycle.Listener } /** - * This will attempt to upgrade a request to WebSocket. + *

Attempts to find a WebSocket mapping and upgrade a request to WebSocket.

* - *

This method returns true if a WebSocket upgrade was attempted in which case this method takes responsibility for - * completing the callback and generating a response, the request may be upgraded to WebSocket or some error response - * will be sent. If this method returns false the WebSocket upgrade was not accepted and the caller is still responsible - * for completing the callback and generating a response.

+ *

Returns {@code true} if the WebSocket upgrade is successful and a successful response is generated and the callback + * eventually completed, or if the WebSocket upgrade failed and a failure response is generated and the callback eventually + * completed. Returns {@code false} if a response is not generated and the caller is responsible for generating a response + * and completing the callback.

* * @param negotiator the negotiator * @param request the request diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java index ab128229348..35b235aa9cc 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/WebSocketNegotiator.java @@ -21,11 +21,11 @@ import org.eclipse.jetty.websocket.core.server.internal.CreatorNegotiator; public interface WebSocketNegotiator extends Configuration.Customizer { /** - * Create a {@link FrameHandler} from the incoming request. + *

Creates a {@link FrameHandler} from the incoming request.

* *

If the negotiator returns null it is responsible for completing the {@link Callback} and sending a response. * If the negotiator intends to return non-null {@link FrameHandler}, it MUST NOT write content to the response or - * complete the {@link Callback}.

+ * complete the {@link Callback}, but it may modify the response headers.

* * @param request the request details * @param response the response details diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java index 51e204a6791..ee2dd2a546d 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/internal/RFC6455Handshaker.java @@ -41,16 +41,6 @@ public final class RFC6455Handshaker extends AbstractHandshaker @Override public boolean isWebSocketUpgradeRequest(Request request) { - if (!HttpVersion.HTTP_1_1.is(request.getConnectionMetaData().getProtocol())) - { - if (LOG.isDebugEnabled()) - LOG.debug("not upgraded version!=1.1 {}", request); - return false; - } - - if (!super.isWebSocketUpgradeRequest(request)) - return false; - if (!HttpMethod.GET.is(request.getMethod())) { if (LOG.isDebugEnabled()) @@ -58,7 +48,14 @@ public final class RFC6455Handshaker extends AbstractHandshaker return false; } - return true; + if (!HttpVersion.HTTP_1_1.is(request.getConnectionMetaData().getProtocol())) + { + if (LOG.isDebugEnabled()) + LOG.debug("not upgraded version!=1.1 {}", request); + return false; + } + + return super.isWebSocketUpgradeRequest(request); } @Override diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java index 33a8d22b7e3..924d3846aa9 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java @@ -31,7 +31,6 @@ import java.util.Locale; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutionException; -import java.util.concurrent.atomic.AtomicReference; import jakarta.servlet.AsyncContext; import jakarta.servlet.AsyncListener; @@ -125,7 +124,6 @@ public class ServletContextRequest extends ContextRequest implements Runnable final HttpInput _httpInput; final String _pathInContext; Charset _queryEncoding; - final AtomicReference _attachment = new AtomicReference<>(); final List _requestAttributeListeners = new ArrayList<>(); diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java index 86973e03767..8f91960a90d 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextResponse.java @@ -20,7 +20,6 @@ import java.util.Collection; import java.util.EnumSet; import java.util.Iterator; import java.util.Locale; -import java.util.concurrent.atomic.AtomicReference; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletOutputStream; @@ -86,7 +85,6 @@ public class ServletContextResponse extends ContextResponse private ResponseWriter _writer; private long _contentLength = -1; - final AtomicReference _attachment = new AtomicReference<>(); public static ServletContextResponse getBaseResponse(ServletResponse response) { diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java index 6b899401f64..8e216d6ec20 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketCreator.java @@ -23,7 +23,7 @@ package org.eclipse.jetty.ee10.websocket.server; public interface JettyWebSocketCreator { /** - * Create a websocket from the incoming request. + *

Creates a websocket from the incoming request.

* *

If no websocket is to be created (return value of null), the {@link JettyWebSocketCreator} * is responsible for sending a response with {@link JettyServerUpgradeResponse#sendError(int, String)}, From 82618073d5e5ec8ddc0da2ad95a99bb9f61777c2 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 17 Aug 2022 16:41:23 +1000 Subject: [PATCH 12/15] PR #8413 - move wrapped req/resp attributes into WebSocketConstants Signed-off-by: Lachlan Roberts --- .../jetty/websocket/core/WebSocketConstants.java | 4 ++++ .../jetty/ee10/servlet/ServletContextRequest.java | 2 -- .../internal/JakartaWebSocketServerContainer.java | 9 +++++---- .../server/JettyWebSocketServerContainer.java | 9 +++++---- .../ee10/websocket/server/JettyWebSocketServlet.java | 9 +++++---- .../server/internal/DelegatedServerUpgradeRequest.java | 4 ++-- .../internal/DelegatedServerUpgradeResponse.java | 4 ++-- .../ee10/websocket/servlet/WebSocketUpgradeFilter.java | 9 +++++---- .../org/eclipse/jetty/ee9/nested/ContextHandler.java | 3 --- .../internal/JakartaWebSocketServerContainer.java | 9 +++++---- .../server/JettyWebSocketServerContainer.java | 10 +++++----- .../ee9/websocket/server/JettyWebSocketServlet.java | 9 +++++---- .../server/internal/DelegatedServerUpgradeRequest.java | 4 ++-- .../internal/DelegatedServerUpgradeResponse.java | 4 ++-- .../ee9/websocket/servlet/WebSocketUpgradeFilter.java | 9 +++++---- 15 files changed, 52 insertions(+), 46 deletions(-) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketConstants.java b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketConstants.java index a0278c1b279..7f0ffe34509 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketConstants.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-common/src/main/java/org/eclipse/jetty/websocket/core/WebSocketConstants.java @@ -32,6 +32,10 @@ public final class WebSocketConstants public static final Duration DEFAULT_IDLE_TIMEOUT = Duration.ofSeconds(30); public static final Duration DEFAULT_WRITE_TIMEOUT = Duration.ZERO; + // Attributes for storing API requests as attributes on the base jetty-core request. + public static final String WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedRequest"; + public static final String WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedResponse"; + /** * Globally Unique Identifier for use in WebSocket handshake within {@code Sec-WebSocket-Accept} and Sec-WebSocket-Key http headers. *

diff --git a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java index 924d3846aa9..f7aa44d945c 100644 --- a/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java +++ b/jetty-ee10/jetty-ee10-servlet/src/main/java/org/eclipse/jetty/ee10/servlet/ServletContextRequest.java @@ -86,8 +86,6 @@ import org.slf4j.LoggerFactory; public class ServletContextRequest extends ContextRequest implements Runnable { public static final String __MULTIPART_CONFIG_ELEMENT = "org.eclipse.jetty.multipartConfig"; - public static final String WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedRequest"; - public static final String WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedResponse"; private static final Logger LOG = LoggerFactory.getLogger(ServletContextRequest.class); private static final Collection __defaultLocale = Collections.singleton(Locale.getDefault()); private static final int INPUT_NONE = 0; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 96a43996e6b..111d67e462f 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -41,6 +41,7 @@ import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; @@ -307,8 +308,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) { @@ -317,8 +318,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta } finally { - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index 44f3d2d7aaf..4f9eca1307a 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -45,6 +45,7 @@ import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; import org.eclipse.jetty.websocket.core.server.Handshaker; @@ -226,8 +227,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) { @@ -237,8 +238,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } finally { - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } return false; diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java index 2b410307655..d698af79d3e 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java @@ -34,6 +34,7 @@ import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; @@ -193,8 +194,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - request.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); - request.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); + request.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); + request.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); if (mapping.upgrade(request, response, callback, null)) { @@ -204,8 +205,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet } finally { - request.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - request.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java index 7855abdab3e..e8059abe3c6 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeRequest.java @@ -31,13 +31,13 @@ import java.util.stream.Collectors; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; -import org.eclipse.jetty.ee10.servlet.ServletContextRequest; import org.eclipse.jetty.ee10.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee10.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeRequest; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest @@ -52,7 +52,7 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest public DelegatedServerUpgradeRequest(ServerUpgradeRequest request) { this.httpServletRequest = (HttpServletRequest)request - .getAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + .getAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); this.upgradeRequest = request; this.queryString = httpServletRequest.getQueryString(); diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java index 6261753b0c1..5a402d2de63 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/internal/DelegatedServerUpgradeResponse.java @@ -22,13 +22,13 @@ import java.util.Set; import java.util.stream.Collectors; import jakarta.servlet.http.HttpServletResponse; -import org.eclipse.jetty.ee10.servlet.ServletContextRequest; import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee10.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee10.websocket.server.JettyServerUpgradeResponse; import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; public class DelegatedServerUpgradeResponse implements JettyServerUpgradeResponse @@ -41,7 +41,7 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons upgradeResponse = response; ServletContextResponse servletContextResponse = Response.as(response, ServletContextResponse.class); this.httpServletResponse = (HttpServletResponse)servletContextResponse.getRequest() - .getAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + .getAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } @Override diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java index 6b474ef7075..763dad797ef 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java @@ -39,6 +39,7 @@ import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.websocket.core.Configuration; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -169,8 +170,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) { @@ -180,8 +181,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable } finally { - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - baseRequest.removeAttribute(ServletContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java index c08d91d37df..3a5f46abc86 100644 --- a/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java +++ b/jetty-ee9/jetty-ee9-nested/src/main/java/org/eclipse/jetty/ee9/nested/ContextHandler.java @@ -2350,9 +2350,6 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu public static class CoreContextRequest extends ContextRequest { - public static final String WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedRequest"; - public static final String WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE = "org.eclipse.jetty.websocket.wrappedResponse"; - private final HttpChannel _httpChannel; protected CoreContextRequest(org.eclipse.jetty.server.handler.ContextHandler contextHandler, diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 2311677b56d..f0c47cc7a78 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -42,6 +42,7 @@ import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.client.WebSocketCoreClient; import org.eclipse.jetty.websocket.core.exception.InvalidSignatureException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; @@ -307,8 +308,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, defaultCustomizer)) { @@ -317,8 +318,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta } finally { - request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index 5f2729e9756..bbcebda6352 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -24,7 +24,6 @@ import java.util.function.Consumer; import jakarta.servlet.ServletContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.nested.HttpChannel; import org.eclipse.jetty.ee9.servlet.ServletContextHandler; import org.eclipse.jetty.ee9.websocket.api.Session; @@ -47,6 +46,7 @@ import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.exception.WebSocketException; import org.eclipse.jetty.websocket.core.internal.util.ReflectUtils; import org.eclipse.jetty.websocket.core.server.Handshaker; @@ -226,8 +226,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (handshaker.upgradeRequest(negotiator, baseRequest, baseResponse, callback, components, customizer)) { @@ -237,8 +237,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements } finally { - baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } return false; } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java index 6afa02695fe..a45ae43107b 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java @@ -36,6 +36,7 @@ import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.FrameHandlerFactory; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; @@ -196,8 +197,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - request.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); - request.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); + request.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, req); + request.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, resp); if (mapping.upgrade(request, response, callback, null)) { @@ -207,8 +208,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet } finally { - request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - request.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + request.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java index aa67e6d9588..7f048c95f62 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeRequest.java @@ -31,13 +31,13 @@ import java.util.stream.Collectors; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; -import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee9.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeRequest; import org.eclipse.jetty.http.BadMessageException; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.util.URIUtil; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.ServerUpgradeRequest; public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest @@ -52,7 +52,7 @@ public class DelegatedServerUpgradeRequest implements JettyServerUpgradeRequest public DelegatedServerUpgradeRequest(ServerUpgradeRequest request) { this.httpServletRequest = (HttpServletRequest)request - .getAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + .getAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); this.upgradeRequest = request; this.queryString = httpServletRequest.getQueryString(); diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java index e56e39adcd7..85fea9763dd 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/internal/DelegatedServerUpgradeResponse.java @@ -22,11 +22,11 @@ import java.util.Set; import java.util.stream.Collectors; import jakarta.servlet.http.HttpServletResponse; -import org.eclipse.jetty.ee9.nested.ContextHandler; import org.eclipse.jetty.ee9.websocket.api.ExtensionConfig; import org.eclipse.jetty.ee9.websocket.common.JettyExtensionConfig; import org.eclipse.jetty.ee9.websocket.server.JettyServerUpgradeResponse; import org.eclipse.jetty.http.HttpStatus; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.ServerUpgradeResponse; public class DelegatedServerUpgradeResponse implements JettyServerUpgradeResponse @@ -38,7 +38,7 @@ public class DelegatedServerUpgradeResponse implements JettyServerUpgradeRespons { this.upgradeResponse = response; this.httpServletResponse = (HttpServletResponse)response.getRequest() - .getAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + .getAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } @Override diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java index d2b6a1c7b51..679e4306997 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java @@ -39,6 +39,7 @@ import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.util.thread.AutoLock; import org.eclipse.jetty.websocket.core.Configuration; +import org.eclipse.jetty.websocket.core.WebSocketConstants; import org.eclipse.jetty.websocket.core.server.WebSocketMappings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -168,8 +169,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); - baseRequest.setAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE, request); + baseRequest.setAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE, response); if (mappings.upgrade(baseRequest, baseResponse, callback, defaultCustomizer)) { @@ -179,8 +180,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable } finally { - baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); - baseRequest.removeAttribute(ContextHandler.CoreContextRequest.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_REQUEST_ATTRIBUTE); + baseRequest.removeAttribute(WebSocketConstants.WEBSOCKET_WRAPPED_RESPONSE_ATTRIBUTE); } } From 206a899629c9dd7c48faad2a1b386e0330e7a99f Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 17 Aug 2022 16:53:39 +1000 Subject: [PATCH 13/15] PR #8413 - add javadoc to Handshaker.isWebSocketUpgradeRequest Signed-off-by: Lachlan Roberts --- .../eclipse/jetty/websocket/core/server/Handshaker.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java index 93b1ba42b35..a6f6d18c998 100644 --- a/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java +++ b/jetty-core/jetty-websocket/jetty-websocket-core-server/src/main/java/org/eclipse/jetty/websocket/core/server/Handshaker.java @@ -29,6 +29,15 @@ public interface Handshaker return new HandshakerSelector(); } + /** + *

A preliminary check to see if a request is likely to be a valid WebSocket Upgrade Request. If this returns true + * the {@link Request} may be a valid upgrade request, but if this returns false returns false you can avoid calling + * {@link #upgradeRequest(WebSocketNegotiator, Request, Response, Callback, WebSocketComponents, Configuration.Customizer)} + * entirely as it will always fail

+ * + * @param request the request + * @return true if the request is thought to be a valid websocket upgrade request. + */ boolean isWebSocketUpgradeRequest(Request request); /** From ac18a0aec2ebb5f2a6949417603da30ad6bf2830 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Wed, 17 Aug 2022 17:28:38 +1000 Subject: [PATCH 14/15] Issue #8448 - remove blocker usage from websocket Signed-off-by: Lachlan Roberts --- .../server/internal/JakartaWebSocketServerContainer.java | 5 +++-- .../ee10/websocket/server/JettyWebSocketServerContainer.java | 5 +++-- .../jetty/ee10/websocket/server/JettyWebSocketServlet.java | 5 +++-- .../jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java | 5 +++-- .../server/internal/JakartaWebSocketServerContainer.java | 5 +++-- .../ee9/websocket/server/JettyWebSocketServerContainer.java | 5 +++-- .../jetty/ee9/websocket/server/JettyWebSocketServlet.java | 5 +++-- .../jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java | 5 +++-- 8 files changed, 24 insertions(+), 16 deletions(-) diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index 111d67e462f..face4e5be99 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee10/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -37,7 +37,7 @@ import org.eclipse.jetty.ee10.websocket.jakarta.server.config.JakartaWebSocketSe import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.http.pathmap.UriTemplatePathSpec; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; @@ -304,7 +304,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta throw new IllegalStateException(); ServletContextResponse baseResponse = baseRequest.getResponse(); - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java index 4f9eca1307a..32ad45c2612 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServerContainer.java @@ -39,7 +39,7 @@ import org.eclipse.jetty.ee10.websocket.server.internal.DelegatedServerUpgradeRe import org.eclipse.jetty.ee10.websocket.server.internal.JettyServerFrameHandlerFactory; import org.eclipse.jetty.ee10.websocket.servlet.WebSocketUpgradeFilter; import org.eclipse.jetty.http.pathmap.PathSpec; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; @@ -223,7 +223,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements WebSocketNegotiator negotiator = WebSocketNegotiator.from(coreCreator, frameHandlerFactory); Handshaker handshaker = webSocketMappings.getHandshaker(); - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java index d698af79d3e..b519cd78294 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee10/websocket/server/JettyWebSocketServlet.java @@ -30,8 +30,8 @@ import org.eclipse.jetty.ee10.websocket.server.internal.DelegatedServerUpgradeRe import org.eclipse.jetty.ee10.websocket.server.internal.JettyServerFrameHandlerFactory; import org.eclipse.jetty.ee10.websocket.servlet.WebSocketUpgradeFilter; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.WebSocketConstants; @@ -190,7 +190,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet if (mapping.getHandshaker().isWebSocketUpgradeRequest(request)) { // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java index 763dad797ef..036089a096c 100644 --- a/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee10/jetty-ee10-websocket/jetty-ee10-websocket-servlet/src/main/java/org/eclipse/jetty/ee10/websocket/servlet/WebSocketUpgradeFilter.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.ee10.servlet.ServletContextRequest; import org.eclipse.jetty.ee10.servlet.ServletContextResponse; import org.eclipse.jetty.ee10.servlet.ServletHandler; import org.eclipse.jetty.server.handler.ContextHandler; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; @@ -166,7 +166,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable if (mappings.getHandshaker().isWebSocketUpgradeRequest(baseRequest)) { // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java index f0c47cc7a78..17af9e32c1d 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jakarta-server/src/main/java/org/eclipse/jetty/ee9/websocket/jakarta/server/internal/JakartaWebSocketServerContainer.java @@ -38,7 +38,7 @@ import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.http.pathmap.UriTemplatePathSpec; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.LifeCycle; import org.eclipse.jetty.websocket.core.WebSocketComponents; @@ -304,7 +304,8 @@ public class JakartaWebSocketServerContainer extends JakartaWebSocketClientConta Request baseRequest = httpChannel.getCoreRequest(); Response baseResponse = httpChannel.getCoreResponse(); - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java index bbcebda6352..96c91e4a74a 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServerContainer.java @@ -40,7 +40,7 @@ import org.eclipse.jetty.ee9.websocket.servlet.WebSocketUpgradeFilter; import org.eclipse.jetty.http.pathmap.PathSpec; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.component.ContainerLifeCycle; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; @@ -222,7 +222,8 @@ public class JettyWebSocketServerContainer extends ContainerLifeCycle implements Request baseRequest = httpChannel.getCoreRequest(); Response baseResponse = httpChannel.getCoreResponse(); - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attachments on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java index a45ae43107b..66a64f1595b 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-jetty-server/src/main/java/org/eclipse/jetty/ee9/websocket/server/JettyWebSocketServlet.java @@ -32,8 +32,8 @@ import org.eclipse.jetty.ee9.websocket.server.internal.JettyServerFrameHandlerFa import org.eclipse.jetty.ee9.websocket.servlet.WebSocketUpgradeFilter; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; import org.eclipse.jetty.util.Callback; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.websocket.core.Configuration; import org.eclipse.jetty.websocket.core.WebSocketComponents; import org.eclipse.jetty.websocket.core.WebSocketConstants; @@ -193,7 +193,8 @@ public abstract class JettyWebSocketServlet extends HttpServlet if (mapping.getHandshaker().isWebSocketUpgradeRequest(request)) { // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. diff --git a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java index 679e4306997..cd7fb7f4297 100644 --- a/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java +++ b/jetty-ee9/jetty-ee9-websocket/jetty-ee9-websocket-servlet/src/main/java/org/eclipse/jetty/ee9/websocket/servlet/WebSocketUpgradeFilter.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.ee9.servlet.FilterMapping; import org.eclipse.jetty.ee9.servlet.ServletHandler; import org.eclipse.jetty.server.Request; import org.eclipse.jetty.server.Response; -import org.eclipse.jetty.util.Blocker; +import org.eclipse.jetty.util.FutureCallback; import org.eclipse.jetty.util.annotation.ManagedObject; import org.eclipse.jetty.util.component.Dumpable; import org.eclipse.jetty.util.component.LifeCycle; @@ -165,7 +165,8 @@ public class WebSocketUpgradeFilter implements Filter, Dumpable if (mappings.getHandshaker().isWebSocketUpgradeRequest(baseRequest)) { // provide a null default customizer the customizer will be on the negotiator in the mapping - try (Blocker.Callback callback = Blocker.callback()) + FutureCallback callback = new FutureCallback(); + try { // Set the wrapped req and resp as attributes on the ServletContext Request/Response, so they // are accessible when websocket-core calls back the Jetty WebSocket creator. From d40c9d4a92a6d562ee2358a2ba1c72fafbabbe48 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Thu, 8 Sep 2022 10:44:46 +1000 Subject: [PATCH 15/15] Merge remote-tracking branch 'origin/jetty-12.0.x' into jetty-12.0.x-websocket-upgrade-contract