Joel Kleier's Electric Froth

up

Anatomy of an HTTP Request

by Joel Kleier on 2015-01-17


Last time Rust 1.0 Alpha was released, and I decided to dive in and learn a little about it by creating a small (very bad and near-featurless) http server.

I decided to create something that looks vaguely like Python’s WSGI (and I do mean 'vaguely'). Basically the process for my server will go like this:

  1. Establish connection and spawn a thread to handle it

  2. Grab all the relevant data out of the request and put it into a more maleable datastructure (HTTPEnv in my case)

  3. Pass the organized data off through a chain of handlers that accept the data and return a structure representing the response

  4. At the end of the chain the response is converted into a string and returned to the requestor

For now, lets get into the the HTTPEnv data structure. This struct will organize all request information — method (IE get/post/delete/etc), target (URI path and GET values), version, headers, and post data.

So far, this is what I have:

struct HTTPEnv {
    method: String,
    target: String,
    version: String,
    headers: HashMap<String, String>,
    get: HashMap<String, String>,
    post: HashMap<String, String>
}

I’m struggling a bit with the whole ownership thing, so the String type might get switched with &str since I really don’t need the more dynamic nature of String for most of these values.

I may or may not decide to continue to pursue Rust for this project. While I’m really interested in rust, I don’t know if I’ll actually get enough benefit out of it for this particular project (which is more than just the http server portion I’m writing about). If I do stick with it, I’ll get more into the structure for responses, and the middle-ware chain that’ll be implemented.

If I don’t continue with it, well, perhaps I’ll find another use for Rust I can use to learn it more deeply!