Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jaguililla committed May 10, 2024
1 parent d09a573 commit 3b0bbf9
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 160 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.gradle.api.tasks.wrapper.Wrapper.DistributionType.ALL

plugins {
// kotlin("jvm") version("1.9.24") apply(false)
kotlin("jvm") version("2.0.0-RC2") apply(false)
kotlin("jvm") version("2.0.0-RC3") apply(false)

id("idea")
id("eclipse")
Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ iconsDirectory=content

# VERSIONS
#kotlinVersion=1.9.24
kotlinVersion=2.0.0-RC2
kotlinVersion=2.0.0-RC3
dokkaVersion=1.9.20
mockkVersion=1.13.10
junitVersion=5.10.2
# TODO Latest version breaks stress test
#gatlingVersion=3.11.2
gatlingVersion=3.10.5
slf4jVersion=2.0.13
jmhVersion=1.37
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.hexagonkt.http.client.netty

import io.netty.channel.ChannelFuture
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import io.netty.channel.SimpleChannelInboundHandler
import io.netty.handler.codec.http.FullHttpResponse
import io.netty.handler.codec.http2.HttpConversionUtil
import io.netty.util.CharsetUtil
import java.util.concurrent.TimeUnit

class Http2ClientResponseHandler : SimpleChannelInboundHandler<FullHttpResponse>() {
private val streamIdMap: MutableMap<Int, MapValues> = HashMap()

fun put(streamId: Int, writeFuture: ChannelFuture, promise: ChannelPromise): MapValues? {
return streamIdMap.put(streamId, MapValues(writeFuture, promise))
}

fun awaitResponses(timeout: Long, unit: TimeUnit): String? {
val itr: MutableIterator<Map.Entry<Int, MapValues>> = streamIdMap.entries.iterator()

var response: String? = null

while (itr.hasNext()) {
val entry = itr.next()
val writeFuture = entry.value.writeFuture

check(
writeFuture.awaitUninterruptibly(
timeout,
unit
)
) { "Timed out waiting to write for stream id " + entry.key }
if (!writeFuture.isSuccess) {
throw RuntimeException(writeFuture.cause())
}
val promise = entry.value.promise

check(promise.awaitUninterruptibly(timeout, unit)) {
"Timed out waiting for response on stream id " + entry.key
}
if (!promise.isSuccess) {
throw RuntimeException(promise.cause())
}
response = entry.value.response

itr.remove()
}

return response
}

override fun channelRead0(ctx: ChannelHandlerContext, msg: FullHttpResponse) {
val streamId = msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text()) ?: return

val value = streamIdMap[streamId]

if (value == null) {
ctx.close()
} else {
val content = msg.content()
if (content.isReadable) {
val contentLength = content.readableBytes()
val arr = ByteArray(contentLength)
content.readBytes(arr)
val response = String(arr, 0, contentLength, CharsetUtil.UTF_8)
value.response = response
}

value.promise
.setSuccess()
}
}

class MapValues(var writeFuture: ChannelFuture, var promise: ChannelPromise) {
var response: String? = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hexagonkt.http.client.netty

import io.netty.channel.ChannelInitializer
import io.netty.channel.socket.SocketChannel
import io.netty.handler.codec.http.*
import io.netty.handler.stream.ChunkedWriteHandler
import io.netty.util.concurrent.EventExecutorGroup

internal class HttpChannelInitializer(
private val executorGroup: EventExecutorGroup?,
private val keepAliveHandler: Boolean = true,
private val httpAggregatorHandler: Boolean = true,
private val chunkedHandler: Boolean = true,
) : ChannelInitializer<SocketChannel>() {

override fun initChannel(channel: SocketChannel) {
val pipeline = channel.pipeline()

pipeline.addLast(HttpServerCodec())

if (keepAliveHandler)
pipeline.addLast(HttpServerKeepAliveHandler())
if (httpAggregatorHandler)
pipeline.addLast(HttpObjectAggregator(Int.MAX_VALUE))
if (chunkedHandler)
pipeline.addLast(ChunkedWriteHandler())

val nettyServerHandler = Http2ClientResponseHandler()

if (executorGroup == null)
pipeline.addLast(nettyServerHandler)
else
pipeline.addLast(executorGroup, nettyServerHandler)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.hexagonkt.http.client.netty

import com.hexagonkt.http.SslSettings
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 io.netty.util.concurrent.EventExecutorGroup

internal class HttpsChannelInitializer(
private val sslContext: SslContext,
private val sslSettings: SslSettings,
private val executorGroup: EventExecutorGroup?,
private val keepAliveHandler: Boolean = true,
private val httpAggregatorHandler: Boolean = true,
private val chunkedHandler: 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())

val serverHandler = Http2ClientResponseHandler()

if (executorGroup == null)
pipeline.addLast(serverHandler)
else
pipeline.addLast(executorGroup, serverHandler)
}
}
Loading

0 comments on commit 3b0bbf9

Please sign in to comment.