C Collections

chttpserver · Section 3

chttpsvr_start

chttpsvr_start - register a listener with the running engine

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
ccol_retval chttpsvr_start(chttpsvr srv,
                             const chttpsvr_config *cfg);

Starts the server on the port and host that cfg names.  cfg may be
NULL, and the server then uses the defaults of
CHTTPSVR_CONFIG_DEFAULT.
 
cfg->host names the TCP listener, with the listener rules of the Go
net package:
 
  NULL or ""     (NULL is the default of CHTTPSVR_CONFIG_DEFAULT)
                 every interface, with one dual-stack socket on [::]
                 (IPV6_V6ONLY off) that also takes IPv4 clients through
                 v4-mapped addresses; 0.0.0.0 on a host without IPv6
                 and on a system without dual-stack sockets (OpenBSD).
                 cfg->ipv6_only makes it an IPv6-only listener.
  an address     exactly that address: "0.0.0.0" is every IPv4
                 interface, "::" every IPv6 interface, "127.0.0.1" the
                 IPv4 loopback alone.  An address in brackets, as in
                 "[::1]", is the address inside the brackets, and a
                 name in brackets fails the start.
  a host name    its first IPv4 address if it has one, else its first
                 IPv6 address, so "localhost" listens on 127.0.0.1.
                 With cfg->ipv6_only, its first IPv6 address if it has
                 one, else its first IPv4 address.
 
The server tries only that one address.  If it can't bind it, for
example because another socket holds the port on that address, the
start fails with ccol_unexpected_failure, and the server never
listens on another address of the same name instead.  A host name
that does not resolve, or a malformed bracketed address, fails the
start in the same way, and the engine logger reports the reason.
 
cfg->host can also be a "unix://path/to/socket" string.  The server
then binds a Unix domain socket at that path and ignores cfg->port.
A socket file at that path on which no process listens is replaced.
The start fails with ccol_unexpected_failure, and leaves the file as
it is, when:
  - The path names an object that is not a socket.
  - The path names a socket on which a live server listens.
chttpsvr_stop and the destroy remove the socket file only while the
path names the socket that this server created, and they reach the
file through the directory that held it at start time, so a later
chdir() changes nothing.  One chttpsvr instance listens on TCP or on
a Unix socket, never on both, so create two instances to use both.
 
An empty path fails the start with ccol_unexpected_failure, and the
engine log names EINVAL.  So does a path that doesn't fit in the
sun_path field of struct sockaddr_un, which holds 108 bytes on Linux
and 104 bytes on the BSDs, the terminating NUL included, and the
engine log then names ENAMETOOLONG.  A relative path is resolved
against the working directory at the time of the start.
 
If the shared ceventloop reactor of the process, and the
idle-timeout sweep thread beside it, are not running yet, this call
starts them and blocks until the library confirms that they run
before it goes on.  This reactor is separate from the engine of
chttpclient, and starting or stopping one has no effect on the other.
The function then binds the listener socket synchronously and returns
once the socket is in the LISTEN state and registered with the
reactor.
 
More than one chttpsvr handle can be started at the same time, on
different ports or different Unix socket paths, and all of them share
the same reactor.
 
A cfg->port of 0 binds an ephemeral port that the system picks, as
port 0 does for Go's net.Listen.  Once this call has succeeded,
chttpsvr_bound_port(3) reports the port and chttpsvr_bound_address(3)
the address.  The system picks again on every start, so a restart can
move the server to another port, and cfg->enable_reuseport has no
effect with port 0 because every such listener gets a port of its
own.  When cfg->host is a "unix://" path the port means nothing and
is ignored.
 
At start time the server creates a cthrpool and dispatches every
buffered handler through it.  The pool has cfg->worker_thread_count
threads and a queue capacity of cfg->worker_queue_capacity, where 0
means 1024 slots per thread and CHTTPSVR_QUEUE_UNBOUNDED means no
capacity limit.  Streaming handlers run on a second pool of their
own, which the server creates when the first streaming request
arrives (see "Slow clients" below), and with
cfg->streaming_thread_count set to CHTTPSVR_STREAMING_POOL_OFF they
run on the worker pool instead.  Both pools belong to the server and
are destroyed with it.
 
