# daohainam/mini-web-server architecture

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

- Interactive diagram: https://gitdiagram.com/daohainam/mini-web-server
- Repository: https://github.com/daohainam/mini-web-server

## Overview

##### Project Type & Purpose
- Mini-Web-Server is a full-fledged web server built in .NET 8. Its goal is twofold: to be a production-capable backend server (supporting HTTP 1.1, HTTPS, WebSocket, etc.) and to serve as a learning resource covering advanced topics (multithreading, HTTP protocols, OOAD, design patterns, etc.).
- It is organized as a multi-project solution where each project provides a set of abstractions or concrete implementations for various server functions. There is heavy use of design patterns (like builder and middleware/pipeline patterns) throughout the code.

##### Analyzing the File Structure
- The solution is split into clearly defined modules. For example:
- Middleware Folder: Contains many middleware components (Authentication, Authorization, CGI, HSTS, HTTPS Redirection, Mvc, OutputCaching, Session, StaticFiles, WebSocket). These components are designed to be plugged into the request/response pipelines.
- MiniWebServer.Abstractions & MiniWebServer.Server.Abstractions: Provide interfaces and base classes for HTTP handling, protocol parsing, and other server functionalities.
- MiniWebServer.HttpParser, MiniWebServer.Server (with ProtocolHandlers sub-folders), and MiniWebServer.MiniApp/MVC projects: Handle the low-level processing of incoming connections, HTTP parsing, the execution of MiniApps (dynamic content apps) and MVC controllers.
- Other supportive projects such as MiniWebServer.Configuration, MiniWebServer.Helpers, and MimeMapping offer configuration management, utility functions, and mapping of MIME types.
- There are also Docker and certificate directories, demonstrating considerations for containerization and secure connections.

