diff --git a/chronos/apps/http/httpcommon.nim b/chronos/apps/http/httpcommon.nim index 3718b101..c186573a 100644 --- a/chronos/apps/http/httpcommon.nim +++ b/chronos/apps/http/httpcommon.nim @@ -25,6 +25,7 @@ type code*: HttpCode HttpRecoverableError* = object of HttpError code*: HttpCode + HttpDisconnectError* = object of HttpError TransferEncodingFlags* {.pure.} = enum Identity, Chunked, Compress, Deflate, Gzip @@ -80,6 +81,11 @@ proc newHttpRecoverableError*(msg: string, tre.code = code tre +proc newHttpDisconnectError*(): ref HttpDisconnectError {. + raises: [HttpDisconnectError].} = + var tre = newException(HttpDisconnectError, "Remote peer disconnected") + tre + iterator queryParams*(query: string): tuple[key: string, value: string] {. raises: [Defect].} = ## Iterate over url-encoded query string. diff --git a/chronos/apps/http/httpserver.nim b/chronos/apps/http/httpserver.nim index 3adda895..08fbd15c 100644 --- a/chronos/apps/http/httpserver.nim +++ b/chronos/apps/http/httpserver.nim @@ -429,10 +429,8 @@ proc getRequest(conn: HttpConnectionRef): Future[HttpRequestRef] {.async.} = raise newHttpCriticalError("Invalid request received", res.error) else: return res.get() - except AsyncStreamIncompleteError: - raise newHttpCriticalError("Remote peer disconnected") - except AsyncStreamReadError: - raise newHttpCriticalError("Connection with remote peer has been lost") + except AsyncStreamIncompleteError, AsyncStreamReadError: + raise newHttpDisconnectError() except AsyncStreamLimitError: raise newHttpCriticalError("Maximum size of request headers reached", Http413) @@ -561,6 +559,9 @@ proc processLoop(server: HttpServerRef, transp: StreamTransport) {.async.} = let error = HttpProcessError.init(HTTPServerError.CriticalError, exc, transp.remoteAddress(), exc.code) arg = RequestFence[HttpRequestRef].err(error) + except HttpDisconnectError: + # If remote peer disconnected we just exiting loop + breakLoop = true except CatchableError as exc: let error = HttpProcessError.init(HTTPServerError.CatchableError, exc, transp.remoteAddress(), Http500)