# stegvdm/simple-web-server architecture

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

- Interactive diagram: https://gitdiagram.com/stegvdm/simple-web-server
- Repository: https://github.com/stegvdm/simple-web-server

## Overview

##### Project type and purpose
- This is a C++ header-only library (with small .cpp example files) providing both HTTP/HTTPS client and server functionality.
- It wraps Asio (Boost.Asio or standalone) for async I/O and OpenSSL for TLS, exposing a simple REST-style interface for C++ applications.

##### File structure
- Top level:
- CMakeLists.txt, .clang-format, .travis.yml – build and CI config
- *.hpp – core headers: server_http, server_https, client_http, client_https, crypto, status_code, utility
- http_examples.cpp, https_examples.cpp – sample applications
- /web – static HTML for example servers
- /tests – unit tests for crypto, I/O, parsing, status codes
- Patterns: header-only API, examples & tests separated, CMake driving build, Travis CI integration.

##### Key README insights
- Asynchronous, multithreaded, platform-independent
- Supports HTTP/1.1 persistent connections and chunked transfers
- User defines REST resources via regex + callbacks
- Dependencies: Asio, Boost (for examples), OpenSSL (for HTTPS)

##### Main components
a. Library API layer
– ServerHTTP module
– ServerHTTPS module (builds on ServerHTTP + Crypto)
– ClientHTTP module
– ClientHTTPS module (builds on ClientHTTP + Crypto)
– Utility & StatusCode modules
b. Underlying I/O and crypto
– Asio (Boost.Asio or standalone)
– OpenSSL for TLS handshake, encryption/decryption
c. Application layer
– Example applications (http_examples, https_examples)
– User’s C++ code invoking library
d. External systems
– Web browser or external HTTP client
– Network (“TCP/IP stack”)

##### Relationships and interactions
- On the Server side:
1. Asio accepts TCP connections on a port
2. (HTTPS) OpenSSL handshake via Crypto module
3. Incoming HTTP message parsed (Utility + StatusCode)
4. Dispatcher matches regex paths → user callback
5. Callback processes request, builds response
6. Response sent back over Asio socket
7. Thread pool used to handle requests concurrently
- On the Client side:
1. User code calls ClientHTTP/ClientHTTPS to send request
2. Asio establishes TCP connection
3. (HTTPS) TLS handshake via OpenSSL
4. HTTP request serialized and sent
5. Response parsed by Utility + StatusCode
6. Control returned to user callback/future

##### Architectural patterns and design principles
- Layered architecture: Application → Library API → Transport (Asio) → OS
- Separation of concerns: HTTP vs HTTPS vs Crypto vs Utility
- Dependency inversion: user code passes callbacks, library controls threading
- Header-only design maximizes portability and ease of integration