a. Main Components:
- Client & Connection Layer:
- TCP/Socket connections coming from clients.
- When using HTTPS, clients connect via an SSL/TLS wrapper.
- Server Core:
- A central connection handler (MiniWebServer.Server, especially HandleNewClientConnectionAsync) that accepts connections and creates MiniWebClientConnection objects.
- The HTTP Parser (MiniWebServer.HttpParser and ProtocolHandlers in the Server module) that reads incoming streams and builds request objects using efficient pipelines.
- Request Processing Pipeline:
- Middleware Pipeline: Several middleware components are chained—including Authentication, Authorization, CGI processing, Session & Output Caching, Static File serving, and WebSocket support.
- Application Logic: Execution branches into either a dynamic MiniApp (via MiniWebServer.MiniApp) or an MVC-style processing (via MiniWebServer.Mvc projects).
- Utility & Configuration Components:
- Configuration modules (MiniWebServer.Configuration) that set options for the host and protocols.
- Helper utilities (MiniWebServer.Helpers, MimeMapping) that assist in parsing, mapping, and constructing responses.
b. Relationships & Data Flow:
- After the middleware chain, show a decision point or branch, where the request is dispatched to either the MiniApp for dynamic content or the MVC component for more structured controller-based processing.
- Optionally, include external dependencies or configuration (e.g., Docker, certificates) on the periphery to indicate the deployment environment.
c. Architectural Patterns & Design Principles:
- Use of the Middleware/Pipeline pattern that allows the extensibility of processing stages.
- Builder pattern used for creating objects like MiniWebServerBuilder, HttpWebRequestBuilder, etc.
- Layering that ensures strong separation of concerns (abstractions, protocol parsing, middleware handling, application logic).
- “read-only” design choices in internal components to promote immutability and resource pooling for performance.
d. Key Technologies & Frameworks:
- .NET 8 is the foundation.
- Built-in .NET libraries are preferred over third-party dependencies.
- Network stream handling via TcpClient, SslStream, and System.IO.Pipelines.
- Optional: Docker-related files and certificate management hint at containerized and secure deployments.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Client & Connection Layer
    subgraph "Client & Connection Layer"
        Client["Client"]:::clientLayer
        TCPConnection["TCP/Socket Connection"]:::clientLayer
        TlsWrapper["HTTPS/TLS Wrapper"]:::clientLayer
    end

    %% Server Core Layer
    subgraph "Server Core"
        ConnectionHandler["Connection Handler (MiniWebServer.Server)"]:::serverCore
        MiniWebClientConnection["MiniWebClientConnection"]:::serverCore
        ServerBuilder["Server Builder (MiniWebServerBuilder)"]:::serverCore
        HTTPParser["HTTP Parser (MiniWebServer.HttpParser)"]:::serverCore
        HTTPAbstractions["HTTP Abstractions (MiniWebServer.Abstractions)"]:::serverCore
        ServerAbstractions["Server Abstractions (MiniWebServer.Server.Abstractions)"]:::serverCore
        ResponseSender["Response Sender (Output)"]:::serverCore
    end

    %% Middleware Pipeline
    subgraph "Middleware Pipeline"
        Auth["Authentication Middleware"]:::middleware
        Author["Authorization Middleware"]:::middleware
        CGI["CGI Processing Middleware"]:::middleware
        HSTS["HSTS Middleware"]:::middleware
        HTTPSRedirect["HTTPS Redirection Middleware"]:::middleware
        OutputCache["Output Caching Middleware"]:::middleware
        Session["Session Middleware"]:::middleware
        StaticFiles["Static Files Middleware"]:::middleware
        WebSocket["WebSocket Support Middleware"]:::middleware
        MVCMW["MVC Middleware"]:::middleware
    end

    %% Application Logic
    subgraph "Application Logic"
        AppDispatch["Dispatch Request"]:::appLayer
        MiniApp["MiniApp Handler"]:::appLayer
        MVCProcessing["MVC Controller Processing"]:::appLayer
        ResponseBuilder["Response Builder"]:::appLayer
    end

    %% Utility & Configuration
    subgraph "Utility & Configuration"
        Config["Configuration"]:::utility
        Helpers["Helper Utilities"]:::utility
        MimeMapping["MIME Mapping"]:::utility
    end

    %% Deployment
    subgraph "Deployment"
        Docker["Docker Setup"]:::deploy
        Certs["Certificate Management"]:::deploy
    end

    %% Flow Connections
    Client --> TCPConnection
    TCPConnection --> TlsWrapper
    TlsWrapper --> ConnectionHandler

    %% Server Core connections
    ConnectionHandler --> HTTPParser
    HTTPParser --> HTTPAbstractions
    HTTPParser --> ServerAbstractions
    ConnectionHandler --> MiniWebClientConnection
    ConnectionHandler --> ServerBuilder

    %% Connection to Middleware Pipeline
    HTTPParser --> Auth
    Auth --> Author
    Author --> CGI
    CGI --> HSTS
    HSTS --> HTTPSRedirect
    HTTPSRedirect --> OutputCache
    OutputCache --> Session
    Session --> StaticFiles
    StaticFiles --> WebSocket
    WebSocket --> MVCMW
    MVCMW --> AppDispatch

    %% Application Logic branching
    AppDispatch --> MiniApp
    AppDispatch --> MVCProcessing
    MiniApp --> ResponseBuilder
    MVCProcessing --> ResponseBuilder

    %% Response flow back through Server Core
    ResponseBuilder --> ResponseSender
    ResponseSender --> Client

    %% Utility & Configuration connections
    ConnectionHandler --- Config
    Config --> Helpers
    Config --> MimeMapping

    %% Deployment connections
    ConnectionHandler --- Docker
    ConnectionHandler --- Certs

    %% Click Events
    click Auth "https://github.com/daohainam/mini-web-server/tree/master/Middleware/Authentication"
    click Author "https://github.com/daohainam/mini-web-server/tree/master/Middleware/Authorization"
    click CGI "https://github.com/daohainam/mini-web-server/tree/master/Middleware/CgiMiddleware"
    click HSTS "https://github.com/daohainam/mini-web-server/tree/master/Middleware/HstsMiddleware"
    click HTTPSRedirect "https://github.com/daohainam/mini-web-server/tree/master/Middleware/HttpsRedirection"
    click OutputCache "https://github.com/daohainam/mini-web-server/tree/master/Middleware/OutputCaching"
    click Session "https://github.com/daohainam/mini-web-server/tree/master/Middleware/Session"
    click StaticFiles "https://github.com/daohainam/mini-web-server/tree/master/Middleware/StaticFiles"
    click WebSocket "https://github.com/daohainam/mini-web-server/tree/master/Middleware/WebSocket"
    click MVCMW "https://github.com/daohainam/mini-web-server/tree/master/Middleware/Mvc"
    click HTTPParser "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.HttpParser"
    click ConnectionHandler "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Server/MiniWebServer.cs"
    click MiniWebClientConnection "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Server/MiniWebClientConnection.cs"
    click ServerBuilder "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Server/MiniWebServerBuilder.cs"
    click HTTPAbstractions "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Abstractions"
    click ServerAbstractions "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Server.Abstractions"
    click MiniApp "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.MiniApp"
    click MVCProcessing "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Mvc.Abstraction"
    click Config "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Configuration"
    click Helpers "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.Helpers"
    click MimeMapping "https://github.com/daohainam/mini-web-server/blob/master/MiniWebServer.MimeMapping"
    click Docker "https://github.com/daohainam/mini-web-server/tree/master/MiniWebServer/Docker"
    click Certs "https://github.com/daohainam/mini-web-server/tree/master/MiniWebServer/Certs"

    %% Styles
    classDef clientLayer fill:#f9e79f,stroke:#000,stroke-width:2px;
    classDef serverCore fill:#aed6f1,stroke:#000,stroke-width:2px;
    classDef middleware fill:#abebc6,stroke:#000,stroke-width:2px;
    classDef appLayer fill:#f5b7b1,stroke:#000,stroke-width:2px;
    classDef utility fill:#d2b4de,stroke:#000,stroke-width:2px;
    classDef deploy fill:#f8c471,stroke:#000,stroke-width:2px;
```
