How daohainam/mini-web-server works
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.