Rejection of a route: A route that does not match, or that is
malformed, is refused with 404, 405 or 500, and the server decides
this before it reads any body byte.  A matched route whose cthrpool is
full is refused with 503 and a "Retry-After: 5" header.  Both get a
courtesy error response, and any middleware of that rejection runs on
a small dedicated pool of the server's own.  No thread ever waits for
the client to read that response.  A write that meets a full socket
parks the connection, without a thread, until the client reads, and
the internal ceiling of 2 seconds (see
max_response_write_duration_us) ends it.  So a client that reads
slowly and gets a "no" can't hold a thread or delay the dispatch of
any other, unrelated connection.  The interim "100 Continue" line of
a buffered route parks in the same way, and the body is read once the
line is out.
 
Lingering close: When the server closes a connection after a
complete response while the client may still be sending (the unread
body of a refused request, a body that a handler left unread, or
pipelined bytes), a plain close would make the kernel answer with a
reset, and that reset can destroy the response before the client
reads it.  So the server sends a TLS close_notify when the connection
uses TLS, shuts down its side of the connection for writing, and
reads and throws away whatever the client still sends, for at most 2
seconds, before it closes.  The client reads the whole response and
then an orderly end of stream, and so does a client that sends the
whole body of a refused upload before it reads the response, as long
as it finishes sending within that time.  A lingering connection
holds no thread, and it counts against cfg->max_connections until it
closes.  A refused request whose framing has no body, with nothing
more in the socket, is closed at once.
 
Limit on the body size: cfg->max_body_size caps the request body, and
0 means no limit.  A buffered route whose body goes past it is
refused with 413 before the handler runs.  The handler of a streaming
route is always called, and there chttpsvr_req_read() returns -1 and
chttpsvr_req_stream_error() reports ccol_msg_too_large once the body
crosses the limit.  In both cases the server closes the connection
after the response (Connection: close) instead of keeping it alive,
because it doesn't read the body bytes past the limit.  That close is
a lingering close; see "Lingering close" above.
 
A buffered route whose declared Content-Length is already above
max_body_size is refused with 413 at once, without waiting for a
first body byte, since a stalled or malicious peer may never send
one.  A streaming route is different: Its handler is always called,
whatever the declared Content-Length, as the contract above says.
One more check applies to both kinds of route.  The server refuses
any single chunk of a chunked Transfer-Encoding whose declared size
is above max_body_size as soon as it parses the chunk-size line of
that chunk, before it waits for the data of the chunk.  This shows up
like any other malformed body for that kind of route, as a 413 or as
ccol_msg_too_large.
 
Every timeout and duration of chttpsvr_config is a uint64_t count
of microseconds, rounded up to whole milliseconds inside the server;
see CHTTPSVR_CONFIG_DEFAULT(3).
 
Timeouts for a body read: cfg->stream_read_timeout_us bounds the gap
between two batches of body bytes, for a buffered and for a streaming
body alike, and 0 waits forever.  It resets on every new byte, so a
client that sends its data just fast enough never trips it, and
cfg->max_body_read_duration_us therefore bounds the *total* time that
the server spends reading the body of one request, whatever the
progress in each gap.  Its default is 300000000, which is 5 minutes,
and 0 turns it off; see chttpsvr_req_read.
cfg->response_write_timeout_us bounds the same gap on the side that
writes the response, where 0 means the value of
stream_read_timeout_us.  It bounds how long a response can wait on a
client that stops reading, and a send queue of the socket that
shrinks counts as progress; see CHTTPSVR_CONFIG_DEFAULT(3).
cfg->max_response_write_duration_us is the write side of
max_body_read_duration_us.  Its default is 300000000, which is 5
minutes, 0 turns it off, and it bounds the *total* time that the
server spends sending one response, whatever the progress in each
call.  When it expires the send of the response fails and the server
closes the connection.
 
On a buffered route no thread waits out any of these four limits.  A
body that stops arriving, or a response that meets a full socket,
parks its connection without a thread (see "Slow clients" below), and
the limits then apply to the parked connection.  The idle-timeout
sweep thread checks them once a second and the server checks them
again when the connection becomes ready, so a client that stalls past
a limit and then comes back gets the same answer.  An expired body
gets 408 Request Timeout and a close, and an expired response is cut
off and closed.  A streaming handler waits on its own thread inside
chttpsvr_req_read(), where the limits bound that wait directly.
 
