How sizgunan/webserv42 works
Main project components
• Configuration Parser: This module (found in the “parsing” directory along with files like config.cpp, configFile.cpp, etc.) reads the lconfig.conf and parses the server configuration to build server objects containing directives and location blocks.
• Server Core: Represented by the “classes” (Server.cpp, Request.cpp) and “includes” directories, this component encapsulates server objects, each configured with its ports, directives, error pages (from “pages/response_pages”), and inheritance of server-level settings.
• Network & Socket Management: Based on the README’s detailed explanation about getaddrinfo, socket(), bind(), listen(), accept(), this module manages the creation of sockets, binding to addresses/ports, and the event loop that uses the select() system call (as described in the I/O Multiplexing section) to handle multiple incoming connections.
• Request Parsing & Response Generation: This involves the “request_response” directory (parsing HTTP headers, validating methods, constructing responses) and also uses the “methods” folder (for GET, POST, DELETE implementations) to process incoming requests and build appropriate responses via the Builder Design Pattern.
• CGI and Cookie Handling: Modules located in “cgi-bin”, “cgiHandling”, and “cgiTests” are responsible for identifying CGI script requests (both PHP and Python), forking new processes, setting environment variables, handling pipes for data redirection, and managing cookies via HTTP headers.
• Utility Modules: These include “utils/utils.cpp” and additional helper files in “includes” (e.g., macros, multiplexing, request/response utilities) which support almost every stage.
Relationships and interactions
• The Server Core uses these objects to initialize multiple listening sockets via the Socket Management module.
• The Network Layer uses I/O Multiplexing (with select()) to monitor active sockets.
• Upon receiving an incoming HTTP request, the Request Parser analyzes the HTTP data, validates headers, methods (GET, POST, DELETE), and routes the request according to the configured location blocks.
• Depending on the request:
– If it’s a standard request, the Response Builder constructs an HTTP response (utilizing the error pages available in the “pages” directory if needed).
– If the request requires CGI execution, control is transferred to the CGI Handling module, which forks a child process, sets up environment variables, and invokes the script interpreter (found in “cgi-bin” and executed via routines in “cgiHandling”).
• The response (whether directly built or received from a CGI transaction) is then sent back over the socket.
Architectural patterns and design principles
• Modular design: Each directory represents discrete concerns (configuration, networking, CGI, utilities).
• Separation of concerns: Parsing, network management, and response handling are clearly separated.
• Builder Design Pattern: Specifically used in the response generation process.
• Event-driven architecture: Utilization of I/O multiplexing (select()) to manage multiple connections efficiently.
Final advice
• Simplify where possible by grouping related files/modules (e.g., combine all configuration parsing details into one “Configuration Parser” block).