mirror of
https://github.com/logos-storage/nim-chronos.git
synced 2026-01-07 16:03:09 +00:00
Send Content-Length: 0 on error.
Handle `DisconnectError`.
This commit is contained in:
parent
60a53eea99
commit
d595e0dbc6
@ -16,10 +16,11 @@ export httptable, httpcommon, httputils, multipart, tlsstream, asyncstream,
|
|||||||
|
|
||||||
type
|
type
|
||||||
HttpServerFlags* {.pure.} = enum
|
HttpServerFlags* {.pure.} = enum
|
||||||
Secure, NoExpectHandler
|
Secure, NoExpectHandler, NotifyDisconnect
|
||||||
|
|
||||||
HttpServerError* {.pure.} = enum
|
HttpServerError* {.pure.} = enum
|
||||||
TimeoutError, CatchableError, RecoverableError, CriticalError
|
TimeoutError, CatchableError, RecoverableError, CriticalError,
|
||||||
|
DisconnectError
|
||||||
|
|
||||||
HttpServerState* {.pure.} = enum
|
HttpServerState* {.pure.} = enum
|
||||||
ServerRunning, ServerStopped, ServerClosed
|
ServerRunning, ServerStopped, ServerClosed
|
||||||
@ -398,8 +399,7 @@ proc sendErrorResponse(conn: HttpConnectionRef, version: HttpVersion,
|
|||||||
answer.add("Date: " & httpDate() & "\r\n")
|
answer.add("Date: " & httpDate() & "\r\n")
|
||||||
if len(datatype) > 0:
|
if len(datatype) > 0:
|
||||||
answer.add("Content-Type: " & datatype & "\r\n")
|
answer.add("Content-Type: " & datatype & "\r\n")
|
||||||
if len(databody) > 0:
|
answer.add("Content-Length: " & $len(databody) & "\r\n")
|
||||||
answer.add("Content-Length: " & $len(databody) & "\r\n")
|
|
||||||
if keepAlive:
|
if keepAlive:
|
||||||
answer.add("Connection: keep-alive\r\n")
|
answer.add("Connection: keep-alive\r\n")
|
||||||
else:
|
else:
|
||||||
@ -435,7 +435,7 @@ proc getRequest(conn: HttpConnectionRef): Future[HttpRequestRef] {.async.} =
|
|||||||
except AsyncStreamIncompleteError, AsyncStreamReadError:
|
except AsyncStreamIncompleteError, AsyncStreamReadError:
|
||||||
raiseHttpDisconnectError()
|
raiseHttpDisconnectError()
|
||||||
except AsyncStreamLimitError:
|
except AsyncStreamLimitError:
|
||||||
raiseHttpCriticalError("Maximum size of request headers reached", Http413)
|
raiseHttpCriticalError("Maximum size of request headers reached", Http431)
|
||||||
|
|
||||||
proc new(ht: typedesc[HttpConnectionRef], server: HttpServerRef,
|
proc new(ht: typedesc[HttpConnectionRef], server: HttpServerRef,
|
||||||
transp: StreamTransport): HttpConnectionRef =
|
transp: StreamTransport): HttpConnectionRef =
|
||||||
@ -561,9 +561,13 @@ proc processLoop(server: HttpServerRef, transp: StreamTransport) {.async.} =
|
|||||||
let error = HttpProcessError.init(HTTPServerError.CriticalError, exc,
|
let error = HttpProcessError.init(HTTPServerError.CriticalError, exc,
|
||||||
transp.remoteAddress(), exc.code)
|
transp.remoteAddress(), exc.code)
|
||||||
arg = RequestFence.err(error)
|
arg = RequestFence.err(error)
|
||||||
except HttpDisconnectError:
|
except HttpDisconnectError as exc:
|
||||||
# If remote peer disconnected we just exiting loop
|
if HttpServerFlags.NotifyDisconnect in server.flags:
|
||||||
breakLoop = true
|
let error = HttpProcessError.init(HttpServerError.DisconnectError, exc,
|
||||||
|
transp.remoteAddress(), Http400)
|
||||||
|
arg = RequestFence.err(error)
|
||||||
|
else:
|
||||||
|
breakLoop = true
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
let error = HttpProcessError.init(HTTPServerError.CatchableError, exc,
|
let error = HttpProcessError.init(HTTPServerError.CatchableError, exc,
|
||||||
transp.remoteAddress(), Http500)
|
transp.remoteAddress(), Http500)
|
||||||
@ -600,6 +604,8 @@ proc processLoop(server: HttpServerRef, transp: StreamTransport) {.async.} =
|
|||||||
discard await conn.sendErrorResponse(HttpVersion11, code, false)
|
discard await conn.sendErrorResponse(HttpVersion11, code, false)
|
||||||
of HTTPServerError.CatchableError:
|
of HTTPServerError.CatchableError:
|
||||||
discard await conn.sendErrorResponse(HttpVersion11, code, false)
|
discard await conn.sendErrorResponse(HttpVersion11, code, false)
|
||||||
|
of HttpServerError.DisconnectError:
|
||||||
|
discard
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
let request = arg.get()
|
let request = arg.get()
|
||||||
@ -867,9 +873,9 @@ proc prepareLengthHeaders(resp: HttpResponseRef, length: int): string {.
|
|||||||
raises: [Defect].}=
|
raises: [Defect].}=
|
||||||
var answer = $(resp.version) & " " & $(resp.status) & "\r\n"
|
var answer = $(resp.version) & " " & $(resp.status) & "\r\n"
|
||||||
answer.doHeaderDef(resp, "Date", httpDate())
|
answer.doHeaderDef(resp, "Date", httpDate())
|
||||||
answer.doHeaderDef(resp, "Content-Type", "text/html; charset=utf-8")
|
|
||||||
if length > 0:
|
if length > 0:
|
||||||
answer.doHeaderVal("Content-Length", $(length))
|
answer.doHeaderDef(resp, "Content-Type", "text/html; charset=utf-8")
|
||||||
|
answer.doHeaderVal("Content-Length", $(length))
|
||||||
if "Connection" notin resp.headersTable:
|
if "Connection" notin resp.headersTable:
|
||||||
if KeepAlive in resp.flags:
|
if KeepAlive in resp.flags:
|
||||||
answer.doHeaderVal("Connection", "keep-alive")
|
answer.doHeaderVal("Connection", "keep-alive")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user