From d9b7b3a5d416c54ea30b8d8063df172f9600a243 Mon Sep 17 00:00:00 2001 From: tursom Date: Sat, 30 Nov 2019 00:48:34 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=86=E5=8C=96=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/tursom/web/netty/NettyHttpHandler.kt | 2 +- .../main/kotlin/cn/tursom/web/HttpHandler.kt | 2 +- .../cn/tursom/web/router/RoutedHttpHandler.kt | 7 +++++-- .../kotlin/cn/tursom/web/utils/Chunked.kt | 19 +++++++++++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/web/netty-web/src/main/kotlin/cn/tursom/web/netty/NettyHttpHandler.kt b/web/netty-web/src/main/kotlin/cn/tursom/web/netty/NettyHttpHandler.kt index c7dd165..5948479 100644 --- a/web/netty-web/src/main/kotlin/cn/tursom/web/netty/NettyHttpHandler.kt +++ b/web/netty-web/src/main/kotlin/cn/tursom/web/netty/NettyHttpHandler.kt @@ -24,7 +24,7 @@ class NettyHttpHandler( override fun exceptionCaught(ctx: ChannelHandlerContext, cause: Throwable) { val content = NettyExceptionContent(ctx, cause) - handler.exception(content) + handler.exceptionCause(content) if (!content.finished) { content.responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR content.finish() diff --git a/web/src/main/kotlin/cn/tursom/web/HttpHandler.kt b/web/src/main/kotlin/cn/tursom/web/HttpHandler.kt index bf3220b..f29d744 100644 --- a/web/src/main/kotlin/cn/tursom/web/HttpHandler.kt +++ b/web/src/main/kotlin/cn/tursom/web/HttpHandler.kt @@ -3,7 +3,7 @@ package cn.tursom.web interface HttpHandler { fun handle(content: T) - fun exception(e: E) { + fun exceptionCause(e: E) { e.cause.printStackTrace() e.responseCode = 500 e.finish() diff --git a/web/src/main/kotlin/cn/tursom/web/router/RoutedHttpHandler.kt b/web/src/main/kotlin/cn/tursom/web/router/RoutedHttpHandler.kt index 4ff05fd..82a0e51 100644 --- a/web/src/main/kotlin/cn/tursom/web/router/RoutedHttpHandler.kt +++ b/web/src/main/kotlin/cn/tursom/web/router/RoutedHttpHandler.kt @@ -20,7 +20,6 @@ open class RoutedHttpHandler( target: Any? = null, val routerMaker: () -> Router<(T) -> Unit> = { SimpleRouter() } ) : HttpHandler { - private val router: Router<(T) -> Unit> = routerMaker() private val routerMap: HashMap Unit>> = HashMap() @@ -35,10 +34,14 @@ open class RoutedHttpHandler( if (handler != null) { handler(content) } else { - content.finish(404) + notFound(content) } } + open fun notFound(content: T) { + content.finish(404) + } + fun addRouter(handler: Any) { @Suppress("LeakingThis") val clazz = handler.javaClass diff --git a/web/src/main/kotlin/cn/tursom/web/utils/Chunked.kt b/web/src/main/kotlin/cn/tursom/web/utils/Chunked.kt index 2705ef6..368a923 100644 --- a/web/src/main/kotlin/cn/tursom/web/utils/Chunked.kt +++ b/web/src/main/kotlin/cn/tursom/web/utils/Chunked.kt @@ -3,9 +3,28 @@ package cn.tursom.web.utils import cn.tursom.core.buffer.ByteBuffer interface Chunked { + /** + * Returns current transfer progress. + */ val progress: Long + + /** + * Returns the length of the input. + * @return the length of the input if the length of the input is known. + * a negative value if the length of the input is unknown. + */ val length: Long + + /** + * Return {@code true} if and only if there is no data left in the stream + * and the stream has reached at its end. + */ val endOfInput: Boolean + fun readChunk(): ByteBuffer + + /** + * Releases the resources associated with the input. + */ fun close() } \ No newline at end of file