Introduction
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.
Features
- HTTP and WebSocket clients with a common, class-based API.
- Asynchronous or synchronous execution via a background worker.
- Rate limiting for both HTTP requests and WebSocket messages.
- Automatic reconnection logic for WebSocket connections.
- Proxy support, configurable timeouts and custom headers.
- Retry logic and ability to cancel outstanding requests.
- Standalone helper functions for one-off HTTP calls.
- Header-only design, works with C++11 and newer.
Usage
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:
#define KURLYK_AUTO_INIT 0
}
Main header file for the Kurlyk library, providing HTTP and WebSocket support.
void init(const bool use_async=true)
Initializes the Kurlyk library, setting up necessary managers and the network worker.
void deinit()
Deinitializes the Kurlyk library, stopping the network worker and releasing resources.
HTTP Client
Basic GET request
std::cout << res->content << std::endl;
});
A client class for making HTTP requests to a specific host.
std::unique_ptr< HttpResponse > HttpResponsePtr
A unique pointer to an HttpResponse object for memory management.
Using a proxy
client.set_proxy("127.0.0.1", 8080, "user", "pass", kurlyk::ProxyType::HTTP);
std::cout << res->content << std::endl;
});
Measuring latency
#include <chrono>
auto start = std::chrono::steady_clock::now();
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start);
std::cout << "Ping: " << ms.count() << " ms" << std::endl;
});
Calling HTTP requests
Requests can be issued with callbacks, futures or standalone functions:
auto fut = cli.get("/ip", {}, {});
auto resp = fut.get();
auto resp2 = future.get();
uint64_t http_get(const std::string &url, const QueryParams &query, const Headers &headers, HttpResponseCallback callback)
Sends an asynchronous HTTP GET request with a callback.
WebSocket Client
ws.on_event([](std::unique_ptr<kurlyk::WebSocketEventData> e) {
std::cout << "Message: " << e->message << std::endl;
});
ws.connect_and_wait();
ws.send_message("Hello!");
ws.disconnect_and_wait();
Provides an interface for managing WebSocket connections, including configuration,...
@ WS_MESSAGE
Message received.
Error Handling
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:
using ErrorHandler = std::function<
void(const std::exception&,
const char* msg,
const char* file,
int line,
const char* func)>;
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.
const char* msg,
const char* file,
int line,
const char* func) {
std::cerr << "Kurlyk error: " << msg
<< " (" << ex.what() << ") at "
<< file << ':' << line
<< " in " << func << std::endl;
});
void add_error_handler(::kurlyk::core::NetworkWorker::ErrorHandler handler)
Registers a global error handler for the network worker.
Installation
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.
Configuration Macros
Several macros can be defined before including the library to tailor the build:
KURLYK_HTTP_SUPPORT
and KURLYK_WEBSOCKET_SUPPORT
toggle the HTTP or WebSocket parts of the library.
KURLYK_AUTO_INIT
controls automatic manager registration during static initialization, while KURLYK_AUTO_INIT_USE_ASYNC
runs the worker thread in the background when auto init is enabled.
Repository
Kurlyk Library GitHub repository.
License
This library is licensed under the MIT License. See the LICENSE file for more details.