# swangarch/42webserv architecture

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

- Interactive diagram: https://gitdiagram.com/swangarch/42webserv
- Repository: https://github.com/swangarch/42webserv

## Overview

##### Project type and purpose
- This is a custom, standalone HTTP/1.1 web server written in C++98, inspired by Nginx, supporting:
- Multiple virtual hosts
- Static file serving
- Dynamic content via CGI (PHP, Python, …)
- Custom configuration file syntax
- IO multiplexing with epoll
- HTTP methods GET, POST, HEAD, DELETE

##### File structure
- Top-level:
- config/default.conf – the server’s routing and host configuration
- includes/… – C++ header files for ConfigManager, Location, ServerInfo, error handling, server core
- srcs/config/… – implementations for parsing config and building in-memory config objects
- srcs/main.cpp – entry point; initializes ConfigManager, constructs server instances, runs event loop
- srcs/server/… – (presumed) HTTP server core: socket setup, epoll loop, request parsing, dispatch
- site/… – sample content tree for several virtual hosts (web1, web2, web3), including static assets, CGI scripts, error pages
- test.sh – automated testing script

##### Additional insights from README
- Key components mentioned:
- Single‐threaded, epoll-based event loop
- Persistent connections and chunked transfer
- CGI support via child processes, pipes for stdout/stderr
- Virtual host resolution by ip:port and server_name
- Modules for autoindex, alias, redirection, upload handling, error pages
- Timeouts for requests and CGI

##### Elements
Client → Listener/epoll → Parser → Router → (Static Handler or CGI Handler) → Response Builder → epoll → Client
- Notes or annotations on edges for key protocols or data formats (HTTP/1.1, chunked, CGI env vars)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Main entry
    Main["Main Entry Point\n(srcs/main.cpp)"]:::internal
    Main --> ConfigManager
    Main --> ListenerMultiplexer

    subgraph "Internal Modules"
        ConfigManager["Config Manager"]:::internal
        ServerInfo["ServerInfo Model"]:::internal
        Location["Location Model"]:::internal
        ErrorHandler["Error Handler & Logger"]:::internal
        Definitions["Shared Definitions & Utilities"]:::internal
        ListenerMultiplexer["Listener & IO Multiplexer\n(epoll)"]:::internal
        SessionManager["Connection & Session Manager"]:::internal
        RequestParser["Request Parser"]:::internal
        Router["Router / Dispatcher"]:::internal
        StaticHandler["Static File Handler"]:::internal
        CGIHandler["CGI Handler"]:::internal
        ResponseBuilder["Response Builder & Sender"]:::internal
    end

    subgraph "OS Facilities"
        OS_epoll["epoll (syscall)"]:::os
        OS_fork["fork/exec (syscall)"]:::os
        OS_fs["Filesystem"]:::os
    end

    subgraph "External Components"
        InterpPHP["PHP/Python Interpreters"]:::ext
    end

    subgraph "Resources"
        ConfigFile["Server Config File\n(config/default.conf)"]:::ext
        SiteContent["Static & CGI Content\n(site/)"]:::ext
        TestScript["Automated Tests\ntest.sh"]:::ext
    end

    Client["Client Browser"]:::ext

    %% Configuration flow
    ConfigFile --> ConfigManager
    ServerInfo --> ConfigManager
    Location --> ConfigManager
    Definitions --> ConfigManager

    %% Request lifecycle
    Client --> ListenerMultiplexer
    ListenerMultiplexer --> OS_epoll
    OS_epoll --> ListenerMultiplexer
    ListenerMultiplexer --> SessionManager
    SessionManager --> RequestParser
    RequestParser --> Router
    Router --> StaticHandler
    Router --> CGIHandler
    StaticHandler --> OS_fs
    StaticHandler --> SiteContent
    StaticHandler --> ResponseBuilder
    CGIHandler --> OS_fork
    CGIHandler --> InterpPHP
    CGIHandler --> ResponseBuilder
    ResponseBuilder --> ListenerMultiplexer
    ListenerMultiplexer --> Client

    %% Error handling
    anyErr((Errors/Timeouts)) 
    anyErr --> ErrorHandler
    ErrorHandler --> StaticHandler
    ErrorHandler --> ResponseBuilder

    %% Click Events
    click ListenerMultiplexer "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click SessionManager "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click RequestParser "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click Router "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click StaticHandler "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click CGIHandler "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click ResponseBuilder "https://github.com/swangarch/42webserv/tree/main/srcs/server/"
    click Main "https://github.com/swangarch/42webserv/blob/main/srcs/main.cpp"
    click ConfigManager "https://github.com/swangarch/42webserv/blob/main/includes/ConfigManager.hpp"
    click ConfigManager "https://github.com/swangarch/42webserv/blob/main/srcs/config/ConfigManager.cpp"
    click ServerInfo "https://github.com/swangarch/42webserv/blob/main/includes/ServerInfo.hpp"
    click ServerInfo "https://github.com/swangarch/42webserv/blob/main/srcs/config/ServerInfo.cpp"
    click Location "https://github.com/swangarch/42webserv/blob/main/includes/Location.hpp"
    click Location "https://github.com/swangarch/42webserv/blob/main/srcs/config/Location.cpp"
    click ErrorHandler "https://github.com/swangarch/42webserv/blob/main/includes/AnyErrorException.hpp"
    click ErrorHandler "https://github.com/swangarch/42webserv/blob/main/srcs/config/AnyErrorException.cpp"
    click FileValidation "https://github.com/swangarch/42webserv/blob/main/includes/CheckFileInfo.hpp"
    click FileValidation "https://github.com/swangarch/42webserv/blob/main/srcs/config/CheckFileInfo.cpp"
    click Definitions "https://github.com/swangarch/42webserv/blob/main/includes/define.hpp"
    click Definitions "https://github.com/swangarch/42webserv/blob/main/includes/standardLib.hpp"
    click Definitions "https://github.com/swangarch/42webserv/blob/main/includes/webserv.hpp"
    click ConfigFile "https://github.com/swangarch/42webserv/blob/main/config/default.conf"
    click SiteContent "https://github.com/swangarch/42webserv/tree/main/site/"
    click TestScript "https://github.com/swangarch/42webserv/blob/main/test.sh"

    %% Styles
    classDef internal fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef os fill:#90EE90,stroke:#000,stroke-width:1px
    classDef ext fill:#FFD580,stroke:#000,stroke-width:1px
```
