Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
varshith257 committed Sep 30, 2024
1 parent c2ee8a5 commit c51db7f
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,31 +248,39 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
val tree = self.tree
Handler
.fromFunctionHandler[Request] { req =>
validateHostHeader(req).flatMap { _ =>
val chunk = tree.get(req.method, req.path)
val allowedMethods = tree.getAllMethods(req.path)

chunk.length match {
case 0 =>
if (!Method.knownMethods.contains(req.method)) {
Handler.status(Status.NotImplemented)
} else if (!allowedMethods.contains(req.method) && allowedMethods.nonEmpty) {
Handler.methodNotAllowed
} else { Handler.notFound }
case 1 => chunk(0)
case n => // TODO: Support precomputed fallback among all chunk elements
var acc = chunk(0)
var i = 1
while (i < n) {
val h = chunk(i)
acc = acc.catchAll { response =>
if (response.status == Status.NotFound) h
else Handler.fail(response)
val chunk = tree.get(req.method, req.path)
val allowedMethods = tree.getAllMethods(req.path)

req.method match {
case Method.CUSTOM(_) =>
Handler.fromZIO(ZIO.succeed(Response.status(Status.NotImplemented)))
case _ if chunk.isEmpty && allowedMethods.nonEmpty =>
Handler.fromZIO(ZIO.succeed(Response.status(Status.MethodNotAllowed)))

case _ if chunk.isEmpty && allowedMethods.isEmpty =>
Handler.notFound
case _ =>
chunk.length match {
// case 0 =>
// if (!Method.knownMethods.contains(req.method)) {
// Handler.status(Status.NotImplemented)
// } else if (!allowedMethods.contains(req.method) && allowedMethods.nonEmpty) {
// Handler.methodNotAllowed
// } else { Handler.notFound }
case 1 => chunk(0)
case n => // TODO: Support precomputed fallback among all chunk elements
var acc = chunk(0)
var i = 1
while (i < n) {
val h = chunk(i)
acc = acc.catchAll { response =>
if (response.status == Status.NotFound) h
else Handler.fail(response)
}
i += 1
}
i += 1
}
acc
}
acc
}
}
}
.merge
Expand Down

0 comments on commit c51db7f

Please sign in to comment.