Pheme: A Web Framework written in Python | DevBlog 2-Request Parsing
Here we are again with another DevBlog of Pheme. This time we’re going to be tackling the problem of request parsing. Let’s get started.
Format of an HTTP request
To parse an HTTP request we need to study the format of an HTTP request. According to Wikipedia, a request message consists of the following:
- Request line (e.g., GET /images/logo.png HTTP/1.1)
- Request header fields(e.g., Accept-Language: en)
- An empty line
- An optional message body
Let’s tackle each of them individually.
Request line consists of three components
- Request method: GET,POST,PUT,DELETE etc
- The path: For example /image
- HTTP version
Furthermore, when I dig a little deeper, I found this w3 page about request format. The format given there was
Request-Line = Method SP Request-URI SP HTTP-Version CRLF
where sp is space and crlf is \r\n.
Then we have to dig into the syntax of the Method and Request-URI.
The Method format is as follows
Method = "OPTIONS" ; Section 9.2
| "GET" ; Section 9.3
| "HEAD" ; Section 9.4
| "POST" ; Section 9.5
| "PUT" ; Section 9.6
| "DELETE" ; Section 9.7
| "TRACE" ; Section 9.8
| "CONNECT" ; Section 9.9
extension-method = token
I had no clue about extension methods. So I googled it. Turns out it is a token of specific format that has to defined by the creator of the program which in our case is the programmer. We don’t need to worry about that.
The Request-URI’s format is as follows
Request-URI = "*" | absoluteURI | abs_path | authority
The asterisks means the request is sent to the server itself not to any specific path.
Absolute URI comes of use when proxies are involved. It doesn’t matter as we are the server to which the proxy is going to be requesting the information.
Abs path is the path itself which we parsed in the last DevBlog.
The authority is used by the connect method. Let’s not worry about that now. Perhaps we’ll tackle that in a future episode.
Request Parser Class
I’m thinking of storing each of the parts of request as separate members of a class. We could delete this class after its use. Now it’s time to parse some requests.
First and foremost, we have to split line requests by \r\n. The first request is obviously the request line. We’ll split the request line by each word and get the method and path from it.
Then we have the headers in every line before the empty line. We find the index of the empty line and parse everything else as a header and parse all that comes after as the body.
We also have to parse the parameters from the path. So I’m going to convert path to dictionary with uri and parameters as different keys.
Now comes the fun part. We can give the requests as parameter to the function that we declared earlier.
We can try out using the parameters to send the response in the main.py.
This is the response received when I sent the request
That’s it for this DevBlog. Next time we’ll extend support to various methods of requesting information including POST,GET and DELETE. At least I hope so. Here’s the Github Link for the code.
As Medium hasn’t integrated payments to India there is no way for me to monetize my content. If you like my article, please consider Buying me a cup of coffee