Skip to content

A lightweight HTTP/1.1 server implementation supporting request routing, file operations, and compression with a process-per-connection concurrency model.

Notifications You must be signed in to change notification settings

Saul-Lara/http-server-csharp

Repository files navigation

progress-banner

🖧 HTTP Server (C#)

GitHub repo size GitHub license GitHub last commit emoji-log

This project is a basic HTTP server built with C# as part of the "Build Your Own HTTP server" challenge. It demonstrates the implementation of core HTTP features, including routing, file serving, header handling, and more.

Note

🧪 Try the challenge yourself on codecrafters.io!

📌 Features

✅ HTTP/1.1 protocol support
✅ Support for multiple endpoints (/, /echo, /user-agent, /files)
✅ Dynamic headers: Content-Length, Content-Type, Content-Encoding
✅ Gzip compression (when requested)
✅ File serving and uploads with configurable root directory
✅ MIME type detection
✅ Support for concurrent connections
✅ Basic error responses (404 Not Found, 500 Internal Server Error, etc.)

🚀 Built With

  • Programming Language: C# (.NET 9.0)
  • Automation: PowerShell (your_program.ps1) for local build/run

🔨 Manual Build & Run

dotnet build --configuration Release
./bin/Release/net9.0/codecrafters-http-server.exe [--directory <path>]

Options:

  • --directory <path>: Root directory for file handling (optional). Defaults to system temp path based on the OS (C:\tmp\ or /tmp/) if not provided.

🔧 Build & Run (PowerShell)

.\your_program.ps1 [-directory <path>]

Options:

  • -directory <path>: Root directory for file handling (optional).

📚 Endpoints

Method Endpoint Description
GET / Returns a 200 OK response
GET /echo/<string> Echoes the message back to the client
GET /user-agent Returns the client’s User-Agent string
GET /files/<filename> Serves a file from the server directory
POST /files/<filename> Uploads/updates a file in server directory

Gzip compression is applied to /echo if the client includes Accept-Encoding: gzip.

📝 Technical Details

  • Listens on port 4221
  • Raw socket usage via System.Net.Sockets
  • Requests are parsed from byte stream to extract method, path, headers, and body
  • MIME type detection
  • Gzip compression (when requested)
  • Concurrent requests handled using the Task-based asynchronous programming

🏷️ Response Headers

  • Content-Type: Appropriate MIME type for response
  • Content-Length: Size of response body
  • Content-Encoding: gzip: When compression is supported and used

⚠️ Error Handling

  • 404 Not Found: For non-existent resources
  • 500 Internal Server Error: For server-side failures

📓 Blog Explanation

Check out my post series for a detailed explanation of how this project was developed:

Bit by Bit: Building an HTTP Server in C#

💭 Conclusion

This project is a demonstration of how a HTTP server can be implemented. During the development, I learnt about some key concepts such as TCP connections, HTTP headers, HTTP verbs, handling multiple connections, and file serving.


About

A lightweight HTTP/1.1 server implementation supporting request routing, file operations, and compression with a process-per-connection concurrency model.

Topics

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •