diff --git a/docs/src/main/paradox/client-side/websocket-support.md b/docs/src/main/paradox/client-side/websocket-support.md index 4df03069b..9c3e85a65 100644 --- a/docs/src/main/paradox/client-side/websocket-support.md +++ b/docs/src/main/paradox/client-side/websocket-support.md @@ -44,6 +44,12 @@ Make sure to read and understand the section about [Half-Closed WebSockets](#hal when using WebSockets for one-way communication may not be what you would expect. @@@ +## Opening handshake headers + +Use @scala[`WebSocketRequest.extraHeaders`]@java[`WebSocketRequest.addHeader`] to add application headers such as +`Authorization` or `Origin`. Pekko HTTP generates `Sec-WebSocket-Key` and `Sec-WebSocket-Version` for the opening +handshake; caller-supplied headers with either of these names are not sent, regardless of name casing. + ## Opening handshake timeout Set `pekko.http.client.websocket.opening-timeout` to bound how long the client waits for a complete upgrade diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/Handshake.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/Handshake.scala index a776964fd..346495cb6 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/Handshake.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/ws/Handshake.scala @@ -274,13 +274,18 @@ private[http] object Handshake { val protocol = if (subprotocols.nonEmpty) `Sec-WebSocket-Protocol`(subprotocols) :: Nil else Nil - // version, protocol, extensions, origin + + // These singleton fields are generated for the handshake (RFC 6455 sections 11.3.1 and 11.3.5). + val additionalHeaders = extraHeaders.filterNot { header => + val name = header.lowercaseName + name == `Sec-WebSocket-Key`.lowercaseName || name == `Sec-WebSocket-Version`.lowercaseName + } val headers = Seq( UpgradeHeader, ConnectionUpgradeHeader, key, - SecWebSocketVersionHeader) ++ protocol ++ extraHeaders + SecWebSocketVersionHeader) ++ protocol ++ additionalHeaders (HttpRequest(HttpMethods.GET, uri.toRelative, headers), key) } diff --git a/http-core/src/main/scala/org/apache/pekko/http/javadsl/model/ws/WebSocketRequest.scala b/http-core/src/main/scala/org/apache/pekko/http/javadsl/model/ws/WebSocketRequest.scala index 29ffcba20..45f40cc3b 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/javadsl/model/ws/WebSocketRequest.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/javadsl/model/ws/WebSocketRequest.scala @@ -26,6 +26,8 @@ abstract class WebSocketRequest { /** * Return a copy of this request that contains the given additional header. + * The opening handshake generates `Sec-WebSocket-Key` and `Sec-WebSocket-Version` itself, + * so additional headers with these names are not sent. */ def addHeader(header: HttpHeader): WebSocketRequest diff --git a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/ws/WebSocketRequest.scala b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/ws/WebSocketRequest.scala index d8d36897c..c097d3581 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/ws/WebSocketRequest.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/scaladsl/model/ws/WebSocketRequest.scala @@ -18,7 +18,8 @@ import org.apache.pekko.http.scaladsl.model.{ HttpHeader, Uri } /** * Represents a WebSocket request. * @param uri The target URI to connect to. - * @param extraHeaders Extra headers to add to the WebSocket request. + * @param extraHeaders Extra headers to add to the WebSocket request. The opening handshake generates + * `Sec-WebSocket-Key` and `Sec-WebSocket-Version` itself, so extra headers with these names are not sent. * @param subprotocol WebSocket subprotocols (comma separated) if required. */ final case class WebSocketRequest( diff --git a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala index 658b46a9f..725921305 100644 --- a/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala +++ b/http-core/src/test/scala/org/apache/pekko/http/impl/engine/ws/WebSocketClientSpec.scala @@ -31,6 +31,9 @@ import pekko.http.scaladsl.Http import pekko.http.scaladsl.model.headers.{ Connection, `Sec-WebSocket-Extensions`, + `Sec-WebSocket-Key`, + `Sec-WebSocket-Protocol`, + `Sec-WebSocket-Version`, `User-Agent`, HttpOrigin, Origin, @@ -39,7 +42,7 @@ import pekko.http.scaladsl.model.headers.{ WebSocketExtension } import pekko.http.scaladsl.model.ws._ -import pekko.http.scaladsl.model.Uri +import pekko.http.scaladsl.model.{ HttpHeader, Uri } import pekko.stream.scaladsl._ import pekko.stream.testkit.{ TestPublisher, TestSubscriber, Utils } import pekko.util.ByteString @@ -57,6 +60,38 @@ class WebSocketClientSpec extends PekkoSpecWithMaterializer("pekko.http.client.w "establish a websocket connection when the user requests it" in new EstablishedConnectionSetup with ClientEchoes + val target = Uri("ws://example.org/ws") + val callerKey = `Sec-WebSocket-Key`(new Array[Byte](16)) + val extraSingletons: Seq[(String, Seq[HttpHeader])] = Seq( + "modeled Scala" -> Seq(callerKey, `Sec-WebSocket-Version`(Seq(13))), + "mixed-case raw Java" -> + pekko.http.javadsl.model.ws.WebSocketRequest.create(target.toString) + .addHeader( + pekko.http.javadsl.model.headers.RawHeader.create("sEc-WeBsOcKeT-kEy", callerKey.value)) + .addHeader(pekko.http.javadsl.model.headers.RawHeader.create("SeC-WeBsOcKeT-vErSiOn", "13")) + .asScala.extraHeaders) + extraSingletons.foreach { case (label, conflictingHeaders) => + s"build one generated handshake key and version with $label extra headers" in { + val callerHeaders: Seq[HttpHeader] = Seq( + Connection(List("x-metadata")), + RawHeader("X-Metadata", "trace"), + Origin(HttpOrigin("https://app.example.com")), + `Sec-WebSocket-Extensions`(Seq(WebSocketExtension("x-custom"))), + `Sec-WebSocket-Protocol`(Seq("additional"))) + val (request, key) = + Handshake.Client.buildRequest(target, conflictingHeaders ++ callerHeaders, Seq("echo"), new Random(0)) + + request.headers.filter(_.is("sec-websocket-key")) shouldEqual Seq(key) + key.value should not be callerKey.value + request.headers.filter(_.is("sec-websocket-version")) shouldEqual Seq(Handshake.SecWebSocketVersionHeader) + request.headers.filter(_.is("sec-websocket-protocol")) shouldEqual Seq( + `Sec-WebSocket-Protocol`(Seq("echo")), callerHeaders.last) + request.headers.filter(_.is("connection")) shouldEqual Seq( + Handshake.ConnectionUpgradeHeader, callerHeaders.head) + request.headers.takeRight(callerHeaders.size) shouldEqual callerHeaders + } + } + "send the caller's Origin header when establishing a WebSocket connection" in new TestSetup with ClientEchoes { override def clientLayer: Http.WebSocketClientLayer = Http(system).webSocketClientLayer(