Slow clients: On a buffered route a worker thread never waits on the
socket of a client.  When the body of a request stops arriving, the
worker parks the connection, which then holds no thread, and goes
back to the pool.  When more of the body is ready, the reactor hands
the connection to any free worker, and that worker carries on where
the other one stopped.  A response write that meets a full socket
parks the connection in the same way, and a later worker writes the
rest.  Keep-alive, pipelining, "Expect: 100-continue", HEAD, 204 and
304 work across a park exactly as without one.  A parked body asks
the kernel to wake the reactor only once the rest of the body, or
16 KiB of it, is queued (SO_RCVLOWAT), so a client that sends one
byte at a time costs one wake-up instead of one per byte.
 
cfg->min_transfer_rate_bps sets a minimum average rate, by default
240 bytes a second, for the body of a request and for the response
to it.  The rate is measured from the start of the body read, or of
the response write, and judged once cfg->min_transfer_rate_grace_us,
by default 5000000 (5 s), has passed.  A body below the floor gets
408 Request Timeout and a close, a response below it is cut off and
closed, and in a streaming handler chttpsvr_req_read() fails, with
chttpsvr_req_stream_error() reporting ccol_timed_out.  Time in which
the server itself holds a request back, while it waits for body
memory or in the streaming queue, never counts against the client.
CHTTPSVR_NO_RATE_FLOOR turns the floor off.
 
cfg->max_partial_body_memory, by default 256 MiB, caps the memory
that the bodies of buffered requests on this server hold together,
from the first byte that the server reads until the handler returns.
A body reserves its buffer as its bytes arrive.  A body with a
Content-Length reserves a first buffer of at most 16 KiB, the bytes
that came with the headers plus one read, before the server reads a
byte of it.  If that doesn't fit, the request waits without a thread,
the server reads nothing from it and sends no "100 Continue" until
memory frees.  After that every body reserves each growth of its
buffer before the read that needs it, the old buffer included while
a growth copies it, never for a buffer larger than max_body_size or
the declared length, and it waits in the same way when it runs out.
The buffer doubles as it grows, so a reservation always covers the
memory that the body holds, a body that waits for its client holds
only its buffer, and a client that declares a large body and sends
little of it holds little memory.  Waiting requests go ahead strictly
in the order in which they began to wait, so small bodies never
starve a large one.  When every body that holds memory is itself
waiting for more, the oldest waiter goes past the limit, one request
at a time, so the limit is passed by less than twice max_body_size
and the server never deadlocks.  The server frees a body, and gives
its reservation back, as soon as its handler returns and before the
response goes out, so a response that waits for a slow reader holds
no body, and a request that is refused, cut off or closed frees both
at once.  A body whose handler is running is not waiting, so a
request that a handler waits for, and whose body doesn't fit next to
the bodies that running handlers hold, waits until its deadline.  A
request that waits longer than cfg->body_memory_wait_timeout_us, by
default 30000000 (30 s), gets 503 Service Unavailable with
"Retry-After: 5" and a close.  A body that arrives complete together
with its headers reserves nothing, and neither does any streaming
route.  CHTTPSVR_NO_MEMORY_CAP turns the limit off, and
CHTTPSVR_NO_DEADLINE removes the deadline of the wait.
 
Streaming handlers run on their own pool of
cfg->streaming_thread_count threads (by default the worker thread
count), so slow streaming clients can take up at most that pool and
never the threads that serve buffered routes.  A streaming request
that finds no free streaming thread waits in a queue of
cfg->streaming_queue_capacity entries (by default four times the
streaming thread count) without holding a thread.  A request that
finds the queue full gets 503 with "Retry-After: 5" at once, and one
that waits longer than cfg->streaming_queue_timeout_us, by default
5000000 (5 s), gets the same answer.  A handler never runs for a
request that got that 503.
 
