细化功能

This commit is contained in:
tursom 2019-11-30 00:48:34 +08:00
parent 702d8a44f3
commit d9b7b3a5d4
4 changed files with 26 additions and 4 deletions

View File

@ -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()

View File

@ -3,7 +3,7 @@ package cn.tursom.web
interface HttpHandler<in T : HttpContent, in E : ExceptionContent> {
fun handle(content: T)
fun exception(e: E) {
fun exceptionCause(e: E) {
e.cause.printStackTrace()
e.responseCode = 500
e.finish()

View File

@ -20,7 +20,6 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
target: Any? = null,
val routerMaker: () -> Router<(T) -> Unit> = { SimpleRouter() }
) : HttpHandler<T, E> {
private val router: Router<(T) -> Unit> = routerMaker()
private val routerMap: HashMap<String, Router<(T) -> Unit>> = HashMap()
@ -35,10 +34,14 @@ open class RoutedHttpHandler<T : HttpContent, in E : ExceptionContent>(
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

View File

@ -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()
}