Webserv
Loading...
Searching...
No Matches
Router.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "http/Handler.hpp"
5#include "http/MimeTypes.hpp"
8
9namespace http {
10
18class Router {
19public:
21 : config_(config), staticFile_(mime) {
22 }
23
30 RouterResult route(int port, HttpRequest &request) const {
31 config::ServerBlock const *server = config_.getServer(port, request.headers["Host"]);
32 if (request.status != 200) {
33 return RouterResult(error_, server);
34 }
35 if (!server) {
36 return RouterResult(notFound_);
37 }
38 config::LocationBlock const *location = server->getLocation(request.path);
39 if (!location) {
40 return RouterResult(notFound_, server);
41 }
42 if (location->hasCgiPass())
43 return RouterResult(cgi_, server, location);
44 return RouterResult(staticFile_, server, location);
45 }
46
47private:
49
50 // Reusable, stateless handler instances owned by the router.
55};
56
57} // namespace http
Represents a single 'location' block from the configuration file.
Definition LocationBlock.hpp:16
bool hasCgiPass() const
Definition LocationBlock.cpp:14
Represents a single 'server' block from the configuration file.
Definition ServerBlock.hpp:18
LocationBlock const * getLocation(std::string const &name) const
Retrieves the configuration for a specific location path.
Definition ServerBlock.cpp:17
A strongly-typed data container for a server block's configuration.
Definition ServerConfig.hpp:18
Handles the execution of CGI scripts.
Definition Handler.hpp:65
Handles the execution of CGI scripts.
Definition Handler.hpp:73
A data container for a parsed HTTP request.
Definition HttpRequest.hpp:18
std::string path
Definition HttpRequest.hpp:26
http::Status status
Definition HttpRequest.hpp:23
HeaderMap headers
Definition HttpRequest.hpp:28
Definition MimeTypes.hpp:11
Handles the generation of not found(404).
Definition Handler.hpp:56
CGIHandler const cgi_
Definition Router.hpp:53
DefaultErrorHandler const error_
Definition Router.hpp:54
RouterResult route(int port, HttpRequest &request) const
Determines the correct handler and context for a request.
Definition Router.hpp:30
StaticFileHandler const staticFile_
Definition Router.hpp:52
Router(config::ServerConfig const &config, MimeTypes const &mime)
Definition Router.hpp:20
config::ServerConfig const & config_
Definition Router.hpp:48
NotFoundHandler const notFound_
Definition Router.hpp:51
Handles serving static files from the filesystem.
Definition Handler.hpp:42
Definition ConfigException.hpp:6
Definition error_pages.hpp:7
A data container for the result of a routing decision.
Definition RouterResult.hpp:17