Every limit here reads 0 as its default, so a configuration that
never mentions a field keeps the defense, and each one is turned off
only through its own named constant from
<ccollections/chttpserver.h>:
 
  CHTTPSVR_NO_RATE_FLOOR       ((unsigned)-1), for min_transfer_rate_bps:
                               no minimum rate; a body read or a
                               response write is then bounded only by
                               stream_read_timeout_us,
                               max_body_read_duration_us,
                               response_write_timeout_us and
                               max_response_write_duration_us.
  CHTTPSVR_NO_MEMORY_CAP       ((size_t)-1), for max_partial_body_memory:
                               no limit on body memory, so no request
                               waits for it.
  CHTTPSVR_NO_DEADLINE         UINT64_MAX, for body_memory_wait_timeout_us
                               and streaming_queue_timeout_us: The wait
                               lasts as long as it takes.  It holds no
                               thread, and a destroy of the server still
                               ends it.  Every other value, however
                               large, is a finite deadline.
  CHTTPSVR_QUEUE_UNBOUNDED     ((size_t)-1), for worker_queue_capacity and
                               streaming_queue_capacity: No limit on the
                               queue, so no request gets a 503 because the
                               queue is full.
  CHTTPSVR_STREAMING_POOL_OFF  (-1), for streaming_thread_count:
                               streaming handlers run on the worker
                               pool, with no pool and no queue of their
                               own.  worker_queue_capacity then bounds
                               them, and a slow streaming client can
                               hold a worker thread for as long as the
                               read limits allow.
 
Every 503 that the server generates carries "Retry-After: 5".
 
Idle timeout of a connection: cfg->read_timeout_us and
cfg->idle_timeout_us are applied by the idle-timeout sweep thread of
this module, which every chttpsvr instance of the process shares, and
no OS timer per connection applies them.  idle_timeout_us wins over
read_timeout_us when it is above 0.  Otherwise the server uses
read_timeout_us, and 0 in both turns the idle timeout off completely.
This bounds only the time that a connection sits idle between
requests, waiting for the next pipelined or keep-alive request, and
never the time that a worker thread spends handling a request.
 
Limit on the duration of a header read:
cfg->max_header_read_duration_us bounds the *total* wall-clock time
that one connection can spend in the phase of one request that the
reactor owns, which is the TLS handshake, if there is one, plus the
read of the whole header block of that request.  Its default is
30000000, which is 30 s, and 0 turns it off.  read_timeout_us and
idle_timeout_us above reset on every byte that arrives, so a peer
that sends its request headers or its handshake one byte at a time,
without ever completing them, gets past both, and such a peer never
gets near max_header_bytes either.  This field is the header-phase
counterpart of max_body_read_duration_us.  The same idle-timeout
sweep thread applies it, and since that thread runs once a second and
closes every expired connection, however many there are, a
connection can live up to about a second past its deadline.  See
CHTTPSVR_CONFIG_DEFAULT(3).  The budget is per request.  It starts
at the first handshake step, or at the first byte of the header
block of the request when there is no handshake, and the next request
of a keep-alive connection gets a fresh budget when its own first
byte arrives.  When the budget expires the server closes the
connection without a response, because the header block was never
complete enough to answer.
 
Limit on the header size: cfg->max_header_bytes bounds the combined
size of the header block of one request, and 0 means the library
default of 64 KiB.  A request whose headers go past it gets 400 Bad
Request and the connection is closed, the same way the request parser
of this module reports every other malformed request before routing.
The answer is best effort, and a peer that is still streaming its
oversized header block when the server closes may hit a reset before
it reads anything.  Whatever this value is, the request line and each
header line must end within 8 KiB, CRLF included, and a request holds
at most 100 header lines.
 
Request validation: The server refuses the following requests before
any route, middleware or handler sees them, and it closes the
connection after the answer.  A request line whose HTTP version is
not 1.x, such as HTTP/0.9, HTTP/2.0 or HTTP/3.0, gets 505 HTTP
Version Not Supported, while a higher minor version of HTTP/1 is
served as HTTP/1.1 (RFC 9110 section 2.5).  A Host header outside the
uri-host [ ":" port ] grammar of RFC 9112 section 3.2 gets 400.  The
host must be a reg-name of unreserved, sub-delims and percent-encoded
bytes, an IPv4 address or a bracketed IP literal, so a '/', '@', '?',
'#' or whitespace, an empty host, or a port that is not all digits is
refused.  A Transfer-Encoding that names a coding other than chunked,
such as "gzip, chunked", gets 501 Not Implemented, because the server
decodes no other coding, while one that does not end in chunked gets
400.  The chunk framing of a body (size lines, chunk extensions and
CRLFs) may go past 16 bytes plus twice the data of each chunk by 16
KiB in total.  A body beyond that is malformed, so a buffered route
answers 400 and chttpsvr_req_read of a streaming route fails with
ccol_http_transfer_aborted.  An HTTP/1.0 request that carries
"Expect: 100-continue" gets no interim 100 response (RFC 9110 section
10.1.1).
 
