diff --git a/cask/src/cask/model/Params.scala b/cask/src/cask/model/Params.scala index 016ec2f9f1..75b70843e6 100644 --- a/cask/src/cask/model/Params.scala +++ b/cask/src/cask/model/Params.scala @@ -6,12 +6,22 @@ import cask.internal.Util import io.undertow.server.HttpServerExchange import io.undertow.server.handlers.CookieImpl -case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String]){ +case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[String]) +extends geny.ByteData with geny.Readable { import collection.JavaConverters._ lazy val cookies: Map[String, Cookie] = { exchange.getRequestCookies.asScala.mapValues(Cookie.fromUndertow).toMap } lazy val data: InputStream = exchange.getInputStream + + /** + * Read all the bytes of the incoming request *with* caching + */ + lazy val bytes = readAllBytes() + + /** + * Read all the bytes of the incoming request *without* caching + */ def readAllBytes() = { val baos = new ByteArrayOutputStream() Util.transferTo(data, baos) @@ -25,6 +35,8 @@ case class Request(exchange: HttpServerExchange, remainingPathSegments: Seq[Stri .map{ header => header.getHeaderName.toString.toLowerCase -> header.asScala } .toMap } + + def readBytesThrough[T](f: InputStream => T) = f(data) } object Cookie{ diff --git a/example/minimalApplication/app/src/MinimalApplication.scala b/example/minimalApplication/app/src/MinimalApplication.scala index e3b1eff932..68eb8d0540 100644 --- a/example/minimalApplication/app/src/MinimalApplication.scala +++ b/example/minimalApplication/app/src/MinimalApplication.scala @@ -7,7 +7,7 @@ object MinimalApplication extends cask.MainRoutes{ @cask.post("/do-thing") def doThing(request: cask.Request) = { - new String(request.readAllBytes()).reverse + request.text().reverse } initialize() diff --git a/example/minimalApplication2/app/src/MinimalApplication2.scala b/example/minimalApplication2/app/src/MinimalApplication2.scala index efe9204f2d..267f22f1cc 100644 --- a/example/minimalApplication2/app/src/MinimalApplication2.scala +++ b/example/minimalApplication2/app/src/MinimalApplication2.scala @@ -8,7 +8,7 @@ case class MinimalRoutes()(implicit val log: cask.Logger) extends cask.Routes{ @cask.post("/do-thing") def doThing(request: cask.Request) = { - new String(request.readAllBytes()).reverse + request.text().reverse } initialize() diff --git a/example/todo/app/src/TodoServer.scala b/example/todo/app/src/TodoServer.scala index 8b498b5e25..a301fc21fb 100644 --- a/example/todo/app/src/TodoServer.scala +++ b/example/todo/app/src/TodoServer.scala @@ -53,7 +53,7 @@ object TodoServer extends cask.MainRoutes{ @transactional @cask.post("/add/:state") def add(state: String, request: cask.Request) = { - val body = new String(request.readAllBytes()) + val body = request.text() run( query[Todo] .insert(_.checked -> lift(false), _.text -> lift(body)) diff --git a/example/todoApi/app/src/TodoMvcApi.scala b/example/todoApi/app/src/TodoMvcApi.scala index 58df1d533c..bd54b8ff59 100644 --- a/example/todoApi/app/src/TodoMvcApi.scala +++ b/example/todoApi/app/src/TodoMvcApi.scala @@ -21,7 +21,7 @@ object TodoMvcApi extends cask.MainRoutes{ @cask.post("/add") def add(request: cask.Request) = { - todos = Seq(Todo(false, new String(request.readAllBytes()))) ++ todos + todos = Seq(Todo(false, request.text())) ++ todos } @cask.post("/toggle/:index") diff --git a/example/todoDb/app/src/TodoMvcDb.scala b/example/todoDb/app/src/TodoMvcDb.scala index 66109f46eb..7140e27f96 100644 --- a/example/todoDb/app/src/TodoMvcDb.scala +++ b/example/todoDb/app/src/TodoMvcDb.scala @@ -63,7 +63,7 @@ object TodoMvcDb extends cask.MainRoutes{ @transactional @cask.post("/add") def add(request: cask.Request) = { - val body = new String(request.readAllBytes()) + val body = request.text() run( query[Todo] .insert(_.checked -> lift(false), _.text -> lift(body)) diff --git a/readme.md b/readme.md index ab0d86201e..c583078b00 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ object MinimalApplication extends cask.MainRoutes{ @cask.post("/do-thing") def doThing(request: cask.Request) = { - new String(request.readAllBytes()).reverse + request.text().reverse } initialize()