Hyper dust
- 1 Devlogs
- 7 Total hours
A from scratch http server written in rust
A from scratch http server written in rust
Build a http server crate from scratch in rust. I want to learn more about rust and I think this is a great project for that.
Because of that I also want to use as little dependencies as possible. But I do want some performance so I will go for async rust and the tokio runtime.
I am also building “test driven” by first defining some unit tests and then building around them.
I am also parallely learing nvim, so developement might be a bit slowed down in the first 10hrs.
I began with defining a layout
├── Cargo.lock
├── Cargo.toml
├── examples
│ └── listener.rs
└─ src
├── lib.rs
├── request.rs
├── response.rs
├── router.rs
├── server.rs
└── shared.rs
I looked at MDN typical http session and built a quick program, which uses a tcp listener and just prints all input. I then used curl to test a request. My first attempt failed because my output was garbage, but that was because HTTP uses CRLF (\r\n) endings which garbles terminal output, because it moves the cursor around. I fixed this by escaping all CRLF’s but still printing a real newline to show the real data and still make it visually seperated.
The header and body are seperated by an empty line / double CRLF
I added strum, which is a crate for (de)serializing enums. I had like 50 lines which just consisted of: “GET” => Method::GET. strum has some macros to do this automatically and is great
!
On my first attempt I just converted everything to a str and then used some splits to parse every part. This worked fine in the tests where the body is just some text, but if the body is something else like a png I would be trying to convert that to text, which is a clear
.
So I switched my approach to a byte focussed approach, where it searches through the bytes to find the sections of the start line and the headers. This way only the necessary parts have to be converted to a str.
Take a look at the glorious output in the attached pictures. Next up I will add some documentation and then move on to the response generation.
This is my second time typing this because the last was too long and stardance just deleted the whole thing as revenge 