Request target: The server accepts an origin-form target, which is a
path that starts with "/", and an absolute-form target with the
scheme http or https, whose path it serves and whose authority it
ignores.  An absolute-form target with an empty path stands for "/".
Every other target, such as a path with no leading "/", a bare query
like "?x" or any other scheme, gets 400 Bad Request and a close,
before the route search and before any middleware runs.
 
The asterisk form works exactly as in Go's net/http server.  The
server answers "OPTIONS *" directly with 200 OK and a Content-Length
of 0, and runs no route, no middleware and no handler.  The response
carries the same headers as any other response (date, content-length
and connection) and no Allow header, and the connection follows the
ordinary keep-alive rules.  The server reads and throws away a
request body of up to 4 KiB.  A larger body, or one that fails to
arrive, still gets the 200, and the connection is closed after it.  A
declared Content-Length above 4 KiB is not read at all, and no 100
Continue invites it.  Any other method with the target "*", including
a method outside the seven chttp_method names such as
"PROPFIND *", gets 400 Bad Request and a close.  A method outside
those seven names with any other target gets 501 Not Implemented.
 
Date: Unless a handler set its own Date with
chttpsvr_resp_set_header(3), every final response has a Date header
with the current time in UTC, in the IMF-fixdate form of RFC 9110
section 5.6.7.  That covers handler responses, HEAD, every rejection
that the server writes itself, "OPTIONS *", and every response on
a keep-alive connection.  The interim 100 Continue has no Date, as in
the net/http server of Go.  The headers that the server adds by
itself are:
  - date.
  - content-length, except on a 204, a 304, or a HEAD
    response for which the handler wrote no body.
  - connection.
  - allow on a 405, and retry-after on a 503 that the server writes
    itself.
 
Host: A request with more than one Host header gets 400 Bad Request
for every version, and so does an HTTP/1.1 request with no Host
header, while an HTTP/1.0 request doesn't need one.  With two Host
lines a front-end could forward one authority while this server reads
another.
 
The request target must not contain whitespace (a space or a
horizontal tab), other control characters or DEL.  A request line
that breaks this rule gets 400 Bad Request and a close, before
routing.
 
Framing: The server reads Transfer-Encoding as one list, merged from
all the Transfer-Encoding lines, and applies these rules:
  - chunked must be the last coding of that list.
  - chunked must occur in the list at most once.
  - A Transfer-Encoding together with a Content-Length is refused.
A request that breaks one of them gets 400 Bad Request and a close,
before routing.
 
A malformed body is a client error, so it gets 400 and not 500.
These bodies are malformed:
  - A body with a chunk size that is not hexadecimal.
  - A body with a chunk that has no CRLF at its end.
  - A body with a malformed trailer line.
  - A body that ends before the length that its framing gives.
A body above max_body_size gets 413 and not 400, and a body that
stops arriving gets 408 and not 400.
 
An HTTP/1.0 request with a Transfer-Encoding gets 400 Bad Request and
a close, whatever its Connection header says, as RFC 9112 section 6.1
requires.  A chunk-size line must follow the full chunk-extension
grammar of RFC 9112 section 7.1.1, and a control byte in an extension
makes the body malformed, so the request gets 400.  A "close" token
in a Connection header closes the connection after the response for
every HTTP version, even when the header also has a "keep-alive"
token.
 
Limit on the connection count: cfg->max_connections bounds how many
connections this listener can have open at one time, and 0 means no
limit.  At capacity new connections stay pending in the kernel's
listen backlog instead of being accepted and then rejected.  The
listener stops accepting until a slot frees and then resumes by
itself, with a worst case of about one second between the slot being
freed and the listener noticing it.
 
A temporary shortage of resources pauses this listener in the same
way.  When the process or the system is out of file descriptors for
a while, or a custom allocator from chttpsvr_create_mp can't serve an
allocation for a connection for a while, new connections stay pending
until a periodic sweep resumes the listener, with the same worst case
of one second.  That way a temporary shortage on one listener doesn't
delay any other, unrelated connection on another chttpsvr instance
that shares the same reactor thread.
 