##### Detailed element checklist
- Labels for each module and external dependency
- Call out the regex-based routing mechanism in the Server layer
- Optional TLS layer in HTTPS path

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Layer
    subgraph "Application Layer"
        direction TB
        HTTP_EX["HTTP Example App"]:::app
        HTTPS_EX["HTTPS Example App"]:::app
        Browser["Web Browser/External HTTP Client"]:::external
        UCB["User Callback\n(regex routing)"]:::app
    end

    %% Library API Layer
    subgraph "Library API"
        direction TB
        SRV_HTTP["ServerHTTP"]:::library
        SRV_HTTPS["ServerHTTPS"]:::library
        CLI_HTTP["ClientHTTP"]:::library
        CLI_HTTPS["ClientHTTPS"]:::library
        UTIL["Utility"]:::library
        STATUS["StatusCode"]:::library
        CRYPTO["Crypto"]:::libdep
    end

    %% Transport Layer
    subgraph "Transport Layer"
        direction TB
        ASIO["Asio (Boost.Asio/standalone)"]:::libdep
    end

    %% OS/Network Layer
    subgraph "OS/Network"
        direction TB
        TCP["TCP/IP Stack"]:::os
        OPENSSL["OpenSSL (TLS)"]:::libdep
    end

    %% Application to Library interactions
    HTTP_EX -->|"calls ClientHTTP"| CLI_HTTP
    HTTPS_EX -->|"calls ClientHTTPS"| CLI_HTTPS
    Browser -->|"connects to"| SRV_HTTP
    Browser -->|"connects to"| SRV_HTTPS

    %% Client Side Flow
    CLI_HTTP -->|"TCP connection"| ASIO
    CLI_HTTP -->|"serialize HTTP req"| UTIL
    UTIL -->|"uses codes"| STATUS

    CLI_HTTPS -->|"calls ClientHTTP"| CLI_HTTP
    CLI_HTTPS -->|"TLS handshake"| OPENSSL
    CLI_HTTPS -->|"uses Crypto"| CRYPTO
    CLI_HTTPS -->|"TCP connection"| ASIO

    %% Server Side Flow
    SRV_HTTP -->|"accept TCP conn"| ASIO
    SRV_HTTP -->|"parse HTTP req"| UTIL
    UTIL --> STATUS
    SRV_HTTP -->|"dispatch"| UCB
    UCB -->|"generates resp"| SRV_HTTP
    SRV_HTTP -->|"send HTTP resp"| ASIO

    SRV_HTTPS -->|"wraps ServerHTTP"| SRV_HTTP
    SRV_HTTPS -->|"uses Crypto"| CRYPTO
    SRV_HTTPS -->|"TLS handshake"| OPENSSL
    SRV_HTTPS -->|"accept TCP conn"| ASIO

    %% Transport to OS/Network
    ASIO -->|"reads/writes"| TCP
    OPENSSL -->|"secure I/O"| TCP

    %% Click Events
    click SRV_HTTP "https://github.com/stegvdm/simple-web-server/blob/master/server_http.hpp"
    click SRV_HTTPS "https://github.com/stegvdm/simple-web-server/blob/master/server_https.hpp"
    click CLI_HTTP "https://github.com/stegvdm/simple-web-server/blob/master/client_http.hpp"
    click CLI_HTTPS "https://github.com/stegvdm/simple-web-server/blob/master/client_https.hpp"
    click CRYPTO "https://github.com/stegvdm/simple-web-server/blob/master/crypto.hpp"
    click UTIL "https://github.com/stegvdm/simple-web-server/blob/master/utility.hpp"
    click STATUS "https://github.com/stegvdm/simple-web-server/blob/master/status_code.hpp"
    click HTTP_EX "https://github.com/stegvdm/simple-web-server/blob/master/http_examples.cpp"
    click HTTPS_EX "https://github.com/stegvdm/simple-web-server/blob/master/https_examples.cpp"
    click Browser "https://github.com/stegvdm/simple-web-server/tree/master/web/"
    click UCB "https://github.com/stegvdm/simple-web-server/tree/master/web/"
    click Tests "https://github.com/stegvdm/simple-web-server/tree/master/tests/"
    click CryptoTest "https://github.com/stegvdm/simple-web-server/blob/master/tests/crypto_test.cpp"
    click IOTest "https://github.com/stegvdm/simple-web-server/blob/master/tests/io_test.cpp"
    click ParseTest "https://github.com/stegvdm/simple-web-server/blob/master/tests/parse_test.cpp"
    click StatusTest "https://github.com/stegvdm/simple-web-server/blob/master/tests/status_code_test.cpp"
    click BuildConfig "https://github.com/stegvdm/simple-web-server/blob/master/CMakeLists.txt"
    click TestsBuild "https://github.com/stegvdm/simple-web-server/blob/master/tests/CMakeLists.txt"
    click CIConfig "https://github.com/stegvdm/simple-web-server/blob/master/.travis.yml"

    %% Styles
    classDef library fill:#cce5ff,stroke:#333,stroke-width:1px
    classDef libdep fill:#d4edda,stroke:#333,stroke-width:1px
    classDef app fill:#fff3cd,stroke:#333,stroke-width:1px
    classDef external fill:#e2e3e5,stroke:#333,stroke-width:1px
    classDef os fill:#f0f0f0,stroke:#333,stroke-width:1px
```
