From e21d5ab3526937619c59dd01114b48521eee9d26 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Wed, 9 Oct 2019 10:34:52 +0800 Subject: Allow `staticFiles` and `staticFiles` to specify headers to respond with (e.g. for caching and expiration) --- cask/src/cask/endpoints/StaticEndpoints.scala | 28 +++++++++++++-------------- cask/src/cask/model/Response.scala | 9 ++++----- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/cask/src/cask/endpoints/StaticEndpoints.scala b/cask/src/cask/endpoints/StaticEndpoints.scala index 1e11055..618da5f 100644 --- a/cask/src/cask/endpoints/StaticEndpoints.scala +++ b/cask/src/cask/endpoints/StaticEndpoints.scala @@ -2,37 +2,35 @@ package cask.endpoints import cask.router.HttpEndpoint import cask.model.Request +object StaticUtil{ + def makePath(t: String, ctx: Request) = { + (cask.internal.Util.splitPath(t) ++ ctx.remainingPathSegments) + .filter(s => s != "." && s != "..") + .mkString("/") + } -class staticFiles(val path: String) extends HttpEndpoint[String, Seq[String]]{ +} +class staticFiles(val path: String, headers: Seq[(String, String)] = Nil) extends HttpEndpoint[String, Seq[String]]{ val methods = Seq("get") type InputParser[T] = QueryParamReader[T] override def subpath = true def wrapFunction(ctx: Request, delegate: Delegate): OuterReturned = { - delegate(Map()).map(t => - cask.model.StaticFile( - (cask.internal.Util.splitPath(t) ++ ctx.remainingPathSegments) - .filter(s => s != "." && s != "..") - .mkString("/") - ) - ) + delegate(Map()).map(t => cask.model.StaticFile(StaticUtil.makePath(t, ctx), headers)) } def wrapPathSegment(s: String): Seq[String] = Seq(s) } -class staticResources(val path: String, resourceRoot: ClassLoader = getClass.getClassLoader) +class staticResources(val path: String, + resourceRoot: ClassLoader = getClass.getClassLoader, + headers: Seq[(String, String)] = Nil) extends HttpEndpoint[String, Seq[String]]{ val methods = Seq("get") type InputParser[T] = QueryParamReader[T] override def subpath = true def wrapFunction(ctx: Request, delegate: Delegate): OuterReturned = { delegate(Map()).map(t => - cask.model.StaticResource( - (cask.internal.Util.splitPath(t) ++ ctx.remainingPathSegments) - .filter(s => s != "." && s != "..") - .mkString("/"), - resourceRoot - ) + cask.model.StaticResource(StaticUtil.makePath(t, ctx), resourceRoot, headers) ) } diff --git a/cask/src/cask/model/Response.scala b/cask/src/cask/model/Response.scala index e9ca672..07e8ecf 100644 --- a/cask/src/cask/model/Response.scala +++ b/cask/src/cask/model/Response.scala @@ -75,7 +75,7 @@ object Abort{ def apply(code: Int) = Response("", code, Nil, Nil) } object StaticFile{ - def apply(path: String) = { + def apply(path: String, headers: Seq[(String, String)]) = { val relPath = java.nio.file.Paths.get(path) val (data0, statusCode0) = if (java.nio.file.Files.exists(relPath) && java.nio.file.Files.isRegularFile(relPath)){ @@ -83,17 +83,16 @@ object StaticFile{ }else{ ("": Response.Data, 404) } - Response(data0, statusCode0, Nil, Nil) + Response(data0, statusCode0, headers, Nil) } } object StaticResource{ - def apply(path: String, resourceRoot: ClassLoader) = { - val relPath = java.nio.file.Paths.get(path) + def apply(path: String, resourceRoot: ClassLoader, headers: Seq[(String, String)]) = { val (data0, statusCode0) = resourceRoot.getResourceAsStream(path) match{ case null => ("": Response.Data, 404) case res => (res: Response.Data, 200) } - Response(data0, statusCode0, Nil, Nil) + Response(data0, statusCode0, headers, Nil) } } -- cgit v1.2.3