Research CommonsResearch Commons
cppnet/Request lifecycle

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.

You implement this

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_string turns "POST" into http::Method::POST.
  • on_url populates request.path ("/user").
  • on_header_field / on_header_value populate request.headers.
  • on_body populates request.body with the JSON payload.
  • on_message_complete flags 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):

  1. The router builds a RouteKey from the method (POST) and path (/user).
  2. It looks the key up in its internal unordered_map of routes.
  3. 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:

  1. handler->handle(request) runs.
  2. The handler parses request.body with nlohmann::json.
  3. It performs the business logic (e.g. storing the new user).
  4. 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.