Skip to main content

ClikaRT::http::HttpServer

class

Header: ClikaRT/http/server.h

An async HTTP / HTTPS server. Move-only.

Exact-path routes (root level)​

get()​

void get(std::string_view path, Handler handler)

Declared in ClikaRT/http/server.h, line 488

post()​

void post(std::string_view path, Handler handler)

POST.

Declared in ClikaRT/http/server.h, line 491

put()​

void put(std::string_view path, Handler handler)

PUT.

Declared in ClikaRT/http/server.h, line 494

patch()​

void patch(std::string_view path, Handler handler)

PATCH.

Declared in ClikaRT/http/server.h, line 497

del()​

void del(std::string_view path, Handler handler)

DELETE (named del; delete is a C++ keyword).

Declared in ClikaRT/http/server.h, line 500

void head(std::string_view path, Handler handler)

HEAD (an unrouted HEAD falls back to the matching GET handler).

Declared in ClikaRT/http/server.h, line 503

options()​

void options(std::string_view path, Handler handler)

OPTIONS.

Declared in ClikaRT/http/server.h, line 506

Static member functions​

create()​

static HttpServer create()

Create a server (HTTPS is always available via set_tls).

Declared in ClikaRT/http/server.h, line 472

Member functions​

HttpServer()​

HttpServer(HttpServer&&) noexcept

Declared in ClikaRT/http/server.h, line 474

operator=()​

HttpServer& operator=(HttpServer&&) noexcept

Move-assign: transfers the server.

Declared in ClikaRT/http/server.h, line 476

~HttpServer()​

~HttpServer()

Releases the server handle.

Declared in ClikaRT/http/server.h, line 478

route()​

void route(
    Method method,
    std::string_view pattern,
    Handler handler
)

re2-pattern route (positional + named captures, read in the handler via ServerRequest::param). Same containment and registration contract as the exact-path group.

Throws

  • ClikaRT::Error: when the pattern or registration is rejected.

Declared in ClikaRT/http/server.h, line 513

group()​

RouteGroup group(std::string_view prefix)

A route group, e.g. server.group("/api/v1").use(auth).

Declared in ClikaRT/http/server.h, line 517

use()​

void use(Middleware middleware)

Append to the GLOBAL middleware chain (runs for every route, groups included, in registration order). A throw from the middleware is contained caller-side (see detail::contain).

Throws

  • ClikaRT::Error: when the registration is rejected.

Declared in ClikaRT/http/server.h, line 524

mount()​

void mount(std::string_view url_prefix, std::string_view dir)

