Search code examples
chttpmongrel2

Obfuscated HTTP parser?


I am learning Go and I thought it would be a good exercise to implement my own HTTP 1.1 parser using Ragel and Go. I thought it would be a good idea to have a look at the code base of Mongrel2 to see how it's done.

The code for Mongrel's HTTP parser is here and I have a difficulty understanding the highlighted function http11_parser.c which seems to do the actual HTTP processing.

My questions are as follows:

  1. In plain English, what's the underlying idea behind the implementation? What does the code do?
  2. Assuming that there is such a thing as idiomatic C, is this code a good example of it? If not, is there a reason for all these gotos, nested if and switches?

PS. Regarding Q2, the only explanation I could find for using the goto is here. Note that my experience with C is 0.5 (on the scale from 1 - 10) which explains why I am having difficulty understanding this code!


Solution

  • The highlighted function was generated by a program. Note the comment near the top of the file:

    /** Machine **/
    
    
    #line 254 "src/http11/http11_parser.rl"
    

    So, you should look at the http11_parser.rl file to see the input that generated this code. It is a lexer for an HTTP/1.1 request.

    The idea behind the function is to parse an HTTP/1.1 request line and the headers that follow. Don't try to follow it to closely, focus on the pattern matching rules of the r1 file, and compare it to the specification in the HTTP/1.1 RFC.