Skip to content

Commit

Permalink
Updated to filter dsl to avoid extraneous path values being accepted
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Jul 15, 2018
1 parent 5b0894a commit dd723c4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions server/src/main/scala/io/youi/server/dsl/ActionFilter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ class ActionFilter(f: HttpConnection => Unit) extends ConnectionFilter {
Some(connection)
}
}

object ActionFilter {
def apply(f: HttpConnection => Unit): ActionFilter = new ActionFilter(f)
}
2 changes: 2 additions & 0 deletions server/src/main/scala/io/youi/server/dsl/PathPart.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import io.youi.http.HttpConnection
object PathPart {
private val Key: String = "PathPart"

def fulfilled(connection: HttpConnection): Boolean = connection.store.getOrElse[List[String]](Key, Nil).isEmpty

def take(connection: HttpConnection, part: String): Option[HttpConnection] = {
val parts = connection.store.getOrElse(Key, connection.request.url.path.parts)
if (parts.nonEmpty && parts.head == part) {
Expand Down
12 changes: 7 additions & 5 deletions server/src/main/scala/io/youi/server/dsl/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ package object dsl {

implicit class MethodConnectionFilter(val method: Method) extends ConditionalFilter(_.request.method == method)

implicit class HttpHandlerFilter(val handler: HttpHandler) extends ActionFilter(handler.handle)
implicit def handler2Filter(handler: HttpHandler): ConnectionFilter = ActionFilter { connection =>
if (PathPart.fulfilled(connection)) {
handler.handle(connection)
}
}

implicit class CachingManagerFilter(val caching: CachingManager) extends LastConnectionFilter(new ActionFilter(caching.handle))

Expand Down Expand Up @@ -68,13 +72,11 @@ package object dsl {
}
}

implicit def content2Filter(content: Content): ConnectionFilter = {
new HttpHandlerFilter(ContentHandler(content, HttpStatus.OK))
}
implicit def content2Filter(content: Content): ConnectionFilter = handler2Filter(ContentHandler(content, HttpStatus.OK))

implicit def restful[Request, Response](restful: Restful[Request, Response])
(implicit decoder: Decoder[Request], encoder: Encoder[Response]): ConnectionFilter = {
new HttpHandlerFilter(Restful(restful)(decoder, encoder))
handler2Filter(Restful(restful)(decoder, encoder))
}

implicit def path2AllowFilter(path: Path): ConnectionFilter = PathFilter(path)
Expand Down

0 comments on commit dd723c4

Please sign in to comment.