How lucielebriquer/webserv works
Project Type and Purpose
• This is a custom, single‐process, event‐driven HTTP server (“Webserv”) written in C++.
• It implements a full TCP + HTTP/1.1 stack, parses a configuration file (nginx‐style), handles virtual hosts and locations, serves static files and invokes CGI (e.g. PHP) for dynamic responses.
High‐Level Architectural Components
• Configuration & Parsing Layer
– Config Parser (sources/parsing/*.cpp + includes/Config.hpp / Location.hpp / Server.hpp / Block.hpp)
– Builds in‐memory data structures representing server blocks, locations, error pages, root paths, allowed methods, CGI mappings.
• Networking & Concurrency Layer
– Socket abstraction (includes/Socket.hpp + sources/engine/class/Socket.cpp)
– epoll‐based Event Loop (sources/engine/epoll.cpp / engine.hpp)
– Client session management (includes/Client.hpp + sources/engine/class/Client.cpp)
• HTTP Processing Layer
– HTTP Request Parser (includes/httpRequest.hpp + sources/request/httpRequest.cpp / httpHeader.cpp)
– Router (matches incoming Host:port + request‐URI against parsed Server/Location objects)
– HTTP Response Builder (includes/httpResponse.hpp / Status.hpp / mime.hpp + sources/response/*.cpp)
• Content Handling Layer
– Static File Handler (reads from disk, infers MIME, supports range requests, chunked encoding)
– CGI Engine (sources/engine/cgi.cpp / env.cpp + execve to run PHP or other CGI)
• Utility & Support
– Logging, error‐handling, helper functions (includes/usefull.hpp, sources/parsing/usefull.cpp, sources/engine/utils.cpp)
Component Relationships & Data Flow
1. Startup (main.cpp)
• Read and parse config files (config/basic.conf, config/tenServ.conf, etc.) → build Config objects.
• Initialize one listening TCP socket per server block (bind/listen on HOST:PORT).
• Create epoll instance and register all listening sockets.
2. Event Loop (epoll_wait)
• On readable event of a listening socket → accept() → create Client instance → register client FD in epoll.
• On readable event of a client FD → recv() raw bytes → HTTP Request Parser → HTTPRequest object.
• Router selects matching Server + Location based on Host header and URL.
• Handler chooses:
– Static: open file, determine MIME, read content (sources/response/default.cpp).
– CGI: fork + execve(CGI_PATH) with env variables (sources/engine/cgi.cpp + env.cpp), read CGI stdout.
• Build HTTPResponse (status line, headers, body) → send() back to client (sources/engine/send.cpp).
• On FIN or error → close FD, remove from epoll.
Elements
• Clear component names matching directories/files (e.g. “Client Session”, “Socket”, “epoll”, “Config Parser”).
• Breakdown of static vs dynamic content paths.
• Indication of environment variables passed to CGI (sub‐step in content handling).