diff --git a/docs/src/main/paradox/server-side/http2.md b/docs/src/main/paradox/server-side/http2.md index 4dd3b0689..dc8426ee9 100644 --- a/docs/src/main/paradox/server-side/http2.md +++ b/docs/src/main/paradox/server-side/http2.md @@ -154,6 +154,10 @@ Scala Java : @@snip[Http2Test.java](/docs/src/test/java/docs/http/javadsl/Http2Test.java) { #trailingHeaders } +For a response that permits content, the attribute also works with an empty entity: the server sends the +trailers directly after the initial headers, without a DATA frame. Responses to HEAD requests still end +with their initial headers. + Pekko HTTP renders outgoing HTTP/2 trailer field names in lowercase, as required by [RFC 9113, Section 8.2](https://www.rfc-editor.org/rfc/rfc9113.html#section-8.2). Field values are unchanged. diff --git a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/Http2StreamHandling.scala b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/Http2StreamHandling.scala index 74c4622a5..77c025b78 100644 --- a/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/Http2StreamHandling.scala +++ b/http-core/src/main/scala/org/apache/pekko/http/impl/engine/http2/Http2StreamHandling.scala @@ -180,6 +180,11 @@ private[http2] trait Http2StreamHandling extends GraphStageLogic with LogHelper val outStream = OutStream(stream) updateState(stream.streamId, _.handleOutgoingCreated(outStream, stream.correlationAttributes), "handleOutgoingCreated") + // A strict empty entity has no DATA to schedule and no upstream completion callback to send its trailers. + outStream.endStreamIfPossible().foreach { frame => + multiplexer.pushControlFrame(frame) + handleOutgoingEnded(stream.streamId) + } } } else // stream was cancelled by peer before our response was ready 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..848319f19 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 @@ -50,6 +50,9 @@ private[http2] class ResponseRendering(settings: ServerSettings, val log: Loggin headerPairs += ":status" -> response.status.intValue.toString } + override protected def endStreamOnEmptyEntity(response: HttpResponse, hasTrailers: Boolean): Boolean = + !response.status.allowsEntity || !hasTrailers + override lazy val peerIdHeader: Option[(String, String)] = settings.serverHeader.map(h => h.lowercaseName -> h.value) // Mirrors the status based part of the rules the HTTP/1.1 renderer applies through @@ -125,6 +128,8 @@ private[http2] sealed abstract class MessageRendering[R <: HttpMessage] extends */ protected def contentLengthAllowed(r: R): Boolean = true + protected def endStreamOnEmptyEntity(r: R, hasTrailers: Boolean): Boolean = true + def apply(r: R): Http2SubStream = { val headerPairs = initialHeaderPairs(r) @@ -134,7 +139,6 @@ private[http2] sealed abstract class MessageRendering[R <: HttpMessage] extends shouldRenderAutoHeaders = true, isTrailer = false, dateHeaderRendering) val streamId = nextStreamId(r) - val headersFrame = ParsedHeadersFrame(streamId, endStream = r.entity.isKnownEmpty, headerPairs.result(), None, None) val trailingHeadersFrame = r.attribute(AttributeKeys.trailer) match { case Some(trailer) if trailer.headers.nonEmpty => @@ -148,6 +152,9 @@ private[http2] sealed abstract class MessageRendering[R <: HttpMessage] extends case _ => OptionVal.None } + val endStream = r.entity.isKnownEmpty && endStreamOnEmptyEntity(r, + trailingHeadersFrame.isDefined && trailingHeadersFrame.get.keyValuePairs.nonEmpty) + val headersFrame = ParsedHeadersFrame(streamId, endStream, headerPairs.result(), None, None) Http2SubStream(r.entity, headersFrame, trailingHeadersFrame, r.attributes.filter(_._2.isInstanceOf[RequestResponseAssociation])) } diff --git a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerSpec.scala b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerSpec.scala index 3b6c1c2ad..50f791904 100644 --- a/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerSpec.scala +++ b/http2-tests/src/test/scala/org/apache/pekko/http/impl/engine/http2/Http2ServerSpec.scala @@ -538,6 +538,19 @@ class Http2ServerSpec extends Http2SpecWithMaterializer(""" val trailingResponseHeaders = network.expectDecodedResponseHEADERSPairs(streamId) trailingResponseHeaders shouldEqual Seq("etag" -> etag.value, "x-result" -> "Complete") }) + + "send trailing headers directly after the initial headers of an empty response".inAssertAllStagesStopped( + new TestSetup with RequestResponseProbes { + val streamId = 1 + val etag = ETag("saved") + network.sendRequest(streamId, Get("/")) + user.expectRequest() + user.emitResponse(streamId, HttpResponse().addAttribute(AttributeKeys.trailer, Trailer(etag))) + + network.expectDecodedResponseHEADERSPairs(streamId, endStream = false) should contain(":status" -> "200") + network.expectDecodedResponseHEADERSPairs(streamId, endStream = true) shouldEqual Seq("etag" -> etag.value) + }) + "ignore the reserved bit of a stream identifier".inAssertAllStagesStopped( new TestSetup with RequestResponseProbes { // RFC 9113 5.1.1: the high bit of the stream identifier is reserved and must be ignored when receiving, @@ -693,6 +706,12 @@ class Http2ServerSpec extends Http2SpecWithMaterializer(""" network.expectDecodedResponseHEADERSPairs(streamId = TheStreamId).toMap should contain(":status" -> "200") }) + "suppress trailing headers for a HEAD response".inAssertAllStagesStopped(new HeadRequestSetup { + sendHeadRequest() + user.emitResponse(TheStreamId, HttpResponse().addAttribute(AttributeKeys.trailer, Trailer(ETag("saved")))) + network.expectDecodedResponseHEADERSPairs(TheStreamId, endStream = true) should contain(":status" -> "200") + }) + // RFC 9110 section 9.3.2: the server MUST NOT send content in a response to a HEAD request. The headers are // still rendered as they would be for a GET, so that the peer learns the size of the resource. "not send the response entity as DATA frames".inAssertAllStagesStopped(new HeadRequestSetup { @@ -778,6 +797,11 @@ class Http2ServerSpec extends Http2SpecWithMaterializer(""" pairs.keySet should not contain "content-length" }) + "suppress trailing headers for a 204 response".inAssertAllStagesStopped(new GetRequestSetup { + val response = HttpResponse(StatusCodes.NoContent).addAttribute(AttributeKeys.trailer, Trailer(ETag("saved"))) + responsePairs(response) should contain(":status" -> "204") + }) + // 205 is deliberately not exempt: RFC 9112 section 6.3 only lets 1xx, 204 and 304 be self delimiting, so a 205 // has to be framed. This is the case PR #962 fixed for HTTP/1.1 and the one http4s/http4s#7919 reports. "render it for a 205".inAssertAllStagesStopped(new GetRequestSetup {