Description: The `Server` class is responsible for handling the core operations of your web server, including routing, handling POST requests, managing session variables, and rendering views. It also interacts with a built-in text-based database.
Constructor
explicit Server(int P_PORT)
Parameters:
P_PORT
: The port number on which the server will listen for incoming connections.
Public Methods
void run_server()
Description: Starts the server and begins listening for incoming connections on the specified port.
void set_assets_routes(const std::string& P_assets_dir)
Description: Sets the directory path for serving static assets like CSS, JavaScript, and images.
Parameters:
P_assets_dir
: The directory containing the static assets.
void set_views_dir(const std::string& P_views_dir)
Description: Sets the directory path where the server will look for view templates to render.
Parameters:
P_views_dir
: The directory containing the view templates.
void set_pages_dir(const std::string& P_pages_dir)
Description: Sets the directory path where the server will look for static HTML pages.
Parameters:
P_pages_dir
: The directory containing the static HTML pages.
void set_databases_dir(const std::string& P_databases_dir)
Description: Sets the directory path where the server will store its text-based databases.
Parameters:
P_databases_dir
: The directory where database files will be stored.
static std::map[std::string, std::string] parseUrlencodedRequestBody(const std::string& req_body)
Description: Parses a URL-encoded request body into a key-value map.
Parameters:
req_body
: The URL-encoded request body as a string.
Returns: A map of key-value pairs extracted from the request body.
void route(std::string P_route, std::string P_file_path)
Description: Defines a route for the server and associates it with a file path.
Parameters:
P_route
: The URL route to handle.P_file_path
: The file path to serve when the route is accessed.
void post(std::string P_post_route, std::function[std::string(const std::string& req_body)] func)
Description: Defines a POST route for the server and associates it with a function to handle the POST request.
Parameters:
P_post_route
: The URL route to handle POST requests.func
: A function that takes the request body as input and returns a response.
void add_session_variable(std::string P_name, std::string P_data_type, std::string P_value)
Description: Adds a session variable to the server, storing it with a specific name, data type, and value.
Parameters:
P_name
: The name of the session variable.P_data_type
: The data type of the session variable.P_value
: The initial value of the session variable.
void render(std::string file_name)
Description: Renders a specified view or page by its file name.
Parameters:
file_name
: The name of the file to render.
Private Methods
static bool ends_with(const std::string& str, const std::string& suffix)
Description: Checks if a string ends with a given suffix.
Parameters:
str
: The string to check.suffix
: The suffix to look for.
Returns: true
if the string ends with the suffix, false
otherwise.
static std::string getSubstringAfterLastChar(const std::string& input, char specialChar)
Description: Extracts the substring that occurs after the last occurrence of a specific character.
Parameters:
input
: The input string.specialChar
: The character after which the substring is extracted.
Returns: The substring after the last occurrence of the specified character.
static std::string get_content_type(const std::string& path)
Description: Determines the content type (MIME type) based on the file extension of the provided path.
Parameters:
path
: The file path for which to determine the content type.
Returns: The content type corresponding to the file extension.
static void initialize_winsock()
Description: Initializes the Winsock library, required for network operations on Windows.
SOCKET create_server_socket() const
Description: Creates a server socket for accepting client connections.
Returns: A SOCKET object representing the server socket.
static SOCKET accept_client(SOCKET server_socket)
Description: Accepts an incoming client connection on the server socket.
Parameters:
server_socket
: The server socket that is listening for connections.
Returns: A SOCKET object representing the accepted client connection.
static std::string parse_http_request(const std::string& request)
Description: Parses an incoming HTTP request string to extract the relevant information.
Parameters:
request
: The raw HTTP request as a string.
Returns: A string containing the parsed request data.
static std::string serve_file(const std::string& path)
Description: Serves the contents of a file located at the given path.
Parameters:
path
: The path to the file that will be served.
Returns: The contents of the file as a string.
static std::string handle_post_request(const std::string& request, const std::function[std::string(const std::string req_body)]& func)
Description: Handles an incoming POST request, passing the request body to a provided handler function and returning the response.
Parameters:
request
: The raw HTTP POST request as a string.func
: A function that takes the request body as input and returns a response string.
Returns: The response generated by the handler function.