I have a question regarding the example code in Play WS documentation, Processing large responses
def downloadFile = Action.async {
// Make the request
ws.url(url).withMethod("GET").stream().map { response =>
// Check that the response was successful
if (response.status == 200) {
// Get the content type
val contentType = response.headers.get("Content-Type").flatMap(_.headOption)
.getOrElse("application/octet-stream")
// If there's a content length, send that, otherwise return the body chunked
response.headers.get("Content-Length") match {
case Some(Seq(length)) =>
Ok.sendEntity(HttpEntity.Streamed(response.bodyAsSource, Some(length.toLong), Some(contentType)))
case _ =>
Ok.chunked(response.bodyAsSource).as(contentType)
}
} else {
//shouldn't the response.bodyAsSource be consumed here?
BadGateway
}
}
}
In the else branch, shouldn’t we consume the streamed response with something like response.bodyAsSource.to(Sink.ignore).run()
otherwise the socket stays open? I don’t have any problem in practice, this is just a theoretical question…