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 479

post()

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

POST.

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

put()

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

PUT.

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

patch()

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

PATCH.

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

del()

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

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

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

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 494

options()

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

OPTIONS.

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

Static member functions

create()

static HttpServer create()

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

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

Member functions

HttpServer()

HttpServer(HttpServer&&) noexcept

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

operator=()

HttpServer& operator=(HttpServer&&) noexcept

Move-assign: transfers the server.

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

~HttpServer()

~HttpServer()

Releases the server handle.

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

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 504

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 508

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 515

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 519

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 522

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 525

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 530

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 534

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 538

set_read_timeout()

void set_read_timeout(int seconds)

Per-connection read timeout, in seconds.

Throws

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

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

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 546

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 549

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 552

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 555

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 559

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 565

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 569

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 573

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 578

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 580

is_running()

bool is_running() const

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

port()

int port() const

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