-
Notifications
You must be signed in to change notification settings - Fork 2
Router: 4. Request Processing Logic
The RequestProcessor handles the processing of HTTP requests by coordinating with handlers and server configurations. It validates and normalizes request paths, executes appropriate handlers (e.g., for redirects, CGI, or static files), and generates error responses when needed.
2. Three stages: The snippet focuses on the three stages of processing after path validation and normalization:
void RequestProcessor::processRequest(const Request& req,
const Handler* handler, Response& res, const Location* location) const {
// validation stage
// Stage 1: Execute handler if available
if (handler) {
if (executeHandler(handler, req, res, location)) {
return;
}
}
// Stage 2: Fallback: try to serve as static file
if (tryServeAsStaticFile(req, res, method)) {
return;
}
// Stage 3: All attempts failed - return 404
router::utils::HttpResponseBuilder::setErrorResponse(res, http::NOT_FOUND_404);
}// Stage 1: Execute handler if available
if (handler) {
if (executeHandler(handler, req, res, location)) {
return;
}
}Attempts to process the request using the provided Handler (e.g., get, post, cgi, redirect, del), which is a callable object std::function<void(Request&, Response&)> retrieved from the Router’s _routes map.
// Stage 2: Fallback: try to serve as static file
if (tryServeAsStaticFile(req, res, method)) {
return;
}Attempts to serve the request as a static file (e.g., index.html, image.jpg) if no handler was provided or the handler failed. This is a fallback for requests that don’t match specific routes (e.g., GET requests for files).
// Stage 3: All attempts failed - return 404
router::utils::HttpResponseBuilder::setErrorResponse(res, http::NOT_FOUND_404);Generates a 404 Not Found response when both the handler and static file attempts fail, ensuring every request gets a response.