Serve the files under directory dir at url_prefix (MIME inferred from each file's extension; see set_mime_type to extend the map).

Throws

  • ClikaRT::Error: when the mount is rejected.

Declared in ClikaRT/http/server.h, line 528

add_static_file()​

void add_static_file(std::string_view url_path, std::string_view file)

Serve one file at one exact URL path.

Throws

  • ClikaRT::Error: when the registration is rejected.

Declared in ClikaRT/http/server.h, line 531

set_mime_type()​

void set_mime_type(std::string_view extension, std::string_view mime)

Map a file extension (e.g. ".wasm") to a MIME type for the static routes.

Throws

  • ClikaRT::Error: when the mapping is rejected.

Declared in ClikaRT/http/server.h, line 534

set_exception_handler()​

void set_exception_handler(ExceptionHandler handler)

Customize the response for a FAILED handler, one that threw or returned a failure Result (default: an opaque 500). Contrast set_error_handler, which styles deliberate error statuses. Installing one does not suppress the Warn log line on ClikaRT::logging::logger().

Declared in ClikaRT/http/server.h, line 539

set_error_handler()​

void set_error_handler(ErrorHandler handler)

Customize the page for responses carrying an HTTP error status (a 404, an explicit error(N), …). Not invoked for a failed handler; that is set_exception_handler's job.

Declared in ClikaRT/http/server.h, line 543

set_logger()​

void set_logger(Logger logger)

Install the per-request access logger (runs after each response; a throwing logger is contained and its line dropped, never the request).

Throws

  • ClikaRT::Error: when installation is rejected.

Declared in ClikaRT/http/server.h, line 547

set_read_timeout()​

void set_read_timeout(int seconds)

The per-READ idle timer, in seconds: how long one read may wait for the NEXT byte of a request (reset by every byte that arrives). It closes a client that goes silent mid-request; it does not bound one that keeps trickling, which set_header_deadline / set_body_deadline do.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 555

set_write_timeout()​

void set_write_timeout(int seconds)

Per-connection write timeout, in seconds.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 558

set_header_deadline()​

void set_header_deadline(int seconds)

The header deadline, in seconds (default kDefaultHeaderDeadlineSec; 0 disables it): a ceiling on the time from a request's first byte to the end of its headers. A request past it is answered 408 Request Timeout (the status runs through set_error_handler like a 413, so you shape its body) and its connection is closed, at the deadline, however slowly the client keeps sending. One Warn line on ClikaRT::logging::logger() names the peer.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 567

set_body_deadline()​

void set_body_deadline(int seconds)

The body deadline, in seconds (default kDefaultBodyDeadlineSec; 0 disables it): a ceiling on the time from the end of a request's headers to the end of its declared body, with the same 408, close and log line. Size it with set_payload_max_length: the largest body you accept must arrive within it over the slowest uplink you serve.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 574

set_keep_alive_timeout()​

void set_keep_alive_timeout(int seconds)

How long an open connection may sit idle, between requests and before its first request's byte, before the server closes it, in seconds (default kDefaultKeepAliveTimeoutSec).

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 579

set_payload_max_length()​

void set_payload_max_length(std::size_t bytes)

Largest request body accepted, in bytes; a larger payload is refused before the handler runs.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 582

set_max_connections()​

void set_max_connections(std::size_t max_connections)

Concurrent-connection ceiling; 0 means unlimited.

Throws

  • ClikaRT::Error: when the value is rejected.

Declared in ClikaRT/http/server.h, line 585

set_default_header()​

void set_default_header(std::string_view name, std::string_view value)

A header appended to every response (repeat for several values).

Throws

  • ClikaRT::Error: when the header is rejected.

Declared in ClikaRT/http/server.h, line 588

set_tls()​

void set_tls(std::string_view cert, std::string_view key)

Serve HTTPS: load this certificate + private key (PEM file paths); every later listen*() speaks TLS.

Throws

  • ClikaRT::Error: when the pair cannot be loaded.

Declared in ClikaRT/http/server.h, line 592

listen()​

void listen(std::string_view host, int port)

Serve on host:port, BLOCKING this thread until stop() (call stop from another thread; this call returns after the drain).

Throws

  • ClikaRT::Error: when the socket cannot bind or listen.

Declared in ClikaRT/http/server.h, line 598

listen_async()​

void listen_async(std::string_view host, int port)

Serve on host:port on a background thread; returns once the server is accepting (pair with wait_until_ready when racing a first request).

Throws

  • ClikaRT::Error: when the socket cannot bind or listen.

Declared in ClikaRT/http/server.h, line 602

bind_to_any_port()​

int bind_to_any_port(std::string_view host)

listen_async with an OS-chosen port: binds, serves in the background, and returns the port (read it again later via port()).

Throws

  • ClikaRT::Error: when no port can be bound.

Declared in ClikaRT/http/server.h, line 606

stop()​

void stop()

Stop accepting new connections and DRAIN: in-flight handlers run to completion and their responses flush before the serving loop exits (a blocking listen() then returns).

Throws

  • ClikaRT::Error: when the stop cannot be issued.

Declared in ClikaRT/http/server.h, line 611

wait_until_ready()​

void wait_until_ready(int timeout_ms = 30000)

Block until the server is accepting, or timeout_ms elapses (→ error).

Declared in ClikaRT/http/server.h, line 613

is_running()​

bool is_running() const

Declared in ClikaRT/http/server.h, line 614

port()​

int port() const

Declared in ClikaRT/http/server.h, line 615