Request lifecycle
Follow an HTTP request through cppnet — from raw socket bytes to a JSON response.
This page traces a single request end-to-end. Consider this raw HTTP POST:
POST /user HTTP/1.1
Host: localhost
Content-Type: application/json
Content-Length: 38
{"username":"alice","role":"developer"}Step 1 — Socket layer (receiving data)
The (yet-to-be-implemented) socket layer — built with a library like Asio — listens for connections and reads raw bytes into a buffer. That raw string is the input to the next stage.
cppnet does not ship the socket layer. Everything below works against the raw
request string once you've read it off the wire.
Step 2 — Parsing layer (http::Parser)
The raw string is fed to a Parser:
parser.feed(rawData, rawDataLength);Internally llhttp processes the bytes and invokes callbacks that populate an
http::Request:
callbacks::method_from_stringturns"POST"intohttp::Method::POST.on_urlpopulatesrequest.path("/user").on_header_field/on_header_valuepopulaterequest.headers.on_bodypopulatesrequest.bodywith the JSON payload.on_message_completeflags parsing as finished.
After this stage the Request is fully populated:
request.method = http::Method::POST
request.path = "/user"
request.headers = { host: "localhost", content-type: "application/json", ... }
request.body = {"username":"alice","role":"developer"}Step 3 — Routing layer (http::Router)
The populated request is passed to router.route_request(request):
- The router builds a
RouteKeyfrom the method (POST) and path (/user). - It looks the key up in its internal
unordered_mapof routes. - It finds the entry registered via
router.add_route(http::Method::POST, "/user", ...)and retrieves the handler.
Step 4 — Business logic (http::handlers)
The router invokes the handler's handle() method:
handler->handle(request)runs.- The handler parses
request.bodywithnlohmann::json. - It performs the business logic (e.g. storing the new user).
- It builds a JSON response, for example:
{"success":true,"message":"User stored","user":{...}}.
Step 5 — The response
The response string returns up the call stack — handler → router → your main loop — and the socket layer writes it back to the client, completing the request/response cycle.
Next
Add your own endpoints in Handlers & routing.