Socket options: The server sets TCP_NODELAY on every TCP connection
that it accepts and leaves the socket buffer sizes of such a
connection to the kernel, so the buffer autotuning of Linux (tcp(7))
stays on.  On a connection of a Unix domain socket listener, which
has no autotuning, it raises both socket buffers to at least 128 KiB.
The three configurable options below all default to false, are best
effort, and do nothing on a Unix domain socket listener.
cfg->enable_keepalive sets SO_KEEPALIVE on every connection that the
server accepts.  cfg->enable_reuseport sets SO_REUSEPORT on the
listening socket, which lets more than one chttpsvr instance bind the
same host and port so that the kernel spreads the accepts over them.
cfg->ipv6_only sets IPV6_V6ONLY on an AF_INET6 listener, so that it
doesn't also accept IPv4-mapped connections.
 
Restart: After chttpsvr_stop you can call chttpsvr_start again on the
same handle, and the new call registers a new listener with the new
configuration.  The call returns at once.  New pools take the place
of the pools of the previous run immediately, and the old pools drain
in the background, so a request of the previous run that is still
running finishes normally with its own response, and the restart
neither waits for it nor cuts it off.  A keep-alive connection that
stayed open across the stop is served under the new configuration
from its next request on.  The server frees any TLS context of the
previous cycle before it installs the new one, and a connection that
still uses the old context keeps it until that connection closes.
 
Do not call chttpsvr_start from a request handler or a middleware of
srv to restart the very server that the handler runs on (such a
handler usually calls chttpsvr_stop first).  The library refuses that
call with ccol_not_permitted instead of attempting it, and srv is
left in a safe state that you can recover from: The configuration
that srv had before, or a stopped server if chttpsvr_stop has already
run.  A later, legitimate call to chttpsvr_start from another thread
restarts it normally.
 
TLS: When cfg->tls is not NULL, it must have both cert_path and
key_path set.  Any other shape is refused with ccol_invalid_args
before any other work, instead of quietly starting the server as
plain, unencrypted HTTP.  Those other shapes are exactly one of the
two set, cfg->tls->ca_bundle_path set with neither of the two, and a
cfg->tls that is otherwise empty, for example CHTTP_TLS_DEFAULT.  A
trust store alone is never a valid server configuration, and neither
is an otherwise empty chttp_tls_config, because a server always
needs an identity certificate of its own.  The client side is
different here: ca_bundle_path alone is the ordinary way to verify
against a custom CA without any client certificate.
 
chttpsvr_start fails with ccol_unexpected_failure, and registers no
listener, when cert_path and key_path are both set but the library
can't load the certificate and key pair.  The load fails for a file
that is missing or unreadable, for a path that does not name a
regular file (a directory, a FIFO or a device, which the library
refuses at once without waiting on it), for a file above the 16 MB
ceiling under which the library reads every PEM artefact, and for
malformed contents.  When cfg->tls->ca_bundle_path is set it must
load successfully as well, under the same rules, since that bundle is
what turns on mutual TLS on the listener, and it must contribute at
least one certificate.  A bundle that the library can read but that
parses to no certificate, or only to certificate revocation lists, is
a load failure and not an empty trust store.  A failure to load it is
reported as ccol_unexpected_failure too, instead of quietly starting
the server without the client-certificate verification you asked for.
 
A ca_bundle_path that holds certificate revocation lists (CRLs) next
to its CA certificates enforces them.  Every certificate of the chain
that a client presents, below its trust anchor, then needs a CRL of
its issuer in the bundle, as the ssl_crl directive of nginx requires.
The handshake fails for a revoked client certificate, for a
certificate whose issuer has no CRL in the bundle, and for a CRL past
its next update time.  The bundle is read here, so a renewed CRL
takes effect at the next chttpsvr_start.  See CHTTP_TLS_DEFAULT(3).
 
By default mutual TLS requires a client certificate.  The handshake
of a client that presents none fails, and so does the handshake of a
client whose certificate does not verify against the bundle.
cfg->tls->client_cert_optional relaxes only the first rule, so the
server then also serves a client that presents no certificate.
chttpsvr_req_peer_cert_verified(3) tells a handler which kind of
client it serves, and chttpsvr_req_peer_cert_der(3),
chttpsvr_req_peer_cert_sha256(3) and chttpsvr_req_peer_cert_subject(3)
tell it which client authenticated.
 
The listener resumes TLS sessions from its session cache and from
TLS 1.3 tickets, for TLS 1.2 and TLS 1.3, with or without a
ca_bundle_path, and both for a client that presents a certificate and
for one that does not.  Each chttpsvr_start builds a new TLS
configuration with a new session cache, so a session from an earlier
cycle ends in a full handshake and never in a failure.
 
