Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C #8

Merged
merged 4 commits into from
Oct 2, 2024
Merged

C #8

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading