diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java index c6c329d2507..dc3a9219714 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java @@ -372,7 +372,7 @@ public class HttpRequest implements Request } @Override - public HttpFields.Mutable getHeaders() + public HttpFields getHeaders() { return headers; } diff --git a/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/ClientUpgradeRequest.java b/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/ClientUpgradeRequest.java index ab74b874ec1..5ce26793ba6 100644 --- a/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/ClientUpgradeRequest.java +++ b/jetty-websocket/websocket-core-client/src/main/java/org/eclipse/jetty/websocket/core/client/ClientUpgradeRequest.java @@ -38,7 +38,6 @@ import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.api.Response; import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.http.HttpField; -import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpScheme; import org.eclipse.jetty.http.HttpStatus; @@ -154,22 +153,26 @@ public abstract class ClientUpgradeRequest extends HttpRequest implements Respon public void setSubProtocols(String... protocols) { - HttpFields.Mutable headers = getHeaders(); - headers.remove(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL); - for (String protocol : protocols) + headers(headers -> { - headers.add(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL, protocol); - } + headers.remove(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL); + for (String protocol : protocols) + { + headers.add(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL, protocol); + } + }); } public void setSubProtocols(List protocols) { - HttpFields.Mutable headers = getHeaders(); - headers.remove(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL); - for (String protocol : protocols) + headers(headers -> { - headers.add(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL, protocol); - } + headers.remove(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL); + for (String protocol : protocols) + { + headers.add(HttpHeader.SEC_WEBSOCKET_SUBPROTOCOL, protocol); + } + }); } @Override @@ -282,7 +285,7 @@ public abstract class ClientUpgradeRequest extends HttpRequest implements Respon .collect(Collectors.joining(",")); if (!StringUtil.isEmpty(extensionString)) - getHeaders().add(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, extensionString); + headers(headers -> headers.add(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, extensionString)); // Notify the listener which may change the headers directly. notifyUpgradeListeners((listener) -> listener.onHandshakeRequest(this)); diff --git a/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java b/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java index e55ec678902..46659dfd430 100644 --- a/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java +++ b/jetty-websocket/websocket-core-tests/src/test/java/org/eclipse/jetty/websocket/core/WebSocketNegotiationTest.java @@ -413,13 +413,12 @@ public class WebSocketNegotiationTest extends WebSocketTester upgradeRequest.setSubProtocols("test"); upgradeRequest.addExtensions("permessage-deflate;server_no_context_takeover"); - CompletableFuture extensionHeader = new CompletableFuture<>(); upgradeRequest.addListener(new UpgradeListener() { @Override public void onHandshakeRequest(HttpRequest request) { - request.getHeaders().put(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, "permessage-deflate"); + request.headers(headers -> headers.put(HttpHeader.SEC_WEBSOCKET_EXTENSIONS, "permessage-deflate")); } }); diff --git a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JsrUpgradeListener.java b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JsrUpgradeListener.java index 64c6d60ba4a..c92bd34eb19 100644 --- a/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JsrUpgradeListener.java +++ b/jetty-websocket/websocket-javax-client/src/main/java/org/eclipse/jetty/websocket/javax/client/internal/JsrUpgradeListener.java @@ -33,7 +33,7 @@ import org.eclipse.jetty.websocket.core.client.UpgradeListener; public class JsrUpgradeListener implements UpgradeListener { - private Configurator configurator; + private final Configurator configurator; public JsrUpgradeListener(Configurator configurator) { @@ -46,7 +46,7 @@ public class JsrUpgradeListener implements UpgradeListener if (configurator == null) return; - HttpFields.Mutable fields = request.getHeaders(); + HttpFields fields = request.getHeaders(); Map> originalHeaders = new HashMap<>(); fields.forEach(field -> { @@ -59,8 +59,11 @@ public class JsrUpgradeListener implements UpgradeListener configurator.beforeRequest(originalHeaders); // Reset headers on HttpRequest per configurator - fields.clear(); - originalHeaders.forEach(fields::put); + request.headers(headers -> + { + headers.clear(); + originalHeaders.forEach(headers::put); + }); } @Override diff --git a/jetty-websocket/websocket-javax-tests/src/main/java/org/eclipse/jetty/websocket/javax/tests/NetworkFuzzer.java b/jetty-websocket/websocket-javax-tests/src/main/java/org/eclipse/jetty/websocket/javax/tests/NetworkFuzzer.java index b0ac7588cf4..f1be31e72e4 100644 --- a/jetty-websocket/websocket-javax-tests/src/main/java/org/eclipse/jetty/websocket/javax/tests/NetworkFuzzer.java +++ b/jetty-websocket/websocket-javax-tests/src/main/java/org/eclipse/jetty/websocket/javax/tests/NetworkFuzzer.java @@ -30,7 +30,6 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.io.EndPoint; import org.eclipse.jetty.util.BufferUtil; import org.eclipse.jetty.util.Callback; @@ -72,11 +71,13 @@ public class NetworkFuzzer extends Fuzzer.Adapter implements Fuzzer, AutoCloseab this.upgradeRequest = new RawUpgradeRequest(client, wsURI); if (requestHeaders != null) { - HttpFields.Mutable fields = this.upgradeRequest.getHeaders(); - requestHeaders.forEach((name, value) -> + this.upgradeRequest.headers(fields -> { - fields.remove(name); - fields.put(name, value); + requestHeaders.forEach((name, value) -> + { + fields.remove(name); + fields.put(name, value); + }); }); } this.client.start(); diff --git a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/impl/JettyClientUpgradeRequest.java b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/impl/JettyClientUpgradeRequest.java index 0f6ddeeef99..0c762a960e7 100644 --- a/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/impl/JettyClientUpgradeRequest.java +++ b/jetty-websocket/websocket-jetty-client/src/main/java/org/eclipse/jetty/websocket/client/impl/JettyClientUpgradeRequest.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.stream.Collectors; import org.eclipse.jetty.client.HttpResponse; -import org.eclipse.jetty.http.HttpFields; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpVersion; import org.eclipse.jetty.io.EndPoint; @@ -49,18 +48,14 @@ public class JettyClientUpgradeRequest extends ClientUpgradeRequest if (request != null) { // Copy request details into actual request - HttpFields.Mutable fields = getHeaders(); - request.getHeaders().forEach(fields::put); + headers(fields -> request.getHeaders().forEach(fields::put)); // Copy manually created Cookies into place List cookies = request.getCookies(); if (cookies != null) { // TODO: remove existing Cookie header (if set)? - for (HttpCookie cookie : cookies) - { - fields.add(HttpHeader.COOKIE, cookie.toString()); - } + headers(fields -> cookies.forEach(cookie -> fields.add(HttpHeader.COOKIE, cookie.toString()))); } // Copy sub-protocols