Pipelining: A client can write its next request before it reads the
previous response, and a keep-alive connection serves such a client
on buffered and streaming routes alike.  Bytes of a later request can
arrive in the same read as the headers or the body of the current
request, and the server keeps them for the next request and never
drops them.  Pipelined requests can arrive together in one TLS record,
and the server answers all of them without needing more bytes from
the client, exactly as over plain TCP.
 
Each response that has a body gives its length in Content-Length.
The server sends a TLS close_notify before it closes a TLS connection
whose handshake is complete, so a client that treats a TLS end of
stream without close_notify as a truncation never sees one from this
server.

ccol_success on success.
ccol_invalid_args when srv is CHTTPSVR_NULL or a stale handle that
has already been destroyed; when cfg->tls is set but cert_path and
key_path are not both set; or when cfg->streaming_thread_count is
negative and not CHTTPSVR_STREAMING_POOL_OFF.
ccol_not_permitted when this server has already started, or when this
call came from a request handler or a middleware of srv.
ccol_not_enough_memory when a memory allocation fails, or when the
library could not create the shared engine, which means its reactor
threads or its own diagnostics logger.
ccol_http_tls_cert_load_failed when cfg->tls was set and the library
could not load its certificate and key pair or its ca_bundle_path.
An allocation that fails during that load gives
ccol_not_enough_memory instead.
ccol_unexpected_failure when the library could not create or bind the
listen socket (for example because another socket holds the port on
the address that cfg->host names, or because cfg->host does not
resolve or is malformed), could not register it with the shared
reactor, or could not start the shared idle-timeout sweep thread or
the thread that serves chttpsvr_engine_stop(3); the server log then
says which.  It is also returned when the process already holds
PTHREAD_KEYS_MAX thread-specific keys, so that the library can't
create the key that marks the worker threads of a server.  In each of
these cases the server does not start, and a later call tries again.

The library leaves the disposition of SIGPIPE to the application and
never changes it, here or anywhere else.  No write of the server
raises that signal, because every write to a connection, a TLS record
and the close_notify of a TLS teardown included, is a send(2) or a
sendmsg(2) with MSG_NOSIGNAL.  A client that closes or resets its
connection while the server writes a response ends only that one
connection and never the process, whatever disposition of SIGPIPE
the application has chosen.

chttpsvr_stop closes only the listener socket.  A connection that the
server accepted before the stop keeps running and can dispatch more
requests on this same handle at any point during a later restart.
Each such request goes to the pools that the server runs with at that
moment, and a request that runs on a pool of the previous run
finishes there before that pool is destroyed.  So a restart is safe
while such a connection is still active, and no request ever goes to
a pool that is being torn down at the same time.  A parked connection
holds no thread, stays with the server and resumes on the new worker
pool.  A streaming request that waits in the streaming queue across a
restart runs on the streaming pool of the new run, with its thread
count, and a streaming request of the new run never waits for a
streaming handler of the earlier run to end.  chttpsvr_destroy waits
for the requests of every run, with the bound that
CHTTPSVR_CONFIG_DEFAULT(3) describes.
 
A chttpsvr_start that restarts srv is also safe when it races a
chttpsvr_stop on the same handle from another thread.  This function
waits until that stop has completely torn down the old listener and
then binds a new listener on the same host and port (a new ephemeral
port for a cfg->port of 0), instead of racing a bind() against a
socket that the stop has not closed yet.
 
chttpsvr_start is safe as well when it races a teardown of the whole
engine, whether that comes from chttpsvr_engine_stop() or from the
graceful shutdown of the shared reactor after the last other server
that references it was destroyed.  The function keeps retrying
until that teardown has finished, instead of racing it.

chttpsvr_req_peer_cert_verified(3), chttpsvr_req_peer_cert_der(3), chttpsvr_req_peer_cert_sha256(3), chttpsvr_req_peer_cert_subject(3), CHTTPSVR_CONFIG_DEFAULT(3), chttpsvr_stop(3), chttpsvr_is_running(3), chttpsvr_bound_port(3), chttpsvr_bound_address(3), chttpsvr_engine_stop(3), chttpsvr_set_engine_logger(3), chttpsvr_set_engine_mem_mgmt_procs(3), chttpsvr_engine_wait(3)