![]() |
Kurlyk
|
Kurlyk is a header-only C++ library that currently wraps libcurl and Simple-WebSocket-Server** to provide convenient HTTP and WebSocket clients. Its core is backend-agnostic, so alternative implementations can be plugged in if needed—planned support includes an Emscripten backend. The library hides much of the networking boilerplate and offers an easy way to issue asynchronous requests, process WebSocket events and apply rate limiting.
By default Kurlyk initializes itself before main and shuts down automatically thanks to an internal startup::AutoInitializer. It registers HttpRequestManager and WebSocketManager with the core::NetworkWorker and starts the worker thread using the KURLYK_AUTO_INIT_USE_ASYNC setting. A shutdown happens when the program exits, so you can usually skip explicit kurlyk::init() and kurlyk::deinit() calls.
Manual control is still possible by disabling automatic startup:
HTTP callbacks run on the NetworkWorker processing path. With asynchronous startup this is the background worker thread; with synchronous startup it is the thread that calls kurlyk::process(). Keep callbacks short and hand work off to application-owned queues or threads when needed, because blocking a callback delays other HTTP/WebSocket work handled by the same worker.
Requests can be issued with callbacks, futures or standalone functions:
Enable streaming when the caller needs body chunks before the transfer finishes, for example when proxying SSE responses. Chunk callbacks carry stream_chunk == true and ready == false; the final callback keeps the usual ready == true contract.
Use stream_chunk first to classify body chunk callbacks. A chunk callback is not a success marker; the final ready response remains authoritative for the transfer result. status_code on a chunk contains the current HTTP status when libcurl has one, but it is not a completion marker. Non-ready callbacks with stream_chunk == false are reserved for non-final request states such as failed attempts before retry and may carry error_code. If at least one streaming chunk was emitted, kurlyk does not retry that transfer automatically, since the caller may already have forwarded bytes to a downstream client.
Kurlyk separates rate limiting from bounded admission. Rate limiting throttles when requests are dispatched to the backend, while backpressure limits how many pending requests are accepted into the global HTTP queue.
For future-based HTTP calls, an admission reject does not throw a runtime_error; instead the future becomes ready immediately and returns an HttpResponse with error_code set to QueueLimitExceeded or ShuttingDown.
WebSocket backpressure applies to the per-client outbound send queue. It does not bound the internal FSM queue or stored event queue in this pass.
The library centralizes exception reporting through the core::NetworkWorker. Applications may register callbacks with kurlyk::add_error_handler to intercept errors raised inside Kurlyk and forward them to a logging system or metrics collector.
Handlers use the kurlyk::core::NetworkWorker::ErrorHandler signature:
Whenever an internal operation encounters an exception and invokes KURLYK_HANDLE_ERROR, all registered handlers receive the exception, an optional message, source file, line and function. This allows detailed logging of issues without terminating the program.
For synchronous admission rejects, the public API uses kurlyk::SubmitResult and std::error_code values such as QueueLimitExceeded and ShuttingDown instead of reporting the condition through KURLYK_HANDLE_ERROR.
Add the include directory to your compiler search path and include <kurlyk.hpp> in your sources. The repository contains third party dependencies used by the examples, but for your own project you need libcurl, OpenSSL and either Boost.Asio or standalone Asio together with Simple-WebSocket-Server. Kurlyk itself is header-only and works with C++11 or later.
Repository examples can also be built through CMake by configuring the root project with -DKURLYK_BUILD_EXAMPLES=ON.
Several macros can be defined before including the library to tailor the build:
Kurlyk Library GitHub repository.
This library is licensed under the MIT License. See the LICENSE file for more details.