You are browsing as a guest. Sign up (or log in) to start making projects!

Hyper dust

  • 1 Devlogs
  • 7 Total hours

A from scratch http server written in rust

Open comments for this post

6h 46m 17s logged

Goals

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.

Start

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

Learing

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.

HTTP request structure

  1. Start line - structured like “METHOD PATH VERSION”, e.g. GET / HTTP/1.1
  2. Headers - pairs of key and values seperated by a : and optionally a space, e.g. Accept: /
  3. Body - Any type of added data. Can be anything from a picture to text.

The header and body are seperated by an empty line / double CRLF

Dependencies

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 :yay: !

Parsing

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 :blunder: .
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 :fear:

0
0
7

Delete project?

Are you sure you want to permanently delete this project? This action cannot be undone.

All devlogs, followers, and associated data will be removed.

Followers

Loading…