A lightweight, high-performance HTTP server built from scratch in C# using raw sockets. This server demonstrates core HTTP protocol implementation with async request handling, middleware pipeline support, and a clean modular architecture.
- Raw Socket Implementation - Built without high-level HTTP frameworks for maximum control and learning
- Async Request Handling - Non-blocking request processing for better performance
- Middleware Pipeline - Extensible middleware system for request/response processing
- Routing System - Simple but effective routing for different HTTP methods and paths
- HTTP/1.1 Support - Proper HTTP request parsing and response formatting
- Can easily add whatever endpoint you want
- I included a simple GET endpoint and a POST endpoint for demonstration
- .NET 8.0 or later
- Any C# development environment
- Clone the repository:
git clone https://github.com/Ozzy-ZY/Minimal-HTTP-Server-Rewrite
cd Minimal-Web-Server-Rewrite
- Build the project:
dotnet build
- Run the server:
dotnet run
The server will start on http://localhost:5000
and display "Server started" in the console.
GET Request:
curl http://localhost:5000/
# Response: hello world
POST Request:
curl -X POST http://localhost:5000/ -d "Hello from POST request"
# Appends the data to data.txt file
Performance benchmarks conducted using Apache Bench (ab) showing server response times and throughput under load.
The server uses async/await throughout for non-blocking I/O operations, allowing it to handle multiple concurrent connections efficiently.
Supports both synchronous and asynchronous request handlers through a clean interface hierarchy:
IHttpHandler
- Base interfaceIAsyncHandler
- For async operationsISyncHandler
- For synchronous operations
Extensible middleware system allows for cross-cutting concerns like logging, authentication, and response modification.
The HttpResponse.ResponseBuilder
class provides a fluent API for constructing HTTP responses.
public class CustomHandler : IAsyncHandler
{
public async Task<HttpResponse> HandleRequestAsync(HttpRequest request, Socket socket)
{
// Your handler logic here
return new HttpResponse.ResponseBuilder()
.WithStatusCode(HttpStatusCode.Ok)
.WithStringBody("Custom response")
.Build();
}
}
public class CustomMiddleware : IResponseMiddleware
{
public Task ProcessAsync(HttpRequest request, ref HttpResponse response, ref LinkedListNode<IResponseMiddleware>? next)
{
// Your middleware logic here
return Task.CompletedTask;
}
}
// In Server.cs Main method
router.AddRoute(HttpMethod.Get, "/custom", new CustomHandler());
pipeline.Use(new CustomMiddleware());
This project demonstrates:
- Raw socket programming in .NET
- HTTP protocol implementation
- Async/await patterns for I/O operations
- Middleware implementation
- Builder and factory patterns
This project is open source and available under the MIT License.
Feel free to fork this project and submit pull requests for improvements. This is a learning project, so educational enhancements are especially welcome!