HttpsChannelInitializer.kt

  1. package com.hexagontk.http.server.netty

  2. import com.hexagontk.http.SslSettings
  3. import com.hexagontk.http.handlers.HttpHandler
  4. import com.hexagontk.http.server.HttpServerSettings
  5. import io.netty.channel.ChannelInitializer
  6. import io.netty.channel.socket.SocketChannel
  7. import io.netty.handler.codec.http.*
  8. import io.netty.handler.ssl.SslContext
  9. import io.netty.handler.stream.ChunkedWriteHandler
  10. import java.util.concurrent.Executor

  11. internal class HttpsChannelInitializer(
  12.     private val handlers: Map<HttpMethod, HttpHandler>,
  13.     private val sslContext: SslContext,
  14.     private val sslSettings: SslSettings,
  15.     private val executor: Executor?,
  16.     private val settings: HttpServerSettings,
  17.     private val keepAliveHandler: Boolean = true,
  18.     private val httpAggregatorHandler: Boolean = true,
  19.     private val chunkedHandler: Boolean = true,
  20.     private val enableWebsockets: Boolean = true,
  21. ) : ChannelInitializer<SocketChannel>() {

  22.     override fun initChannel(channel: SocketChannel) {
  23.         val pipeline = channel.pipeline()
  24.         val sslHandler = sslContext.newHandler(channel.alloc())
  25.         val handlerSsl = if (sslSettings.clientAuth) sslHandler else null

  26.         pipeline.addLast(sslHandler)
  27.         pipeline.addLast(HttpServerCodec())

  28.         if (keepAliveHandler)
  29.             pipeline.addLast(HttpServerKeepAliveHandler())
  30.         if (httpAggregatorHandler)
  31.             pipeline.addLast(HttpObjectAggregator(Int.MAX_VALUE))
  32.         if (chunkedHandler)
  33.             pipeline.addLast(ChunkedWriteHandler())
  34.         if (settings.zip)
  35.             pipeline.addLast(HttpContentCompressor())

  36.         val serverHandler = NettyServerHandler(handlers, executor, handlerSsl, enableWebsockets)

  37.         pipeline.addLast(serverHandler)
  38.     }
  39. }