Skip to content

gabfec/rust-http-server

Repository files navigation

HTTP Server in Rust (Built from Scratch)

Tests License Made with Rust Rust

A minimal HTTP/1.1 server implemented from scratch in Rust using raw TCP sockets.


Features

  • Manual HTTP/1.1 request parsing
  • Manual response construction
  • Thread-per-connection concurrency
  • Persistent connections (keep-alive)
  • Gzip compression (when Accept-Encoding: gzip is sent)
  • Static file serving
  • File upload via POST
  • Content-Length handling
  • Proper CRLF formatting

Supported Routes

Route Method Description
/ GET Returns 200 OK
/echo/{text} GET Returns {text}
/user-agent GET Returns the User-Agent header
/files/{filename} GET Serves file from directory
/files/{filename} POST Writes body to file

Unknown routes return 404 Not Found.


Run

Start the server:

cargo run

Serve files from a directory:

cargo run -- --directory ./public

Server runs on:

127.0.0.1:4221

Example

curl http://localhost:4221/echo/hello
curl -X POST --data "data" http://localhost:4221/files/test.txt
curl -H "Accept-Encoding: gzip" http://localhost:4221/echo/hello

Project Structure

.
├── main.rs
├── server.rs
├── handlers.rs
├── utils.rs
└── http/
    ├── request.rs
    └── response.rs

Tests

Run tests:

cargo test

Educational Purpose

This project was built to understand TCP networking and HTTP/1.1 internals by implementing a server without external frameworks.

Challenge by CodeCrafters: https://codecrafters.io


License

MIT License — see the LICENSE file.

About

HTTP Server written in Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors