How anasjwd/webserver works
Project Type and Purpose
• This is a standalone, full-feature HTTP/1.1 web server (“Webserv” / “GEET”) implemented in C++.
• It listens on TCP sockets, parses a custom configuration format, accepts multiple virtual hosts, parses HTTP requests, serves static files, executes CGI scripts, and builds HTTP responses.
File Structure
• Top-level entrypoints:
• Webserv.cpp & server_core.cpp: program initialization, argument parsing, setup of the listening socket, main event loop (select()).
• Connection.cpp/hpp: abstraction for client connections.
• conf/: configuration parser and directives
• cfg_parser, tokenizer, grammar rules, DirectiveFactory, individual directive classes (Listen, ServerName, Location, AutoIndex, etc.).
• request/: HTTP request modeling and parsing
• RequestLine, RequestHeaders, RequestBody, FileHandler for multipart/form-data.
• response/: HTTP response construction
• ResponseHandler, CgiHandler, ErrorResponse, FileResponse, DirectoryListing, ReturnHandler, MimeTypes.
• www/: document root, static assets, custom error pages, CGI scripts under cgi-bin/.
• Makefile: build orchestrator.
README
• Confirms use of non-blocking sockets with select(), support for GET/POST/DELETE, virtual hosting, chunked transfer, CGI execution, autoindex, MIME detection, and custom error pages.
Main Components & Their Interactions
a. Network Layer
• TCP Listener (socket(), bind(), listen())
• Connection Manager (select-based I/O loop)
• Connection objects (Connection.cpp)
b. Configuration Loader
• cfg_parser + DirectiveFactory → in‐memory config model (Servers, Locations, Directives)
c. Request Processing
• RequestParser module (request/srcs):
– Parse request line, headers, body
– Validate method, target, HTTP version
d. Routing & Dispatch
• Virtual Host selector (by “Host” header and listen directives)
• Location matcher (path prefixes, regex)
• Handler resolver (static file vs. directory listing vs. CGI vs. autoindex vs. error pages vs. custom return)
e. Response Generation
• FileResponse for static content
• DirectoryListing for autoindex
• CgiHandler for CGI scripts (fork(), exec(), environment setup)
• ErrorResponse for HTTP error codes
• Chunked encoding if needed
f. Resource Store
• File system under www/
• CGI scripts under www/cgi-bin/
g. Utilities
• MimeTypes lookup, logging (if any), utils for URL decoding, file I/O.
Architectural Patterns & Design Principles
• Modular layering: Network → Core Loop → Config → Request → Routing → Handlers → Response
• Factory pattern for directives and possibly for handlers
• Strategy pattern: different response handlers behind a common ResponseHandler interface
• Single-threaded, event-driven: select() loop for multiplexing
• Separation of concerns: parsing, routing, business logic (handlers), I/O