How qinguoyi/tinywebserver works
Project Type and Purpose
• TinyWebServer is a lightweight, high‑performance, full‑stack C++ web server for Linux.
• It implements a browser/server (B/S) model, supporting HTTP GET/POST, static file serving (HTML, images, video), dynamic pages via MySQL-backed user registration/login, nonblocking sockets with epoll (ET/LT), thread pool concurrency (half‑sync/half‑reactive), Reactor/Proactor event models, timers for idle‐connection cleanup, and synchronous/asynchronous logging.
File Structure
• Top‑level:
• main.cpp, webserver.cpp/h: server entry point & core initialization
• build.sh, makefile: build scripts
• config.cpp/h: runtime configuration parser
• root/: static HTML and media assets
• CGImysql/: MySQL connection pool + CGI login/register code
• http/: HTTP connection handling, request parsing state machines
• threadpool/: thread‑pool implementation
• timer/: idle‑connection timer (sorted list)
• log/: synchronous/asynchronous logging subsystem
• lock/: mutex/sem wrappers
• test_pressure/: bundled Webbench tool for stress testing
• Configuration files (.sh, makefile) set up build, port, model flags.
• README documents architecture, demo, pressure test results, and command‑line options.
README Insights
• Concurrency model: thread pool + nonblocking sockets + epoll (supports ET & LT) + Reactor & simulated Proactor.
• HTTP parsing via finite state machine supports GET/POST.
• Synchronous/asynchronous logging.
• MySQL connection pool and CGI for user auth.
• Timer subsystem for timeouts.
• Command‑line switches to tune ports, log mode, epoll trigger modes, linger, SQL pool size, thread count, actor model.
• Client (Web browser or test tool)
• Network Interface / Listen Socket
• Event Demultiplexer (epoll instance)
• Reactor/Proactor Dispatcher
• Thread Pool
• HTTP Connection Handler (http_conn module, state machine)
• Timer Manager (lst_timer) for idle timeouts
• MySQL Connection Pool (CGImysql module)
• Logging Subsystem (sync/async log)
• Static File System (root directory)
• Configuration Manager (config module)
• External MySQL Database
b. Relationships & Data Flows:
1. Client → Listen Socket: new TCP connection.
2. Listen Socket → epoll: register FDs with ET/LT.
3. epoll events → Reactor/Proactor: demultiplex to accept/read/write handlers.
4. For each read event: Reactor enqueues HTTP task to Thread Pool; Proactor may already have read data.
5. Worker Thread → HTTP Connection Handler: parse request (state machine).
6. HTTP Handler:
• If dynamic (login/register) → CGI module → MySQL Pool → Database → response data.
• If static → file I/O from root directory.
7. After processing → write response (via epoll EPOLLOUT).
8. On each new/active connection → Timer Manager resets or schedules a timer. Timer expiration → close connection.
9. Throughout: Logging Subsystem records events to disk (sync or via logging thread with BlockQueue).
c. Architectural Patterns & Principles:
• Half‑Sync/Half‑Async: epoll for async I/O + thread‑pool for sync processing.
• Reactor & Proactor event models selectable at runtime.
• Resource pooling: database connections, threads.
• RAII for connection/lock management.
• Finite State Machine for HTTP parsing.
• Timer wheel / sorted list for efficient timeouts.
d. Key Technologies & Libraries:
• C++11 (RAII, smart pointers)
• Linux sockets API, non‑blocking fcntl
• epoll (ET/LT modes)
• POSIX threads + custom threadpool
• MySQL C API (pooled connections)
• Custom logging with blocking queue for async mode