diff --git a/docs/index.md b/docs/index.md index a07ed4a53d..7c191ead2e 100644 --- a/docs/index.md +++ b/docs/index.md @@ -13,17 +13,29 @@ ZIO HTTP is designed in terms of **HTTP as function**, where both server and cli Some of the key features of ZIO HTTP are: **ZIO Native**: ZIO HTTP is built atop ZIO, a type-safe, composable, and asynchronous effect system for Scala. It inherits all the benefits of ZIO, including testability, composability, and type safety. + **Cloud-Native**: ZIO HTTP is designed for cloud-native environments and supports building highly scalable and performant web applications. Built atop ZIO, it features built-in support for concurrency, parallelism, resource management, error handling, structured logging, configuration management, and metrics instrumentation. + **Imperative and Declarative Endpoints**: ZIO HTTP provides a declarative API for defining HTTP endpoints besides the imperative API. With imperative endpoints, both the shape of the endpoint and the logic are defined together, while with declarative endpoints, the description of the endpoint is separated from its logic. Developers can choose the style that best fit their needs. + **Type-Driven API Design**: Beside the fact that ZIO HTTP supports declarative endpoint descriptions, it also provides a type-driven API design that leverages Scala's type system to ensure correctness and safety at compile time. So the implementation of the endpoint is type-checked against the description of the endpoint. + **Middleware Support**: ZIO HTTP offers middleware support for incorporating cross-cutting concerns such as logging, metrics, authentication, and more into your services. + **Error Handling**: Built-in support exists for handling errors at the HTTP layer, distinguishing between handled and unhandled errors. + **WebSockets**: Built-in support for WebSockets allows for the creation of real-time applications using ZIO HTTP. + **Testkit**: ZIO HTTP provides first-class testing utilities that facilitate test writing without requiring a live server instance. + **Interoperability**: Interoperability with existing Scala/Java libraries is provided, enabling seamless integration with functionality from the Scala/Java ecosystem through the importation of blocking and non-blocking operations. + **JSON and Binary Codecs**: Built-in support for ZIO Schema enables encoding and decoding of request/response bodies, supporting various data types including JSON, Protobuf, Avro, and Thrift. + **Template System**: A built-in DSL facilitates writing HTML templates using Scala code. + **OpenAPI Support**: Built-in support is available for generating OpenAPI documentation for HTTP applications, and conversely, for generating HTTP endpoints from OpenAPI documentation. + **ZIO HTTP CLI**: Command-line applications can be built to interact with HTTP APIs by leveraging the power of [ZIO CLI](https://zio.dev/zio-cli) and ZIO HTTP. ## Installation diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/NettyBodyWriter.scala b/zio-http/jvm/src/main/scala/zio/http/netty/NettyBodyWriter.scala index f63c20c044..93acabad5a 100644 --- a/zio-http/jvm/src/main/scala/zio/http/netty/NettyBodyWriter.scala +++ b/zio-http/jvm/src/main/scala/zio/http/netty/NettyBodyWriter.scala @@ -40,7 +40,6 @@ object NettyBodyWriter { body: Body, contentLength: Option[Long], ctx: ChannelHandlerContext, - compressionEnabled: Boolean, )(implicit trace: Trace, ): Option[Task[Unit]] = { @@ -57,21 +56,16 @@ object NettyBodyWriter { } body match { - case body: ByteBufBody => + case body: ByteBufBody => ctx.write(new DefaultHttpContent(body.byteBuf)) ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT) None - case body: FileBody if compressionEnabled => + case body: FileBody => // We need to stream the file when compression is enabled otherwise the response encoding fails val stream = ZStream.fromFile(body.file) - val size = Some(body.fileSize) - val s = StreamBody(stream, knownContentLength = size, mediaType = body.mediaType) - NettyBodyWriter.writeAndFlush(s, size, ctx, compressionEnabled) - case body: FileBody => - ctx.write(new DefaultFileRegion(body.file, 0, body.fileSize)) - ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT) - None - case AsyncBody(async, _, _, _) => + val s = StreamBody(stream, None, mediaType = body.mediaType) + NettyBodyWriter.writeAndFlush(s, None, ctx) + case AsyncBody(async, _, _, _) => async( new UnsafeAsync { override def apply(message: Chunk[Byte], isLast: Boolean): Unit = { @@ -87,10 +81,10 @@ object NettyBodyWriter { }, ) None - case AsciiStringBody(asciiString, _, _) => + case AsciiStringBody(asciiString, _, _) => writeArray(asciiString.array(), isLast = true) None - case StreamBody(stream, _, _, _) => + case StreamBody(stream, _, _, _) => Some( contentLength.orElse(body.knownContentLength) match { case Some(length) => @@ -131,13 +125,13 @@ object NettyBodyWriter { } }, ) - case ArrayBody(data, _, _) => + case ArrayBody(data, _, _) => writeArray(data, isLast = true) None - case ChunkBody(data, _, _) => + case ChunkBody(data, _, _) => writeArray(data.toArray, isLast = true) None - case EmptyBody => + case EmptyBody => ctx.writeAndFlush(LastHttpContent.EMPTY_LAST_CONTENT) None } diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/client/ClientInboundHandler.scala b/zio-http/jvm/src/main/scala/zio/http/netty/client/ClientInboundHandler.scala index b1021d924e..db8438a4b8 100644 --- a/zio-http/jvm/src/main/scala/zio/http/netty/client/ClientInboundHandler.scala +++ b/zio-http/jvm/src/main/scala/zio/http/netty/client/ClientInboundHandler.scala @@ -52,7 +52,7 @@ final class ClientInboundHandler( ctx.writeAndFlush(fullRequest): Unit case _: HttpRequest => ctx.write(jReq) - NettyBodyWriter.writeAndFlush(req.body, None, ctx, compressionEnabled = false).foreach { effect => + NettyBodyWriter.writeAndFlush(req.body, None, ctx).foreach { effect => rtm.run(ctx, NettyRuntime.noopEnsuring)(effect)(Unsafe.unsafe, trace) } } diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala index 5298e14c6f..65e425cd68 100644 --- a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala +++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerInboundHandler.scala @@ -187,7 +187,7 @@ private[zio] final case class ServerInboundHandler( } ctx.writeAndFlush(jResponse) - NettyBodyWriter.writeAndFlush(response.body, contentLength, ctx, isResponseCompressible(jRequest)) + NettyBodyWriter.writeAndFlush(response.body, contentLength, ctx) } else { ctx.writeAndFlush(jResponse) None diff --git a/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala b/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala index a75c0234f7..16cb728ef8 100644 --- a/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala +++ b/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala @@ -17,6 +17,7 @@ package zio.http import zio.test.Assertion.equalTo +import zio.test.TestAspect.withLiveClock import zio.test.{Gen, assertCompletes, assertNever, assertZIO} import zio.{Scope, ZLayer} @@ -35,6 +36,7 @@ object SSLSpec extends ZIOHttpSpec { val app: Routes[Any, Response] = Routes( Method.GET / "success" -> handler(Response.ok), + Method.GET / "file" -> Handler.fromResource("TestStatic/TestFile1.txt"), ).sandbox val httpUrl = @@ -43,6 +45,9 @@ object SSLSpec extends ZIOHttpSpec { val httpsUrl = URL.decode("https://localhost:8073/success").toOption.get + val staticFileUrl = + URL.decode("https://localhost:8073/file").toOption.get + override def spec = suite("SSL")( Server .install(app) @@ -110,6 +115,19 @@ object SSLSpec extends ZIOHttpSpec { ZLayer.succeed(NettyConfig.defaultWithFastShutdown), Scope.default, ), + test("static files") { + val actual = Client + .request(Request.get(staticFileUrl)) + .flatMap(_.body.asString) + assertZIO(actual)(equalTo("This file is added for testing Static File Server.")) + }.provide( + Client.customized, + ZLayer.succeed(ZClient.Config.default.ssl(ClientSSLConfig.Default)), + NettyClientDriver.live, + DnsResolver.default, + ZLayer.succeed(NettyConfig.defaultWithFastShutdown), + Scope.default, + ) @@ withLiveClock, ), ), ).provideShared(