Webserv
Loading...
Searching...
No Matches
http::Router Class Reference

The central request dispatcher (Facade). More...

#include <Router.hpp>

Public Member Functions

 Router (config::ServerConfig const &cfg, MimeTypes const &mime)
 Constructs the Router.
void matchServerAndLocation (int port, Request &request) const
 Populates the request with the matching server and location blocks.
void dispatch (int port, const Request &request, Response &response) const
 Dispatches a fully parsed request to the correct handler.
void handleError (Request const &, Response &) const

Private Member Functions

void executeHandler (Request const &request, Response &response) const
 Internal: Selects and executes the correct handler.

Static Private Member Functions

static void handleError (Request const &request, Response &response, MimeTypes const &mimeTypes)
 Populates an error response.
static bool isMethodAllowed (const Request &request)

Private Attributes

config::ServerConfig const & config_
MimeTypes const & mimeTypes_

Detailed Description

The central request dispatcher (Facade).

This class acts as the main "brain" for handling requests. It uses the ServerConfig to find the correct route, selects the appropriate handler (e.g., StaticFile, CGI), and populates an Response. It also coordinates error page generation.

Constructor & Destructor Documentation

◆ Router()

http::Router::Router ( config::ServerConfig const & cfg,
MimeTypes const & mime )

Constructs the Router.

Parameters
cfgA constant reference to the global server configuration.
mimeA constant reference to the loaded MIME types.

Member Function Documentation

◆ dispatch()

void http::Router::dispatch ( int port,
const Request & request,
Response & response ) const

Dispatches a fully parsed request to the correct handler.

This is the main entry point for the Reactor. It finds the route, executes the correct handler, and formats any errors that occur.

Parameters
portThe port the connection was received on.
requestThe fully populated client request.
responseThe Response object to be populated by the handler.

◆ executeHandler()

void http::Router::executeHandler ( Request const & request,
Response & response ) const
private

Internal: Selects and executes the correct handler.

◆ handleError() [1/2]

void http::Router::handleError ( Request const & request,
Response & response ) const

◆ handleError() [2/2]

void http::Router::handleError ( Request const & request,
Response & response,
MimeTypes const & mimeTypes )
staticprivate

Populates an error response.

◆ isMethodAllowed()

bool http::Router::isMethodAllowed ( const Request & request)
staticprivate

◆ matchServerAndLocation()

void http::Router::matchServerAndLocation ( int port,
Request & request ) const

Populates the request with the matching server and location blocks.

This method is intended to be called by the Reactor before the body is parsed, allowing for route-specific body validation (e.g., size limits).

Parameters
portThe port the connection was received on.
requestThe HttpRequest object to populate.

Member Data Documentation

◆ config_

config::ServerConfig const& http::Router::config_
private

◆ mimeTypes_

MimeTypes const& http::Router::mimeTypes_
private

The documentation for this class was generated from the following files: