summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:34:52 +0800
committerLi Haoyi <haoyi.sg@gmail.com>2019-10-09 10:34:52 +0800
commite21d5ab3526937619c59dd01114b48521eee9d26 (patch)
tree488de7aaf17a84ffe0aeff845ec041d7d54c7fa0
parentfb203e85b849817943f85d9c7368695fb8c78583 (diff)
downloadcask-e21d5ab3526937619c59dd01114b48521eee9d26.tar.gz
cask-e21d5ab3526937619c59dd01114b48521eee9d26.tar.bz2
cask-e21d5ab3526937619c59dd01114b48521eee9d26.zip
Allow `staticFiles` and `staticFiles` to specify headers to respond with (e.g. for caching and expiration)
-rw-r--r--cask/src/cask/endpoints/StaticEndpoints.scala28
-rw-r--r--cask/src/cask/model/Response.scala9
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)
}
}