Delta Blogs
Published in

Delta Blogs

Pheme: A Web Framework written in Python | DevBlog 2-Request Parsing

Photo by JJ Ying on Unsplash

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

Request line consists of three components

  1. Request method: GET,POST,PUT,DELETE etc
  2. The path: For example /image
  3. 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.

Method

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
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.

Request-URI

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.

Request Parameter

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

http://127.0.0.1:8000/?a=a

Conclusion

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

--

--

--

This is a publication where developers can come together to blog about their projects.

Recommended from Medium

My first XSS Challenge which hosted by bugpoc.com

How I improved the ScholarX Email Notification System?

RESTful API, HOW TO | Part 3 — Testing

How to Store Data in Python

The Flutter Missing Best Practices Book

Meet the team: Kasper Grubbe

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Uber Dev

Uber Dev

I am a Developer. I love to create projects in the field of Computer Science and Software Development.

More from Medium

Build Earthquake Update Program With Python Modularization and Upload to PyPI

A quick hack to share non-input data between templates in a Flask app

Getting Started with Kivy: How to Code Desktop and Mobile Apps with Python

Screen Shot 2022-03-09 at 2.27.30 PM.png

Run Python in browser using Skulpt: Part 1: The basic stuff