HttpsChannelInitializer.kt
- package com.hexagontk.http.server.netty
- import com.hexagontk.http.SslSettings
- import com.hexagontk.http.handlers.HttpHandler
- import com.hexagontk.http.server.HttpServerSettings
- import io.netty.channel.ChannelInitializer
- import io.netty.channel.socket.SocketChannel
- import io.netty.handler.codec.http.*
- import io.netty.handler.ssl.SslContext
- import io.netty.handler.stream.ChunkedWriteHandler
- import java.util.concurrent.Executor
- internal class HttpsChannelInitializer(
- private val handlers: Map<HttpMethod, HttpHandler>,
- private val sslContext: SslContext,
- private val sslSettings: SslSettings,
- private val executor: Executor?,
- private val settings: HttpServerSettings,
- private val keepAliveHandler: Boolean = true,
- private val httpAggregatorHandler: Boolean = true,
- private val chunkedHandler: Boolean = true,
- private val enableWebsockets: Boolean = true,
- ) : ChannelInitializer<SocketChannel>() {
- override fun initChannel(channel: SocketChannel) {
- val pipeline = channel.pipeline()
- val sslHandler = sslContext.newHandler(channel.alloc())
- val handlerSsl = if (sslSettings.clientAuth) sslHandler else null
- pipeline.addLast(sslHandler)
- pipeline.addLast(HttpServerCodec())
- if (keepAliveHandler)
- pipeline.addLast(HttpServerKeepAliveHandler())
- if (httpAggregatorHandler)
- pipeline.addLast(HttpObjectAggregator(Int.MAX_VALUE))
- if (chunkedHandler)
- pipeline.addLast(ChunkedWriteHandler())
- if (settings.zip)
- pipeline.addLast(HttpContentCompressor())
- val serverHandler = NettyServerHandler(handlers, executor, handlerSsl, enableWebsockets)
- pipeline.addLast(serverHandler)
- }
- }