2021-01-27 06:14:17 +00:00
|
|
|
#
|
|
|
|
# Chronos HTTP/S server implementation
|
2021-02-02 22:33:14 +00:00
|
|
|
# (c) Copyright 2021-Present
|
2021-01-27 06:14:17 +00:00
|
|
|
# Status Research & Development GmbH
|
|
|
|
#
|
|
|
|
# Licensed under either of
|
|
|
|
# Apache License, version 2.0, (LICENSE-APACHEv2)
|
|
|
|
# MIT license (LICENSE-MIT)
|
2023-11-21 10:01:44 +00:00
|
|
|
|
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-09-05 11:08:52 +00:00
|
|
|
import std/[tables, uri, strutils]
|
2023-11-13 09:56:19 +00:00
|
|
|
import stew/[base10], httputils, results
|
2021-01-27 06:14:17 +00:00
|
|
|
import ../../asyncloop, ../../asyncsync
|
2021-03-17 13:40:40 +00:00
|
|
|
import ../../streams/[asyncstream, boundstream, chunkstream]
|
2023-11-13 09:56:19 +00:00
|
|
|
import "."/[httptable, httpcommon, multipart]
|
2021-08-26 11:22:29 +00:00
|
|
|
export asyncloop, asyncsync, httptable, httpcommon, httputils, multipart,
|
2022-09-05 11:08:52 +00:00
|
|
|
asyncstream, boundstream, chunkstream, uri, tables, results
|
2021-01-27 06:14:17 +00:00
|
|
|
|
|
|
|
type
|
2021-01-31 05:57:58 +00:00
|
|
|
HttpServerFlags* {.pure.} = enum
|
2021-05-17 19:39:24 +00:00
|
|
|
Secure,
|
|
|
|
## Internal flag which indicates that server working in secure TLS mode
|
|
|
|
NoExpectHandler,
|
|
|
|
## Do not handle `Expect` header automatically
|
|
|
|
NotifyDisconnect,
|
|
|
|
## Notify user-callback when remote client disconnects.
|
|
|
|
QueryCommaSeparatedArray
|
|
|
|
## Enable usage of comma as an array item delimiter in url-encoded
|
|
|
|
## entities (e.g. query string or POST body).
|
2023-06-26 13:28:33 +00:00
|
|
|
Http11Pipeline
|
|
|
|
## Enable HTTP/1.1 pipelining.
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpServerError* {.pure.} = enum
|
2023-07-14 10:35:08 +00:00
|
|
|
InterruptError, TimeoutError, CatchableError, RecoverableError,
|
|
|
|
CriticalError, DisconnectError
|
2021-01-27 19:39:14 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpServerState* {.pure.} = enum
|
|
|
|
ServerRunning, ServerStopped, ServerClosed
|
|
|
|
|
2021-01-27 19:39:14 +00:00
|
|
|
HttpProcessError* = object
|
2023-07-14 10:35:08 +00:00
|
|
|
kind*: HttpServerError
|
2021-02-03 10:47:03 +00:00
|
|
|
code*: HttpCode
|
2021-01-28 03:37:16 +00:00
|
|
|
exc*: ref CatchableError
|
2023-07-14 10:35:08 +00:00
|
|
|
remote*: Opt[TransportAddress]
|
2021-01-27 19:39:14 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
ConnectionFence* = Result[HttpConnectionRef, HttpProcessError]
|
|
|
|
ResponseFence* = Result[HttpResponseRef, HttpProcessError]
|
2021-02-18 12:08:21 +00:00
|
|
|
RequestFence* = Result[HttpRequestRef, HttpProcessError]
|
2021-01-27 06:14:17 +00:00
|
|
|
|
|
|
|
HttpRequestFlags* {.pure.} = enum
|
2021-08-06 10:13:55 +00:00
|
|
|
BoundBody, UnboundBody, MultipartForm, UrlencodedForm, ClientExpect
|
2021-01-31 05:57:58 +00:00
|
|
|
|
|
|
|
HttpResponseFlags* {.pure.} = enum
|
2021-09-14 17:32:58 +00:00
|
|
|
KeepAlive, Stream
|
|
|
|
|
|
|
|
HttpResponseStreamType* {.pure.} = enum
|
|
|
|
Plain, SSE, Chunked
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType* {.pure.} = enum
|
|
|
|
KeepAlive, Graceful, Immediate
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpResponseState* {.pure.} = enum
|
2023-07-14 10:35:08 +00:00
|
|
|
Empty, Prepared, Sending, Finished, Failed, Cancelled, Default
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-01-27 19:39:14 +00:00
|
|
|
HttpProcessCallback* =
|
exception tracking (#166)
* exception tracking
This PR adds minimal exception tracking to chronos, moving the goalpost
one step further.
In particular, it becomes invalid to raise exceptions from `callSoon`
callbacks: this is critical for writing correct error handling because
there's no reasonable way that a user of chronos can possibly _reason_
about exceptions coming out of there: the event loop will be in an
indeterminite state when the loop is executing an _random_ callback.
As expected, there are several issues in the error handling of chronos:
in particular, it will end up in an inconsistent internal state whenever
the selector loop operations fail, because the internal state update
functions are not written in an exception-safe way. This PR turns this
into a Defect, which probably is not the optimal way of handling things
- expect more work to be done here.
Some API have no way of reporting back errors to callers - for example,
when something fails in the accept loop, there's not much it can do, and
no way to report it back to the user of the API - this has been fixed
with the new accept flow - the old one should be deprecated.
Finally, there is information loss in the API: in composite operations
like `poll` and `waitFor` there's no way to differentiate internal
errors from user-level errors originating from callbacks.
* store `CatchableError` in future
* annotate proc's with correct raises information
* `selectors2` to avoid non-CatchableError IOSelectorsException
* `$` should never raise
* remove unnecessary gcsafe annotations
* fix exceptions leaking out of timer waits
* fix some imports
* functions must signal raising the union of all exceptions across all
platforms to enable cross-platform code
* switch to unittest2
* add `selectors2` which supercedes the std library version and fixes
several exception handling issues in there
* fixes
* docs, platform-independent eh specifiers for some functions
* add feature flag for strict exception mode
also bump version to 3.0.0 - _most_ existing code should be compatible
with this version of exception handling but some things might need
fixing - callbacks, existing raises specifications etc.
* fix AsyncCheck for non-void T
2021-03-24 09:08:33 +00:00
|
|
|
proc(req: RequestFence): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpResponseError]), gcsafe.}
|
2021-01-27 19:39:14 +00:00
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
HttpConnectionCallback* =
|
|
|
|
proc(server: HttpServerRef,
|
2021-05-17 19:39:24 +00:00
|
|
|
transp: StreamTransport): Future[HttpConnectionRef] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: [CancelledError, HttpConnectionError]), gcsafe.}
|
2021-03-17 13:40:40 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
HttpCloseConnectionCallback* =
|
|
|
|
proc(connection: HttpConnectionRef): Future[void] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: []), gcsafe.}
|
2023-07-14 10:35:08 +00:00
|
|
|
|
|
|
|
HttpConnectionHolder* = object of RootObj
|
|
|
|
connection*: HttpConnectionRef
|
|
|
|
server*: HttpServerRef
|
|
|
|
future*: Future[void]
|
|
|
|
transp*: StreamTransport
|
|
|
|
acceptMoment*: Moment
|
|
|
|
connectionId*: string
|
|
|
|
|
|
|
|
HttpConnectionHolderRef* = ref HttpConnectionHolder
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpServer* = object of RootObj
|
2021-01-27 06:14:17 +00:00
|
|
|
instance*: StreamServer
|
2021-02-20 17:41:22 +00:00
|
|
|
address*: TransportAddress
|
2021-01-27 06:14:17 +00:00
|
|
|
# semaphore*: AsyncSemaphore
|
|
|
|
maxConnections*: int
|
2022-07-04 09:31:18 +00:00
|
|
|
backlogSize*: int
|
2021-01-27 06:14:17 +00:00
|
|
|
baseUri*: Uri
|
2021-03-11 11:41:13 +00:00
|
|
|
serverIdent*: string
|
2021-01-27 06:14:17 +00:00
|
|
|
flags*: set[HttpServerFlags]
|
2021-02-02 10:48:04 +00:00
|
|
|
socketFlags*: set[ServerFlags]
|
2023-07-14 10:35:08 +00:00
|
|
|
connections*: OrderedTable[string, HttpConnectionHolderRef]
|
2023-11-21 10:01:44 +00:00
|
|
|
acceptLoop*: Future[void].Raising([])
|
2021-01-27 06:14:17 +00:00
|
|
|
lifetime*: Future[void]
|
2022-07-04 09:31:18 +00:00
|
|
|
headersTimeout*: Duration
|
|
|
|
bufferSize*: int
|
|
|
|
maxHeadersSize*: int
|
|
|
|
maxRequestBodySize*: int
|
|
|
|
processCallback*: HttpProcessCallback
|
|
|
|
createConnCallback*: HttpConnectionCallback
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpServerRef* = ref HttpServer
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpRequest* = object of RootObj
|
2021-08-06 10:13:55 +00:00
|
|
|
state*: HttpState
|
2021-02-02 22:33:14 +00:00
|
|
|
headers*: HttpTable
|
|
|
|
query*: HttpTable
|
2022-09-05 11:08:52 +00:00
|
|
|
postTable: Opt[HttpTable]
|
2021-01-27 06:14:17 +00:00
|
|
|
rawPath*: string
|
|
|
|
uri*: Uri
|
|
|
|
scheme*: string
|
|
|
|
version*: HttpVersion
|
|
|
|
meth*: HttpMethod
|
|
|
|
contentEncoding*: set[ContentEncodingFlags]
|
|
|
|
transferEncoding*: set[TransferEncodingFlags]
|
|
|
|
requestFlags*: set[HttpRequestFlags]
|
2022-09-20 12:59:34 +00:00
|
|
|
contentLength*: int
|
2022-09-05 11:08:52 +00:00
|
|
|
contentTypeData*: Opt[ContentTypeData]
|
2021-02-01 16:04:38 +00:00
|
|
|
connection*: HttpConnectionRef
|
2022-09-05 11:08:52 +00:00
|
|
|
response*: Opt[HttpResponseRef]
|
2021-02-01 16:04:38 +00:00
|
|
|
|
|
|
|
HttpRequestRef* = ref HttpRequest
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpResponse* = object of RootObj
|
2021-01-31 05:57:58 +00:00
|
|
|
status*: HttpCode
|
2021-01-27 19:39:14 +00:00
|
|
|
version*: HttpVersion
|
|
|
|
headersTable: HttpTable
|
2021-02-01 16:04:38 +00:00
|
|
|
body: seq[byte]
|
|
|
|
flags: set[HttpResponseFlags]
|
2021-03-02 13:26:07 +00:00
|
|
|
state*: HttpResponseState
|
2021-02-01 16:04:38 +00:00
|
|
|
connection*: HttpConnectionRef
|
2021-09-14 17:32:58 +00:00
|
|
|
streamType*: HttpResponseStreamType
|
|
|
|
writer: AsyncStreamWriter
|
2021-01-27 19:39:14 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpResponseRef* = ref HttpResponse
|
|
|
|
|
|
|
|
HttpConnection* = object of RootObj
|
2021-08-06 10:13:55 +00:00
|
|
|
state*: HttpState
|
2021-02-01 16:04:38 +00:00
|
|
|
server*: HttpServerRef
|
2023-07-14 10:35:08 +00:00
|
|
|
transp*: StreamTransport
|
2021-03-17 13:40:40 +00:00
|
|
|
mainReader*: AsyncStreamReader
|
|
|
|
mainWriter*: AsyncStreamWriter
|
2021-02-03 10:47:03 +00:00
|
|
|
reader*: AsyncStreamReader
|
|
|
|
writer*: AsyncStreamWriter
|
2023-07-14 10:35:08 +00:00
|
|
|
closeCb*: HttpCloseConnectionCallback
|
|
|
|
createMoment*: Moment
|
2023-08-09 07:57:49 +00:00
|
|
|
currentRawQuery*: Opt[string]
|
2021-01-27 06:14:17 +00:00
|
|
|
buffer: seq[byte]
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
HttpConnectionRef* = ref HttpConnection
|
|
|
|
|
2021-03-02 13:26:07 +00:00
|
|
|
ByteChar* = string | seq[byte]
|
|
|
|
|
2021-12-10 09:55:25 +00:00
|
|
|
proc init(htype: typedesc[HttpProcessError], error: HttpServerError,
|
2023-07-14 10:35:08 +00:00
|
|
|
exc: ref CatchableError, remote: Opt[TransportAddress],
|
2023-11-21 10:01:44 +00:00
|
|
|
code: HttpCode): HttpProcessError =
|
2023-07-14 10:35:08 +00:00
|
|
|
HttpProcessError(kind: error, exc: exc, remote: remote, code: code)
|
|
|
|
|
|
|
|
proc init(htype: typedesc[HttpProcessError],
|
2023-11-21 10:01:44 +00:00
|
|
|
error: HttpServerError): HttpProcessError =
|
2023-07-14 10:35:08 +00:00
|
|
|
HttpProcessError(kind: error)
|
|
|
|
|
|
|
|
proc new(htype: typedesc[HttpConnectionHolderRef], server: HttpServerRef,
|
|
|
|
transp: StreamTransport,
|
|
|
|
connectionId: string): HttpConnectionHolderRef =
|
|
|
|
HttpConnectionHolderRef(
|
|
|
|
server: server, transp: transp, acceptMoment: Moment.now(),
|
|
|
|
connectionId: connectionId)
|
|
|
|
|
|
|
|
proc error*(e: HttpProcessError): HttpServerError = e.kind
|
2021-01-28 03:37:16 +00:00
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
proc createConnection(server: HttpServerRef,
|
2023-11-21 10:01:44 +00:00
|
|
|
transp: StreamTransport): Future[HttpConnectionRef] {.
|
|
|
|
async: (raises: [CancelledError, HttpConnectionError]).}
|
2021-03-17 13:40:40 +00:00
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
proc new*(htype: typedesc[HttpServerRef],
|
2021-01-27 06:14:17 +00:00
|
|
|
address: TransportAddress,
|
2021-01-28 03:37:16 +00:00
|
|
|
processCallback: HttpProcessCallback,
|
2021-01-31 05:57:58 +00:00
|
|
|
serverFlags: set[HttpServerFlags] = {},
|
|
|
|
socketFlags: set[ServerFlags] = {ReuseAddr},
|
2021-01-27 06:14:17 +00:00
|
|
|
serverUri = Uri(),
|
2021-03-11 11:41:13 +00:00
|
|
|
serverIdent = "",
|
2021-01-27 06:14:17 +00:00
|
|
|
maxConnections: int = -1,
|
|
|
|
bufferSize: int = 4096,
|
2023-07-31 19:40:00 +00:00
|
|
|
backlogSize: int = DefaultBacklogSize,
|
2021-01-27 06:14:17 +00:00
|
|
|
httpHeadersTimeout = 10.seconds,
|
|
|
|
maxHeadersSize: int = 8192,
|
2023-10-30 13:27:50 +00:00
|
|
|
maxRequestBodySize: int = 1_048_576,
|
2023-11-21 10:01:44 +00:00
|
|
|
dualstack = DualStackType.Auto): HttpResult[HttpServerRef] =
|
2021-01-31 05:57:58 +00:00
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
let serverUri =
|
|
|
|
if len(serverUri.hostname) > 0:
|
|
|
|
serverUri
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
parseUri("http://" & $address & "/")
|
|
|
|
except TransportAddressError as exc:
|
|
|
|
return err(exc.msg)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
let serverInstance =
|
2021-02-10 13:13:36 +00:00
|
|
|
try:
|
2021-03-17 13:40:40 +00:00
|
|
|
createStreamServer(address, flags = socketFlags, bufferSize = bufferSize,
|
2023-10-30 13:27:50 +00:00
|
|
|
backlog = backlogSize, dualstack = dualstack)
|
2021-03-17 13:40:40 +00:00
|
|
|
except TransportOsError as exc:
|
|
|
|
return err(exc.msg)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2022-07-04 09:31:18 +00:00
|
|
|
var res = HttpServerRef(
|
2023-07-14 10:35:08 +00:00
|
|
|
address: serverInstance.localAddress(),
|
2022-07-04 09:31:18 +00:00
|
|
|
instance: serverInstance,
|
|
|
|
processCallback: processCallback,
|
|
|
|
createConnCallback: createConnection,
|
|
|
|
baseUri: serverUri,
|
|
|
|
serverIdent: serverIdent,
|
|
|
|
flags: serverFlags,
|
|
|
|
socketFlags: socketFlags,
|
|
|
|
maxConnections: maxConnections,
|
|
|
|
bufferSize: bufferSize,
|
|
|
|
backlogSize: backlogSize,
|
|
|
|
headersTimeout: httpHeadersTimeout,
|
|
|
|
maxHeadersSize: maxHeadersSize,
|
|
|
|
maxRequestBodySize: maxRequestBodySize,
|
|
|
|
# semaphore:
|
|
|
|
# if maxConnections > 0:
|
|
|
|
# newAsyncSemaphore(maxConnections)
|
|
|
|
# else:
|
|
|
|
# nil
|
|
|
|
lifetime: newFuture[void]("http.server.lifetime"),
|
2023-07-14 10:35:08 +00:00
|
|
|
connections: initOrderedTable[string, HttpConnectionHolderRef]()
|
2022-07-04 09:31:18 +00:00
|
|
|
)
|
2021-03-17 13:40:40 +00:00
|
|
|
ok(res)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
proc getServerFlags(req: HttpRequestRef): set[HttpServerFlags] =
|
|
|
|
var defaultFlags: set[HttpServerFlags] = {}
|
|
|
|
if isNil(req): return defaultFlags
|
|
|
|
if isNil(req.connection): return defaultFlags
|
|
|
|
if isNil(req.connection.server): return defaultFlags
|
|
|
|
req.connection.server.flags
|
|
|
|
|
|
|
|
proc getResponseFlags(req: HttpRequestRef): set[HttpResponseFlags] =
|
2023-06-26 13:28:33 +00:00
|
|
|
var defaultFlags: set[HttpResponseFlags] = {}
|
|
|
|
case req.version
|
|
|
|
of HttpVersion11:
|
2023-07-14 10:35:08 +00:00
|
|
|
if HttpServerFlags.Http11Pipeline notin req.getServerFlags():
|
2023-06-26 13:28:33 +00:00
|
|
|
return defaultFlags
|
|
|
|
let header = req.headers.getString(ConnectionHeader, "keep-alive")
|
|
|
|
if header == "keep-alive":
|
|
|
|
{HttpResponseFlags.KeepAlive}
|
|
|
|
else:
|
|
|
|
defaultFlags
|
|
|
|
else:
|
|
|
|
defaultFlags
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getResponseVersion(reqFence: RequestFence): HttpVersion =
|
2023-07-14 10:35:08 +00:00
|
|
|
if reqFence.isErr():
|
|
|
|
HttpVersion11
|
|
|
|
else:
|
|
|
|
reqFence.get().version
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getResponse*(req: HttpRequestRef): HttpResponseRef =
|
2021-02-01 16:04:38 +00:00
|
|
|
if req.response.isNone():
|
|
|
|
var resp = HttpResponseRef(
|
|
|
|
status: Http200,
|
|
|
|
state: HttpResponseState.Empty,
|
|
|
|
version: req.version,
|
|
|
|
headersTable: HttpTable.init(),
|
|
|
|
connection: req.connection,
|
2023-06-26 13:28:33 +00:00
|
|
|
flags: req.getResponseFlags()
|
2021-02-01 16:04:38 +00:00
|
|
|
)
|
2022-09-05 11:08:52 +00:00
|
|
|
req.response = Opt.some(resp)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp
|
|
|
|
else:
|
|
|
|
req.response.get()
|
|
|
|
|
2021-03-11 11:41:13 +00:00
|
|
|
proc getHostname*(server: HttpServerRef): string =
|
|
|
|
if len(server.baseUri.port) > 0:
|
|
|
|
server.baseUri.hostname & ":" & server.baseUri.port
|
|
|
|
else:
|
|
|
|
server.baseUri.hostname
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc defaultResponse*(): HttpResponseRef =
|
2023-07-14 10:35:08 +00:00
|
|
|
## Create an empty response to return when request processor got no request.
|
|
|
|
HttpResponseRef(state: HttpResponseState.Default, version: HttpVersion11)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc dumbResponse*(): HttpResponseRef {.
|
2023-07-14 10:35:08 +00:00
|
|
|
deprecated: "Please use defaultResponse() instead".} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Create an empty response to return when request processor got no request.
|
2023-07-14 10:35:08 +00:00
|
|
|
defaultResponse()
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getId(transp: StreamTransport): Result[string, string] {.inline.} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Returns string unique transport's identifier as string.
|
2021-11-12 16:13:56 +00:00
|
|
|
try:
|
|
|
|
ok($transp.remoteAddress() & "_" & $transp.localAddress())
|
|
|
|
except TransportOsError as exc:
|
|
|
|
err($exc.msg)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc hasBody*(request: HttpRequestRef): bool =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Returns ``true`` if request has body.
|
|
|
|
request.requestFlags * {HttpRequestFlags.BoundBody,
|
|
|
|
HttpRequestFlags.UnboundBody} != {}
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
proc prepareRequest(conn: HttpConnectionRef,
|
2023-11-21 10:01:44 +00:00
|
|
|
req: HttpRequestHeader): HttpResultCode[HttpRequestRef] =
|
2021-08-06 10:13:55 +00:00
|
|
|
var request = HttpRequestRef(connection: conn, state: HttpState.Alive)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
|
|
|
if req.version notin {HttpVersion10, HttpVersion11}:
|
|
|
|
return err(Http505)
|
|
|
|
|
2021-02-02 10:48:04 +00:00
|
|
|
request.scheme =
|
|
|
|
if HttpServerFlags.Secure in conn.server.flags:
|
|
|
|
"https"
|
|
|
|
else:
|
|
|
|
"http"
|
|
|
|
|
2021-01-27 06:14:17 +00:00
|
|
|
request.version = req.version
|
|
|
|
request.meth = req.meth
|
|
|
|
|
|
|
|
request.rawPath =
|
|
|
|
block:
|
|
|
|
let res = req.uri()
|
|
|
|
if len(res) == 0:
|
|
|
|
return err(Http400)
|
|
|
|
res
|
|
|
|
|
|
|
|
request.uri =
|
|
|
|
if request.rawPath != "*":
|
|
|
|
let uri = parseUri(request.rawPath)
|
|
|
|
if uri.scheme notin ["http", "https", ""]:
|
|
|
|
return err(Http400)
|
|
|
|
uri
|
|
|
|
else:
|
|
|
|
var uri = initUri()
|
|
|
|
uri.path = "*"
|
|
|
|
uri
|
|
|
|
|
2021-02-02 22:33:14 +00:00
|
|
|
request.query =
|
2021-01-27 06:14:17 +00:00
|
|
|
block:
|
2021-05-17 19:39:24 +00:00
|
|
|
let queryFlags =
|
|
|
|
if QueryCommaSeparatedArray in conn.server.flags:
|
|
|
|
{QueryParamsFlag.CommaSeparatedArray}
|
|
|
|
else:
|
|
|
|
{}
|
2021-01-27 06:14:17 +00:00
|
|
|
var table = HttpTable.init()
|
2021-05-17 19:39:24 +00:00
|
|
|
for key, value in queryParams(request.uri.query, queryFlags):
|
2021-01-27 06:14:17 +00:00
|
|
|
table.add(key, value)
|
|
|
|
table
|
|
|
|
|
2021-02-02 22:33:14 +00:00
|
|
|
request.headers =
|
2021-01-27 06:14:17 +00:00
|
|
|
block:
|
|
|
|
var table = HttpTable.init()
|
|
|
|
# Retrieve headers and values
|
|
|
|
for key, value in req.headers():
|
|
|
|
table.add(key, value)
|
|
|
|
# Validating HTTP request headers
|
|
|
|
# Some of the headers must be present only once.
|
2021-05-10 07:26:36 +00:00
|
|
|
if table.count(ContentTypeHeader) > 1:
|
2021-01-27 06:14:17 +00:00
|
|
|
return err(Http400)
|
2021-05-10 07:26:36 +00:00
|
|
|
if table.count(ContentLengthHeader) > 1:
|
2021-01-27 06:14:17 +00:00
|
|
|
return err(Http400)
|
2021-05-10 07:26:36 +00:00
|
|
|
if table.count(TransferEncodingHeader) > 1:
|
2021-01-27 06:14:17 +00:00
|
|
|
return err(Http400)
|
|
|
|
table
|
|
|
|
|
|
|
|
# Preprocessing "Content-Encoding" header.
|
|
|
|
request.contentEncoding =
|
|
|
|
block:
|
2021-05-10 07:26:36 +00:00
|
|
|
let res = getContentEncoding(
|
|
|
|
request.headers.getList(ContentEncodingHeader))
|
2021-01-27 06:14:17 +00:00
|
|
|
if res.isErr():
|
|
|
|
return err(Http400)
|
|
|
|
else:
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
# Preprocessing "Transfer-Encoding" header.
|
|
|
|
request.transferEncoding =
|
|
|
|
block:
|
|
|
|
let res = getTransferEncoding(
|
2021-05-10 07:26:36 +00:00
|
|
|
request.headers.getList(TransferEncodingHeader))
|
2021-01-27 06:14:17 +00:00
|
|
|
if res.isErr():
|
|
|
|
return err(Http400)
|
|
|
|
else:
|
|
|
|
res.get()
|
|
|
|
|
|
|
|
# Almost all HTTP requests could have body (except TRACE), we perform some
|
|
|
|
# steps to reveal information about body.
|
2021-05-10 07:26:36 +00:00
|
|
|
if ContentLengthHeader in request.headers:
|
|
|
|
let length = request.headers.getInt(ContentLengthHeader)
|
2022-08-05 16:59:26 +00:00
|
|
|
if length >= 0:
|
2021-01-27 06:14:17 +00:00
|
|
|
if request.meth == MethodTrace:
|
|
|
|
return err(Http400)
|
2022-08-05 16:59:26 +00:00
|
|
|
# Because of coversion to `int` we should avoid unexpected OverflowError.
|
2021-01-27 06:14:17 +00:00
|
|
|
if length > uint64(high(int)):
|
|
|
|
return err(Http413)
|
|
|
|
if length > uint64(conn.server.maxRequestBodySize):
|
|
|
|
return err(Http413)
|
|
|
|
request.contentLength = int(length)
|
|
|
|
request.requestFlags.incl(HttpRequestFlags.BoundBody)
|
|
|
|
else:
|
|
|
|
if TransferEncodingFlags.Chunked in request.transferEncoding:
|
|
|
|
if request.meth == MethodTrace:
|
|
|
|
return err(Http400)
|
|
|
|
request.requestFlags.incl(HttpRequestFlags.UnboundBody)
|
|
|
|
|
|
|
|
if request.hasBody():
|
|
|
|
# If request has body, we going to understand how its encoded.
|
2021-05-10 07:26:36 +00:00
|
|
|
if ContentTypeHeader in request.headers:
|
2022-08-05 16:59:26 +00:00
|
|
|
let contentType =
|
|
|
|
getContentType(request.headers.getList(ContentTypeHeader)).valueOr:
|
|
|
|
return err(Http415)
|
|
|
|
if contentType == UrlEncodedContentType:
|
2021-01-31 05:57:58 +00:00
|
|
|
request.requestFlags.incl(HttpRequestFlags.UrlencodedForm)
|
2022-08-05 16:59:26 +00:00
|
|
|
elif contentType == MultipartContentType:
|
2021-01-31 05:57:58 +00:00
|
|
|
request.requestFlags.incl(HttpRequestFlags.MultipartForm)
|
2022-09-05 11:08:52 +00:00
|
|
|
request.contentTypeData = Opt.some(contentType)
|
2021-01-31 05:57:58 +00:00
|
|
|
|
2021-05-10 07:26:36 +00:00
|
|
|
if ExpectHeader in request.headers:
|
|
|
|
let expectHeader = request.headers.getString(ExpectHeader)
|
2021-01-31 05:57:58 +00:00
|
|
|
if strip(expectHeader).toLowerAscii() == "100-continue":
|
|
|
|
request.requestFlags.incl(HttpRequestFlags.ClientExpect)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
trackCounter(HttpServerRequestTrackerName)
|
2021-01-27 06:14:17 +00:00
|
|
|
ok(request)
|
|
|
|
|
2021-02-10 08:43:05 +00:00
|
|
|
proc getBodyReader*(request: HttpRequestRef): HttpResult[HttpBodyReader] =
|
2021-01-28 03:37:16 +00:00
|
|
|
## Returns stream's reader instance which can be used to read request's body.
|
2021-01-31 05:57:58 +00:00
|
|
|
##
|
|
|
|
## Please be sure to handle ``Expect`` header properly.
|
2021-02-01 16:04:38 +00:00
|
|
|
##
|
|
|
|
## Streams which was obtained using this procedure must be closed to avoid
|
|
|
|
## leaks.
|
2021-01-27 06:14:17 +00:00
|
|
|
if HttpRequestFlags.BoundBody in request.requestFlags:
|
2021-02-10 08:43:05 +00:00
|
|
|
let bstream = newBoundedStreamReader(request.connection.reader,
|
2021-04-26 11:05:37 +00:00
|
|
|
uint64(request.contentLength))
|
2021-02-10 08:43:05 +00:00
|
|
|
ok(newHttpBodyReader(bstream))
|
2021-01-27 06:14:17 +00:00
|
|
|
elif HttpRequestFlags.UnboundBody in request.requestFlags:
|
2021-02-10 08:43:05 +00:00
|
|
|
let maxBodySize = request.connection.server.maxRequestBodySize
|
2021-04-26 11:05:37 +00:00
|
|
|
let cstream = newChunkedStreamReader(request.connection.reader)
|
|
|
|
let bstream = newBoundedStreamReader(cstream, uint64(maxBodySize),
|
2021-02-10 08:43:05 +00:00
|
|
|
comparison = BoundCmp.LessOrEqual)
|
2021-04-26 11:05:37 +00:00
|
|
|
ok(newHttpBodyReader(bstream, cstream))
|
2021-01-27 06:14:17 +00:00
|
|
|
else:
|
|
|
|
err("Request do not have body available")
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc handleExpect*(request: HttpRequestRef) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-01-31 05:57:58 +00:00
|
|
|
## Handle expectation for ``Expect`` header.
|
|
|
|
## https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expect
|
|
|
|
if HttpServerFlags.NoExpectHandler notin request.connection.server.flags:
|
|
|
|
if HttpRequestFlags.ClientExpect in request.requestFlags:
|
|
|
|
if request.version == HttpVersion11:
|
|
|
|
try:
|
|
|
|
let message = $request.version & " " & $Http100 & "\r\n\r\n"
|
2021-02-03 10:47:03 +00:00
|
|
|
await request.connection.writer.write(message)
|
2021-01-31 05:57:58 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
|
|
|
raiseHttpCriticalError(
|
|
|
|
"Unable to send `100-continue` response, reason: " & $exc.msg)
|
2021-01-31 05:57:58 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getBody*(request: HttpRequestRef): Future[seq[byte]] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Obtain request's body as sequence of bytes.
|
2021-08-06 10:13:55 +00:00
|
|
|
let bodyReader = request.getBodyReader()
|
|
|
|
if bodyReader.isErr():
|
2021-01-27 06:14:17 +00:00
|
|
|
return @[]
|
|
|
|
else:
|
2021-08-06 10:13:55 +00:00
|
|
|
var reader = bodyReader.get()
|
2021-01-27 06:14:17 +00:00
|
|
|
try:
|
2021-01-31 05:57:58 +00:00
|
|
|
await request.handleExpect()
|
2021-08-06 10:13:55 +00:00
|
|
|
let res = await reader.read()
|
2021-04-26 11:05:37 +00:00
|
|
|
if reader.hasOverflow():
|
2021-08-06 10:13:55 +00:00
|
|
|
await reader.closeWait()
|
|
|
|
reader = nil
|
2021-04-26 11:05:37 +00:00
|
|
|
raiseHttpCriticalError(MaximumBodySizeError, Http413)
|
2021-08-06 10:13:55 +00:00
|
|
|
else:
|
|
|
|
await reader.closeWait()
|
|
|
|
reader = nil
|
|
|
|
return res
|
|
|
|
except CancelledError as exc:
|
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
|
|
|
raise exc
|
2023-11-28 16:57:13 +00:00
|
|
|
except HttpCriticalError as exc:
|
2021-08-06 10:13:55 +00:00
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
2023-11-21 10:01:44 +00:00
|
|
|
raise exc
|
|
|
|
except AsyncStreamError as exc:
|
|
|
|
let msg = "Unable to read request's body, reason: " & $exc.msg
|
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
|
|
|
raiseHttpCriticalError(msg)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc consumeBody*(request: HttpRequestRef): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Consume/discard request's body.
|
2021-08-06 10:13:55 +00:00
|
|
|
let bodyReader = request.getBodyReader()
|
|
|
|
if bodyReader.isErr():
|
2021-01-27 06:14:17 +00:00
|
|
|
return
|
|
|
|
else:
|
2021-08-06 10:13:55 +00:00
|
|
|
var reader = bodyReader.get()
|
2021-01-27 06:14:17 +00:00
|
|
|
try:
|
2021-01-31 05:57:58 +00:00
|
|
|
await request.handleExpect()
|
2021-01-27 06:14:17 +00:00
|
|
|
discard await reader.consume()
|
2021-04-26 11:05:37 +00:00
|
|
|
if reader.hasOverflow():
|
2021-08-06 10:13:55 +00:00
|
|
|
await reader.closeWait()
|
|
|
|
reader = nil
|
2021-04-26 11:05:37 +00:00
|
|
|
raiseHttpCriticalError(MaximumBodySizeError, Http413)
|
2021-08-06 10:13:55 +00:00
|
|
|
else:
|
|
|
|
await reader.closeWait()
|
|
|
|
reader = nil
|
|
|
|
return
|
|
|
|
except CancelledError as exc:
|
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
|
|
|
raise exc
|
2023-11-28 16:57:13 +00:00
|
|
|
except HttpCriticalError as exc:
|
2023-11-21 10:01:44 +00:00
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
|
|
|
raise exc
|
|
|
|
except AsyncStreamError as exc:
|
|
|
|
let msg = "Unable to consume request's body, reason: " & $exc.msg
|
2021-08-06 10:13:55 +00:00
|
|
|
if not(isNil(reader)):
|
|
|
|
await reader.closeWait()
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError(msg)
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-07-28 14:08:38 +00:00
|
|
|
proc getAcceptInfo*(request: HttpRequestRef): Result[AcceptInfo, cstring] =
|
|
|
|
## Returns value of `Accept` header as `AcceptInfo` object.
|
|
|
|
##
|
|
|
|
## If ``Accept`` header is missing in request headers, ``*/*`` content
|
|
|
|
## type will be returned.
|
|
|
|
let acceptHeader = request.headers.getString(AcceptHeaderName)
|
|
|
|
getAcceptInfo(acceptHeader)
|
|
|
|
|
2021-09-27 22:41:29 +00:00
|
|
|
proc preferredContentMediaType*(acceptHeader: string): MediaType =
|
|
|
|
## Returns preferred content-type using ``Accept`` header value specified by
|
|
|
|
## string ``acceptHeader``.
|
2021-07-28 14:08:38 +00:00
|
|
|
let res = getAcceptInfo(acceptHeader)
|
|
|
|
if res.isErr():
|
|
|
|
# If `Accept` header is incorrect, client accepts any type of content.
|
|
|
|
MediaType.init("*", "*")
|
|
|
|
else:
|
|
|
|
let mediaTypes = res.get().data
|
|
|
|
if len(mediaTypes) > 0:
|
|
|
|
mediaTypes[0].mediaType
|
|
|
|
else:
|
|
|
|
MediaType.init("*", "*")
|
|
|
|
|
2021-09-27 22:41:29 +00:00
|
|
|
proc preferredContentType*(acceptHeader: string,
|
2022-08-06 10:53:40 +00:00
|
|
|
types: varargs[MediaType]
|
|
|
|
): Result[MediaType, cstring] =
|
|
|
|
## Match or obtain preferred content type using ``Accept`` header specified by
|
|
|
|
## string ``acceptHeader`` and server preferred content types ``types``.
|
2021-07-28 14:08:38 +00:00
|
|
|
##
|
|
|
|
## If ``Accept`` header is missing in client's request - ``types[0]`` or
|
|
|
|
## ``*/*`` value will be returned as result.
|
|
|
|
##
|
|
|
|
## If ``Accept`` header has incorrect format in client's request -
|
|
|
|
## ``types[0]`` or ``*/*`` value will be returned as result.
|
|
|
|
##
|
2022-08-06 10:53:40 +00:00
|
|
|
## If ``Accept`` header is present in request to server and it has one or more
|
|
|
|
## content types supported by client, the best value will be selected from
|
|
|
|
## ``types`` using position and quality value (weight) reported in ``Accept``
|
|
|
|
## header. If client do not support any methods in ``types`` error
|
|
|
|
## will be returned.
|
|
|
|
##
|
|
|
|
## Note: Quality value (weight) for content type has priority over server's
|
|
|
|
## preferred content-type.
|
2021-07-28 14:08:38 +00:00
|
|
|
if len(types) == 0:
|
|
|
|
if len(acceptHeader) == 0:
|
|
|
|
# If `Accept` header is missing, return `*/*`.
|
2022-01-21 15:16:59 +00:00
|
|
|
ok(wildCardMediaType)
|
2021-07-28 14:08:38 +00:00
|
|
|
else:
|
|
|
|
let res = getAcceptInfo(acceptHeader)
|
|
|
|
if res.isErr():
|
|
|
|
# If `Accept` header is incorrect, client accepts any type of content.
|
2022-01-21 15:16:59 +00:00
|
|
|
ok(wildCardMediaType)
|
2021-07-28 14:08:38 +00:00
|
|
|
else:
|
|
|
|
let mediaTypes = res.get().data
|
2022-08-06 10:53:40 +00:00
|
|
|
var
|
|
|
|
currentType = MediaType()
|
|
|
|
currentWeight = 0.0
|
|
|
|
# `Accept` header values array is not sorted, so we need to find value
|
|
|
|
# with the biggest ``q-value``.
|
|
|
|
for item in mediaTypes:
|
|
|
|
if currentWeight < item.qvalue:
|
|
|
|
currentType = item.mediaType
|
|
|
|
currentWeight = item.qvalue
|
|
|
|
if len(currentType.media) == 0 and len(currentType.subtype) == 0:
|
2022-01-21 15:16:59 +00:00
|
|
|
ok(wildCardMediaType)
|
2022-08-06 10:53:40 +00:00
|
|
|
else:
|
|
|
|
ok(currentType)
|
2021-07-28 14:08:38 +00:00
|
|
|
else:
|
|
|
|
if len(acceptHeader) == 0:
|
|
|
|
# If `Accept` header is missing, client accepts any type of content.
|
|
|
|
ok(types[0])
|
|
|
|
else:
|
2021-10-21 11:01:46 +00:00
|
|
|
let ares = getAcceptInfo(acceptHeader)
|
|
|
|
if ares.isErr():
|
2021-07-28 14:08:38 +00:00
|
|
|
# If `Accept` header is incorrect, client accepts any type of content.
|
|
|
|
ok(types[0])
|
|
|
|
else:
|
2022-08-06 10:53:40 +00:00
|
|
|
# ``maxWeight`` represents maximum possible weight value which can be
|
|
|
|
# obtained.
|
2022-08-10 11:07:51 +00:00
|
|
|
let maxWeight = (1.0, 0)
|
2022-08-06 10:53:40 +00:00
|
|
|
var
|
|
|
|
currentType = MediaType()
|
2022-08-10 11:07:51 +00:00
|
|
|
currentIndex = -1
|
|
|
|
currentWeight = (-1.0, 0)
|
|
|
|
|
2022-08-06 10:53:40 +00:00
|
|
|
for itemType in ares.get().data:
|
2022-08-10 11:07:51 +00:00
|
|
|
let preferredIndex = types.find(itemType.mediaType)
|
|
|
|
if preferredIndex != -1:
|
|
|
|
let weight = (itemType.qvalue, -preferredIndex)
|
2022-08-06 10:53:40 +00:00
|
|
|
if currentWeight < weight:
|
|
|
|
currentType = types[preferredIndex]
|
|
|
|
currentWeight = weight
|
2022-08-10 11:07:51 +00:00
|
|
|
currentIndex = preferredIndex
|
2022-08-06 10:53:40 +00:00
|
|
|
|
|
|
|
if currentWeight == maxWeight:
|
|
|
|
# There is no reason to continue search, because maximum possible
|
|
|
|
# weight is already achieved, so this is the best match.
|
|
|
|
break
|
|
|
|
|
2022-08-10 11:07:51 +00:00
|
|
|
if currentIndex == -1:
|
2022-08-06 10:53:40 +00:00
|
|
|
err("Preferred content type not found")
|
|
|
|
else:
|
|
|
|
ok(currentType)
|
2021-07-28 14:08:38 +00:00
|
|
|
|
2021-09-27 22:41:29 +00:00
|
|
|
proc preferredContentMediaType*(request: HttpRequestRef): MediaType =
|
|
|
|
## Returns preferred content-type using ``Accept`` header specified by
|
|
|
|
## client in request ``request``.
|
|
|
|
preferredContentMediaType(request.headers.getString(AcceptHeaderName))
|
|
|
|
|
|
|
|
proc preferredContentType*(request: HttpRequestRef,
|
2022-08-06 10:53:40 +00:00
|
|
|
types: varargs[MediaType]
|
|
|
|
): Result[MediaType, cstring] =
|
2021-09-27 22:41:29 +00:00
|
|
|
## Match or obtain preferred content-type using ``Accept`` header specified by
|
|
|
|
## client in request ``request``.
|
|
|
|
preferredContentType(request.headers.getString(AcceptHeaderName), types)
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
proc sendErrorResponse(conn: HttpConnectionRef, version: HttpVersion,
|
2021-01-27 06:14:17 +00:00
|
|
|
code: HttpCode, keepAlive = true,
|
|
|
|
datatype = "text/text",
|
2023-11-21 10:01:44 +00:00
|
|
|
databody = "") {.
|
|
|
|
async: (raises: [CancelledError]).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
var answer = $version & " " & $code & "\r\n"
|
2021-05-10 07:26:36 +00:00
|
|
|
answer.add(DateHeader)
|
|
|
|
answer.add(": ")
|
|
|
|
answer.add(httpDate())
|
|
|
|
answer.add("\r\n")
|
2021-02-02 22:33:14 +00:00
|
|
|
if len(datatype) > 0:
|
2021-05-10 07:26:36 +00:00
|
|
|
answer.add(ContentTypeHeader)
|
|
|
|
answer.add(": ")
|
|
|
|
answer.add(datatype)
|
|
|
|
answer.add("\r\n")
|
|
|
|
answer.add(ContentLengthHeader)
|
|
|
|
answer.add(": ")
|
|
|
|
answer.add(Base10.toString(uint64(len(databody))))
|
|
|
|
answer.add("\r\n")
|
2021-01-27 06:14:17 +00:00
|
|
|
if keepAlive:
|
2021-05-10 07:26:36 +00:00
|
|
|
answer.add(ConnectionHeader)
|
|
|
|
answer.add(": keep-alive\r\n")
|
2021-01-27 06:14:17 +00:00
|
|
|
else:
|
2021-05-10 07:26:36 +00:00
|
|
|
answer.add(ConnectionHeader)
|
|
|
|
answer.add(": close\r\n")
|
2021-08-15 01:29:48 +00:00
|
|
|
answer.add("\r\n")
|
2021-01-27 06:14:17 +00:00
|
|
|
if len(databody) > 0:
|
|
|
|
answer.add(databody)
|
|
|
|
try:
|
2021-02-03 10:47:03 +00:00
|
|
|
await conn.writer.write(answer)
|
2023-07-14 10:35:08 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError:
|
2023-07-14 10:35:08 +00:00
|
|
|
# We ignore errors here, because we indicating error already.
|
|
|
|
discard
|
|
|
|
|
2023-07-19 17:33:28 +00:00
|
|
|
proc sendErrorResponse(
|
|
|
|
conn: HttpConnectionRef,
|
|
|
|
reqFence: RequestFence,
|
|
|
|
respError: HttpProcessError
|
2023-11-21 10:01:44 +00:00
|
|
|
): Future[HttpProcessExitType] {.async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
let version = getResponseVersion(reqFence)
|
|
|
|
try:
|
|
|
|
if reqFence.isOk():
|
|
|
|
case respError.kind
|
|
|
|
of HttpServerError.CriticalError:
|
|
|
|
await conn.sendErrorResponse(version, respError.code, false)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.RecoverableError:
|
|
|
|
await conn.sendErrorResponse(version, respError.code, true)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.CatchableError:
|
|
|
|
await conn.sendErrorResponse(version, respError.code, false)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.DisconnectError,
|
|
|
|
HttpServerError.InterruptError,
|
|
|
|
HttpServerError.TimeoutError:
|
|
|
|
raiseAssert("Unexpected response error: " & $respError.kind)
|
|
|
|
else:
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2021-01-27 06:14:17 +00:00
|
|
|
except CancelledError:
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Immediate
|
2023-07-14 10:35:08 +00:00
|
|
|
|
2023-07-19 17:33:28 +00:00
|
|
|
proc sendDefaultResponse(
|
|
|
|
conn: HttpConnectionRef,
|
|
|
|
reqFence: RequestFence,
|
|
|
|
response: HttpResponseRef
|
2023-11-21 10:01:44 +00:00
|
|
|
): Future[HttpProcessExitType] {.async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
let
|
|
|
|
version = getResponseVersion(reqFence)
|
|
|
|
keepConnection =
|
2023-07-19 17:33:28 +00:00
|
|
|
if isNil(response) or (HttpResponseFlags.KeepAlive notin response.flags):
|
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
else:
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.KeepAlive
|
|
|
|
|
|
|
|
template toBool(hpet: HttpProcessExitType): bool =
|
|
|
|
case hpet
|
|
|
|
of HttpProcessExitType.KeepAlive:
|
|
|
|
true
|
|
|
|
of HttpProcessExitType.Immediate:
|
|
|
|
false
|
|
|
|
of HttpProcessExitType.Graceful:
|
|
|
|
false
|
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
try:
|
|
|
|
if reqFence.isOk():
|
|
|
|
if isNil(response):
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.sendErrorResponse(version, Http404, keepConnection.toBool())
|
2023-07-14 10:35:08 +00:00
|
|
|
keepConnection
|
|
|
|
else:
|
|
|
|
case response.state
|
|
|
|
of HttpResponseState.Empty:
|
|
|
|
# Response was ignored, so we respond with not found.
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.sendErrorResponse(version, Http404,
|
|
|
|
keepConnection.toBool())
|
2023-07-14 10:35:08 +00:00
|
|
|
keepConnection
|
|
|
|
of HttpResponseState.Prepared:
|
|
|
|
# Response was prepared but not sent, so we can respond with some
|
|
|
|
# error code
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.sendErrorResponse(HttpVersion11, Http409,
|
|
|
|
keepConnection.toBool())
|
2023-07-14 10:35:08 +00:00
|
|
|
keepConnection
|
|
|
|
of HttpResponseState.Sending, HttpResponseState.Failed,
|
|
|
|
HttpResponseState.Cancelled:
|
|
|
|
# Just drop connection, because we dont know at what stage we are
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Immediate
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpResponseState.Default:
|
|
|
|
# Response was ignored, so we respond with not found.
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.sendErrorResponse(version, Http404,
|
|
|
|
keepConnection.toBool())
|
2023-07-14 10:35:08 +00:00
|
|
|
keepConnection
|
|
|
|
of HttpResponseState.Finished:
|
|
|
|
keepConnection
|
|
|
|
else:
|
|
|
|
case reqFence.error.kind
|
|
|
|
of HttpServerError.TimeoutError:
|
|
|
|
await conn.sendErrorResponse(version, reqFence.error.code, false)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.CriticalError:
|
|
|
|
await conn.sendErrorResponse(version, reqFence.error.code, false)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.RecoverableError:
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.sendErrorResponse(version, reqFence.error.code, false)
|
|
|
|
HttpProcessExitType.Graceful
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.CatchableError:
|
|
|
|
await conn.sendErrorResponse(version, reqFence.error.code, false)
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Graceful
|
2023-07-17 13:39:41 +00:00
|
|
|
of HttpServerError.DisconnectError:
|
|
|
|
# When `HttpServerFlags.NotifyDisconnect` is set.
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Immediate
|
2023-07-17 13:39:41 +00:00
|
|
|
of HttpServerError.InterruptError:
|
2023-07-14 10:35:08 +00:00
|
|
|
raiseAssert("Unexpected request error: " & $reqFence.error.kind)
|
|
|
|
except CancelledError:
|
2023-07-19 17:33:28 +00:00
|
|
|
HttpProcessExitType.Immediate
|
|
|
|
except CatchableError:
|
|
|
|
HttpProcessExitType.Immediate
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getRequest(conn: HttpConnectionRef): Future[HttpRequestRef] {.
|
|
|
|
async: (raises: [CancelledError, HttpError]).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
try:
|
|
|
|
conn.buffer.setLen(conn.server.maxHeadersSize)
|
2021-02-03 10:47:03 +00:00
|
|
|
let res = await conn.reader.readUntil(addr conn.buffer[0], len(conn.buffer),
|
2021-01-27 06:14:17 +00:00
|
|
|
HeadersMark)
|
|
|
|
conn.buffer.setLen(res)
|
|
|
|
let header = parseRequest(conn.buffer)
|
|
|
|
if header.failed():
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Malformed request recieved")
|
2021-01-27 06:14:17 +00:00
|
|
|
else:
|
|
|
|
let res = prepareRequest(conn, header)
|
|
|
|
if res.isErr():
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Invalid request received", res.error)
|
2021-01-27 06:14:17 +00:00
|
|
|
else:
|
|
|
|
return res.get()
|
2021-02-03 10:47:03 +00:00
|
|
|
except AsyncStreamLimitError:
|
2021-02-21 02:22:59 +00:00
|
|
|
raiseHttpCriticalError("Maximum size of request headers reached", Http431)
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError:
|
|
|
|
raiseHttpDisconnectError()
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
proc init*(value: var HttpConnection, server: HttpServerRef,
|
|
|
|
transp: StreamTransport) =
|
|
|
|
value = HttpConnection(
|
2021-08-06 10:13:55 +00:00
|
|
|
state: HttpState.Alive,
|
2021-02-01 16:04:38 +00:00
|
|
|
server: server,
|
2021-03-17 13:40:40 +00:00
|
|
|
transp: transp,
|
2021-02-01 16:04:38 +00:00
|
|
|
buffer: newSeq[byte](server.maxHeadersSize),
|
2021-03-17 13:40:40 +00:00
|
|
|
mainReader: newAsyncStreamReader(transp),
|
|
|
|
mainWriter: newAsyncStreamWriter(transp)
|
2021-01-27 06:14:17 +00:00
|
|
|
)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc closeUnsecureConnection(conn: HttpConnectionRef) {.async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
if conn.state == HttpState.Alive:
|
|
|
|
conn.state = HttpState.Closing
|
|
|
|
var pending: seq[Future[void]]
|
|
|
|
pending.add(conn.mainReader.closeWait())
|
|
|
|
pending.add(conn.mainWriter.closeWait())
|
|
|
|
pending.add(conn.transp.closeWait())
|
2023-09-15 16:38:39 +00:00
|
|
|
await noCancel(allFutures(pending))
|
2023-07-14 10:35:08 +00:00
|
|
|
untrackCounter(HttpServerUnsecureConnectionTrackerName)
|
2023-08-09 07:57:49 +00:00
|
|
|
reset(conn[])
|
2023-07-14 10:35:08 +00:00
|
|
|
conn.state = HttpState.Closed
|
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
proc new(ht: typedesc[HttpConnectionRef], server: HttpServerRef,
|
|
|
|
transp: StreamTransport): HttpConnectionRef =
|
|
|
|
var res = HttpConnectionRef()
|
|
|
|
res[].init(server, transp)
|
|
|
|
res.reader = res.mainReader
|
|
|
|
res.writer = res.mainWriter
|
2023-07-14 10:35:08 +00:00
|
|
|
res.closeCb = closeUnsecureConnection
|
|
|
|
res.createMoment = Moment.now()
|
|
|
|
trackCounter(HttpServerUnsecureConnectionTrackerName)
|
2021-03-17 13:40:40 +00:00
|
|
|
res
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc gracefulCloseWait*(conn: HttpConnectionRef) {.async: (raises: []).} =
|
|
|
|
try:
|
|
|
|
await noCancel(conn.transp.shutdownWait())
|
|
|
|
except TransportError:
|
|
|
|
# We try to gracefully close connection, so we ignore any errors here,
|
|
|
|
# because right after this operation we closing connection.
|
|
|
|
discard
|
2023-07-19 17:33:28 +00:00
|
|
|
await conn.closeCb(conn)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc closeWait*(conn: HttpConnectionRef): Future[void] {.
|
|
|
|
async: (raw: true, raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
conn.closeCb(conn)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc closeWait*(req: HttpRequestRef) {.async: (raises: []).} =
|
2021-08-06 10:13:55 +00:00
|
|
|
if req.state == HttpState.Alive:
|
|
|
|
if req.response.isSome():
|
|
|
|
req.state = HttpState.Closing
|
|
|
|
let resp = req.response.get()
|
2021-09-14 17:32:58 +00:00
|
|
|
if (HttpResponseFlags.Stream in resp.flags) and not(isNil(resp.writer)):
|
2023-09-15 16:38:39 +00:00
|
|
|
await closeWait(resp.writer)
|
2023-08-09 07:57:49 +00:00
|
|
|
reset(resp[])
|
2023-07-14 10:35:08 +00:00
|
|
|
untrackCounter(HttpServerRequestTrackerName)
|
2023-08-09 07:57:49 +00:00
|
|
|
reset(req[])
|
2021-08-06 10:13:55 +00:00
|
|
|
req.state = HttpState.Closed
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2021-02-03 10:47:03 +00:00
|
|
|
proc createConnection(server: HttpServerRef,
|
|
|
|
transp: StreamTransport): Future[HttpConnectionRef] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: [CancelledError, HttpConnectionError]).} =
|
|
|
|
HttpConnectionRef.new(server, transp)
|
2021-02-03 10:47:03 +00:00
|
|
|
|
2021-09-14 17:32:58 +00:00
|
|
|
proc `keepalive=`*(resp: HttpResponseRef, value: bool) =
|
|
|
|
doAssert(resp.state == HttpResponseState.Empty)
|
|
|
|
if value:
|
|
|
|
resp.flags.incl(HttpResponseFlags.KeepAlive)
|
|
|
|
else:
|
|
|
|
resp.flags.excl(HttpResponseFlags.KeepAlive)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc keepalive*(resp: HttpResponseRef): bool =
|
2021-09-14 17:32:58 +00:00
|
|
|
HttpResponseFlags.KeepAlive in resp.flags
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getRemoteAddress(transp: StreamTransport): Opt[TransportAddress] =
|
2023-07-14 10:35:08 +00:00
|
|
|
if isNil(transp): return Opt.none(TransportAddress)
|
|
|
|
try:
|
|
|
|
Opt.some(transp.remoteAddress())
|
2023-11-21 10:01:44 +00:00
|
|
|
except TransportOsError:
|
2023-07-14 10:35:08 +00:00
|
|
|
Opt.none(TransportAddress)
|
2021-02-03 10:47:03 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getRemoteAddress(connection: HttpConnectionRef): Opt[TransportAddress] =
|
2023-07-14 10:35:08 +00:00
|
|
|
if isNil(connection): return Opt.none(TransportAddress)
|
|
|
|
getRemoteAddress(connection.transp)
|
|
|
|
|
|
|
|
proc getResponseFence*(connection: HttpConnectionRef,
|
|
|
|
reqFence: RequestFence): Future[ResponseFence] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: []).} =
|
2021-02-03 10:47:03 +00:00
|
|
|
try:
|
2023-07-14 10:35:08 +00:00
|
|
|
let res = await connection.server.processCallback(reqFence)
|
|
|
|
ResponseFence.ok(res)
|
2021-02-03 10:47:03 +00:00
|
|
|
except CancelledError:
|
2023-07-14 10:35:08 +00:00
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.InterruptError))
|
2021-02-03 10:47:03 +00:00
|
|
|
except HttpCriticalError as exc:
|
2023-07-14 10:35:08 +00:00
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CriticalError, exc, address, exc.code))
|
|
|
|
except HttpRecoverableError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.RecoverableError, exc, address, exc.code))
|
2023-11-28 16:57:13 +00:00
|
|
|
except HttpResponseError as exc:
|
|
|
|
# There should be only 2 children of HttpResponseError, and all of them
|
|
|
|
# should be handled.
|
|
|
|
raiseAssert "Unexpected response error " & $exc.name & ", reason: " &
|
|
|
|
$exc.msg
|
2023-07-14 10:35:08 +00:00
|
|
|
|
|
|
|
proc getResponseFence*(server: HttpServerRef,
|
|
|
|
connFence: ConnectionFence): Future[ResponseFence] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
doAssert(connFence.isErr())
|
|
|
|
try:
|
|
|
|
let
|
|
|
|
reqFence = RequestFence.err(connFence.error)
|
|
|
|
res = await server.processCallback(reqFence)
|
|
|
|
ResponseFence.ok(res)
|
|
|
|
except CancelledError:
|
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.InterruptError))
|
|
|
|
except HttpCriticalError as exc:
|
|
|
|
let address = Opt.none(TransportAddress)
|
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CriticalError, exc, address, exc.code))
|
|
|
|
except HttpRecoverableError as exc:
|
|
|
|
let address = Opt.none(TransportAddress)
|
|
|
|
ResponseFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.RecoverableError, exc, address, exc.code))
|
2023-11-28 16:57:13 +00:00
|
|
|
except HttpResponseError as exc:
|
|
|
|
# There should be only 2 children of HttpResponseError, and all of them
|
|
|
|
# should be handled.
|
|
|
|
raiseAssert "Unexpected response error " & $exc.name & ", reason: " &
|
|
|
|
$exc.msg
|
2023-07-14 10:35:08 +00:00
|
|
|
|
|
|
|
proc getRequestFence*(server: HttpServerRef,
|
|
|
|
connection: HttpConnectionRef): Future[RequestFence] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
try:
|
|
|
|
let res =
|
|
|
|
if server.headersTimeout.isInfinite():
|
|
|
|
await connection.getRequest()
|
2021-02-21 02:22:59 +00:00
|
|
|
else:
|
2023-07-14 10:35:08 +00:00
|
|
|
await connection.getRequest().wait(server.headersTimeout)
|
2023-08-09 07:57:49 +00:00
|
|
|
connection.currentRawQuery = Opt.some(res.rawPath)
|
2023-07-14 10:35:08 +00:00
|
|
|
RequestFence.ok(res)
|
|
|
|
except CancelledError:
|
|
|
|
RequestFence.err(HttpProcessError.init(HttpServerError.InterruptError))
|
|
|
|
except AsyncTimeoutError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
RequestFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.TimeoutError, exc, address, Http408))
|
|
|
|
except HttpRecoverableError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
RequestFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.RecoverableError, exc, address, exc.code))
|
|
|
|
except HttpCriticalError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
RequestFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CriticalError, exc, address, exc.code))
|
|
|
|
except HttpDisconnectError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
RequestFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.DisconnectError, exc, address, Http400))
|
|
|
|
except CatchableError as exc:
|
|
|
|
let address = connection.getRemoteAddress()
|
|
|
|
RequestFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CatchableError, exc, address, Http500))
|
|
|
|
|
|
|
|
proc getConnectionFence*(server: HttpServerRef,
|
|
|
|
transp: StreamTransport): Future[ConnectionFence] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
try:
|
|
|
|
let res = await server.createConnCallback(server, transp)
|
|
|
|
ConnectionFence.ok(res)
|
|
|
|
except CancelledError:
|
|
|
|
ConnectionFence.err(HttpProcessError.init(HttpServerError.InterruptError))
|
|
|
|
except HttpCriticalError as exc:
|
2023-08-09 07:57:49 +00:00
|
|
|
# On error `transp` will be closed by `createConnCallback()` call.
|
|
|
|
let address = Opt.none(TransportAddress)
|
2023-07-14 10:35:08 +00:00
|
|
|
ConnectionFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CriticalError, exc, address, exc.code))
|
2023-08-09 07:57:49 +00:00
|
|
|
except CatchableError as exc:
|
|
|
|
# On error `transp` will be closed by `createConnCallback()` call.
|
|
|
|
let address = Opt.none(TransportAddress)
|
|
|
|
ConnectionFence.err(HttpProcessError.init(
|
|
|
|
HttpServerError.CriticalError, exc, address, Http503))
|
2023-07-14 10:35:08 +00:00
|
|
|
|
|
|
|
proc processRequest(server: HttpServerRef,
|
|
|
|
connection: HttpConnectionRef,
|
2023-11-21 10:01:44 +00:00
|
|
|
connId: string): Future[HttpProcessExitType] {.
|
|
|
|
async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
let requestFence = await getRequestFence(server, connection)
|
|
|
|
if requestFence.isErr():
|
|
|
|
case requestFence.error.kind
|
|
|
|
of HttpServerError.InterruptError:
|
2023-07-19 17:33:28 +00:00
|
|
|
return HttpProcessExitType.Immediate
|
2023-07-14 10:35:08 +00:00
|
|
|
of HttpServerError.DisconnectError:
|
|
|
|
if HttpServerFlags.NotifyDisconnect notin server.flags:
|
2023-07-19 17:33:28 +00:00
|
|
|
return HttpProcessExitType.Immediate
|
2023-07-14 10:35:08 +00:00
|
|
|
else:
|
|
|
|
discard
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
let responseFence = await getResponseFence(connection, requestFence)
|
|
|
|
if responseFence.isErr() and
|
|
|
|
(responseFence.error.kind == HttpServerError.InterruptError):
|
2023-08-09 07:57:49 +00:00
|
|
|
if requestFence.isOk():
|
|
|
|
await requestFence.get().closeWait()
|
2023-07-19 17:33:28 +00:00
|
|
|
return HttpProcessExitType.Immediate
|
2021-08-06 10:13:55 +00:00
|
|
|
|
2023-08-09 07:57:49 +00:00
|
|
|
let res =
|
|
|
|
if responseFence.isErr():
|
|
|
|
await connection.sendErrorResponse(requestFence, responseFence.error)
|
|
|
|
else:
|
|
|
|
await connection.sendDefaultResponse(requestFence, responseFence.get())
|
|
|
|
|
|
|
|
if requestFence.isOk():
|
|
|
|
await requestFence.get().closeWait()
|
|
|
|
|
|
|
|
res
|
2023-07-14 10:35:08 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc processLoop(holder: HttpConnectionHolderRef) {.async: (raises: []).} =
|
2023-07-14 10:35:08 +00:00
|
|
|
let
|
|
|
|
server = holder.server
|
|
|
|
transp = holder.transp
|
|
|
|
connectionId = holder.connectionId
|
|
|
|
connection =
|
|
|
|
block:
|
|
|
|
let res = await server.getConnectionFence(transp)
|
|
|
|
if res.isErr():
|
|
|
|
if res.error.kind != HttpServerError.InterruptError:
|
|
|
|
discard await server.getResponseFence(res)
|
|
|
|
server.connections.del(connectionId)
|
|
|
|
return
|
|
|
|
res.get()
|
2021-01-31 05:57:58 +00:00
|
|
|
|
2023-07-14 10:35:08 +00:00
|
|
|
holder.connection = connection
|
2021-01-28 03:37:16 +00:00
|
|
|
|
2023-07-19 17:33:28 +00:00
|
|
|
var runLoop = HttpProcessExitType.KeepAlive
|
|
|
|
while runLoop == HttpProcessExitType.KeepAlive:
|
2023-08-09 07:57:49 +00:00
|
|
|
runLoop =
|
|
|
|
try:
|
|
|
|
await server.processRequest(connection, connectionId)
|
|
|
|
except CancelledError:
|
|
|
|
HttpProcessExitType.Immediate
|
|
|
|
except CatchableError as exc:
|
|
|
|
raiseAssert "Unexpected error [" & $exc.name & "] happens: " & $exc.msg
|
|
|
|
|
|
|
|
case runLoop
|
|
|
|
of HttpProcessExitType.KeepAlive:
|
|
|
|
await connection.closeWait()
|
|
|
|
of HttpProcessExitType.Immediate:
|
|
|
|
await connection.closeWait()
|
|
|
|
of HttpProcessExitType.Graceful:
|
|
|
|
await connection.gracefulCloseWait()
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-09-15 16:38:39 +00:00
|
|
|
server.connections.del(connectionId)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc acceptClientLoop(server: HttpServerRef) {.async: (raises: []).} =
|
2023-08-09 07:57:49 +00:00
|
|
|
var runLoop = true
|
|
|
|
while runLoop:
|
2021-01-27 06:14:17 +00:00
|
|
|
try:
|
|
|
|
# if server.maxConnections > 0:
|
|
|
|
# await server.semaphore.acquire()
|
|
|
|
let transp = await server.instance.accept()
|
2021-11-12 16:13:56 +00:00
|
|
|
let resId = transp.getId()
|
|
|
|
if resId.isErr():
|
|
|
|
# We are unable to identify remote peer, it means that remote peer
|
|
|
|
# disconnected before identification.
|
|
|
|
await transp.closeWait()
|
2023-08-09 07:57:49 +00:00
|
|
|
runLoop = false
|
2021-11-12 16:13:56 +00:00
|
|
|
else:
|
|
|
|
let connId = resId.get()
|
2023-07-14 10:35:08 +00:00
|
|
|
let holder = HttpConnectionHolderRef.new(server, transp, resId.get())
|
|
|
|
server.connections[connId] = holder
|
|
|
|
holder.future = processLoop(holder)
|
2023-08-09 07:57:49 +00:00
|
|
|
except TransportTooManyError, TransportAbortedError:
|
|
|
|
# Non-critical error
|
2023-07-14 10:35:08 +00:00
|
|
|
discard
|
2023-08-09 07:57:49 +00:00
|
|
|
except CancelledError, TransportOsError, CatchableError:
|
|
|
|
# Critical, cancellation or unexpected error
|
|
|
|
runLoop = false
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc state*(server: HttpServerRef): HttpServerState =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Returns current HTTP server's state.
|
|
|
|
if server.lifetime.finished():
|
|
|
|
ServerClosed
|
|
|
|
else:
|
|
|
|
if isNil(server.acceptLoop):
|
|
|
|
ServerStopped
|
|
|
|
else:
|
|
|
|
if server.acceptLoop.finished():
|
|
|
|
ServerStopped
|
|
|
|
else:
|
|
|
|
ServerRunning
|
|
|
|
|
2021-02-01 16:04:38 +00:00
|
|
|
proc start*(server: HttpServerRef) =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Starts HTTP server.
|
|
|
|
if server.state == ServerStopped:
|
|
|
|
server.acceptLoop = acceptClientLoop(server)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc stop*(server: HttpServerRef) {.async: (raises: []).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Stop HTTP server from accepting new connections.
|
|
|
|
if server.state == ServerRunning:
|
|
|
|
await server.acceptLoop.cancelAndWait()
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc drop*(server: HttpServerRef) {.async: (raises: []).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Drop all pending HTTP connections.
|
2021-02-19 12:07:20 +00:00
|
|
|
var pending: seq[Future[void]]
|
2021-01-27 06:14:17 +00:00
|
|
|
if server.state in {ServerStopped, ServerRunning}:
|
2023-07-14 10:35:08 +00:00
|
|
|
for holder in server.connections.values():
|
|
|
|
if not(isNil(holder.future)) and not(holder.future.finished()):
|
|
|
|
pending.add(holder.future.cancelAndWait())
|
2023-09-15 16:38:39 +00:00
|
|
|
await noCancel(allFutures(pending))
|
2023-07-14 10:35:08 +00:00
|
|
|
server.connections.clear()
|
2021-01-27 06:14:17 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc closeWait*(server: HttpServerRef) {.async: (raises: []).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Stop HTTP server and drop all the pending connections.
|
|
|
|
if server.state != ServerClosed:
|
|
|
|
await server.stop()
|
|
|
|
await server.drop()
|
|
|
|
await server.instance.closeWait()
|
|
|
|
server.lifetime.complete()
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc join*(server: HttpServerRef): Future[void] {.
|
|
|
|
async: (raw: true, raises: [CancelledError]).} =
|
2021-01-27 06:14:17 +00:00
|
|
|
## Wait until HTTP server will not be closed.
|
|
|
|
var retFuture = newFuture[void]("http.server.join")
|
|
|
|
|
|
|
|
proc continuation(udata: pointer) {.gcsafe.} =
|
|
|
|
if not(retFuture.finished()):
|
|
|
|
retFuture.complete()
|
|
|
|
|
|
|
|
proc cancellation(udata: pointer) {.gcsafe.} =
|
|
|
|
if not(retFuture.finished()):
|
|
|
|
server.lifetime.removeCallback(continuation, cast[pointer](retFuture))
|
|
|
|
|
|
|
|
if server.state == ServerClosed:
|
|
|
|
retFuture.complete()
|
|
|
|
else:
|
|
|
|
server.lifetime.addCallback(continuation, cast[pointer](retFuture))
|
|
|
|
retFuture.cancelCallback = cancellation
|
|
|
|
|
|
|
|
retFuture
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc getMultipartReader*(req: HttpRequestRef): HttpResult[MultiPartReaderRef] =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Create new MultiPartReader interface for specific request.
|
|
|
|
if req.meth in PostMethods:
|
|
|
|
if MultipartForm in req.requestFlags:
|
2022-08-05 16:59:26 +00:00
|
|
|
if req.contentTypeData.isSome():
|
|
|
|
let boundary = ? getMultipartBoundary(req.contentTypeData.get())
|
2021-02-10 08:43:05 +00:00
|
|
|
var stream = ? req.getBodyReader()
|
2021-02-01 16:04:38 +00:00
|
|
|
ok(MultiPartReaderRef.new(stream, boundary))
|
2022-08-05 16:59:26 +00:00
|
|
|
else:
|
|
|
|
err("Content type is missing or invalid")
|
2021-02-01 16:04:38 +00:00
|
|
|
else:
|
|
|
|
err("Request's data is not multipart encoded")
|
|
|
|
else:
|
|
|
|
err("Request's method do not supports multipart")
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc post*(req: HttpRequestRef): Future[HttpTable] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-01-30 18:18:06 +00:00
|
|
|
## Return POST parameters
|
|
|
|
if req.postTable.isSome():
|
|
|
|
return req.postTable.get()
|
|
|
|
else:
|
|
|
|
if req.meth notin PostMethods:
|
|
|
|
return HttpTable.init()
|
|
|
|
|
|
|
|
if UrlencodedForm in req.requestFlags:
|
2021-05-17 19:39:24 +00:00
|
|
|
let queryFlags =
|
|
|
|
if QueryCommaSeparatedArray in req.connection.server.flags:
|
|
|
|
{QueryParamsFlag.CommaSeparatedArray}
|
|
|
|
else:
|
|
|
|
{}
|
2021-01-30 18:18:06 +00:00
|
|
|
var table = HttpTable.init()
|
2021-01-31 05:57:58 +00:00
|
|
|
# getBody() will handle `Expect`.
|
2021-01-30 18:18:06 +00:00
|
|
|
var body = await req.getBody()
|
2021-02-10 08:43:05 +00:00
|
|
|
# TODO (cheatfate) double copy here, because of `byte` to `char`
|
|
|
|
# conversion.
|
2021-01-30 18:18:06 +00:00
|
|
|
var strbody = newString(len(body))
|
|
|
|
if len(body) > 0:
|
|
|
|
copyMem(addr strbody[0], addr body[0], len(body))
|
2021-05-17 19:39:24 +00:00
|
|
|
for key, value in queryParams(strbody, queryFlags):
|
2021-01-30 18:18:06 +00:00
|
|
|
table.add(key, value)
|
2022-09-05 11:08:52 +00:00
|
|
|
req.postTable = Opt.some(table)
|
2021-01-30 18:18:06 +00:00
|
|
|
return table
|
|
|
|
elif MultipartForm in req.requestFlags:
|
|
|
|
var table = HttpTable.init()
|
2021-02-01 16:04:38 +00:00
|
|
|
let res = getMultipartReader(req)
|
|
|
|
if res.isErr():
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Unable to retrieve multipart form data")
|
2021-02-01 16:04:38 +00:00
|
|
|
var mpreader = res.get()
|
|
|
|
|
|
|
|
# We must handle `Expect` first.
|
|
|
|
try:
|
|
|
|
await req.handleExpect()
|
2021-02-02 10:48:04 +00:00
|
|
|
except CancelledError as exc:
|
2021-02-10 08:43:05 +00:00
|
|
|
await mpreader.closeWait()
|
2021-02-02 10:48:04 +00:00
|
|
|
raise exc
|
2021-02-01 16:04:38 +00:00
|
|
|
except HttpCriticalError as exc:
|
2021-02-10 08:43:05 +00:00
|
|
|
await mpreader.closeWait()
|
2021-02-01 16:04:38 +00:00
|
|
|
raise exc
|
|
|
|
|
|
|
|
# Reading multipart/form-data parts.
|
2021-01-30 18:18:06 +00:00
|
|
|
var runLoop = true
|
|
|
|
while runLoop:
|
2021-02-02 10:48:04 +00:00
|
|
|
var part: MultiPart
|
2021-01-30 18:18:06 +00:00
|
|
|
try:
|
2021-02-02 10:48:04 +00:00
|
|
|
part = await mpreader.readPart()
|
2021-02-01 16:04:38 +00:00
|
|
|
var value = await part.getBody()
|
2021-02-10 08:43:05 +00:00
|
|
|
# TODO (cheatfate) double copy here, because of `byte` to `char`
|
|
|
|
# conversion.
|
2021-01-30 18:18:06 +00:00
|
|
|
var strvalue = newString(len(value))
|
|
|
|
if len(value) > 0:
|
|
|
|
copyMem(addr strvalue[0], addr value[0], len(value))
|
2021-02-01 16:04:38 +00:00
|
|
|
table.add(part.name, strvalue)
|
2021-02-10 08:43:05 +00:00
|
|
|
await part.closeWait()
|
|
|
|
except MultipartEOMError:
|
2021-01-30 18:18:06 +00:00
|
|
|
runLoop = false
|
2021-02-10 08:43:05 +00:00
|
|
|
except HttpCriticalError as exc:
|
2021-02-02 10:48:04 +00:00
|
|
|
if not(part.isEmpty()):
|
2021-02-10 08:43:05 +00:00
|
|
|
await part.closeWait()
|
|
|
|
await mpreader.closeWait()
|
2021-02-02 10:48:04 +00:00
|
|
|
raise exc
|
2021-02-10 08:43:05 +00:00
|
|
|
except CancelledError as exc:
|
2021-02-02 10:48:04 +00:00
|
|
|
if not(part.isEmpty()):
|
2021-02-10 08:43:05 +00:00
|
|
|
await part.closeWait()
|
|
|
|
await mpreader.closeWait()
|
2021-02-02 10:48:04 +00:00
|
|
|
raise exc
|
2021-02-10 08:43:05 +00:00
|
|
|
await mpreader.closeWait()
|
2022-09-05 11:08:52 +00:00
|
|
|
req.postTable = Opt.some(table)
|
2021-01-30 18:18:06 +00:00
|
|
|
return table
|
|
|
|
else:
|
2021-01-31 05:57:58 +00:00
|
|
|
if HttpRequestFlags.BoundBody in req.requestFlags:
|
2021-01-30 18:18:06 +00:00
|
|
|
if req.contentLength != 0:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Unsupported request body")
|
2021-01-30 18:18:06 +00:00
|
|
|
return HttpTable.init()
|
2021-01-31 05:57:58 +00:00
|
|
|
elif HttpRequestFlags.UnboundBody in req.requestFlags:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Unsupported request body")
|
2021-01-30 18:18:06 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc setHeader*(resp: HttpResponseRef, key, value: string) =
|
2021-03-11 11:41:13 +00:00
|
|
|
## Sets value of header ``key`` to ``value``.
|
2021-02-01 16:04:38 +00:00
|
|
|
doAssert(resp.state == HttpResponseState.Empty)
|
|
|
|
resp.headersTable.set(key, value)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc setHeaderDefault*(resp: HttpResponseRef, key, value: string) =
|
2021-03-11 11:41:13 +00:00
|
|
|
## Sets value of header ``key`` to ``value``, only if header ``key`` is not
|
|
|
|
## present in the headers table.
|
|
|
|
discard resp.headersTable.hasKeyOrPut(key, value)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc addHeader*(resp: HttpResponseRef, key, value: string) =
|
2021-03-11 11:41:13 +00:00
|
|
|
## Adds value ``value`` to header's ``key`` value.
|
2021-02-01 16:04:38 +00:00
|
|
|
doAssert(resp.state == HttpResponseState.Empty)
|
|
|
|
resp.headersTable.add(key, value)
|
|
|
|
|
|
|
|
proc getHeader*(resp: HttpResponseRef, key: string,
|
2023-11-21 10:01:44 +00:00
|
|
|
default: string = ""): string =
|
2021-03-11 11:41:13 +00:00
|
|
|
## Returns value of header with name ``name`` or ``default``, if header is
|
|
|
|
## not present in the table.
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.headersTable.getString(key, default)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc hasHeader*(resp: HttpResponseRef, key: string): bool =
|
2021-03-11 11:41:13 +00:00
|
|
|
## Returns ``true`` if header with name ``key`` present in the headers table.
|
2021-02-01 16:04:38 +00:00
|
|
|
key in resp.headersTable
|
|
|
|
|
|
|
|
template checkPending(t: untyped) =
|
|
|
|
if t.state != HttpResponseState.Empty:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response body was already sent")
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2021-09-14 17:32:58 +00:00
|
|
|
func createHeaders(resp: HttpResponseRef): string =
|
|
|
|
var answer = $(resp.version) & " " & $(resp.status) & "\r\n"
|
|
|
|
for k, v in resp.headersTable.stringItems():
|
|
|
|
if len(v) > 0:
|
|
|
|
answer.add(normalizeHeaderName(k))
|
|
|
|
answer.add(": ")
|
|
|
|
answer.add(v)
|
|
|
|
answer.add("\r\n")
|
|
|
|
answer.add("\r\n")
|
|
|
|
answer
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc prepareLengthHeaders(resp: HttpResponseRef, length: int): string =
|
2021-05-10 07:26:36 +00:00
|
|
|
if not(resp.hasHeader(DateHeader)):
|
|
|
|
resp.setHeader(DateHeader, httpDate())
|
2022-09-20 12:59:34 +00:00
|
|
|
if length > 0:
|
|
|
|
if not(resp.hasHeader(ContentTypeHeader)):
|
|
|
|
resp.setHeader(ContentTypeHeader, "text/html; charset=utf-8")
|
2021-05-10 07:26:36 +00:00
|
|
|
if not(resp.hasHeader(ContentLengthHeader)):
|
|
|
|
resp.setHeader(ContentLengthHeader, Base10.toString(uint64(length)))
|
|
|
|
if not(resp.hasHeader(ServerHeader)):
|
|
|
|
resp.setHeader(ServerHeader, resp.connection.server.serverIdent)
|
|
|
|
if not(resp.hasHeader(ConnectionHeader)):
|
2021-09-14 17:32:58 +00:00
|
|
|
if HttpResponseFlags.KeepAlive in resp.flags:
|
2021-05-10 07:26:36 +00:00
|
|
|
resp.setHeader(ConnectionHeader, "keep-alive")
|
2021-02-01 16:04:38 +00:00
|
|
|
else:
|
2021-05-10 07:26:36 +00:00
|
|
|
resp.setHeader(ConnectionHeader, "close")
|
2021-09-14 17:32:58 +00:00
|
|
|
resp.createHeaders()
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc prepareChunkedHeaders(resp: HttpResponseRef): string =
|
2021-05-10 07:26:36 +00:00
|
|
|
if not(resp.hasHeader(DateHeader)):
|
|
|
|
resp.setHeader(DateHeader, httpDate())
|
|
|
|
if not(resp.hasHeader(ContentTypeHeader)):
|
|
|
|
resp.setHeader(ContentTypeHeader, "text/html; charset=utf-8")
|
|
|
|
if not(resp.hasHeader(TransferEncodingHeader)):
|
|
|
|
resp.setHeader(TransferEncodingHeader, "chunked")
|
|
|
|
if not(resp.hasHeader(ServerHeader)):
|
|
|
|
resp.setHeader(ServerHeader, resp.connection.server.serverIdent)
|
|
|
|
if not(resp.hasHeader(ConnectionHeader)):
|
2021-09-14 17:32:58 +00:00
|
|
|
if HttpResponseFlags.KeepAlive in resp.flags:
|
2021-05-10 07:26:36 +00:00
|
|
|
resp.setHeader(ConnectionHeader, "keep-alive")
|
2021-02-01 16:04:38 +00:00
|
|
|
else:
|
2021-05-10 07:26:36 +00:00
|
|
|
resp.setHeader(ConnectionHeader, "close")
|
2021-09-14 17:32:58 +00:00
|
|
|
resp.createHeaders()
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc prepareServerSideEventHeaders(resp: HttpResponseRef): string =
|
2021-09-14 17:32:58 +00:00
|
|
|
if not(resp.hasHeader(DateHeader)):
|
|
|
|
resp.setHeader(DateHeader, httpDate())
|
|
|
|
if not(resp.hasHeader(ContentTypeHeader)):
|
|
|
|
resp.setHeader(ContentTypeHeader, "text/event-stream")
|
|
|
|
if not(resp.hasHeader(ServerHeader)):
|
|
|
|
resp.setHeader(ServerHeader, resp.connection.server.serverIdent)
|
|
|
|
if not(resp.hasHeader(ConnectionHeader)):
|
|
|
|
resp.flags.excl(HttpResponseFlags.KeepAlive)
|
|
|
|
resp.setHeader(ConnectionHeader, "close")
|
|
|
|
resp.createHeaders()
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc preparePlainHeaders(resp: HttpResponseRef): string =
|
2021-09-14 17:32:58 +00:00
|
|
|
if not(resp.hasHeader(DateHeader)):
|
|
|
|
resp.setHeader(DateHeader, httpDate())
|
|
|
|
if not(resp.hasHeader(ServerHeader)):
|
|
|
|
resp.setHeader(ServerHeader, resp.connection.server.serverIdent)
|
|
|
|
if not(resp.hasHeader(ConnectionHeader)):
|
|
|
|
resp.flags.excl(HttpResponseFlags.KeepAlive)
|
|
|
|
resp.setHeader(ConnectionHeader, "close")
|
|
|
|
resp.createHeaders()
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc sendBody*(resp: HttpResponseRef, pbytes: pointer, nbytes: int) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Send HTTP response at once by using bytes pointer ``pbytes`` and length
|
|
|
|
## ``nbytes``.
|
|
|
|
doAssert(not(isNil(pbytes)), "pbytes must not be nil")
|
|
|
|
doAssert(nbytes >= 0, "nbytes should be bigger or equal to zero")
|
|
|
|
checkPending(resp)
|
|
|
|
let responseHeaders = resp.prepareLengthHeaders(nbytes)
|
|
|
|
resp.state = HttpResponseState.Prepared
|
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(responseHeaders)
|
2021-02-01 16:04:38 +00:00
|
|
|
if nbytes > 0:
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(pbytes, nbytes)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Finished
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc sendBody*(resp: HttpResponseRef, data: ByteChar) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Send HTTP response at once by using data ``data``.
|
|
|
|
checkPending(resp)
|
|
|
|
let responseHeaders = resp.prepareLengthHeaders(len(data))
|
|
|
|
resp.state = HttpResponseState.Prepared
|
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(responseHeaders)
|
2021-02-01 16:04:38 +00:00
|
|
|
if len(data) > 0:
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(data)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Finished
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc sendError*(resp: HttpResponseRef, code: HttpCode, body = "") {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Send HTTP error status response.
|
|
|
|
checkPending(resp)
|
|
|
|
resp.status = code
|
|
|
|
let responseHeaders = resp.prepareLengthHeaders(len(body))
|
|
|
|
resp.state = HttpResponseState.Prepared
|
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(responseHeaders)
|
2021-02-01 16:04:38 +00:00
|
|
|
if len(body) > 0:
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(body)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Finished
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2021-09-14 17:32:58 +00:00
|
|
|
proc prepare*(resp: HttpResponseRef,
|
2023-11-21 10:01:44 +00:00
|
|
|
streamType = HttpResponseStreamType.Chunked) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Prepare for HTTP stream response.
|
|
|
|
##
|
|
|
|
## Such responses will be sent chunk by chunk using ``chunked`` encoding.
|
|
|
|
resp.checkPending()
|
2021-09-14 17:32:58 +00:00
|
|
|
let responseHeaders =
|
|
|
|
case streamType
|
|
|
|
of HttpResponseStreamType.Plain:
|
|
|
|
resp.preparePlainHeaders()
|
|
|
|
of HttpResponseStreamType.SSE:
|
|
|
|
resp.prepareServerSideEventHeaders()
|
|
|
|
of HttpResponseStreamType.Chunked:
|
|
|
|
resp.prepareChunkedHeaders()
|
|
|
|
resp.streamType = streamType
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Prepared
|
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-02-03 10:47:03 +00:00
|
|
|
await resp.connection.writer.write(responseHeaders)
|
2021-09-14 17:32:58 +00:00
|
|
|
case streamType
|
|
|
|
of HttpResponseStreamType.Plain, HttpResponseStreamType.SSE:
|
|
|
|
resp.writer = newAsyncStreamWriter(resp.connection.writer)
|
|
|
|
of HttpResponseStreamType.Chunked:
|
|
|
|
resp.writer = newChunkedStreamWriter(resp.connection.writer)
|
|
|
|
resp.flags.incl(HttpResponseFlags.Stream)
|
2021-02-01 16:04:38 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc prepareChunked*(resp: HttpResponseRef): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
## Prepare for HTTP chunked stream response.
|
|
|
|
##
|
|
|
|
## Such responses will be sent chunk by chunk using ``chunked`` encoding.
|
|
|
|
resp.prepare(HttpResponseStreamType.Chunked)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc preparePlain*(resp: HttpResponseRef): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
## Prepare for HTTP plain stream response.
|
|
|
|
##
|
|
|
|
## Such responses will be sent without any encoding.
|
|
|
|
resp.prepare(HttpResponseStreamType.Plain)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc prepareSSE*(resp: HttpResponseRef): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
## Prepare for HTTP server-side event stream response.
|
|
|
|
resp.prepare(HttpResponseStreamType.SSE)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc send*(resp: HttpResponseRef, pbytes: pointer, nbytes: int) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Send single chunk of data pointed by ``pbytes`` and ``nbytes``.
|
|
|
|
doAssert(not(isNil(pbytes)), "pbytes must not be nil")
|
|
|
|
doAssert(nbytes >= 0, "nbytes should be bigger or equal to zero")
|
2021-09-14 17:32:58 +00:00
|
|
|
if HttpResponseFlags.Stream notin resp.flags:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response was not prepared")
|
2021-02-01 16:04:38 +00:00
|
|
|
if resp.state notin {HttpResponseState.Prepared, HttpResponseState.Sending}:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response in incorrect state")
|
2021-02-01 16:04:38 +00:00
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-09-14 17:32:58 +00:00
|
|
|
await resp.writer.write(pbytes, nbytes)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Sending
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc send*(resp: HttpResponseRef, data: ByteChar) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Send single chunk of data ``data``.
|
2021-09-14 17:32:58 +00:00
|
|
|
if HttpResponseFlags.Stream notin resp.flags:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response was not prepared")
|
2021-02-01 16:04:38 +00:00
|
|
|
if resp.state notin {HttpResponseState.Prepared, HttpResponseState.Sending}:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response in incorrect state")
|
2021-02-01 16:04:38 +00:00
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-09-14 17:32:58 +00:00
|
|
|
await resp.writer.write(data)
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Sending
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-02-01 16:04:38 +00:00
|
|
|
|
2021-09-14 17:32:58 +00:00
|
|
|
proc sendChunk*(resp: HttpResponseRef, pbytes: pointer,
|
2023-11-21 10:01:44 +00:00
|
|
|
nbytes: int): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
resp.send(pbytes, nbytes)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc sendChunk*(resp: HttpResponseRef, data: ByteChar): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
resp.send(data)
|
|
|
|
|
|
|
|
proc sendEvent*(resp: HttpResponseRef, eventName: string,
|
2023-11-21 10:01:44 +00:00
|
|
|
data: string): Future[void] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-09-14 17:32:58 +00:00
|
|
|
## Send server-side event with name ``eventName`` and payload ``data`` to
|
|
|
|
## remote peer.
|
|
|
|
let data =
|
|
|
|
block:
|
|
|
|
var res = ""
|
|
|
|
if len(eventName) > 0:
|
|
|
|
res.add("event: ")
|
|
|
|
res.add(eventName)
|
|
|
|
res.add("\r\n")
|
|
|
|
res.add("data: ")
|
|
|
|
res.add(data)
|
|
|
|
res.add("\r\n\r\n")
|
|
|
|
res
|
|
|
|
resp.send(data)
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc finish*(resp: HttpResponseRef) {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-02-01 16:04:38 +00:00
|
|
|
## Sending last chunk of data, so it will indicate end of HTTP response.
|
2021-09-14 17:32:58 +00:00
|
|
|
if HttpResponseFlags.Stream notin resp.flags:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response was not prepared")
|
2021-02-01 16:04:38 +00:00
|
|
|
if resp.state notin {HttpResponseState.Prepared, HttpResponseState.Sending}:
|
2021-02-17 00:03:12 +00:00
|
|
|
raiseHttpCriticalError("Response in incorrect state")
|
2021-02-01 16:04:38 +00:00
|
|
|
try:
|
|
|
|
resp.state = HttpResponseState.Sending
|
2021-09-14 17:32:58 +00:00
|
|
|
await resp.writer.finish()
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Finished
|
|
|
|
except CancelledError as exc:
|
|
|
|
resp.state = HttpResponseState.Cancelled
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-02-01 16:04:38 +00:00
|
|
|
resp.state = HttpResponseState.Failed
|
2023-11-21 10:01:44 +00:00
|
|
|
raiseHttpCriticalError("Unable to send response, reason: " & $exc.msg)
|
2021-01-31 05:57:58 +00:00
|
|
|
|
2021-03-02 13:26:07 +00:00
|
|
|
proc respond*(req: HttpRequestRef, code: HttpCode, content: ByteChar,
|
2023-11-21 10:01:44 +00:00
|
|
|
headers: HttpTable): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raises: [CancelledError, HttpCriticalError]).} =
|
2021-03-02 13:26:07 +00:00
|
|
|
## Responds to the request with the specified ``HttpCode``, HTTP ``headers``
|
|
|
|
## and ``content``.
|
2021-02-02 22:33:14 +00:00
|
|
|
let response = req.getResponse()
|
|
|
|
response.status = code
|
|
|
|
for k, v in headers.stringItems():
|
|
|
|
response.addHeader(k, v)
|
|
|
|
await response.sendBody(content)
|
2023-11-21 10:01:44 +00:00
|
|
|
response
|
2021-02-02 22:33:14 +00:00
|
|
|
|
2021-03-02 13:26:07 +00:00
|
|
|
proc respond*(req: HttpRequestRef, code: HttpCode,
|
2023-11-21 10:01:44 +00:00
|
|
|
content: ByteChar): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-03-02 13:26:07 +00:00
|
|
|
## Responds to the request with specified ``HttpCode`` and ``content``.
|
|
|
|
respond(req, code, content, HttpTable.init())
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc respond*(req: HttpRequestRef, code: HttpCode): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-05-10 07:26:36 +00:00
|
|
|
## Responds to the request with specified ``HttpCode`` only.
|
2021-03-02 13:26:07 +00:00
|
|
|
respond(req, code, "", HttpTable.init())
|
|
|
|
|
2022-06-17 09:17:49 +00:00
|
|
|
proc redirect*(req: HttpRequestRef, code: HttpCode,
|
2023-11-21 10:01:44 +00:00
|
|
|
location: string, headers: HttpTable): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2022-06-17 09:17:49 +00:00
|
|
|
## Responds to the request with redirection to location ``location`` and
|
|
|
|
## additional headers ``headers``.
|
|
|
|
##
|
|
|
|
## Note, ``location`` argument's value has priority over "Location" header's
|
|
|
|
## value in ``headers`` argument.
|
|
|
|
var mheaders = headers
|
|
|
|
mheaders.set("location", location)
|
|
|
|
respond(req, code, "", mheaders)
|
|
|
|
|
|
|
|
proc redirect*(req: HttpRequestRef, code: HttpCode,
|
2023-11-21 10:01:44 +00:00
|
|
|
location: Uri, headers: HttpTable): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2022-06-17 09:17:49 +00:00
|
|
|
## Responds to the request with redirection to location ``location`` and
|
|
|
|
## additional headers ``headers``.
|
|
|
|
##
|
|
|
|
## Note, ``location`` argument's value has priority over "Location" header's
|
|
|
|
## value in ``headers`` argument.
|
|
|
|
redirect(req, code, $location, headers)
|
|
|
|
|
2021-05-10 07:26:36 +00:00
|
|
|
proc redirect*(req: HttpRequestRef, code: HttpCode,
|
2023-11-21 10:01:44 +00:00
|
|
|
location: Uri): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-05-10 07:26:36 +00:00
|
|
|
## Responds to the request with redirection to location ``location``.
|
2022-06-17 09:17:49 +00:00
|
|
|
redirect(req, code, location, HttpTable.init())
|
2021-05-10 07:26:36 +00:00
|
|
|
|
|
|
|
proc redirect*(req: HttpRequestRef, code: HttpCode,
|
2023-11-21 10:01:44 +00:00
|
|
|
location: string): Future[HttpResponseRef] {.
|
2023-11-28 16:57:13 +00:00
|
|
|
async: (raw: true, raises: [CancelledError, HttpCriticalError]).} =
|
2021-05-10 07:26:36 +00:00
|
|
|
## Responds to the request with redirection to location ``location``.
|
2022-06-17 09:17:49 +00:00
|
|
|
redirect(req, code, location, HttpTable.init())
|
2021-05-10 07:26:36 +00:00
|
|
|
|
2021-03-02 13:26:07 +00:00
|
|
|
proc responded*(req: HttpRequestRef): bool =
|
|
|
|
## Returns ``true`` if request ``req`` has been responded or responding.
|
|
|
|
if isSome(req.response):
|
|
|
|
if req.response.get().state == HttpResponseState.Empty:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc remoteAddress*(conn: HttpConnectionRef): TransportAddress {.
|
|
|
|
raises: [HttpAddressError].} =
|
2021-02-21 15:34:55 +00:00
|
|
|
## Returns address of the remote host that established connection ``conn``.
|
2023-11-21 10:01:44 +00:00
|
|
|
try:
|
|
|
|
conn.transp.remoteAddress()
|
|
|
|
except TransportOsError as exc:
|
|
|
|
raiseHttpAddressError($exc.msg)
|
2021-02-21 15:34:55 +00:00
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc remoteAddress*(request: HttpRequestRef): TransportAddress {.
|
|
|
|
raises: [HttpAddressError].} =
|
2021-02-21 15:34:55 +00:00
|
|
|
## Returns address of the remote host that made request ``request``.
|
|
|
|
request.connection.remoteAddress()
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc requestInfo*(req: HttpRequestRef, contentType = "text/text"): string =
|
2021-02-02 22:33:14 +00:00
|
|
|
## Returns comprehensive information about request for specific content
|
|
|
|
## type.
|
|
|
|
##
|
|
|
|
## Only two content-types are supported: "text/text" and "text/html".
|
2021-02-02 10:48:04 +00:00
|
|
|
proc h(t: string): string =
|
|
|
|
case contentType
|
|
|
|
of "text/text":
|
|
|
|
"\r\n" & t & " ===\r\n"
|
|
|
|
of "text/html":
|
|
|
|
"<h3>" & t & "</h3>"
|
2021-01-30 18:18:06 +00:00
|
|
|
else:
|
2021-02-02 10:48:04 +00:00
|
|
|
t
|
|
|
|
proc kv(k, v: string): string =
|
|
|
|
case contentType
|
|
|
|
of "text/text":
|
|
|
|
k & ": " & v & "\r\n"
|
|
|
|
of "text/html":
|
|
|
|
"<div><code><b>" & k & "</b></code><code>" & v & "</code></div>"
|
|
|
|
else:
|
|
|
|
k & ": " & v
|
|
|
|
|
|
|
|
let header =
|
|
|
|
case contentType
|
|
|
|
of "text/html":
|
|
|
|
"<html><head><title>Request Information</title>" &
|
|
|
|
"<style>code {padding-left: 30px;}</style>" &
|
|
|
|
"</head><body>"
|
|
|
|
else:
|
|
|
|
""
|
|
|
|
|
|
|
|
let footer =
|
|
|
|
case contentType
|
|
|
|
of "text/html":
|
|
|
|
"</body></html>"
|
|
|
|
else:
|
|
|
|
""
|
|
|
|
|
|
|
|
var res = h("Request information")
|
|
|
|
res.add(kv("request.scheme", $req.scheme))
|
|
|
|
res.add(kv("request.method", $req.meth))
|
|
|
|
res.add(kv("request.version", $req.version))
|
|
|
|
res.add(kv("request.uri", $req.uri))
|
|
|
|
res.add(kv("request.flags", $req.requestFlags))
|
|
|
|
res.add(kv("request.TransferEncoding", $req.transferEncoding))
|
|
|
|
res.add(kv("request.ContentEncoding", $req.contentEncoding))
|
|
|
|
|
|
|
|
let body =
|
|
|
|
if req.hasBody():
|
|
|
|
if req.contentLength == 0:
|
|
|
|
"present, size not available"
|
|
|
|
else:
|
|
|
|
"present, size = " & $req.contentLength
|
|
|
|
else:
|
|
|
|
"not available"
|
|
|
|
res.add(kv("request.body", body))
|
|
|
|
|
2021-02-02 22:33:14 +00:00
|
|
|
if not(req.query.isEmpty()):
|
2021-02-02 10:48:04 +00:00
|
|
|
res.add(h("Query arguments"))
|
2021-02-02 22:33:14 +00:00
|
|
|
for k, v in req.query.stringItems():
|
2021-02-02 10:48:04 +00:00
|
|
|
res.add(kv(k, v))
|
|
|
|
|
2021-02-02 22:33:14 +00:00
|
|
|
if not(req.headers.isEmpty()):
|
2021-02-02 10:48:04 +00:00
|
|
|
res.add(h("HTTP headers"))
|
2021-02-02 22:33:14 +00:00
|
|
|
for k, v in req.headers.stringItems(true):
|
2021-02-02 10:48:04 +00:00
|
|
|
res.add(kv(k, v))
|
|
|
|
|
|
|
|
if req.meth in PostMethods:
|
|
|
|
if req.postTable.isSome():
|
|
|
|
let postTable = req.postTable.get()
|
|
|
|
if not(postTable.isEmpty()):
|
|
|
|
res.add(h("POST arguments"))
|
|
|
|
for k, v in postTable.stringItems():
|
|
|
|
res.add(kv(k, v))
|
|
|
|
|
|
|
|
res.add(h("Connection information"))
|
2021-02-10 13:13:36 +00:00
|
|
|
let localAddress =
|
|
|
|
try:
|
|
|
|
$req.connection.transp.localAddress()
|
|
|
|
except TransportError:
|
|
|
|
"incorrect address"
|
|
|
|
let remoteAddress =
|
|
|
|
try:
|
|
|
|
$req.connection.transp.remoteAddress()
|
|
|
|
except TransportError:
|
|
|
|
"incorrect address"
|
|
|
|
|
|
|
|
res.add(kv("local.address", localAddress))
|
|
|
|
res.add(kv("remote.address", remoteAddress))
|
2021-02-02 10:48:04 +00:00
|
|
|
|
|
|
|
res.add(h("Server configuration"))
|
|
|
|
let maxConn =
|
|
|
|
if req.connection.server.maxConnections < 0:
|
|
|
|
"unlimited"
|
|
|
|
else:
|
|
|
|
$req.connection.server.maxConnections
|
|
|
|
res.add(kv("server.maxConnections", $maxConn))
|
|
|
|
res.add(kv("server.maxHeadersSize", $req.connection.server.maxHeadersSize))
|
|
|
|
res.add(kv("server.maxRequestBodySize",
|
|
|
|
$req.connection.server.maxRequestBodySize))
|
2021-12-10 09:55:25 +00:00
|
|
|
res.add(kv("server.backlog", $req.connection.server.backlogSize))
|
2021-02-02 10:48:04 +00:00
|
|
|
res.add(kv("server.headersTimeout", $req.connection.server.headersTimeout))
|
|
|
|
res.add(kv("server.baseUri", $req.connection.server.baseUri))
|
|
|
|
res.add(kv("server.flags", $req.connection.server.flags))
|
|
|
|
res.add(kv("server.socket.flags", $req.connection.server.socketFlags))
|
|
|
|
header & res & footer
|