# sizgunan/webserv42 architecture

> Architecture diagram of the GitHub repository sizgunan/webserv42, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sizgunan/webserv42
- Repository: https://github.com/sizgunan/webserv42

## Overview

##### 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).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Configuration & Initialization Layer
    subgraph "Configuration & Initialization"
        CP["Configuration Parser<br>(reads lconfig.conf)"]:::green
        SS["Server Setup<br>(initializes Server objects)"]:::green
    end

    %% Networking Layer
    subgraph "Networking"
        NSM["Socket & Network Management<br>(socket, bind, listen, accept, select)"]:::blue
    end

    %% Request Handling Layer
    subgraph "Request Handling"
        HRP["HTTP Request Parser<br>(parses HTTP headers/methods)"]:::green
    end

    %% CGI Handling Layer
    subgraph "CGI Handling"
        CH["CGI Handler<br>(executes CGI scripts & manages cookies)"]:::orange
    end

    %% Response Generation Layer
    subgraph "Response Generation"
        RB["Response Builder<br>(Builder Pattern & response templates)"]:::green
    end

    %% Transmission Layer
    subgraph "Transmission"
        NT["Network Transmission<br>(sends HTTP Response)"]:::blue
    end

    %% External Client
    Client["Client<br>(HTTP Request/Response)"]:::yellow

    %% Utility Modules (Support)
    UM["Utility Modules<br>(utils, macros, multiplexing)"]:::purple

    %% Flow Connections
    CP -->|"parseConfig"| SS
    SS -->|"initServers"| NSM
    NSM -->|"incomingRequest"| HRP
    HRP -->|"direct"| RB
    HRP -->|"CGI"| CH
    CH -->|"CGIOutput"| RB
    RB -->|"sendResponse"| NT
    NT -->|"deliverResponse"| Client

    %% Utility Modules support arrows (dotted lines)
    UM -.-> CP
    UM -.-> SS
    UM -.-> NSM
    UM -.-> HRP
    UM -.-> CH
    UM -.-> RB

    %% Click Events
    click CP "https://github.com/sizgunan/webserv42/blob/main/parsing/config.cpp"
    click SS "https://github.com/sizgunan/webserv42/blob/main/classes/Server.cpp"
    click NSM "https://github.com/sizgunan/webserv42/blob/main/includes/multiplexing.hpp"
    click HRP "https://github.com/sizgunan/webserv42/blob/main/request_response/parseRequestHeader.cpp"
    click CH "https://github.com/sizgunan/webserv42/blob/main/cgiHandling/cgi.cpp"
    click RB "https://github.com/sizgunan/webserv42/tree/main/pages/response_pages"
    click UM "https://github.com/sizgunan/webserv42/blob/main/utils/utils.cpp"

    %% Styles
    classDef green fill:#c8e6c9,stroke:#388e3c,stroke-width:2px;
    classDef blue fill:#bbdefb,stroke:#1976d2,stroke-width:2px;
    classDef orange fill:#ffe0b2,stroke:#f57c00,stroke-width:2px;
    classDef purple fill:#e1bee7,stroke:#8e24aa,stroke-width:2px;
    classDef yellow fill:#fff9c4,stroke:#fbc02d,stroke-width:2px;
```
