2021-03-17 13:40:40 +00:00
|
|
|
#
|
|
|
|
# Chronos HTTP/S server implementation
|
|
|
|
# (c) Copyright 2021-Present
|
|
|
|
# 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: [].}
|
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
import httpserver
|
|
|
|
import ../../asyncloop, ../../asyncsync
|
|
|
|
import ../../streams/[asyncstream, tlsstream]
|
2021-08-26 11:22:29 +00:00
|
|
|
export asyncloop, asyncsync, httpserver, asyncstream, tlsstream
|
2021-03-17 13:40:40 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
SecureHttpServer* = object of HttpServer
|
|
|
|
secureFlags*: set[TLSFlags]
|
|
|
|
tlsPrivateKey: TLSPrivateKey
|
|
|
|
tlsCertificate: TLSCertificate
|
|
|
|
|
|
|
|
SecureHttpServerRef* = ref SecureHttpServer
|
|
|
|
|
|
|
|
SecureHttpConnection* = object of HttpConnection
|
|
|
|
tlsStream*: TLSAsyncStream
|
|
|
|
|
|
|
|
SecureHttpConnectionRef* = ref SecureHttpConnection
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc closeSecConnection(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.writer.closeWait())
|
|
|
|
pending.add(conn.reader.closeWait())
|
2023-09-15 16:38:39 +00:00
|
|
|
pending.add(conn.mainReader.closeWait())
|
|
|
|
pending.add(conn.mainWriter.closeWait())
|
|
|
|
pending.add(conn.transp.closeWait())
|
|
|
|
await noCancel(allFutures(pending))
|
2023-08-09 07:57:49 +00:00
|
|
|
reset(cast[SecureHttpConnectionRef](conn)[])
|
2023-07-14 10:35:08 +00:00
|
|
|
untrackCounter(HttpServerSecureConnectionTrackerName)
|
|
|
|
conn.state = HttpState.Closed
|
|
|
|
|
2023-11-21 10:01:44 +00:00
|
|
|
proc new(ht: typedesc[SecureHttpConnectionRef], server: SecureHttpServerRef,
|
|
|
|
transp: StreamTransport): Result[SecureHttpConnectionRef, string] =
|
2021-03-17 13:40:40 +00:00
|
|
|
var res = SecureHttpConnectionRef()
|
|
|
|
HttpConnection(res[]).init(HttpServerRef(server), transp)
|
|
|
|
let tlsStream =
|
2023-11-21 10:01:44 +00:00
|
|
|
try:
|
|
|
|
newTLSServerAsyncStream(res.mainReader, res.mainWriter,
|
|
|
|
server.tlsPrivateKey,
|
|
|
|
server.tlsCertificate,
|
|
|
|
minVersion = TLSVersion.TLS12,
|
|
|
|
flags = server.secureFlags)
|
|
|
|
except TLSStreamError as exc:
|
|
|
|
return err(exc.msg)
|
2021-03-17 13:40:40 +00:00
|
|
|
res.tlsStream = tlsStream
|
|
|
|
res.reader = AsyncStreamReader(tlsStream.reader)
|
|
|
|
res.writer = AsyncStreamWriter(tlsStream.writer)
|
2023-07-14 10:35:08 +00:00
|
|
|
res.closeCb = closeSecConnection
|
|
|
|
trackCounter(HttpServerSecureConnectionTrackerName)
|
2023-11-21 10:01:44 +00:00
|
|
|
ok(res)
|
2021-03-17 13:40:40 +00:00
|
|
|
|
|
|
|
proc createSecConnection(server: HttpServerRef,
|
|
|
|
transp: StreamTransport): Future[HttpConnectionRef] {.
|
2023-11-21 10:01:44 +00:00
|
|
|
async: (raises: [CancelledError, HttpConnectionError]).} =
|
|
|
|
let
|
|
|
|
secureServ = cast[SecureHttpServerRef](server)
|
|
|
|
sconn = SecureHttpConnectionRef.new(secureServ, transp).valueOr:
|
|
|
|
raiseHttpConnectionError(error)
|
|
|
|
|
2021-03-17 13:40:40 +00:00
|
|
|
try:
|
|
|
|
await handshake(sconn.tlsStream)
|
2023-11-21 10:01:44 +00:00
|
|
|
HttpConnectionRef(sconn)
|
2021-03-17 13:40:40 +00:00
|
|
|
except CancelledError as exc:
|
|
|
|
await HttpConnectionRef(sconn).closeWait()
|
|
|
|
raise exc
|
2023-11-21 10:01:44 +00:00
|
|
|
except AsyncStreamError as exc:
|
2021-03-17 13:40:40 +00:00
|
|
|
await HttpConnectionRef(sconn).closeWait()
|
2023-11-21 10:01:44 +00:00
|
|
|
let msg = "Unable to establish secure connection, reason: " & $exc.msg
|
|
|
|
raiseHttpConnectionError(msg)
|
2021-03-17 13:40:40 +00:00
|
|
|
|
|
|
|
proc new*(htype: typedesc[SecureHttpServerRef],
|
|
|
|
address: TransportAddress,
|
|
|
|
processCallback: HttpProcessCallback,
|
|
|
|
tlsPrivateKey: TLSPrivateKey,
|
|
|
|
tlsCertificate: TLSCertificate,
|
|
|
|
serverFlags: set[HttpServerFlags] = {},
|
|
|
|
socketFlags: set[ServerFlags] = {ReuseAddr},
|
|
|
|
serverUri = Uri(),
|
|
|
|
serverIdent = "",
|
|
|
|
secureFlags: set[TLSFlags] = {},
|
|
|
|
maxConnections: int = -1,
|
|
|
|
bufferSize: int = 4096,
|
2023-07-31 19:40:00 +00:00
|
|
|
backlogSize: int = DefaultBacklogSize,
|
2021-03-17 13:40:40 +00:00
|
|
|
httpHeadersTimeout = 10.seconds,
|
|
|
|
maxHeadersSize: int = 8192,
|
2023-10-30 13:27:50 +00:00
|
|
|
maxRequestBodySize: int = 1_048_576,
|
|
|
|
dualstack = DualStackType.Auto
|
2023-11-21 10:01:44 +00:00
|
|
|
): HttpResult[SecureHttpServerRef] =
|
2021-03-17 13:40:40 +00:00
|
|
|
|
|
|
|
doAssert(not(isNil(tlsPrivateKey)), "TLS private key must not be nil!")
|
|
|
|
doAssert(not(isNil(tlsCertificate)), "TLS certificate must not be nil!")
|
|
|
|
|
|
|
|
let serverUri =
|
|
|
|
if len(serverUri.hostname) > 0:
|
|
|
|
serverUri
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
parseUri("https://" & $address & "/")
|
|
|
|
except TransportAddressError as exc:
|
|
|
|
return err(exc.msg)
|
|
|
|
|
|
|
|
let serverInstance =
|
|
|
|
try:
|
|
|
|
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)
|
|
|
|
|
2022-07-04 09:31:18 +00:00
|
|
|
let res = SecureHttpServerRef(
|
|
|
|
address: address,
|
|
|
|
instance: serverInstance,
|
|
|
|
processCallback: processCallback,
|
|
|
|
createConnCallback: createSecConnection,
|
|
|
|
baseUri: serverUri,
|
|
|
|
serverIdent: serverIdent,
|
2023-07-14 10:35:08 +00:00
|
|
|
flags: serverFlags + {HttpServerFlags.Secure},
|
2022-07-04 09:31:18 +00:00
|
|
|
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
|
|
|
tlsCertificate: tlsCertificate,
|
|
|
|
tlsPrivateKey: tlsPrivateKey,
|
|
|
|
secureFlags: secureFlags
|
|
|
|
)
|
2021-03-17 13:40:40 +00:00
|
|
|
ok(res)
|