KupsHTTP is a small educational C++ HTTP server framework written with portability and clarity in mind. It provides a minimal, header-friendly API to create a basic HTTP server that supports common features like GET/POST/DELETE handlers, middleware, static files, file uploads, and cookie handling. The project targets modern C++ (C++17) and uses CMake for builds.
- Simple
Serverclass with routing helpers (get,post,put,del). Routersupporting route handlers and middleware.- Support for serving static content and handling file uploads (basic implementation).
- Request and Response classes encapsulating headers, body, cookies, and method parsing.
- Thread-per-connection or thread-pool support (configurable in code).
- Lightweight and intended as a learning / prototyping server, not production-grade.
- A C++17-capable compiler (AppleClang, GCC 8+, or Clang 6+)
- CMake (3.10+ recommended)
- POSIX sockets (Linux, macOS; Windows not directly tested)
include/KupsHTTP/— public headers (server, router, request, response, etc.)src/— implementation filesexamples/— example binary that demonstrates usagepublic/— example static files served by the server (if present)CMakeLists.txt— build configuration
From the project root (KupsHTTP):
mkdir -p build
cd build
cmake ..
make -jThis will produce a static library libKupsHTTP.a and an example executable named KupsHTTP_example (or similar depending on the CMake configuration) inside the build folder.
Run the example server from the build directory:
cd build
./KupsHTTP_exampleThe example binds to localhost:8080 by default (this can be changed in examples/main.cpp or passed via API). Visit http://localhost:8080 in a browser or use curl to exercise endpoints:
- GET
/— welcome page - GET
/time— current server time - POST
/echo— echo request body
Example curl requests:
curl http://localhost:8080/
curl http://localhost:8080/time
curl -X POST -d "Hello world" http://localhost:8080/echoCreate a server:
#include <KupsHTTP/server.h>
KupsHTTP::Server server("0.0.0.0", 8080);Register routes:
server.get("/", [](const KupsHTTP::Request &req) {
KupsHTTP::Response res(200, "OK");
res.setContent("<h1>Hello</h1>", "text/html");
return res;
});
server.post("/echo", [](const KupsHTTP::Request &req) {
KupsHTTP::Response res(200, "OK");
res.setContent(req.getBody(), req.getHeader("Content-Type"));
return res;
});Add middleware (logging, auth, etc.) using the router instance:
server.getRouter().use([](KupsHTTP::Request &req, KupsHTTP::Response &res) {
// return true to continue to next middleware/handler, false to short-circuit
std::cout << "[" << time(nullptr) << "] " << req.getMethodString() << " " << req.getPath() << std::endl;
return true;
});Start/stop server:
server.start(); // blocks or runs event loop depending on implementation
// or run in a background thread depending on your usage
server.stop();- Default host/port are set in the
Serverconstructor. UsesetHost()/setPort()to change programmatically. - The server implementation is intentionally minimal. For production use prefer battle-tested libraries (Boost.Beast, libcpp-httplib, Crow, or full HTTP servers like nginx).
- On macOS you might need to allow binding to low ports (<1024) with elevated privileges.
- If you change headers or public API, update the examples accordingly.
- If CMake can't find a C++17-capable compiler, ensure your system toolchain is up-to-date.
- If port is already in use, change the port in
examples/main.cppor configure the Server beforestart(). - For linker errors, make sure the
includepath is correct and CMake built theKupsHTTPtarget.
Contributions are welcome. Please follow these steps:
- Open an issue describing the bug/feature.
- Create a branch for your change.
- Add tests where applicable.
- Open a pull request with a description and the rationale.