diff --git a/.github/workflows/http-conformance.yml b/.github/workflows/http-conformance.yml
new file mode 100644
index 0000000000..c8cf83fc11
--- /dev/null
+++ b/.github/workflows/http-conformance.yml
@@ -0,0 +1,77 @@
+name: HTTP Conformance
+
+on:
+ pull_request:
+ branches: ["**"]
+ push:
+ branches: ["**"]
+ tags: [v*]
+
+env:
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+ JDK_JAVA_OPTIONS: "-Xms4G -Xmx8G -XX:+UseG1GC -Xss10M -XX:ReservedCodeCacheSize=1G -XX:NonProfiledCodeHeapSize=512m -Dfile.encoding=UTF-8"
+ SBT_OPTS: "-Xms4G -Xmx8G -XX:+UseG1GC -Xss10M -XX:ReservedCodeCacheSize=1G -XX:NonProfiledCodeHeapSize=512m -Dfile.encoding=UTF-8"
+
+jobs:
+ build:
+ name: Build and Test
+ strategy:
+ matrix:
+ os: [ubuntu-latest]
+ scala: [2.12.19, 2.13.14, 3.3.3]
+ java:
+ - graal_graalvm@17
+ - graal_graalvm@21
+ - temurin@17
+ - temurin@21
+ runs-on: ${{ matrix.os }}
+ timeout-minutes: 60
+
+ steps:
+ - name: Checkout current branch (full)
+ uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
+
+ - name: Setup GraalVM (graal_graalvm@17)
+ if: matrix.java == 'graal_graalvm@17'
+ uses: graalvm/setup-graalvm@v1
+ with:
+ java-version: 17
+ distribution: graalvm
+ components: native-image
+ github-token: ${{ secrets.GITHUB_TOKEN }}
+ cache: sbt
+
+ - uses: coursier/setup-action@v1
+ with:
+ apps: sbt
+
+ - name: Setup GraalVM (graal_graalvm@21)
+ if: matrix.java == 'graal_graalvm@21'
+ uses: graalvm/setup-graalvm@v1
+ with:
+ java-version: 21
+ distribution: graalvm
+ components: native-image
+ github-token: ${{ secrets.GITHUB_TOKEN }}
+ cache: sbt
+
+ - name: Setup Java (temurin@17)
+ if: matrix.java == 'temurin@17'
+ uses: actions/setup-java@v4
+ with:
+ distribution: temurin
+ java-version: 17
+ cache: sbt
+
+ - name: Setup Java (temurin@21)
+ if: matrix.java == 'temurin@21'
+ uses: actions/setup-java@v4
+ with:
+ distribution: temurin
+ java-version: 21
+ cache: sbt
+
+ - name: Run HTTP Conformance Tests
+ run: sbt "project zioHttpJVM" "testOnly zio.http.ConformanceSpec zio.http.ConformanceE2ESpec"
diff --git a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala
index f3823bf3ee..39d0c9a47c 100644
--- a/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala
+++ b/zio-http-example/src/main/scala/example/HelloWorldAdvanced.scala
@@ -27,16 +27,17 @@ object HelloWorldAdvanced extends ZIOAppDefault {
// Configure thread count using CLI
val nThreads: Int = args.headOption.flatMap(x => Try(x.toInt).toOption).getOrElse(0)
- val config = Server.Config.default
+ val config = Server.Config.default
.port(PORT)
- val nettyConfig = NettyConfig.default
+ val nettyConfig = NettyConfig.default
.leakDetection(LeakDetectionLevel.PARANOID)
.maxThreads(nThreads)
- val configLayer = ZLayer.succeed(config)
- val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ val configLayer = ZLayer.succeed(config)
+ val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ val serverRuntimeConfig = configLayer.flatMap(env => ZLayer.succeed(ServerRuntimeConfig(env.get)))
(fooBar ++ app)
.serve[Any]
- .provide(configLayer, nettyConfigLayer, Server.customized)
+ .provide(serverRuntimeConfig, nettyConfigLayer, Server.customized)
}
}
diff --git a/zio-http-example/src/main/scala/example/PlainTextBenchmarkServer.scala b/zio-http-example/src/main/scala/example/PlainTextBenchmarkServer.scala
index f5f61b1ece..d85b426fbe 100644
--- a/zio-http-example/src/main/scala/example/PlainTextBenchmarkServer.scala
+++ b/zio-http-example/src/main/scala/example/PlainTextBenchmarkServer.scala
@@ -40,10 +40,11 @@ object PlainTextBenchmarkServer extends ZIOAppDefault {
private val nettyConfig = NettyConfig.default
.leakDetection(LeakDetectionLevel.DISABLED)
- private val configLayer = ZLayer.succeed(config)
- private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ private val configLayer = ZLayer.succeed(config)
+ private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ private val serverRuntimeConfigLayer = configLayer.flatMap(env => ZLayer.succeed(ServerRuntimeConfig(env.get)))
val run: UIO[ExitCode] =
- Server.serve(routes).provide(configLayer, nettyConfigLayer, Server.customized).exitCode
+ Server.serve(routes).provide(serverRuntimeConfigLayer, nettyConfigLayer, Server.customized).exitCode
}
diff --git a/zio-http-example/src/main/scala/example/SimpleEffectBenchmarkServer.scala b/zio-http-example/src/main/scala/example/SimpleEffectBenchmarkServer.scala
index 0f54036120..ac714f9fdd 100644
--- a/zio-http-example/src/main/scala/example/SimpleEffectBenchmarkServer.scala
+++ b/zio-http-example/src/main/scala/example/SimpleEffectBenchmarkServer.scala
@@ -37,10 +37,11 @@ object SimpleEffectBenchmarkServer extends ZIOAppDefault {
private val nettyConfig = NettyConfig.default
.leakDetection(LeakDetectionLevel.DISABLED)
- private val configLayer = ZLayer.succeed(config)
- private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ private val configLayer = ZLayer.succeed(config)
+ private val nettyConfigLayer = ZLayer.succeed(nettyConfig)
+ private val serverRuntimeConfigLayer = configLayer.flatMap(env => ZLayer.succeed(ServerRuntimeConfig(env.get)))
val run: UIO[ExitCode] =
- Server.serve(routes).provide(configLayer, nettyConfigLayer, Server.customized).exitCode
+ Server.serve(routes).provide(serverRuntimeConfigLayer, nettyConfigLayer, Server.customized).exitCode
}
diff --git a/zio-http-testkit/src/main/scala/zio/http/TestServer.scala b/zio-http-testkit/src/main/scala/zio/http/TestServer.scala
index 6ff2d2f18d..1c20e5025a 100644
--- a/zio-http-testkit/src/main/scala/zio/http/TestServer.scala
+++ b/zio-http-testkit/src/main/scala/zio/http/TestServer.scala
@@ -142,6 +142,7 @@ object TestServer {
val default: ZLayer[Any, Nothing, TestServer] = ZLayer.make[TestServer][Nothing](
TestServer.layer.orDie,
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
+ ServerRuntimeConfig.layer,
NettyDriver.customized.orDie,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
)
diff --git a/zio-http-testkit/src/test/scala/zio/http/RoutesPrecedentsSpec.scala b/zio-http-testkit/src/test/scala/zio/http/RoutesPrecedentsSpec.scala
index e03a7a26c5..7c4ead4d24 100644
--- a/zio-http-testkit/src/test/scala/zio/http/RoutesPrecedentsSpec.scala
+++ b/zio-http-testkit/src/test/scala/zio/http/RoutesPrecedentsSpec.scala
@@ -45,6 +45,7 @@ object RoutesPrecedentsSpec extends ZIOSpecDefault {
ZLayer.succeed(new MyServiceLive(code)),
)
}.provide(
+ ServerRuntimeConfig.layer,
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
TestServer.layer,
Client.default,
diff --git a/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala b/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala
index 3e47680c4d..67bb8066f8 100644
--- a/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala
+++ b/zio-http-testkit/src/test/scala/zio/http/SocketContractSpec.scala
@@ -103,6 +103,7 @@ object SocketContractSpec extends ZIOHttpSpec {
_ <- promise.await.timeout(10.seconds)
} yield assert(response.status)(equalTo(Status.SwitchingProtocols))
}.provideSome[Client](
+ ServerRuntimeConfig.layer,
TestServer.layer,
NettyDriver.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala b/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
index a6b90871bb..3562ab50e1 100644
--- a/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
+++ b/zio-http-testkit/src/test/scala/zio/http/TestServerSpec.scala
@@ -115,6 +115,10 @@ object TestServerSpec extends ZIOHttpSpec {
port <- ZIO.serviceWithZIO[Server](_.port)
} yield Request
.get(url = URL.root.port(port))
- .addHeaders(Headers(Header.Accept(MediaType.text.`plain`)))
-
+ .addHeaders(
+ Headers(
+ Header.Accept(MediaType.text.`plain`),
+ Header.Host("localhost"),
+ ),
+ )
}
diff --git a/zio-http/jvm/src/main/scala/zio/http/ServerPlatformSpecific.scala b/zio-http/jvm/src/main/scala/zio/http/ServerPlatformSpecific.scala
index 47f09f673e..35194e3048 100644
--- a/zio-http/jvm/src/main/scala/zio/http/ServerPlatformSpecific.scala
+++ b/zio-http/jvm/src/main/scala/zio/http/ServerPlatformSpecific.scala
@@ -10,11 +10,12 @@ trait ServerPlatformSpecific {
private[http] def base: ZLayer[Driver & Config, Throwable, Server]
- val customized: ZLayer[Config & NettyConfig, Throwable, Driver with Server] = {
+ val customized: ZLayer[ServerRuntimeConfig & NettyConfig, Throwable, Driver with Server] = {
// tmp val Needed for Scala2
val tmp: ZLayer[Driver & Config, Throwable, Server] = ZLayer.suspend(base)
- ZLayer.makeSome[Config & NettyConfig, Driver with Server](
+ ZLayer.makeSome[ServerRuntimeConfig & NettyConfig, Driver with Server](
+ ZLayer.fromFunction((runtime: ServerRuntimeConfig) => runtime.config),
NettyDriver.customized,
tmp,
)
diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/NettyDriver.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/NettyDriver.scala
index 334b186711..43feaa2550 100644
--- a/zio-http/jvm/src/main/scala/zio/http/netty/server/NettyDriver.scala
+++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/NettyDriver.scala
@@ -22,21 +22,21 @@ import java.net.InetSocketAddress
import zio._
import zio.http.Driver.StartResult
+import zio.http._
import zio.http.netty._
import zio.http.netty.client.NettyClientDriver
-import zio.http.{ClientDriver, Driver, Response, Routes, Server}
import io.netty.bootstrap.ServerBootstrap
-import io.netty.channel._
+import io.netty.channel.{Channel => NettyChannel, ChannelFactory, ChannelInitializer, ChannelOption, ServerChannel}
import io.netty.util.ResourceLeakDetector
private[zio] final case class NettyDriver(
appRef: RoutesRef,
channelFactory: ChannelFactory[ServerChannel],
- channelInitializer: ChannelInitializer[Channel],
+ channelInitializer: ChannelInitializer[NettyChannel],
serverInboundHandler: ServerInboundHandler,
eventLoopGroups: ServerEventLoopGroups,
- serverConfig: Server.Config,
+ serverConfig: ServerRuntimeConfig,
nettyConfig: NettyConfig,
) extends Driver { self =>
@@ -47,9 +47,9 @@ private[zio] final case class NettyDriver(
.group(eventLoopGroups.boss, eventLoopGroups.worker)
.channelFactory(channelFactory)
.childHandler(channelInitializer)
- .option[Integer](ChannelOption.SO_BACKLOG, serverConfig.soBacklog)
- .childOption[JBoolean](ChannelOption.TCP_NODELAY, serverConfig.tcpNoDelay)
- .bind(serverConfig.address)
+ .option[Integer](ChannelOption.SO_BACKLOG, serverConfig.config.soBacklog)
+ .childOption[JBoolean](ChannelOption.TCP_NODELAY, serverConfig.config.tcpNoDelay)
+ .bind(serverConfig.config.address)
}
_ <- NettyFutureExecutor.scoped(chf)
_ <- ZIO.succeed(ResourceLeakDetector.setLevel(nettyConfig.leakDetectionLevel.toNetty))
@@ -96,9 +96,9 @@ object NettyDriver {
val make: ZIO[
RoutesRef
& ChannelFactory[ServerChannel]
- & ChannelInitializer[Channel]
+ & ChannelInitializer[NettyChannel]
& ServerEventLoopGroups
- & Server.Config
+ & ServerRuntimeConfig
& NettyConfig
& ServerInboundHandler,
Nothing,
@@ -107,9 +107,9 @@ object NettyDriver {
for {
app <- ZIO.service[RoutesRef]
cf <- ZIO.service[ChannelFactory[ServerChannel]]
- cInit <- ZIO.service[ChannelInitializer[Channel]]
+ cInit <- ZIO.service[ChannelInitializer[NettyChannel]]
elg <- ZIO.service[ServerEventLoopGroups]
- sc <- ZIO.service[Server.Config]
+ sc <- ZIO.service[ServerRuntimeConfig]
nsc <- ZIO.service[NettyConfig]
sih <- ZIO.service[ServerInboundHandler]
} yield new NettyDriver(
@@ -122,10 +122,11 @@ object NettyDriver {
nettyConfig = nsc,
)
- val manual
- : ZLayer[ServerEventLoopGroups & ChannelFactory[ServerChannel] & Server.Config & NettyConfig, Nothing, Driver] = {
+ val manual: ZLayer[ServerEventLoopGroups & ChannelFactory[
+ ServerChannel,
+ ] & ServerRuntimeConfig & NettyConfig, Nothing, Driver] = {
implicit val trace: Trace = Trace.empty
- ZLayer.makeSome[ServerEventLoopGroups & ChannelFactory[ServerChannel] & Server.Config & NettyConfig, Driver](
+ ZLayer.makeSome[ServerEventLoopGroups & ChannelFactory[ServerChannel] & ServerRuntimeConfig & NettyConfig, Driver](
ZLayer(AppRef.empty),
ServerChannelInitializer.layer,
ServerInboundHandler.live,
@@ -133,12 +134,12 @@ object NettyDriver {
)
}
- val customized: ZLayer[Server.Config & NettyConfig, Throwable, Driver] = {
+ val customized: ZLayer[ServerRuntimeConfig & NettyConfig, Throwable, Driver] = {
val serverChannelFactory: ZLayer[NettyConfig, Nothing, ChannelFactory[ServerChannel]] =
ChannelFactories.Server.fromConfig
val eventLoopGroup: ZLayer[NettyConfig, Nothing, ServerEventLoopGroups] = ServerEventLoopGroups.live
- ZLayer.makeSome[Server.Config & NettyConfig, Driver](
+ ZLayer.makeSome[ServerRuntimeConfig & NettyConfig, Driver](
eventLoopGroup,
serverChannelFactory,
manual,
@@ -148,6 +149,7 @@ object NettyDriver {
val live: ZLayer[Server.Config, Throwable, Driver] =
ZLayer.makeSome[Server.Config, Driver](
ZLayer.succeed(NettyConfig.default),
+ ServerRuntimeConfig.layer,
customized,
)
}
diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerChannelInitializer.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerChannelInitializer.scala
index c719377123..db157cc15e 100644
--- a/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerChannelInitializer.scala
+++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/ServerChannelInitializer.scala
@@ -21,10 +21,10 @@ import java.util.concurrent.TimeUnit
import zio._
import zio.stacktracer.TracingImplicits.disableAutoTrace
-import zio.http.Server
import zio.http.Server.RequestStreaming
import zio.http.netty.model.Conversions
import zio.http.netty.{HybridContentLengthHandler, Names}
+import zio.http.{Server, ServerRuntimeConfig}
import io.netty.channel.ChannelHandler.Sharable
import io.netty.channel._
@@ -38,7 +38,7 @@ import io.netty.handler.timeout.ReadTimeoutHandler
*/
@Sharable
private[zio] final case class ServerChannelInitializer(
- cfg: Server.Config,
+ cfg: ServerRuntimeConfig,
reqHandler: ChannelInboundHandler,
) extends ChannelInitializer[Channel] {
@@ -48,11 +48,11 @@ private[zio] final case class ServerChannelInitializer(
val pipeline = channel.pipeline()
// SSL
// Add SSL Handler if CTX is available
- cfg.sslConfig.foreach { sslCfg =>
- pipeline.addFirst(Names.SSLHandler, new ServerSSLDecoder(sslCfg, cfg))
+ cfg.config.sslConfig.foreach { sslCfg =>
+ pipeline.addFirst(Names.SSLHandler, new ServerSSLDecoder(sslCfg, cfg.config))
}
- cfg.idleTimeout.foreach { timeout =>
+ cfg.config.idleTimeout.foreach { timeout =>
pipeline.addLast(Names.ReadTimeoutHandler, new ReadTimeoutHandler(timeout.toMillis, TimeUnit.MILLISECONDS))
}
@@ -62,19 +62,21 @@ private[zio] final case class ServerChannelInitializer(
Names.HttpRequestDecoder,
new HttpRequestDecoder(
new HttpDecoderConfig()
- .setMaxInitialLineLength(cfg.maxInitialLineLength)
- .setMaxHeaderSize(cfg.maxHeaderSize)
+ .setMaxInitialLineLength(cfg.config.maxInitialLineLength)
+ .setMaxHeaderSize(cfg.config.maxHeaderSize)
.setMaxChunkSize(DEFAULT_MAX_CHUNK_SIZE)
- .setValidateHeaders(false),
+ .setValidateHeaders(cfg.validateHeaders),
),
)
pipeline.addLast(Names.HttpResponseEncoder, new HttpResponseEncoder())
// HttpContentDecompressor
- if (cfg.requestDecompression.enabled)
- pipeline.addLast(Names.HttpRequestDecompression, new HttpContentDecompressor(cfg.requestDecompression.strict, 0))
-
- cfg.responseCompression.foreach(ops => {
+ if (cfg.config.requestDecompression.enabled)
+ pipeline.addLast(
+ Names.HttpRequestDecompression,
+ new HttpContentDecompressor(cfg.config.requestDecompression.strict, 0),
+ )
+ cfg.config.responseCompression.foreach(ops => {
pipeline.addLast(
Names.HttpResponseCompression,
new HttpContentCompressor(ops.contentThreshold, ops.options.map(Conversions.compressionOptionsToNetty): _*),
@@ -82,7 +84,7 @@ private[zio] final case class ServerChannelInitializer(
})
// ObjectAggregator
- cfg.requestStreaming match {
+ cfg.config.requestStreaming match {
case RequestStreaming.Enabled =>
case RequestStreaming.Disabled(maximumContentLength) =>
pipeline.addLast(Names.HttpObjectAggregator, new HttpObjectAggregator(maximumContentLength))
@@ -93,11 +95,12 @@ private[zio] final case class ServerChannelInitializer(
// ExpectContinueHandler
// Add expect continue handler is settings is true
- if (cfg.acceptContinue) pipeline.addLast(Names.HttpServerExpectContinue, new HttpServerExpectContinueHandler())
+ if (cfg.config.acceptContinue)
+ pipeline.addLast(Names.HttpServerExpectContinue, new HttpServerExpectContinueHandler())
// KeepAliveHandler
// Add Keep-Alive handler is settings is true
- if (cfg.keepAlive) pipeline.addLast(Names.HttpKeepAliveHandler, new HttpServerKeepAliveHandler)
+ if (cfg.config.keepAlive) pipeline.addLast(Names.HttpKeepAliveHandler, new HttpServerKeepAliveHandler)
pipeline.addLast(Names.HttpServerFlushConsolidation, new FlushConsolidationHandler())
@@ -112,10 +115,11 @@ private[zio] final case class ServerChannelInitializer(
object ServerChannelInitializer {
implicit val trace: Trace = Trace.empty
- val layer: ZLayer[SimpleChannelInboundHandler[HttpObject] with Server.Config, Nothing, ServerChannelInitializer] =
+ val layer
+ : ZLayer[SimpleChannelInboundHandler[HttpObject] with ServerRuntimeConfig, Nothing, ServerChannelInitializer] =
ZLayer.fromZIO {
for {
- cfg <- ZIO.service[Server.Config]
+ cfg <- ZIO.service[ServerRuntimeConfig]
handler <- ZIO.service[SimpleChannelInboundHandler[HttpObject]]
} yield ServerChannelInitializer(cfg, handler)
}
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 e319215113..e843ba4dbc 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
@@ -42,7 +42,7 @@ import io.netty.util.ReferenceCountUtil
@Sharable
private[zio] final case class ServerInboundHandler(
appRef: RoutesRef,
- config: Server.Config,
+ config: ServerRuntimeConfig,
)(implicit trace: Trace)
extends SimpleChannelInboundHandler[HttpObject](false) { self =>
@@ -51,9 +51,11 @@ private[zio] final case class ServerInboundHandler(
private var handler: Handler[Any, Nothing, Request, Response] = _
private var runtime: NettyRuntime = _
+ private val cfg = config.config
+
val inFlightRequests: LongAdder = new LongAdder()
- private val readClientCert = config.sslConfig.exists(_.includeClientCert)
- private val avoidCtxSwitching = config.avoidContextSwitching
+ private val readClientCert = cfg.sslConfig.exists(_.includeClientCert)
+ private val avoidCtxSwitching = cfg.avoidContextSwitching
def refreshApp(): Unit = {
val pair = appRef.get()
@@ -87,12 +89,17 @@ private[zio] final case class ServerInboundHandler(
)
releaseRequest()
} else {
- val req = makeZioRequest(ctx, jReq)
- val exit = handler(req)
- if (attemptImmediateWrite(ctx, req.method, exit)) {
+ val req = makeZioRequest(ctx, jReq)
+ if (config.validateHeaders && !validateHostHeader(req)) {
+ attemptFastWrite(ctx, req.method, Response.status(Status.BadRequest))
releaseRequest()
} else {
- writeResponse(ctx, runtime, exit, req)(releaseRequest)
+ val exit = handler(req)
+ if (attemptImmediateWrite(ctx, req.method, exit)) {
+ releaseRequest()
+ } else {
+ writeResponse(ctx, runtime, exit, req)(releaseRequest)
+ }
}
}
} finally {
@@ -108,6 +115,43 @@ private[zio] final case class ServerInboundHandler(
}
+ private def validateHostHeader(req: Request): Boolean = {
+ val host = req.headers.getUnsafe("Host")
+ if (host != null) {
+ var i = 0
+ var isValidHost = true
+
+ while (i < host.length && host.charAt(i) != ':') {
+ val c = host.charAt(i)
+ if (!(c.isLetterOrDigit || c == '.' || c == '-')) {
+ isValidHost = false
+ i = host.length
+ }
+ i += 1
+ }
+
+ val colonIdx = host.indexOf(':')
+ val isValidPort =
+ if (colonIdx == -1) true
+ else {
+ var j = colonIdx + 1
+ var portValid = true
+ while (j < host.length) {
+ if (!host.charAt(j).isDigit) {
+ portValid = false
+ j = host.length
+ }
+ j += 1
+ }
+ portValid
+ }
+
+ isValidHost && isValidPort
+ } else {
+ false
+ }
+ }
+
override def exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable): Unit =
cause match {
case ioe: IOException if {
@@ -115,7 +159,7 @@ private[zio] final case class ServerInboundHandler(
(msg ne null) && msg.contains("Connection reset")
} =>
case t =>
- if ((runtime ne null) && config.logWarningOnFatalError) {
+ if ((runtime ne null) && cfg.logWarningOnFatalError) {
runtime.unsafeRunSync {
// We cannot return the generated response from here, but still calling the handler for its side effect
// for example logging.
@@ -297,7 +341,7 @@ private[zio] final case class ServerInboundHandler(
.addLast(
new WebSocketServerProtocolHandler(
NettySocketProtocol
- .serverBuilder(webSocketApp.customConfig.getOrElse(config.webSocketConfig))
+ .serverBuilder(webSocketApp.customConfig.getOrElse(cfg.webSocketConfig))
.build(),
),
)
@@ -361,7 +405,7 @@ private[zio] final case class ServerInboundHandler(
object ServerInboundHandler {
val live: ZLayer[
- RoutesRef & Server.Config,
+ RoutesRef & ServerRuntimeConfig,
Nothing,
ServerInboundHandler,
] = {
@@ -369,7 +413,7 @@ object ServerInboundHandler {
ZLayer.fromZIO {
for {
appRef <- ZIO.service[RoutesRef]
- config <- ZIO.service[Server.Config]
+ config <- ZIO.service[ServerRuntimeConfig]
} yield ServerInboundHandler(appRef, config)
}
}
diff --git a/zio-http/jvm/src/main/scala/zio/http/netty/server/package.scala b/zio-http/jvm/src/main/scala/zio/http/netty/server/package.scala
index 11da4dc374..4cca605d8f 100644
--- a/zio-http/jvm/src/main/scala/zio/http/netty/server/package.scala
+++ b/zio-http/jvm/src/main/scala/zio/http/netty/server/package.scala
@@ -37,7 +37,8 @@ package object server {
val live: ZLayer[Server.Config, Throwable, Driver] =
NettyDriver.live
- val manual
- : ZLayer[ServerEventLoopGroups & ChannelFactory[ServerChannel] & Server.Config & NettyConfig, Nothing, Driver] =
+ val manual: ZLayer[ServerEventLoopGroups & ChannelFactory[
+ ServerChannel,
+ ] & ServerRuntimeConfig & NettyConfig, Nothing, Driver] =
NettyDriver.manual
}
diff --git a/zio-http/jvm/src/test/scala-3/zio/http/endpoint/UnionRoundtripSpec.scala b/zio-http/jvm/src/test/scala-3/zio/http/endpoint/UnionRoundtripSpec.scala
index f28b91d703..e038f25d9b 100644
--- a/zio-http/jvm/src/test/scala-3/zio/http/endpoint/UnionRoundtripSpec.scala
+++ b/zio-http/jvm/src/test/scala-3/zio/http/endpoint/UnionRoundtripSpec.scala
@@ -39,6 +39,7 @@ import zio.http.netty.NettyConfig
object UnionRoundtripSpec extends ZIOHttpSpec {
val testLayer: ZLayer[Any, Throwable, Server & Client & Scope] =
ZLayer.make[Server & Client & Scope](
+ ServerRuntimeConfig.layer,
Server.customized,
ZLayer.succeed(Server.Config.default.onAnyOpenPort.enableRequestStreaming),
Client.customized.map(env => ZEnvironment(env.get)),
@@ -318,6 +319,7 @@ object UnionRoundtripSpec extends ZIOHttpSpec {
)
},
).provide(
+ ServerRuntimeConfig.layer,
Server.customized,
ZLayer.succeed(Server.Config.default.onAnyOpenPort.enableRequestStreaming),
Client.customized.map(env => ZEnvironment(env.get @@ clientDebugAspect)),
diff --git a/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala
index 077aa1fa39..62d992422d 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ClientStreamingSpec.scala
@@ -331,6 +331,7 @@ object ClientStreamingSpec extends RoutesRunnableSpec {
)
.idleTimeout(100.seconds),
),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
)
.fork
diff --git a/zio-http/jvm/src/test/scala/zio/http/ConformanceE2ESpec.scala b/zio-http/jvm/src/test/scala/zio/http/ConformanceE2ESpec.scala
new file mode 100644
index 0000000000..c02108a5eb
--- /dev/null
+++ b/zio-http/jvm/src/test/scala/zio/http/ConformanceE2ESpec.scala
@@ -0,0 +1,47 @@
+package zio.http
+
+import zio._
+import zio.test.Assertion._
+import zio.test.TestAspect._
+import zio.test._
+
+import zio.http._
+import zio.http.internal.{DynamicServer, RoutesRunnableSpec}
+import zio.http.netty.NettyConfig
+
+object ConformanceE2ESpec extends RoutesRunnableSpec {
+ private val port = 8080
+ private val MaxSize = 1024 * 10
+ val config = Server.Config.default
+ .requestDecompression(true)
+ .disableRequestStreaming(MaxSize)
+ .port(port)
+ .responseCompression()
+ .validateHeaders(true)
+
+ private val app = serve
+ def conformanceSpec = suite("ConformanceE2ESpec")(
+ test("should return 400 Bad Request if Host header is missing") {
+ val routes = Handler.ok.toRoutes
+ val res = routes.deploy.status.run(path = Path.root, headers = Headers(Header.Host("%%%%invalid%%%%")))
+ assertZIO(res)(equalTo(Status.BadRequest))
+ },
+ test("should return 200 OK if Host header is present") {
+ val routes = Handler.ok.toRoutes
+ val res = routes.deploy.status.run(path = Path.root, headers = Headers(Header.Host("localhost")))
+ assertZIO(res)(equalTo(Status.Ok))
+ },
+ )
+ override def spec =
+ suite("ConformanceE2ESpec") {
+ val spec = conformanceSpec
+ suite("app without request streaming") { app.as(List(spec)) }
+ }.provideShared(
+ Scope.default,
+ DynamicServer.live,
+ ZLayer.succeed(config),
+ Server.customized,
+ Client.default,
+ ZLayer.succeed(NettyConfig.default),
+ ) @@ sequential @@ withLiveClock
+}
diff --git a/zio-http/jvm/src/test/scala/zio/http/ConformanceSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ConformanceSpec.scala
new file mode 100644
index 0000000000..318ab01d8f
--- /dev/null
+++ b/zio-http/jvm/src/test/scala/zio/http/ConformanceSpec.scala
@@ -0,0 +1,1314 @@
+package zio.http
+
+import java.time.ZonedDateTime
+import java.time.format.DateTimeFormatter
+
+import zio._
+import zio.test.Assertion._
+import zio.test.TestAspect.{ignore, _}
+import zio.test._
+
+import zio.http._
+import zio.http.codec.{HeaderCodec, PathCodec}
+import zio.http.endpoint.Endpoint
+
+object ConformanceSpec extends ZIOSpecDefault {
+
+ /**
+ * This test suite is inspired by and built upon the findings from the
+ * research paper: "Who's Breaking the Rules? Studying Conformance to the HTTP
+ * Specifications and its Security Impact" by Jannis Rautenstrauch and Ben
+ * Stock, presented at the 19th ACM Asia Conference on Computer and
+ * Communications Security (ASIA CCS) 2024.
+ *
+ * Paper URL: https://doi.org/10.1145/3634737.3637678 GitHub Project:
+ * https://github.com/cispa/http-conformance
+ */
+
+ val validUrl = URL.decode("http://example.com").toOption.getOrElse(URL.root)
+
+ override def spec =
+ suite("ConformanceSpec")(
+ suite("Statuscodes")(
+ test("should not send body for 204 No Content responses(code_204_no_additional_content)") {
+ val app = Routes(
+ Method.GET / "no-content" -> Handler.fromResponse(
+ Response.status(Status.NoContent),
+ ),
+ )
+
+ val request = Request.get("/no-content")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.NoContent,
+ response.body.isEmpty,
+ )
+ },
+ test("should not send body for 205 Reset Content responses(code_205_no_content_allowed)") {
+ val app = Routes(
+ Method.GET / "reset-content" -> Handler.fromResponse(
+ Response.status(Status.ResetContent),
+ ),
+ )
+
+ val request = Request.get("/reset-content")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(response.status == Status.ResetContent, response.body.isEmpty)
+ },
+ test("should include Content-Range for 206 Partial Content response(code_206_content_range)") {
+ val app = Routes(
+ Method.GET / "partial" -> Handler.fromResponse(
+ Response
+ .status(Status.PartialContent)
+ .addHeader(Header.ContentRange.StartEnd("bytes", 0, 14)),
+ ),
+ )
+
+ val request = Request.get("/partial")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.PartialContent,
+ response.headers.contains(Header.ContentRange.name),
+ )
+ },
+ test(
+ "should not include Content-Range in header for multipart/byteranges response(code_206_content_range_of_multiple_part_response)",
+ ) {
+ val boundary = zio.http.Boundary("A12345")
+
+ val app = Routes(
+ Method.GET / "partial" -> Handler.fromResponse(
+ Response
+ .status(Status.PartialContent)
+ .addHeader(Header.ContentType(MediaType("multipart", "byteranges"), Some(boundary))),
+ ),
+ )
+
+ val request = Request.get("/partial")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.PartialContent,
+ !response.headers.contains(Header.ContentRange.name),
+ response.headers.contains(Header.ContentType.name),
+ )
+ },
+ test("should include necessary headers in 206 Partial Content response(code_206_headers)") {
+ val app = Routes(
+ Method.GET / "partial" -> Handler.fromResponse(
+ Response
+ .status(Status.PartialContent)
+ .addHeader(Header.ETag.Strong("abc"))
+ .addHeader(Header.CacheControl.MaxAge(3600)),
+ ),
+ Method.GET / "full" -> Handler.fromResponse(
+ Response
+ .status(Status.Ok)
+ .addHeader(Header.ETag.Strong("abc"))
+ .addHeader(Header.CacheControl.MaxAge(3600)),
+ ),
+ )
+
+ val requestWithRange =
+ Request.get("/partial").addHeader(Header.Range.Single("bytes", 0, Some(14)))
+ val requestWithoutRange = Request.get("/full")
+
+ for {
+ responseWithRange <- app.runZIO(requestWithRange)
+ responseWithoutRange <- app.runZIO(requestWithoutRange)
+ } yield assertTrue(
+ responseWithRange.status == Status.PartialContent,
+ responseWithRange.headers.contains(Header.ETag.name),
+ responseWithRange.headers.contains(Header.CacheControl.name),
+ responseWithoutRange.status == Status.Ok,
+ )
+ },
+ test("should include WWW-Authenticate header for 401 Unauthorized response(code_401_www_authenticate)") {
+ val app = Routes(
+ Method.GET / "unauthorized" -> Handler.fromResponse(
+ Response
+ .status(Status.Unauthorized)
+ .addHeader(Header.WWWAuthenticate.Basic(Some("simple"))),
+ ),
+ )
+
+ val request = Request.get("/unauthorized")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.Unauthorized,
+ response.headers.contains(Header.WWWAuthenticate.name),
+ )
+ },
+ test("should return 401 Unauthorized when Authorization header is missing(code_401_missing_authorization)") {
+ val app = Routes(
+ Endpoint(RoutePattern.GET / "protected")
+ .header(HeaderCodec.authorization)
+ .out[String]
+ .implement { _ => ZIO.succeed("Authenticated") },
+ )
+
+ val requestWithoutAuth = Request.get("/protected")
+
+ for {
+ response <- app.runZIO(requestWithoutAuth)
+ } yield assertTrue(
+ response.status == Status.Unauthorized,
+ )
+ } @@ ignore,
+ test(
+ "should include Proxy-Authenticate header for 407 Proxy Authentication Required response(code_407_proxy_authenticate)",
+ ) {
+ val app = Routes(
+ Method.GET / "proxy-auth" -> Handler.fromResponse(
+ Response
+ .status(Status.ProxyAuthenticationRequired)
+ .addHeader(
+ Header.ProxyAuthenticate(Header.AuthenticationScheme.Basic, Some("proxy")),
+ ),
+ ),
+ )
+
+ val request = Request.get("/proxy-auth")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.ProxyAuthenticationRequired,
+ response.headers.contains(Header.ProxyAuthenticate.name),
+ )
+ },
+ test("should return 304 without content(code_304_no_content)") {
+ val app = Routes(
+ Method.GET / "no-content" -> Handler.fromResponse(
+ Response
+ .status(Status.NotModified)
+ .copy(body = Body.empty),
+ ),
+ )
+
+ val request = Request.get("/no-content")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.NotModified,
+ response.body.isEmpty,
+ )
+ },
+ test("should return 304 with correct headers(code_304_headers)") {
+ val headers = Headers(
+ Header.ETag.Strong("abc"),
+ Header.CacheControl.MaxAge(3600),
+ Header.Vary("Accept-Encoding"),
+ )
+
+ val app = Routes(
+ Method.GET / "with-headers" -> Handler.fromResponse(
+ Response
+ .status(Status.NotModified)
+ .addHeaders(headers),
+ ),
+ )
+
+ val request = Request.get("/with-headers")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.NotModified,
+ response.headers.contains(Header.ETag.name),
+ response.headers.contains(Header.CacheControl.name),
+ response.headers.contains(Header.Vary.name),
+ )
+ },
+ test("should include Location header in 300 MULTIPLE CHOICES response(code_300_location)") {
+ val testUrl = URL.decode("/People.html#tim").toOption.getOrElse(URL.root)
+
+ val validResponse = Response
+ .status(Status.MultipleChoices)
+ .addHeader(Header.Location(testUrl))
+
+ val invalidResponse = Response
+ .status(Status.MultipleChoices)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.MultipleChoices,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.MultipleChoices,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test("300 MULTIPLE CHOICES response should have body content(code_300_metadata)") {
+ val validResponse = Response
+ .status(Status.MultipleChoices)
+ .copy(body = Body.fromString("
ABC
"))
+
+ val invalidResponse = Response
+ .status(Status.MultipleChoices)
+ .copy(body = Body.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ validBody <- responseValid.body.asString
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ invalidBody <- responseInvalid.body.asString
+
+ } yield assertTrue(
+ responseValid.status == Status.MultipleChoices,
+ validBody.contains("ABC"),
+ responseInvalid.status == Status.MultipleChoices,
+ invalidBody.isEmpty,
+ )
+ },
+ test("should not require body content for HEAD requests(code_300_metadata)") {
+ val response = Response
+ .status(Status.MultipleChoices)
+ .copy(body = Body.empty)
+ val app = Routes(
+ Method.HEAD / "head" -> Handler.fromResponse(response),
+ )
+
+ for {
+ headResponse <- app.runZIO(Request.head("/head"))
+ } yield assertTrue(
+ headResponse.status == Status.MultipleChoices,
+ headResponse.body.isEmpty,
+ )
+ },
+ test("should include Location header in 301 MOVED PERMANENTLY response(code_301_location)") {
+
+ val validResponse = Response
+ .status(Status.MovedPermanently)
+ .addHeader(Header.Location(validUrl))
+
+ val invalidResponse = Response
+ .status(Status.MovedPermanently)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.MovedPermanently,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.MovedPermanently,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test("should include Location header in 302 FOUND response(code_302_location)") {
+
+ val validResponse = Response
+ .status(Status.Found)
+ .addHeader(Header.Location(validUrl))
+
+ val invalidResponse = Response
+ .status(Status.Found)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.Found,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.Found,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test("should include Location header in 303 SEE OTHER response(code_303_location)") {
+
+ val validResponse = Response
+ .status(Status.SeeOther)
+ .addHeader(Header.Location(validUrl))
+
+ val invalidResponse = Response
+ .status(Status.SeeOther)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.SeeOther,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.SeeOther,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test("should include Location header in 307 TEMPORARY REDIRECT response(code_307_location)") {
+
+ val validResponse = Response
+ .status(Status.TemporaryRedirect)
+ .addHeader(Header.Location(validUrl))
+
+ val invalidResponse = Response
+ .status(Status.TemporaryRedirect)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.TemporaryRedirect,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.TemporaryRedirect,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test("should include Location header in 308 PERMANENT REDIRECT response(code_308_location)") {
+
+ val validResponse = Response
+ .status(Status.PermanentRedirect)
+ .addHeader(Header.Location(validUrl))
+
+ val invalidResponse = Response
+ .status(Status.PermanentRedirect)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.PermanentRedirect,
+ responseValid.headers.contains(Header.Location.name),
+ responseInvalid.status == Status.PermanentRedirect,
+ !responseInvalid.headers.contains(Header.Location.name),
+ )
+ },
+ test(
+ "should include Retry-After header in 413 Content Too Large response if condition is temporary (code_413_retry_after)",
+ ) {
+ val validResponse = Response
+ .status(Status.RequestEntityTooLarge)
+ .addHeader(Header.RetryAfter.ByDuration(10.seconds))
+
+ val invalidResponse = Response
+ .status(Status.RequestEntityTooLarge)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.RequestEntityTooLarge,
+ responseValid.headers.contains(Header.RetryAfter.name),
+ responseInvalid.status == Status.RequestEntityTooLarge,
+ !responseInvalid.headers.contains(Header.RetryAfter.name),
+ )
+ },
+ test(
+ "should include Accept or Accept-Encoding header in 415 Unsupported Media Type response (code_415_unsupported_media_type)",
+ ) {
+ val validResponse = Response
+ .status(Status.UnsupportedMediaType)
+ .addHeader(Header.Accept(MediaType.application.json))
+
+ val invalidResponse = Response
+ .status(Status.UnsupportedMediaType)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.UnsupportedMediaType,
+ responseValid.headers.contains(Header.Accept.name) ||
+ responseValid.headers.contains(Header.AcceptEncoding.name),
+ responseInvalid.status == Status.UnsupportedMediaType,
+ !responseInvalid.headers.contains(Header.Accept.name) &&
+ !responseInvalid.headers.contains(Header.AcceptEncoding.name),
+ )
+ },
+ test("should include Content-Range header in 416 Range Not Satisfiable response (code_416_content_range)") {
+ val validResponse = Response
+ .status(Status.RequestedRangeNotSatisfiable)
+ .addHeader(Header.ContentRange.RangeTotal("bytes", 47022))
+
+ val invalidResponse = Response
+ .status(Status.RequestedRangeNotSatisfiable)
+ .addHeader(Header.Custom("Content-Range", ",;"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.status == Status.RequestedRangeNotSatisfiable,
+ responseValid.headers.contains(Header.ContentRange.name),
+ responseInvalid.status == Status.RequestedRangeNotSatisfiable,
+ responseInvalid.headers.contains(Header.ContentRange.name),
+ responseInvalid.headers.get(Header.ContentRange.name).contains(",;"),
+ )
+ },
+ ),
+ suite("HTTP Headers")(
+ test("should not include Content-Length header for 2XX CONNECT responses(content_length_2XX_connect)") {
+ val app = Routes(
+ Method.CONNECT / "" -> Handler.fromResponse(
+ Response.status(Status.Ok),
+ ),
+ )
+
+ val decodedUrl = URL.decode("https://example.com:443")
+
+ val request = decodedUrl match {
+ case Right(url) => Request(method = Method.CONNECT, url = url)
+ case Left(_) => throw new RuntimeException("Failed to decode the URL")
+ }
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.Ok,
+ !response.headers.contains(Header.ContentLength.name),
+ )
+ },
+ test("should not include Transfer-Encoding header for 2XX CONNECT responses(transfer_encoding_2XX_connect)") {
+ val app = Routes(
+ Method.CONNECT / "" -> Handler.fromResponse(
+ Response.status(Status.Ok),
+ ),
+ )
+
+ val decodedUrl = URL.decode("https://example.com:443")
+
+ val request = decodedUrl match {
+ case Right(url) => Request(method = Method.CONNECT, url = url)
+ case Left(_) => throw new RuntimeException("Failed to decode the URL")
+ }
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.Ok,
+ !response.headers.contains(Header.TransferEncoding.name),
+ )
+ },
+ test("should not return overly detailed Server header(server_header_long)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Server", "SimpleServer"))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Server", "a" * 101))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ assertTrue(
+ responseValid.headers.get(Header.Server.name).exists(_.length <= 100),
+ responseInvalid.headers.get(Header.Server.name).exists(_.length > 100),
+ )
+ }
+ },
+ test("should include Content-Type header for responses with content(content_type_header_required)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentType(MediaType.text.html))
+ .copy(body = Body.fromString("ABC
"))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .copy(body = Body.fromString("ABC
"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ assertTrue(
+ responseValid.headers.contains(Header.ContentType.name),
+ !responseInvalid.headers.contains(Header.ContentType.name),
+ )
+ }
+ },
+ test("should include Accept-Patch header when PATCH is supported(accept_patch_presence)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.AcceptPatch(NonEmptyChunk(MediaType.application.json)))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.OPTIONS / "valid" -> Handler.fromResponse(validResponse),
+ Method.OPTIONS / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.options("/valid"))
+ responseInvalid <- app.runZIO(Request.options("/invalid"))
+ } yield {
+ assertTrue(
+ responseValid.headers.contains(Header.AcceptPatch.name),
+ !responseInvalid.headers.contains(Header.AcceptPatch.name),
+ )
+ }
+ },
+ test("should include Date header in responses (date_header_required)") {
+ val validDate = ZonedDateTime.parse("Thu, 20 Mar 2025 20:03:00 GMT", DateTimeFormatter.RFC_1123_DATE_TIME)
+
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Date(validDate))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .copy(headers = Headers.empty)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.headers.contains(Header.Date.name),
+ !responseInvalid.headers.contains(Header.Date.name),
+ )
+ },
+ suite("CSP Header")(
+ test("should not send more than one CSP header (duplicate_csp)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentSecurityPolicy.defaultSrc(Header.ContentSecurityPolicy.Source.Self))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentSecurityPolicy.defaultSrc(Header.ContentSecurityPolicy.Source.Self))
+ .addHeader(Header.ContentSecurityPolicy.imgSrc(Header.ContentSecurityPolicy.Source.Self))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ val cspHeadersValid = responseValid.headers.toList.collect {
+ case h if h.headerName == Header.ContentSecurityPolicy.name => h
+ }
+ val cspHeadersInvalid = responseInvalid.headers.toList.collect {
+ case h if h.headerName == Header.ContentSecurityPolicy.name => h
+ }
+
+ assertTrue(
+ cspHeadersValid.length == 1,
+ cspHeadersInvalid.length > 1,
+ )
+ }
+ },
+ // Note: Content-Security-Policy-Report-Only Header to be Supported
+ ),
+ ),
+ suite("sts")(
+ // Note: Strict-Transport-Security Header to be Supported
+
+ ),
+ suite("Transfer-Encoding")(
+ suite("no_transfer_encoding_1xx_204")(
+ test("should return valid when Transfer-Encoding is not present for 1xx or 204 status") {
+ val app = Routes(
+ Method.GET / "no-content" -> Handler.fromResponse(
+ Response.status(Status.NoContent),
+ ),
+ Method.GET / "continue" -> Handler.fromResponse(
+ Response.status(Status.Continue),
+ ),
+ )
+ for {
+ responseNoContent <- app.runZIO(Request.get("/no-content"))
+ responseContinue <- app.runZIO(Request.get("/continue"))
+ } yield assertTrue(responseNoContent.status == Status.NoContent) &&
+ assertTrue(!responseNoContent.headers.contains(Header.TransferEncoding.name)) &&
+ assertTrue(responseContinue.status == Status.Continue) &&
+ assertTrue(!responseContinue.headers.contains(Header.TransferEncoding.name))
+ },
+ test("should return invalid when Transfer-Encoding is present for 1xx or 204 status") {
+ val app = Routes(
+ Method.GET / "no-content" -> Handler.fromResponse(
+ Response.status(Status.NoContent).addHeader(Header.TransferEncoding.Chunked),
+ ),
+ Method.GET / "continue" -> Handler.fromResponse(
+ Response.status(Status.Continue).addHeader(Header.TransferEncoding.Chunked),
+ ),
+ )
+
+ for {
+ responseNoContent <- app.runZIO(Request.get("/no-content"))
+ responseContinue <- app.runZIO(Request.get("/continue"))
+ } yield assertTrue(responseNoContent.status == Status.NoContent) &&
+ assertTrue(responseNoContent.headers.contains(Header.TransferEncoding.name)) &&
+ assertTrue(responseContinue.status == Status.Continue) &&
+ assertTrue(responseContinue.headers.contains(Header.TransferEncoding.name))
+ },
+ ),
+ suite("transfer_encoding_http11")(
+ test("should send Transfer-Encoding in response if request HTTP version is 1.1 or higher") {
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromResponse(
+ Response.ok.addHeader(Header.TransferEncoding.Chunked),
+ ),
+ )
+
+ val request = Request.get("/test").copy(version = Version.`HTTP/1.1`)
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.Ok,
+ response.headers.contains(Header.TransferEncoding.name),
+ )
+ },
+ ),
+ ),
+ suite("HTTP-Methods")(
+ test("should not send body for HEAD requests(content_head_request)") {
+ val route = Routes(
+ Method.GET / "test" -> Handler.fromResponse(Response.text("This is the body")),
+ Method.HEAD / "test" -> Handler.fromResponse(Response(status = Status.Ok)),
+ )
+ val app = route
+ val headRequest = Request.head("/test")
+ for {
+ response <- app.runZIO(headRequest)
+ } yield assertTrue(
+ response.status == Status.Ok,
+ response.body.isEmpty,
+ )
+ },
+ test("should not return 206, 304, or 416 status codes for POST requests(post_invalid_response_codes)") {
+
+ val app = Routes(
+ Method.POST / "test" -> Handler.fromResponse(Response.status(Status.Ok)),
+ )
+ for {
+ res <- app.runZIO(Request.post("/test", Body.empty))
+
+ } yield assertTrue(
+ res.status != Status.PartialContent,
+ res.status != Status.NotModified,
+ res.status != Status.RequestedRangeNotSatisfiable,
+ res.status == Status.Ok,
+ )
+ },
+ test("should send the same headers for HEAD and GET requests (head_get_headers)") {
+ val getResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentType(MediaType.text.html))
+ .addHeader(Header.Custom("X-Custom-Header", "value"))
+ .copy(body = Body.fromString("ABC
"))
+
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromResponse(getResponse),
+ Method.HEAD / "test" -> Handler.fromResponse(getResponse.copy(body = Body.empty)),
+ )
+
+ for {
+ getResponse <- app.runZIO(Request.get("/test"))
+ headResponse <- app.runZIO(Request.head("/test"))
+ getHeaders = getResponse.headers.toList.map(_.headerName).toSet
+ headHeaders = headResponse.headers.toList.map(_.headerName).toSet
+ } yield assertTrue(
+ getHeaders == headHeaders,
+ )
+ },
+ test("should reply with 404 response for truly non-existent path") {
+ val app = Routes(
+ Method.GET / "existing-path" -> Handler.ok,
+ )
+ val request = Request.get(URL(Path.root / "non-existent-path"))
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.NotFound,
+ )
+ },
+ ),
+ suite("HTTP/1.1")(
+ test("should not generate a bare CR in headers for HTTP/1.1(no_bare_cr)") {
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromZIO {
+ ZIO.succeed(
+ Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("A", "1\r\nB: 2")),
+ )
+ },
+ )
+
+ val request = Request
+ .get("/test")
+ .copy(version = Version.Http_1_1)
+
+ for {
+ response <- app.runZIO(request)
+ headersString = response.headers.toString
+ isValid = !headersString.contains("\r") || headersString.contains("\r\n")
+ } yield assertTrue(isValid)
+ },
+ test("should allow one CRLF in front of the request line (allow_crlf_start)") {
+ val crlfPrefix = "\r\n".getBytes
+
+ val validRequest = Request
+ .get("/valid")
+ .withBody(Body.fromChunk(Chunk.fromArray(crlfPrefix ++ "GET /valid HTTP/1.1".getBytes)))
+
+ val invalidRequest = Request
+ .get("/invalid")
+ .withBody(Body.fromChunk(Chunk.fromArray(crlfPrefix ++ "GET /invalid HTTP/1.1".getBytes)))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(Response.status(Status.Ok)),
+ Method.GET / "invalid" -> Handler.fromResponse(Response.status(Status.NotFound)),
+ )
+
+ for {
+ responseValid <- app.runZIO(validRequest)
+ responseInvalid <- app.runZIO(invalidRequest)
+ } yield {
+ assertTrue(
+ responseValid.status.isSuccess || responseValid.status == Status.NotFound,
+ responseInvalid.status == Status.NotFound,
+ )
+ }
+ },
+ test("should send a 'Connection: close' option in final response (close_option_in_final_response)") {
+ val validRequest = Request
+ .get("/valid")
+ .addHeader(Header.Connection.Close)
+
+ val invalidRequest = Request
+ .get("/invalid")
+ .addHeader(Header.Connection.KeepAlive)
+
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Connection.Close)
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Connection.KeepAlive)
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(validRequest)
+ responseInvalid <- app.runZIO(invalidRequest)
+ } yield {
+ assertTrue(
+ responseValid.headers.toList.exists(h =>
+ h.headerName == Header.Connection.name && h.renderedValue == "close",
+ ),
+ responseInvalid.headers.toList.exists(h =>
+ h.headerName == Header.Connection.name && h.renderedValue == "keep-alive",
+ ),
+ )
+ }
+ },
+ ),
+ suite("HTTP")(
+ test("should send Upgrade header with 426 Upgrade Required response(send_upgrade_426)") {
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromResponse(
+ Response
+ .status(Status.UpgradeRequired)
+ .addHeader(Header.Upgrade.Protocol("https", "1.1")),
+ ),
+ )
+
+ val request = Request.get("/test")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.UpgradeRequired,
+ response.headers.contains(Header.Upgrade.name),
+ )
+ },
+ test("should send Upgrade header with 101 Switching Protocols response(send_upgrade_101)") {
+ val app = Routes(
+ Method.GET / "switch" -> Handler.fromResponse(
+ Response
+ .status(Status.SwitchingProtocols)
+ .addHeader(Header.Upgrade.Protocol("https", "1.1")),
+ ),
+ )
+
+ val request = Request.get("/switch")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.SwitchingProtocols,
+ response.headers.contains(Header.Upgrade.name),
+ )
+ },
+ test("should not include Content-Length header for 1xx and 204 No Content responses(content_length_1XX_204)") {
+ val route1xxContinue = Method.GET / "continue" -> Handler.fromResponse(Response(status = Status.Continue))
+ val route1xxSwitch =
+ Method.GET / "switching-protocols" -> Handler.fromResponse(Response(status = Status.SwitchingProtocols))
+ val route1xxProcess =
+ Method.GET / "processing" -> Handler.fromResponse(Response(status = Status.Processing))
+ val route204NoContent =
+ Method.GET / "no-content" -> Handler.fromResponse(Response(status = Status.NoContent))
+
+ val app = Routes(route1xxContinue, route1xxSwitch, route1xxProcess, route204NoContent)
+
+ val requestContinue = Request.get("/continue")
+ val requestSwitch = Request.get("/switching-protocols")
+ val requestProcess = Request.get("/processing")
+ val requestNoContent = Request.get("/no-content")
+
+ for {
+ responseContinue <- app.runZIO(requestContinue)
+ responseSwitch <- app.runZIO(requestSwitch)
+ responseProcess <- app.runZIO(requestProcess)
+ responseNoContent <- app.runZIO(requestNoContent)
+
+ } yield assertTrue(
+ !responseContinue.headers.contains(Header.ContentLength.name),
+ !responseSwitch.headers.contains(Header.ContentLength.name),
+ !responseProcess.headers.contains(Header.ContentLength.name),
+ !responseNoContent.headers.contains(Header.ContentLength.name),
+ )
+ },
+ test(
+ "should not switch to a protocol not indicated by the client in the Upgrade header(switch_protocol_without_client)",
+ ) {
+ val app = Routes(
+ Method.GET / "switch" -> Handler.fromFunctionZIO { (request: Request) =>
+ val clientUpgrade = request.headers.get(Header.Upgrade.name)
+
+ ZIO.succeed {
+ clientUpgrade match {
+ case Some("https/1.1") =>
+ Response
+ .status(Status.SwitchingProtocols)
+ .addHeader(Header.Upgrade.Protocol("https", "1.1"))
+ case Some(_) =>
+ Response.status(Status.BadRequest)
+ case None =>
+ Response.status(Status.Ok)
+ }
+ }
+ },
+ )
+
+ val requestWithUpgrade = Request
+ .get("/switch")
+ .addHeader(Header.Upgrade.Protocol("https", "1.1"))
+
+ val requestWithUnsupportedUpgrade = Request
+ .get("/switch")
+ .addHeader(Header.Upgrade.Protocol("unsupported", "1.0"))
+
+ val requestWithoutUpgrade = Request.get("/switch")
+
+ for {
+ responseWithUpgrade <- app.runZIO(requestWithUpgrade)
+ responseWithUnsupportedUpgrade <- app.runZIO(requestWithUnsupportedUpgrade)
+ responseWithoutUpgrade <- app.runZIO(requestWithoutUpgrade)
+
+ } yield assertTrue(
+ responseWithUpgrade.status == Status.SwitchingProtocols,
+ responseWithUpgrade.headers.contains(Header.Upgrade.name),
+ responseWithUnsupportedUpgrade.status == Status.BadRequest,
+ responseWithoutUpgrade.status == Status.Ok,
+ )
+ },
+ test(
+ "should send 100 Continue before 101 Switching Protocols when both Upgrade and Expect headers are present(continue_before_upgrade)",
+ ) {
+ val continueHandler = Handler.fromZIO {
+ ZIO.succeed(Response.status(Status.Continue))
+ }
+
+ val switchingProtocolsHandler = Handler.fromZIO {
+ ZIO.succeed(
+ Response
+ .status(Status.SwitchingProtocols)
+ .addHeader(Header.Connection.KeepAlive)
+ .addHeader(Header.Upgrade.Protocol("https", "1.1")),
+ )
+ }
+ val app = Routes(
+ Method.POST / "upgrade" -> continueHandler,
+ Method.GET / "switch" -> switchingProtocolsHandler,
+ )
+ val initialRequest = Request
+ .post("/upgrade", Body.empty)
+ .addHeader(Header.Expect.`100-continue`)
+ .addHeader(Header.Connection.KeepAlive)
+ .addHeader(Header.Upgrade.Protocol("https", "1.1"))
+
+ val followUpRequest = Request.get("/switch")
+
+ for {
+ firstResponse <- app.runZIO(initialRequest)
+ secondResponse <- app.runZIO(followUpRequest)
+
+ } yield assertTrue(
+ firstResponse.status == Status.Continue,
+ secondResponse.status == Status.SwitchingProtocols,
+ secondResponse.headers.contains(Header.Upgrade.name),
+ secondResponse.headers.contains(Header.Connection.name),
+ )
+ },
+ suite("Content-Length")(
+ test("Content-Length in HEAD must match the one in GET (content_length_same_head_get)") {
+ val getResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentLength(14))
+ .copy(body = Body.fromString("ABC
"))
+
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromResponse(getResponse),
+ Method.HEAD / "test" -> Handler.fromResponse(getResponse.copy(body = Body.empty)),
+ )
+
+ for {
+ getResponse <- app.runZIO(Request.get("/test"))
+ headResponse <- app.runZIO(Request.head("/test"))
+ getContentLength = getResponse.headers.get(Header.ContentLength.name).map(_.toInt)
+ headContentLength = headResponse.headers.get(Header.ContentLength.name).map(_.toInt)
+ } yield assertTrue(
+ headContentLength == getContentLength,
+ )
+ },
+ test("Content-Length in 304 Not Modified must match the one in 200 OK (content_length_same_304_200)") {
+ val app = Routes(
+ Method.GET / "test" -> Handler.fromFunction { (request: Request) =>
+ request.headers.get(Header.IfModifiedSince.name) match {
+ case Some(_) =>
+ Response.status(Status.NotModified).addHeader(Header.ContentLength(14)).copy(body = Body.empty)
+ case None =>
+ Response
+ .status(Status.Ok)
+ .addHeader(Header.ContentLength(14))
+ .copy(body = Body.fromString("ABC
"))
+ }
+ },
+ )
+
+ val conditionalRequest = Request
+ .get("/test")
+ .addHeader(
+ Header.IfModifiedSince(
+ ZonedDateTime.parse("Thu, 20 Mar 2025 07:28:00 GMT", DateTimeFormatter.RFC_1123_DATE_TIME),
+ ),
+ )
+
+ for {
+ normalResponse <- app.runZIO(Request.get("/test"))
+ conditionalResponse <- app.runZIO(conditionalRequest)
+ normalContentLength = normalResponse.headers.get(Header.ContentLength.name).map(_.toInt)
+ conditionalContentLength = conditionalResponse.headers.get(Header.ContentLength.name).map(_.toInt)
+ } yield assertTrue(
+ normalContentLength == conditionalContentLength,
+ )
+ },
+ ),
+ ),
+ suite("cache-control")(
+ test("Cache-Control should not have quoted string for max-age directive(response_directive_max_age)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.CacheControl.MaxAge(5))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", """max-age="5""""))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.headers.get(Header.CacheControl.name).contains("max-age=5"),
+ responseInvalid.headers.get(Header.CacheControl.name).contains("""max-age="5""""),
+ )
+ },
+ test("Cache-Control should not have quoted string for s-maxage directive(response_directive_s_maxage)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.CacheControl.SMaxAge(10))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", """s-maxage="10""""))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.headers.get(Header.CacheControl.name).contains("s-maxage=10"),
+ responseInvalid.headers.get(Header.CacheControl.name).contains("""s-maxage="10""""),
+ )
+ },
+ test("Cache-Control should use quoted-string form for no-cache directive(response_directive_no_cache)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", """no-cache="age""""))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", "no-cache=age"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.headers.get(Header.CacheControl.name).contains("""no-cache="age""""),
+ responseInvalid.headers.get(Header.CacheControl.name).contains("no-cache=age"),
+ )
+ },
+ test("Cache-Control should use quoted-string form for private directive(response_directive_private)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", """private="x-frame-options""""))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Cache-Control", "private=x-frame-options"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield assertTrue(
+ responseValid.headers.get(Header.CacheControl.name).contains("""private="x-frame-options""""),
+ responseInvalid.headers.get(Header.CacheControl.name).contains("private=x-frame-options"),
+ )
+ },
+ ),
+ suite("cookies")(
+ test("should not have duplicate cookie attributes in Set-Cookie header(duplicate_cookie_attribute)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.SetCookie(Cookie.Response("test", "test", path = Some(Path.root))))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Set-Cookie", "test=test; path=/; path=/abc"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ val validCookieAttributes = responseValid.headers.toList.collect {
+ case h if h.headerName == Header.SetCookie.name => h.renderedValue
+ }
+ val invalidCookieAttributes = responseInvalid.headers.toList.collect {
+ case h if h.headerName == Header.SetCookie.name => h.renderedValue
+ }
+ assertTrue(
+ validCookieAttributes.nonEmpty,
+ validCookieAttributes.exists(_.toLowerCase.contains("path=/")),
+ !validCookieAttributes.exists(_.toLowerCase.contains("path=/abc")),
+ ) &&
+ assertTrue(
+ invalidCookieAttributes.nonEmpty,
+ invalidCookieAttributes.forall(_.contains("path=/")),
+ )
+ }
+ },
+ test("should not have duplicate cookies with the same name(duplicate_cookies)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.SetCookie(Cookie.Response("test", "test")))
+ .addHeader(Header.SetCookie(Cookie.Response("test2", "test2")))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.SetCookie(Cookie.Response("test", "test")))
+ .addHeader(Header.SetCookie(Cookie.Response("test", "test2")))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ val validCookies = responseValid.headers.toList.collect {
+ case h if h.headerName == Header.SetCookie.name => h.renderedValue
+ }
+ val invalidCookies = responseInvalid.headers.toList.collect {
+ case h if h.headerName == Header.SetCookie.name => h.renderedValue
+ }
+ assertTrue(
+ validCookies.count(_.contains("test=")) == 1,
+ ) &&
+ assertTrue(
+ invalidCookies.count(_.contains("test=")) == 2,
+ )
+ }
+ },
+ test("should use IMF-fixdate for cookie expiration date(cookie_IMF_fixdate)") {
+ val validResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.SetCookie(Cookie.Response("test", "test", maxAge = Some(Duration.fromSeconds(86400)))))
+
+ val invalidResponse = Response
+ .status(Status.Ok)
+ .addHeader(Header.Custom("Set-Cookie", "test=test; expires=Thu, 20 Mar 25 15:14:45 GMT"))
+
+ val app = Routes(
+ Method.GET / "valid" -> Handler.fromResponse(validResponse),
+ Method.GET / "invalid" -> Handler.fromResponse(invalidResponse),
+ )
+
+ for {
+ responseValid <- app.runZIO(Request.get("/valid"))
+ responseInvalid <- app.runZIO(Request.get("/invalid"))
+ } yield {
+ val expiresValid = responseValid.headers.toList.exists(_.renderedValue.contains("Expires="))
+ val expiresInvalid =
+ responseInvalid.headers.toList.exists(_.renderedValue.contains("expires=Thu, 20 Mar 25"))
+
+ assertTrue(
+ expiresValid,
+ expiresInvalid,
+ )
+ }
+ },
+ ),
+ suite("conformance")(
+ test("should not include Content-Length header for 204 No Content responses") {
+ val route = Method.GET / "no-content" -> Handler.fromResponse(Response(status = Status.NoContent))
+ val app = Routes(route)
+
+ val request = Request.get("/no-content")
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(!response.headers.contains(Header.ContentLength.name))
+ },
+ test("should not send content for 304 Not Modified responses") {
+ val app = Routes(
+ Method.GET / "not-modified" -> Handler.fromResponse(
+ Response.status(Status.NotModified),
+ ),
+ )
+
+ val request = Request.get("/not-modified")
+
+ for {
+ response <- app.runZIO(request)
+ } yield assertTrue(
+ response.status == Status.NotModified,
+ response.body.isEmpty,
+ !response.headers.contains(Header.ContentLength.name),
+ !response.headers.contains(Header.TransferEncoding.name),
+ )
+ },
+ ),
+ )
+}
diff --git a/zio-http/jvm/src/test/scala/zio/http/DualSSLSpec.scala b/zio-http/jvm/src/test/scala/zio/http/DualSSLSpec.scala
index 3969080c9d..21b3a57f4a 100644
--- a/zio-http/jvm/src/test/scala/zio/http/DualSSLSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/DualSSLSpec.scala
@@ -126,6 +126,7 @@ object DualSSLSpec extends ZIOHttpSpec {
),
),
).provideShared(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(config),
diff --git a/zio-http/jvm/src/test/scala/zio/http/DynamicAppTest.scala b/zio-http/jvm/src/test/scala/zio/http/DynamicAppTest.scala
index 8f9975a1bd..3eee4ade58 100644
--- a/zio-http/jvm/src/test/scala/zio/http/DynamicAppTest.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/DynamicAppTest.scala
@@ -42,6 +42,7 @@ object DynamicAppTest extends ZIOHttpSpec {
NettyClientDriver.live,
Client.customized,
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
DnsResolver.default,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http/jvm/src/test/scala/zio/http/HybridStreamingSpec.scala b/zio-http/jvm/src/test/scala/zio/http/HybridStreamingSpec.scala
index b23d016632..a2c1cd447e 100644
--- a/zio-http/jvm/src/test/scala/zio/http/HybridStreamingSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/HybridStreamingSpec.scala
@@ -76,6 +76,7 @@ object HybridRequestStreamingServerSpec extends RoutesRunnableSpec {
Scope.default,
DynamicServer.live,
ZLayer.succeed(configAppWithHybridRequestStreaming),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
Client.live,
diff --git a/zio-http/jvm/src/test/scala/zio/http/NettyMaxHeaderLengthSpec.scala b/zio-http/jvm/src/test/scala/zio/http/NettyMaxHeaderLengthSpec.scala
index e4e3ecaef5..459ae051ea 100644
--- a/zio-http/jvm/src/test/scala/zio/http/NettyMaxHeaderLengthSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/NettyMaxHeaderLengthSpec.scala
@@ -53,6 +53,7 @@ object NettyMaxHeaderLengthSpec extends ZIOHttpSpec {
} yield assertTrue(extractStatus(res) == Status.InternalServerError, data == "")
}.provide(
Client.default,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(serverConfig),
diff --git a/zio-http/jvm/src/test/scala/zio/http/NettyMaxInitialLineLengthSpec.scala b/zio-http/jvm/src/test/scala/zio/http/NettyMaxInitialLineLengthSpec.scala
index 0a940a7002..084aa13149 100644
--- a/zio-http/jvm/src/test/scala/zio/http/NettyMaxInitialLineLengthSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/NettyMaxInitialLineLengthSpec.scala
@@ -55,6 +55,7 @@ object NettyMaxInitialLineLength extends ZIOHttpSpec {
} yield assertTrue(extractStatus(res) == Status.InternalServerError, data == "")
}.provide(
Client.default,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(serverConfig),
diff --git a/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala b/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala
index 04ce505d54..fb608d6ffa 100644
--- a/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/RequestStreamingServerSpec.scala
@@ -111,6 +111,7 @@ object RequestStreamingServerSpec extends RoutesRunnableSpec {
.provideShared(
DynamicServer.live,
ZLayer.succeed(configAppWithRequestStreaming),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
Client.default,
diff --git a/zio-http/jvm/src/test/scala/zio/http/ResponseCompressionSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ResponseCompressionSpec.scala
index 302f8fd1af..60c679217d 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ResponseCompressionSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ResponseCompressionSpec.scala
@@ -125,6 +125,7 @@ object ResponseCompressionSpec extends ZIOHttpSpec {
},
).provide(
ZLayer.succeed(serverConfig),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
Client.default,
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 06be0b0b14..bfa9879f43 100644
--- a/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/SSLSpec.scala
@@ -118,6 +118,7 @@ object SSLSpec extends ZIOHttpSpec {
),
),
).provideShared(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(config),
diff --git a/zio-http/jvm/src/test/scala/zio/http/ServerClientJKSMutualSSLSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ServerClientJKSMutualSSLSpec.scala
index 1cac737c9f..46a11a0116 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ServerClientJKSMutualSSLSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ServerClientJKSMutualSSLSpec.scala
@@ -145,6 +145,7 @@ object ServerClientJKSMutualSSLSpec extends ZIOHttpSpec {
),
),
).provideShared(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(config),
diff --git a/zio-http/jvm/src/test/scala/zio/http/ServerJKSKeyStoreSSLSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ServerJKSKeyStoreSSLSpec.scala
index 690ee023ec..1f2ecb6cce 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ServerJKSKeyStoreSSLSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ServerJKSKeyStoreSSLSpec.scala
@@ -127,6 +127,7 @@ object ServerJKSKeyStoreSSLSpec extends ZIOHttpSpec {
),
),
).provideShared(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
ZLayer.succeed(config),
diff --git a/zio-http/jvm/src/test/scala/zio/http/ServerRuntimeSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ServerRuntimeSpec.scala
index a964a1ae1a..58be1b8993 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ServerRuntimeSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ServerRuntimeSpec.scala
@@ -120,6 +120,7 @@ object ServerRuntimeSpec extends RoutesRunnableSpec {
.provide(
Scope.default,
DynamicServer.live,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(Server.Config.default),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http/jvm/src/test/scala/zio/http/ServerSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ServerSpec.scala
index 23153ee809..02414d068a 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ServerSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ServerSpec.scala
@@ -527,6 +527,7 @@ object ServerSpec extends RoutesRunnableSpec {
Scope.default,
DynamicServer.live,
ZLayer.succeed(configApp),
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
Client.default,
diff --git a/zio-http/jvm/src/test/scala/zio/http/ServerStartSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ServerStartSpec.scala
index 762309a240..3a9db0e822 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ServerStartSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ServerStartSpec.scala
@@ -33,6 +33,7 @@ object ServerStartSpec extends RoutesRunnableSpec {
serve(Routes.empty).flatMap { port =>
assertZIO(ZIO.attempt(port))(equalTo(port))
}.provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(config),
DynamicServer.live,
Server.customized,
@@ -45,6 +46,7 @@ object ServerStartSpec extends RoutesRunnableSpec {
serve(Routes.empty).flatMap { bindPort =>
assertZIO(ZIO.attempt(bindPort))(not(equalTo(port)))
}.provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(config),
DynamicServer.live,
Server.customized,
@@ -56,6 +58,7 @@ object ServerStartSpec extends RoutesRunnableSpec {
.succeed(assertCompletes)
.provide(
Server.customized.unit,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(Server.Config.default.port(8089)),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
)
diff --git a/zio-http/jvm/src/test/scala/zio/http/WebSocketSpec.scala b/zio-http/jvm/src/test/scala/zio/http/WebSocketSpec.scala
index cc3e222b6c..87f7cfc608 100644
--- a/zio-http/jvm/src/test/scala/zio/http/WebSocketSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/WebSocketSpec.scala
@@ -230,6 +230,7 @@ object WebSocketSpec extends RoutesRunnableSpec {
.provideSome[DynamicServer & Server & Client](Scope.default)
.provideShared(
DynamicServer.live,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(Server.Config.default.onAnyOpenPort.enableRequestStreaming),
testNettyServerConfig,
Server.customized,
diff --git a/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala b/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala
index fa31a5e5a0..2d0b9a9dc9 100644
--- a/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/ZClientAspectSpec.scala
@@ -184,6 +184,7 @@ object ZClientAspectSpec extends ZIOHttpSpec {
},
),
).provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http/jvm/src/test/scala/zio/http/endpoint/RoundtripSpec.scala b/zio-http/jvm/src/test/scala/zio/http/endpoint/RoundtripSpec.scala
index 0e1e7ce207..9e0b990711 100644
--- a/zio-http/jvm/src/test/scala/zio/http/endpoint/RoundtripSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/endpoint/RoundtripSpec.scala
@@ -39,6 +39,7 @@ import zio.http.netty.NettyConfig
object RoundtripSpec extends ZIOHttpSpec {
val testLayer: ZLayer[Any, Throwable, Server & Client & Scope] =
ZLayer.make[Server & Client & Scope](
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(Server.Config.default.onAnyOpenPort.enableRequestStreaming),
Client.customized.map(env => ZEnvironment(env.get)),
@@ -708,6 +709,7 @@ object RoundtripSpec extends ZIOHttpSpec {
}
}.provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(Server.Config.default.onAnyOpenPort.enableRequestStreaming),
Client.customized.map(env => ZEnvironment(env.get @@ clientDebugAspect)) >>>
diff --git a/zio-http/jvm/src/test/scala/zio/http/endpoint/ServerSentEventEndpointSpec.scala b/zio-http/jvm/src/test/scala/zio/http/endpoint/ServerSentEventEndpointSpec.scala
index fbbf36974b..265c267d3e 100644
--- a/zio-http/jvm/src/test/scala/zio/http/endpoint/ServerSentEventEndpointSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/endpoint/ServerSentEventEndpointSpec.scala
@@ -108,7 +108,10 @@ object ServerSentEventEndpointSpec extends ZIOHttpSpec {
} yield assertTrue(events.size == 5 && events.forall(event => Try(event.data.timeStamp).isSuccess))
},
)
- .provideSomeLayer[Client & Server.Config & NettyConfig](Server.customized)
+ .provideSomeLayer[Client & Server.Config & NettyConfig](
+ (ZLayer.fromFunction[Server.Config => ServerRuntimeConfig](ServerRuntimeConfig(_)) ++ ZLayer
+ .service[NettyConfig]) >>> Server.customized,
+ )
.provideShared(
Client.live,
ZLayer.succeed(Server.Config.default.port(0)),
diff --git a/zio-http/jvm/src/test/scala/zio/http/internal/package.scala b/zio-http/jvm/src/test/scala/zio/http/internal/package.scala
index c0d6d0ee20..d4300c019a 100644
--- a/zio-http/jvm/src/test/scala/zio/http/internal/package.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/internal/package.scala
@@ -36,6 +36,7 @@ package object internal {
val serverTestLayer: ZLayer[Any, Throwable, Server.Config with Server] =
ZLayer.make[Server.Config with Server](
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
testServerConfig,
testNettyServerConfig,
Server.customized,
diff --git a/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala b/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala
index e49f17d5a1..d5da80ea22 100644
--- a/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/netty/NettyStreamBodySpec.scala
@@ -37,6 +37,7 @@ object NettyStreamBodySpec extends RoutesRunnableSpec {
.intoPromise(portPromise)
.zipRight(ZIO.never)
.provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown.leakDetection(LeakDetectionLevel.PARANOID)),
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Server.customized,
@@ -136,6 +137,7 @@ object NettyStreamBodySpec extends RoutesRunnableSpec {
.intoPromise(portPromise)
.zipRight(ZIO.never)
.provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown.leakDetection(LeakDetectionLevel.PARANOID)),
ZLayer.succeed(Server.Config.default.onAnyOpenPort),
Server.customized,
diff --git a/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala b/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala
index ed5b246356..f34e23a041 100644
--- a/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/netty/client/NettyConnectionPoolSpec.scala
@@ -198,6 +198,7 @@ object NettyConnectionPoolSpec extends RoutesRunnableSpec {
DynamicServer.live,
ZLayer.succeed(Server.Config.default.idleTimeout(500.millis).onAnyOpenPort.logWarningOnFatalError(false)),
testNettyServerConfig,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
) @@ withLiveClock
} + test("idle timeout is refreshed on each request") {
@@ -215,6 +216,7 @@ object NettyConnectionPoolSpec extends RoutesRunnableSpec {
DynamicServer.live,
ZLayer.succeed(Server.Config.default.idleTimeout(500.millis).onAnyOpenPort.logWarningOnFatalError(false)),
testNettyServerConfig,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
Client.live,
ZLayer.succeed(Client.Config.default.idleTimeout(500.millis)),
diff --git a/zio-http/jvm/src/test/scala/zio/http/security/ExceptionSpec.scala b/zio-http/jvm/src/test/scala/zio/http/security/ExceptionSpec.scala
index 1acd4ecd20..63d04f2f37 100644
--- a/zio-http/jvm/src/test/scala/zio/http/security/ExceptionSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/security/ExceptionSpec.scala
@@ -85,6 +85,7 @@ object ExceptionSpec extends ZIOSpecDefault {
},
).provide(
Scope.default,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(
Server.Config.default,
diff --git a/zio-http/jvm/src/test/scala/zio/http/security/MetricsSpec.scala b/zio-http/jvm/src/test/scala/zio/http/security/MetricsSpec.scala
index 1f3518fdf6..e003200686 100644
--- a/zio-http/jvm/src/test/scala/zio/http/security/MetricsSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/security/MetricsSpec.scala
@@ -112,6 +112,7 @@ object MetricsSpec extends ZIOHttpSpec {
port => form => Request.post(s"http://localhost:$port", Body.fromMultipartForm(form, Boundary("-"))),
),
).provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(Server.Config.default),
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http/jvm/src/test/scala/zio/http/security/SizeLimitsSpec.scala b/zio-http/jvm/src/test/scala/zio/http/security/SizeLimitsSpec.scala
index 65b4b2d2f9..9f0e029f3a 100644
--- a/zio-http/jvm/src/test/scala/zio/http/security/SizeLimitsSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/security/SizeLimitsSpec.scala
@@ -156,6 +156,7 @@ object SizeLimitsSpec extends ZIOHttpSpec {
)
},
).provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(
Server.Config.default
@@ -232,6 +233,7 @@ object SizeLimitsSpec extends ZIOHttpSpec {
)
},
).provide(
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
ZLayer.succeed(Server.Config.default),
Server.customized,
ZLayer.succeed(NettyConfig.defaultWithFastShutdown),
diff --git a/zio-http/jvm/src/test/scala/zio/http/security/UserDataSpec.scala b/zio-http/jvm/src/test/scala/zio/http/security/UserDataSpec.scala
index 7b860e5d15..9729f4aa29 100644
--- a/zio-http/jvm/src/test/scala/zio/http/security/UserDataSpec.scala
+++ b/zio-http/jvm/src/test/scala/zio/http/security/UserDataSpec.scala
@@ -158,6 +158,7 @@ object UserDataSpec extends ZIOSpecDefault {
},
).provide(
Scope.default,
+ ZLayer.fromFunction((c: Server.Config) => ServerRuntimeConfig(c)),
Server.customized,
ZLayer.succeed(
Server.Config.default,
diff --git a/zio-http/shared/src/main/scala/zio/http/Handler.scala b/zio-http/shared/src/main/scala/zio/http/Handler.scala
index d25e7ca660..f150461bbe 100644
--- a/zio-http/shared/src/main/scala/zio/http/Handler.scala
+++ b/zio-http/shared/src/main/scala/zio/http/Handler.scala
@@ -1042,6 +1042,18 @@ object Handler extends HandlerPlatformSpecific with HandlerVersionSpecific {
def notFound(message: => String): Handler[Any, Nothing, Any, Response] =
error(Status.NotFound, message)
+ /**
+ * Creates a handler which always responds with a 501 status code.
+ */
+ val notImplemented: Handler[Any, Nothing, Any, Response] =
+ error(Status.NotImplemented)
+
+ /**
+ * Creates a handler which always responds with a 501 status code.
+ */
+ def notImplemented(message: => String): Handler[Any, Nothing, Any, Response] =
+ error(Status.NotImplemented, message)
+
/**
* Creates a handler which always responds with a 200 status code.
*/
diff --git a/zio-http/shared/src/main/scala/zio/http/Server.scala b/zio-http/shared/src/main/scala/zio/http/Server.scala
index a774d9f889..5975689033 100644
--- a/zio-http/shared/src/main/scala/zio/http/Server.scala
+++ b/zio-http/shared/src/main/scala/zio/http/Server.scala
@@ -205,6 +205,14 @@ object Server extends ServerPlatformSpecific {
def tcpNoDelay(value: Boolean): Config =
self.copy(tcpNoDelay = value)
+ /**
+ * Configure the server to enable/disable HTTP header validation. When
+ * enabled, the server will validate incoming headers such as the Host
+ * header.
+ */
+ def validateHeaders(value: Boolean): ServerRuntimeConfig =
+ ServerRuntimeConfig(self, value)
+
def webSocketConfig(webSocketConfig: WebSocketConfig): Config =
self.copy(webSocketConfig = webSocketConfig)
}
@@ -227,7 +235,6 @@ object Server extends ServerPlatformSpecific {
zio.Config.boolean("avoid-context-switching").withDefault(Config.default.avoidContextSwitching) ++
zio.Config.int("so-backlog").withDefault(Config.default.soBacklog) ++
zio.Config.boolean("tcp-nodelay").withDefault(Config.default.tcpNoDelay)
-
}.map {
case (
sslConfig,
@@ -578,3 +585,18 @@ object Server extends ServerPlatformSpecific {
}
}
+
+final case class ServerRuntimeConfig(
+ config: Server.Config,
+ validateHeaders: Boolean = false,
+)
+
+object ServerRuntimeConfig {
+ def config: zio.Config[ServerRuntimeConfig] =
+ (Server.Config.config ++ zio.Config.boolean("validate-headers").withDefault(false)).map { case (cfg, validate) =>
+ ServerRuntimeConfig(cfg, validate)
+ }
+
+ val layer: ZLayer[Server.Config, Nothing, ServerRuntimeConfig] =
+ ZLayer.fromFunction(cfg => ServerRuntimeConfig(cfg, false))
+}