Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/src/main/paradox/client-side/websocket-support.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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(
Expand Down