Skip to content

Commit

Permalink
Handle OPTIONS Method
Browse files Browse the repository at this point in the history
  • Loading branch information
Saturn225 authored Oct 19, 2024
1 parent 70d026e commit f2ccf4a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,18 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
val tree = self.tree
Handler
.fromFunctionHandler[Request] { req =>
println(s"[DEBUG] Incoming request: Method = ${req.method}, Path = ${req.path}")

val chunk = tree.get(req.method, req.path)
def allowedMethods = tree.getAllMethods(req.path)
println(s"[DEBUG] Chunk length for Method ${req.method} and Path ${req.path} = ${chunk.length}")
println(s"[DEBUG] Allowed methods for Path ${req.path} = ${allowedMethods.mkString(", ")}")
req.method match {
case Method.CUSTOM(_) =>
Handler.notImplemented
case _ =>
if (chunk.isEmpty) {
if (allowedMethods.isEmpty) {
if (allowedMethods.isEmpty || allowedMethods == Set(Method.OPTIONS)) {
// If no methods are allowed for the path, return 404 Not Found
Handler.notFound
} else {
Expand All @@ -281,6 +285,7 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
}
}
}

}
.merge
}
Expand Down

0 comments on commit f2ccf4a

Please sign in to comment.