1

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!

Community
  • 1
  • 1
Sergey Koulikov
  • 267
  • 4
  • 7
  • 16
  • 1
    Quite honestly, that code looks like it was generated by some program (something similar to `lex`, maybe?). Definitely not a coding style I would try to imitate. – jxh Jun 19 '13 at 02:53
  • @jxh You're right! This is a Ragel based parser. http11_parser.c is generated from [http11_parser.rl](https://github.com/zedshaw/mongrel2/blob/master/src/http11/http11_parser.rl) using the goto driven format. That explains it! – Sergey Koulikov Jun 19 '13 at 03:11
  • Nicely done. I was typing in a quick answer when you posted this. – jxh Jun 19 '13 at 03:20

1 Answers1

1

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.

jxh
  • 69,070
  • 8
  • 110
  • 193