-
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d09a573
commit 3b0bbf9
Showing
6 changed files
with
336 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...lient_netty/src/main/kotlin/com/hexagonkt/http/client/netty/Http2ClientResponseHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...tp_client_netty/src/main/kotlin/com/hexagonkt/http/client/netty/HttpChannelInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...p_client_netty/src/main/kotlin/com/hexagonkt/http/client/netty/HttpsChannelInitializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.