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!
✅ 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.)
- Programming Language: C# (.NET 9.0)
- Automation: PowerShell (
your_program.ps1
) for local 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.
.\your_program.ps1 [-directory <path>]
Options:
-directory <path>
: Root directory for file handling (optional).
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 includesAccept-Encoding: gzip
.
- 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
Content-Type
: Appropriate MIME type for responseContent-Length
: Size of response bodyContent-Encoding: gzip
: When compression is supported and used
404 Not Found
: For non-existent resources500 Internal Server Error
: For server-side failures
Check out my post series for a detailed explanation of how this project was developed:
Bit by Bit: Building an HTTP Server in C#
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.