# victra15/webserv architecture

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

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

## Overview

##### Project type and purpose
- This is a standalone, C++-based HTTP/1.1 server implementing socket I/O multiplexing (kqueue on macOS), configuration parsing, static file serving, CGI execution, and basic HTTP methods (GET, POST, DELETE).

##### File structure
- Top-level executables and scripts:
- cgi/php-cgi (actual PHP CGI binary)
- var/ (test HTML, PHP, Python CGI scripts and resources)
- conf/ (server configuration files)
- error_page/ (custom error pages)
- Core C++ code:
- / (public headers: ConfigInfo, HTTPRequest/Response, ServerHandler, LocationConfig, utils)
- srcs/ (corresponding .cpp implementations and main webserv.cpp)
- Build and run:
- Makefile, .gitignore

##### Additional README insights
- Uses Reactor pattern via kqueue for non-blocking, event-driven I/O.
- Components split into:
- ServerHandler (socket setup, event loop)
- ConfigInfo + LocationConfig (parsing .conf into in-memory server & location blocks)
- HTTPMessage / HTTPRequest / HTTPResponse (parsing, serializing)
- CGI integration (spawn external processes)
- Static file I/O and autoindex

##### Detail
- Eight client states (RECV_HEADER, RECV_BODY, GET, POST, DELETE, MAKE_RESPONSE, MAKE_CGI_RESPONSE, SEND_RESPONSE) as sub-steps or annotations around the Reactor component
- How configuration (conf/*.conf) flows into ConfigInfo → LocationConfig and informs routing rules, allowed methods, CGI mappings, error pages, and root directories

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Entities
    Client["Client (Browser/HTTP Tool)"]:::external

    %% Core Engine
    subgraph "Core Engine"
        direction TB
        ServerHandler["ServerHandler (Reactor/Event Loop)"]:::core
    end

    %% Configuration Layer
    subgraph "Configuration Parsing" 
        direction TB
        ConfigInfo["ConfigInfo & ServerConfig"]:::config
        LocationConfig["LocationConfig"]:::config
    end

    %% Parsing & Serialization
    subgraph "HTTP Processing" 
        direction TB
        HTTPRequest["HTTPRequest Parser"]:::parsing
        HTTPResponse["HTTPResponse Serializer"]:::parsing
    end

    %% Routing & Dispatch
    Router["Dispatcher/Router"]:::core

    %% I/O Layer
    subgraph "I/O Handling"
        direction TB
        StaticFS["Static File System (var/)"]:::io
        CGIExec["CGI Executor"]:::io
        ErrorPage["Error Page Manager"]:::io
    end

    %% External Resources
    subgraph "External Resources"
        direction TB
        CGIbin["cgi/php-cgi"]:::external
        ConfFiles["conf/test.conf"]:::external
        VarDir["var/ (HTML, PHP, Python CGI)"]:::external
        ErrorPagesDir["error_page/"]:::external
    end

    %% Build System
    subgraph "Build System"
        direction TB
        MakefileNode["Makefile & .gitignore"]:::external
    end

    %% Connections
    Client -->|"TCP Connection"| ServerHandler
    ServerHandler -->|"Read Event (kqueue)"| HTTPRequest
    HTTPRequest -->|"Parsed Request"| Router
    Router -->|"Routing Decision"| StaticFS
    Router -->|"Routing Decision"| CGIExec
    Router -->|"Routing Decision"| ErrorPage
    StaticFS -->|"Raw File Bytes"| HTTPResponse
    CGIExec -->|"CGI stdout"| HTTPResponse
    ErrorPage -->|"Error HTML"| HTTPResponse
    HTTPResponse -->|"Serialized Response"| ServerHandler
    ServerHandler -->|"Send over Socket"| Client

    %% Configuration flows
    ConfFiles --> ConfigInfo
    ConfigInfo --> Router
    ConfigInfo --> ServerHandler
    ConfigInfo --> LocationConfig
    LocationConfig --> Router

    %% External resource links
    VarDir --> StaticFS
    CGIbin --> CGIExec
    ErrorPagesDir --> ErrorPage

    %% Click Events
    click ServerHandler "https://github.com/victra15/webserv/blob/main/include/ServerHandler.hpp"
    click ServerHandler "https://github.com/victra15/webserv/blob/main/srcs/ServerHandler.cpp"
    click ConfigInfo "https://github.com/victra15/webserv/blob/main/include/ConfigInfo.hpp"
    click ConfigInfo "https://github.com/victra15/webserv/blob/main/srcs/ConfigInfo.cpp"
    click ConfigInfo "https://github.com/victra15/webserv/blob/main/include/ServerConfig.hpp"
    click ConfigInfo "https://github.com/victra15/webserv/blob/main/srcs/ServerConfig.cpp"
    click LocationConfig "https://github.com/victra15/webserv/blob/main/include/LocationConfig.hpp"
    click LocationConfig "https://github.com/victra15/webserv/blob/main/srcs/LocationConfig.cpp"
    click HTTPMessage "https://github.com/victra15/webserv/blob/main/include/HTTPMessage.hpp"
    click HTTPMessage "https://github.com/victra15/webserv/blob/main/srcs/HTTPMessage.cpp"
    click HTTPRequest "https://github.com/victra15/webserv/blob/main/include/HTTPRequest.hpp"
    click HTTPRequest "https://github.com/victra15/webserv/blob/main/srcs/HTTPRequest.cpp"
    click HTTPResponse "https://github.com/victra15/webserv/blob/main/include/HTTPResponse.hpp"
    click HTTPResponse "https://github.com/victra15/webserv/blob/main/srcs/HTTPResponse.cpp"
    click utils "https://github.com/victra15/webserv/blob/main/include/utils.hpp"
    click utils "https://github.com/victra15/webserv/blob/main/srcs/utils.cpp"
    click manageStdout "https://github.com/victra15/webserv/blob/main/include/manageStdout.hpp"
    click webserv "https://github.com/victra15/webserv/blob/main/include/webserv.hpp"
    click webserv "https://github.com/victra15/webserv/blob/main/srcs/webserv.cpp"
    click CGIbin "https://github.com/victra15/webserv/tree/main/cgi/php-cgi"
    click ConfFiles "https://github.com/victra15/webserv/blob/main/conf/test.conf"
    click VarDir "https://github.com/victra15/webserv/tree/main/var/"
    click ErrorPagesDir "https://github.com/victra15/webserv/tree/main/error_page/"
    click MakefileNode "https://github.com/victra15/webserv/tree/main/Makefile"

    %% Styles
    classDef core fill:#b2f2bb,stroke:#2f9e44,stroke-width:2px
    classDef config fill:#cfe2ff,stroke:#3366cc,stroke-width:2px
    classDef parsing fill:#e7e6ff,stroke:#5f27cd,stroke-width:2px
    classDef io fill:#ffe8cc,stroke:#e8590c,stroke-width:2px
    classDef external fill:#f1f3f5,stroke:#868e96,stroke-width:2px,stroke-dasharray: 5 5
```
