Chapter 5: Routing and Callback Results
This chapter explains the differences between route keys, HTTP status codes, processing stages, and STTNet flow control.
1. Default routing does not separate methods
setFunction("/users", ...) uses request.loc by default, so GET, POST, and DELETE /users share the same key. Validate the method in the callback or create a custom key.
2. Multiple stages on one route
server.setFunction("/admin",
[](HttpServerFDHandler &client, HttpRequestInformation &request) {
if(request.headerValue("authorization").empty())
// Reply, then stop later stages without closing keep-alive.
return client.sendText("unauthorized", "401 Unauthorized") ? -1 : -2;
return 1; // Authentication passed; continue.
});
server.setFunction("/admin",
[](HttpServerFDHandler &client, HttpRequestInformation &) {
return client.sendText("admin area") ? 1 : -2;
});Returning 1 after sending 401 continues the remaining stages. A completed rejection response normally returns -1. It would continue into the business stage and could produce a second response or bypass the intended gate.
3. Synchronous and Worker completion semantics
| Result | Behavior |
|---|---|
| 1 | Continue to the next stage; finish when no stage remains. |
| 0 | Wait for a previously submitted Worker task. |
| -1 | Discard remaining stages for this flow and keep the connection. |
| -2 | Stop and close the connection. |
4. Method + path keys
server.setGetKeyFunction(
[](HttpServerFDHandler &, HttpRequestInformation &request) {
request.ctx["key"] = request.type + " " + request.loc;
return 1;
});
server.setFunction("GET /users", getUsers);
server.setFunction("POST /users", createUser);