diff --git a/docs/src/main/paradox/client-side/http2.md b/docs/src/main/paradox/client-side/http2.md index c003a1748..2f9309cd0 100644 --- a/docs/src/main/paradox/client-side/http2.md +++ b/docs/src/main/paradox/client-side/http2.md @@ -121,3 +121,5 @@ Java : @@snip [HttpClientExampleDocTest.java](/docs/src/test/java/docs/http/javadsl/Http2ClientApp.java) { #response-future-association } Connection-specific headers set on the model are dropped with a warning when sent over HTTP/2. + +The `:authority` pseudo-header carries the request host and port, without the optional URI userinfo. diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRendering.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRendering.scala index bc0fcf9aa..55a9bd59d 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRendering.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRendering.scala @@ -87,14 +87,18 @@ private[http2] class RequestRendering( headerPairs } - // a `Host` header names the authority of a relative URI, as it does in HTTP/1.1, ahead of the connection's - private def authority(request: HttpRequest): String = - if (!request.uri.authority.isEmpty) request.uri.authority.toString - else - request.header[Host] match { - case Some(host) if !host.isEmpty => Uri.Authority(host.host, host.port).toString - case _ => connectionAuthority.toString - } + private def authority(request: HttpRequest): String = { + val uriAuthority = request.uri.authority + val effectiveAuthority = + if (uriAuthority.nonEmpty) uriAuthority + else + // A Host header names the authority of a relative URI ahead of the connection's authority. + request.header[Host] match { + case Some(host) if !host.isEmpty => Uri.Authority(host.host, host.port) + case _ => connectionAuthority + } + HttpMessageRendering.renderRequestAuthority(effectiveAuthority) + } // `Raw-Request-URI` is a SyntheticHeader, so it is already excluded from the rendered header block by the // `renderInRequests` filter and is only consumed here. As in HTTP/1.1, the value is taken as given; the header @@ -157,6 +161,11 @@ private[http2] sealed abstract class MessageRendering[R <: HttpMessage] extends @InternalApi private[http2] object HttpMessageRendering { + // RFC 9113 Section 8.3.1 excludes userinfo from the HTTP(S) :authority field. + def renderRequestAuthority(authority: Uri.Authority): String = + if (authority.userinfo.isEmpty) authority.toString + else authority.copy(userinfo = "").toString + // One policy for initial fields and both streamed and attribute-supplied trailers. def allowsHeader(header: HttpHeader, isServer: Boolean, isTrailer: Boolean, log: LoggingAdapter): Boolean = { diff --git a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRenderingSpec.scala b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRenderingSpec.scala index 1b8980da4..b58451c54 100644 --- a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRenderingSpec.scala +++ b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/HttpMessageRenderingSpec.scala @@ -150,6 +150,19 @@ class HttpMessageRenderingSpec extends AnyWordSpec with Matchers { value1.exists(_._1 == "date") shouldBe false } + "render only the host and port of a modeled HTTP/2 request authority" in { + val host = Uri.Host("example.test") + val ipv6 = Uri.Host("[2001:db8::7]") + val syntheticUserInfo = "synthetic-component" + val cases = Seq( + Uri.Authority(host) -> "example.test", + Uri.Authority(host, userinfo = syntheticUserInfo) -> "example.test", + Uri.Authority(host, 8443, syntheticUserInfo) -> "example.test:8443", + Uri.Authority(ipv6, 8443, syntheticUserInfo) -> "[2001:db8::7]:8443") + for ((authority, expected) <- cases) + HttpMessageRendering.renderRequestAuthority(authority) shouldBe expected + } + "take ':scheme' and ':authority' of a relative URI from the connection" in { // RFC 9113 8.3.1: ':scheme' is mandatory and ':authority' must not be empty pseudoHeaders(HttpRequest(uri = "/foo"), "https", Uri.Authority(Uri.Host("example.com"))) shouldEqual