Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist session and cookies set on result #35

Merged
merged 2 commits into from
Apr 18, 2024
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
10 changes: 5 additions & 5 deletions play-v29/src/main/scala/play.filters.brotli/BrotliFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material
result.body match {

case HttpEntity.Strict(data, contentType) =>
Future.successful(Result(header, compressStrictEntity(data, contentType)))
Future.successful(result.copy(header = header, body = compressStrictEntity(data, contentType)))


case entity @ HttpEntity.Streamed(_, Some(contentLength), contentType) if contentLength <= config.chunkedThreshold =>
// It's below the chunked threshold, so buffer then compress and send
entity.consumeData.map { data =>
Result(header, compressStrictEntity(data, contentType))
result.copy(header = header, body = compressStrictEntity(data, contentType))
}

case HttpEntity.Streamed(data, _, contentType) if request.version == HttpProtocol.HTTP_1_0 =>
Expand All @@ -109,7 +109,7 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material
case HttpEntity.Streamed(data, _, contentType) =>
// It's above the chunked threshold, compress through the brotli flow, and send as chunked
val compressed = data.via(createBrotliFlow).map(d => HttpChunk.Chunk(d))
Future.successful(Result(header, HttpEntity.Chunked(compressed, contentType)))
Future.successful(result.copy(header = header, body = HttpEntity.Chunked(compressed, contentType)))

case HttpEntity.Chunked(chunks, contentType) =>
val wrappedFlow = Flow.fromGraph(GraphDSL.create[FlowShape[HttpChunk, HttpChunk]]() { implicit builder =>
Expand Down Expand Up @@ -138,7 +138,7 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material

new FlowShape(broadcast.in, concat.out)
})
Future.successful(Result(header, HttpEntity.Chunked(chunks via wrappedFlow, contentType)))
Future.successful(result.copy(header = header, body = HttpEntity.Chunked(chunks via wrappedFlow, contentType)))
}
} else {
Future.successful(result)
Expand Down Expand Up @@ -283,4 +283,4 @@ trait BrotliFilterComponents {

lazy val brotliFilterConfig: BrotliFilterConfig = BrotliFilterConfig.fromConfiguration(configuration)
lazy val brotliFilter: BrotliFilter = new BrotliFilter(brotliFilterConfig)(materializer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import scala.util.Random
import org.specs2.matcher.{DataTables, MatchResult}

import com.aayushatharva.brotli4j.decoder.BrotliInputStream;
import play.api.mvc.Cookie

object BrotliFilterSpec extends PlaySpecification with DataTables {

Expand Down Expand Up @@ -142,6 +143,18 @@ object BrotliFilterSpec extends PlaySpecification with DataTables {
header(SERVER, result) must beSome("Play")
}

"preserve original cookies" in withApplication(Ok("hello").withCookies(Cookie("foo", "bar"))) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
cookies(result).get("foo") must beSome(Cookie("foo", "bar"))
}

"preserve original session" in withApplication(Ok("hello").withSession("foo" -> "bar")) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
session(result).get("foo") must beSome("bar")
}

"preserve original Vary header values" in withApplication(Ok("hello").withHeaders(VARY -> "original")) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
Expand Down Expand Up @@ -219,4 +232,4 @@ object BrotliFilterSpec extends PlaySpecification with DataTables {
header(CONTENT_ENCODING, result) must beNone
contentAsString(result) must_== body
}
}
}
10 changes: 5 additions & 5 deletions play-v30/src/main/scala/play.filters.brotli/BrotliFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material
result.body match {

case HttpEntity.Strict(data, contentType) =>
Future.successful(Result(header, compressStrictEntity(data, contentType)))
Future.successful(result.copy(header = header, body = compressStrictEntity(data, contentType)))


case entity @ HttpEntity.Streamed(_, Some(contentLength), contentType) if contentLength <= config.chunkedThreshold =>
// It's below the chunked threshold, so buffer then compress and send
entity.consumeData.map { data =>
Result(header, compressStrictEntity(data, contentType))
result.copy(header = header, body = compressStrictEntity(data, contentType))
}

case HttpEntity.Streamed(data, _, contentType) if request.version == HttpProtocol.HTTP_1_0 =>
Expand All @@ -109,7 +109,7 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material
case HttpEntity.Streamed(data, _, contentType) =>
// It's above the chunked threshold, compress through the brotli flow, and send as chunked
val compressed = data.via(createBrotliFlow).map(d => HttpChunk.Chunk(d))
Future.successful(Result(header, HttpEntity.Chunked(compressed, contentType)))
Future.successful(result.copy(header = header, body = HttpEntity.Chunked(compressed, contentType)))

case HttpEntity.Chunked(chunks, contentType) =>
val wrappedFlow = Flow.fromGraph(GraphDSL.create[FlowShape[HttpChunk, HttpChunk]]() { implicit builder =>
Expand Down Expand Up @@ -138,7 +138,7 @@ class BrotliFilter @Inject() (config: BrotliFilterConfig)(implicit mat: Material

new FlowShape(broadcast.in, concat.out)
})
Future.successful(Result(header, HttpEntity.Chunked(chunks via wrappedFlow, contentType)))
Future.successful(result.copy(header = header, body = HttpEntity.Chunked(chunks via wrappedFlow, contentType)))
}
} else {
Future.successful(result)
Expand Down Expand Up @@ -283,4 +283,4 @@ trait BrotliFilterComponents {

lazy val brotliFilterConfig: BrotliFilterConfig = BrotliFilterConfig.fromConfiguration(configuration)
lazy val brotliFilter: BrotliFilter = new BrotliFilter(brotliFilterConfig)(materializer)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import scala.util.Random
import org.specs2.matcher.{DataTables, MatchResult}

import com.aayushatharva.brotli4j.decoder.BrotliInputStream;
import play.api.mvc.Cookie

object BrotliFilterSpec extends PlaySpecification with DataTables {

Expand Down Expand Up @@ -142,6 +143,18 @@ object BrotliFilterSpec extends PlaySpecification with DataTables {
header(SERVER, result) must beSome("Play")
}

"preserve original cookies" in withApplication(Ok("hello").withCookies(Cookie("foo", "bar"))) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
cookies(result).get("foo") must beSome(Cookie("foo", "bar"))
}

"preserve original session" in withApplication(Ok("hello").withSession("foo" -> "bar")) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
session(result).get("foo") must beSome("bar")
}

"preserve original Vary header values" in withApplication(Ok("hello").withHeaders(VARY -> "original")) { implicit app =>
val result = makeBrotliRequest(app)
checkCompressed(result)
Expand Down Expand Up @@ -220,4 +233,4 @@ object BrotliFilterSpec extends PlaySpecification with DataTables {
header(CONTENT_ENCODING, result) must beNone
contentAsString(result) must_== body
}
}
}