Skip to content

Commit

Permalink
feat(conformance): add review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Saturn225 authored Oct 2, 2024
1 parent b609cd9 commit 62e78a3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,16 @@ private[zio] final case class ServerInboundHandler(
}

private def validateHostHeader(req: Request): Boolean = {
req.headers.get("Host") match {
case Some(host) =>
val parts = host.split(":")
val hostname = parts(0)
val isValidHost = validateHostname(hostname)
val isValidPort = parts.length == 1 || (parts.length == 2 && parts(1).forall(_.isDigit))
val isValid = isValidHost && isValidPort
isValid
case None =>
false
val host = req.headers.get("Host").getOrElse(null)
if (host != null) {
val parts = host.split(":")
val hostname = parts(0)
val isValidHost = validateHostname(hostname)
val isValidPort = parts.length == 1 || (parts.length == 2 && parts(1).forall(_.isDigit))
val isValid = isValidHost && isValidPort
isValid
} else {
false
}
}

Expand All @@ -143,6 +143,7 @@ private[zio] final case class ServerInboundHandler(

override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable): Unit =
cause match {

case ioe: IOException if {
val msg = ioe.getMessage
(msg ne null) && msg.contains("Connection reset")
Expand Down
2 changes: 1 addition & 1 deletion zio-http/jvm/src/test/scala/zio/http/ConformanceSpec.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package zio.http

import java.time.format.DateTimeFormatter
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

import zio._
import zio.test.Assertion._
Expand Down
46 changes: 25 additions & 21 deletions zio-http/shared/src/main/scala/zio/http/Routes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -249,32 +249,36 @@ final case class Routes[-Env, +Err](routes: Chunk[zio.http.Route[Env, Err]]) { s
Handler
.fromFunctionHandler[Request] { req =>
val chunk = tree.get(req.method, req.path)
val allowedMethods = tree.getAllMethods(req.path)
def allowedMethods = tree.getAllMethods(req.path)
req.method match {
case Method.CUSTOM(_) =>
Handler.notImplemented
case _ =>
chunk.length match {
case 0 =>
if (allowedMethods.nonEmpty) {
val allowHeader = Header.Allow(NonEmptyChunk.fromIterableOption(allowedMethods).get)
Handler.methodNotAllowed.addHeader(allowHeader)
} 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)
if (chunk.isEmpty) {
if (allowedMethods.isEmpty) {
// If no methods are allowed for the path, return 404 Not Found
Handler.notFound
} else {
// If there are allowed methods for the path but none match the request method, return 405 Method Not Allowed
val allowHeader = Header.Allow(NonEmptyChunk.fromIterableOption(allowedMethods).get)
Handler.methodNotAllowed.addHeader(allowHeader)
}
} else {
chunk.length match {
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
}
}
}
}
Expand Down

0 comments on commit 62e78a3

Please sign in to comment.