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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private[http] class HttpResponseParser(protected val settings: ParserSettings,

private var contextForCurrentResponse: Option[ResponseContext] = None
private var statusCode: StatusCode = StatusCodes.OK
private var pendingResetContentResponse: Option[ResponseStart] = None

final override val isResponseParser = true

Expand All @@ -56,9 +57,21 @@ private[http] class HttpResponseParser(protected val settings: ParserSettings,

final def onUpstreamFinish(): Boolean = shouldComplete()

override final def emit(output: ResponseOutput): Unit = {
if (output == MessageEnd) contextForCurrentResponse = None
super.emit(output)
override final def emit(output: ResponseOutput): Unit = output match {
case EntityChunk(chunk) if pendingResetContentResponse.isDefined =>
if (!chunk.isLastChunk) throw new ParsingException("A 205 response must not contain content")
case MessageEnd =>
pendingResetContentResponse.foreach(super.emit)
pendingResetContentResponse = None
contextForCurrentResponse = None
super.emit(output)
case EntityStreamError(info) if pendingResetContentResponse.isDefined =>
pendingResetContentResponse = None
super.emit(MessageStartError(StatusCodes.BadRequest, info))
case _: MessageStartError =>
pendingResetContentResponse = None
super.emit(output)
case _ => super.emit(output)
}

override protected def parseMessage(input: ByteString, offset: Int): StateResult =
Expand Down Expand Up @@ -146,7 +159,8 @@ private[http] class HttpResponseParser(protected val settings: ParserSettings,

def emitResponseStart(
createEntity: EntityCreator[ResponseOutput, ResponseEntity],
headers: List[HttpHeader] = headers) = {
headers: List[HttpHeader] = headers,
defer: Boolean = false) = {

val attributes: Map[AttributeKey[?], Any] =
if (settings.includeSslSessionAttribute) Map(AttributeKeys.sslSession -> SslSessionInfo(sslSession))
Expand All @@ -162,7 +176,9 @@ private[http] class HttpResponseParser(protected val settings: ParserSettings,
trigger.tryFailure(OneHundredContinueError)
true
}
emit(ResponseStart(statusCode, protocol, attributes, headers, createEntity, close))
val start = ResponseStart(statusCode, protocol, attributes, headers, createEntity, close)
if (defer) pendingResetContentResponse = Some(start)
else emit(start)
}

def finishEmptyResponse() =
Expand All @@ -183,7 +199,24 @@ private[http] class HttpResponseParser(protected val settings: ParserSettings,
startNewMessage(input, bodyStart)
}

if (statusCode.allowsEntity) {
// A 205 forbids content, but unlike 204 it still uses HTTP/1 message framing (RFC 9112, 6.3).
// Keep its public entity known-empty. An empty chunked body is consumed before the response is published.
// Without Content-Length or Transfer-Encoding it is strictly close-delimited, but since it can't carry content
// it is delivered immediately, as before, so a server that keeps the connection open doesn't stall the client.
if (statusCode == StatusCodes.ResetContent &&
contextForCurrentResponse.get.requestMethod != HttpMethods.HEAD &&
contextForCurrentResponse.get.requestMethod != HttpMethods.CONNECT) {
if (isChunked) {
if (clh.isEmpty) {
emitResponseStart(emptyEntity(cth), defer = true)
parseChunk(input, bodyStart, closeAfterResponseCompletion, totalBytesRead = 0L)
} else failMessageStart("A chunked response must not contain a Content-Length header")
} else clh match {
case Some(`Content-Length`(0)) => finishEmptyResponse()
case Some(_) => failMessageStart("A 205 response must not contain content")
case None => finishEmptyResponse()
}
} else if (statusCode.allowsEntity) {
contextForCurrentResponse.get.requestMethod match {
case HttpMethods.HEAD => clh match {
case Some(`Content-Length`(contentLength)) if contentLength > 0 =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,47 @@ class LowLevelOutgoingConnectionSpec extends PekkoSpecWithMaterializer with Insi
}
}

"handle empty 205 responses on a persistent connection" should {
for ((framing, body) <- Seq(
"Content-Length: 0" -> "",
"Transfer-Encoding: chunked" -> "0\n\n")) {
s"consume $framing before the next response" in new TestSetup {
sendStandardRequest()
sendWireData(s"HTTP/1.1 205 Reset Content\n$framing\n\n$body")
val reset = expectResponse()
reset.status shouldEqual StatusCodes.ResetContent
reset.entity.isKnownEmpty shouldBe true

sendStandardRequest()
sendWireData("HTTP/1.1 200 OK\nContent-Length: 0\n\n")
expectResponse() shouldEqual HttpResponse()

requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}
}
}

"deliver an empty 205 without framing headers immediately on a persistent connection" in new TestSetup {
sendStandardRequest()
sendWireData("HTTP/1.1 205 Reset Content\n\n")
val reset = expectResponse()
reset.status shouldEqual StatusCodes.ResetContent
reset.entity.isKnownEmpty shouldBe true

// A server that keeps the connection open must not stall the next response.
sendStandardRequest()
sendWireData("HTTP/1.1 200 OK\nContent-Length: 0\n\n")
expectResponse() shouldEqual HttpResponse()

requestsSub.sendComplete()
netOut.expectComplete()
netInSub.sendComplete()
responses.expectComplete()
}

"handle several requests on one persistent connection".which {
"has a first response that was chunked" in new TestSetup {
requestsSub.sendNext(HttpRequest())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,27 @@ abstract class ResponseParserSpec(mode: String, newLine: String) extends PekkoSp
closeAfterResponseCompletion shouldEqual Seq(false)
}

"a 205 response only after its empty transfer framing completes" in new Test {
val parser = new HttpResponseParser(parserSettings, HttpHeaderParser(parserSettings, system.log))
parser.setContextForNextResponse(HttpResponseParser.ResponseContext(GET, None))
parser.parseBytes(ByteString(prep("HTTP/1.1 205 Reset Content\nTransfer-Encoding: chunked\n\n"))) shouldEqual
NeedMoreData
parser.parseBytes(ByteString("0")) shouldEqual NeedMoreData
parser.parseBytes(ByteString(newLine)) shouldEqual NeedMoreData
val start = parser.parseBytes(ByteString(newLine)).asInstanceOf[ResponseStart]
start.statusCode shouldEqual ResetContent
start.createEntity shouldEqual StrictEntityCreator(HttpEntity.Empty)
parser.onPull() shouldEqual MessageEnd
}

"an empty 205 response without framing headers immediately" in new Test {
"HTTP/1.1 205 Reset Content\n\n" should parseTo(GET, HttpResponse(ResetContent))
}

"a 205 response to HEAD without waiting for framing" in new Test {
"HTTP/1.1 205 Reset Content\n\n" should parseTo(HEAD, HttpResponse(ResetContent))
}

"a response with a simple body" in new Test {
collectBlocking(rawParse(
GET,
Expand Down
Loading