Skip to content

Commit

Permalink
revert netty changes for now
Browse files Browse the repository at this point in the history
  • Loading branch information
jdegoes committed Jul 20, 2023
1 parent d998b93 commit 403f2ff
Show file tree
Hide file tree
Showing 6 changed files with 521 additions and 117 deletions.
34 changes: 16 additions & 18 deletions zio-http/src/main/scala/zio/http/Handler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -596,21 +596,21 @@ sealed trait Handler[-R, +Err, -In, +Out] { self =>
self(request).timeout(duration).map(_.getOrElse(out))
}

/**
* Converts the request handler into an HTTP application. Note that the
* handler of the HTTP application is not identical to this handler, because
* the handler has been appropriately sandboxed, turning all possible failures
* into well-formed HTTP responses.
*/
def toHttpApp(implicit err: Err <:< Response, in: Request <:< In, out: Out <:< Response): HttpApp[R] = {
val handler: Handler[R, Response, Request, Response] =
self.asInstanceOf[Handler[R, Response, Request, Response]]

HttpApp(Routes.singleton(handler.contramap[(Path, Request)](_._2)))
}

def toHttpAppWS(implicit err: Err <:< Throwable, in: WebSocketChannel <:< In): HttpApp[R] = {
val handler1: Handler[R, Throwable, WebSocketChannel, Any] =
self.asInstanceOf[Handler[R, Throwable, WebSocketChannel, Any]]

val handler2 = Handler.fromZIO(handler1.toResponse)

HttpApp(Routes.singleton(handler2.contramap[(Path, Request)](_._2)))
}
def toHttpAppWS(implicit err: Err <:< Throwable, in: WebSocketChannel <:< In): HttpApp[R] =
Handler.fromZIO(self.toResponse).toHttpApp

/**
* Creates a new response from a socket handler.
Expand Down Expand Up @@ -815,11 +815,11 @@ object Handler {
}
}

def fromFile[R](makeFile: => File)(implicit trace: Trace): Handler[R, Response, Any, Response] =
def fromFile[R](makeFile: => File)(implicit trace: Trace): Handler[R, Throwable, Any, Response] =
fromFileZIO(ZIO.attempt(makeFile))

def fromFileZIO[R](getFile: ZIO[R, Throwable, File])(implicit trace: Trace): Handler[R, Response, Any, Response] = {
Handler.fromZIO[R, Response, Response](
def fromFileZIO[R](getFile: ZIO[R, Throwable, File])(implicit trace: Trace): Handler[R, Throwable, Any, Response] = {
Handler.fromZIO[R, Throwable, Response](
getFile.flatMap { file =>
ZIO.suspend {
if (!file.exists()) {
Expand All @@ -845,27 +845,26 @@ object Handler {
}
}
}
}.mapError(Response.fromThrowable(_)),
},
)
}

/**
* Creates a handler from a resource path
*/
def fromResource(path: String)(implicit trace: Trace): Handler[Any, Response, Any, Response] =
def fromResource(path: String)(implicit trace: Trace): Handler[Any, Throwable, Any, Response] =
Handler.fromZIO {
ZIO
.attemptBlocking(getClass.getClassLoader.getResource(path))
.map { resource =>
if (resource == null) Handler.fail(Response(status = Status.NotFound))
if (resource == null) Handler.fail(new FileNotFoundException(s"Resource $path not found"))
else fromResourceWithURL(resource)
}
.mapError(Response.fromThrowable(_))
}.flatten

private[zio] def fromResourceWithURL(
url: java.net.URL,
)(implicit trace: Trace): Handler[Any, Response, Any, Response] = {
)(implicit trace: Trace): Handler[Any, Throwable, Any, Response] = {
url.getProtocol match {
case "file" => Handler.fromFile(new File(url.getPath))
case "jar" =>
Expand Down Expand Up @@ -901,11 +900,10 @@ object Handler {
.addHeader(Header.ContentLength(contentLength))
}
}
.mapError(Response.fromThrowable(_))
}

case proto =>
Handler.fail(Response.badRequest(s"Unsupported protocol: $proto"))
Handler.fail(new IllegalArgumentException(s"Unsupported protocol: $proto"))
}
}

Expand Down
19 changes: 13 additions & 6 deletions zio-http/src/main/scala/zio/http/Route.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ package zio.http

import zio._

import zio.http.Route.Provide
import zio.http.Route.Provided

/**
* Represents a single route, which has either handled its errors by converting
* them into responses, or which has polymorphic errors, which must later be
* converted into responses before the route can be executed.
*
* Routes have the property that, before conversion into handlers, they will
* fully handle all errors, including defects, translating them appropriately
* into responses that can be delivered to clients. Thus, the handlers returned
* by `toHandler` will never fail, and will always produce a valid response.
*
* Individual routes can be aggregated using [[ziop.http.Routes]].
*/
sealed trait Route[-Env, +Err] { self =>
import Route.{Augmented, Handled, Provide, Unhandled}
import Route.{Augmented, Handled, Provided, Unhandled}

/**
* Augments this route with the specified middleware.
Expand Down Expand Up @@ -42,7 +49,7 @@ sealed trait Route[-Env, +Err] { self =>
*/
final def handleErrorCause(f: Cause[Err] => Response): Route[Env, Nothing] =
self match {
case Provide(route, env) => Provide(route.handleErrorCause(f), env)
case Provided(route, env) => Provided(route.handleErrorCause(f), env)
case Augmented(route, aspect) => Augmented(route.handleErrorCause(f), aspect)
case Handled(routePattern, handler, location) => Handled(routePattern, handler, location)

Expand Down Expand Up @@ -78,7 +85,7 @@ sealed trait Route[-Env, +Err] { self =>
def location: Trace

final def provideEnvironment(env: ZEnvironment[Env]): Route[Any, Err] =
Route.Provide(self, env)
Route.Provided(self, env)

/**
* The route pattern over which the route is defined. The route can only
Expand Down Expand Up @@ -147,7 +154,7 @@ object Route {
Unhandled(rpm, handler, zippable, trace)
}

private final case class Provide[Env, +Err](
private final case class Provided[Env, +Err](
route: Route[Env, Err],
env: ZEnvironment[Env],
) extends Route[Any, Err] {
Expand All @@ -158,7 +165,7 @@ object Route {
override def toHandler(implicit ev: Err <:< Response): Handler[Any, Response, Request, Response] =
route.toHandler.provideEnvironment(env)

override def toString() = s"Route.Provide(${route}, ${env})"
override def toString() = s"Route.Provided(${route}, ${env})"
}

private final case class Augmented[-Env, +Err](
Expand Down
Loading

0 comments on commit 403f2ff

Please sign in to comment.