2

I have a programming project where I have to create a multithreaded web server which handles HTTP requests.

I just learned socket programming and I got a client and a server running. I wanted to know what the best method would be for parsing the HTTP request headers. I saw this: how to parse http request in c++ a few minutes back. But I would rather not shift to C++ at this point. So how should one go about parsing an HTTP request in C?

Community
  • 1
  • 1
Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
  • Use Ragel or some other state machine to parse http headers. Just like mongrel/2. More on that: http://www.zedshaw.com/essays/ragel_state_charts.html – Eimantas Oct 31 '13 at 09:07
  • read some opensource http server. – moeCake Oct 31 '13 at 09:07
  • You can try this :http://stackoverflow.com/questions/11208299/http-get-request-using-c-without-libcurl or this https://github.com/joyent/http-parser – Stef Oct 31 '13 at 09:08
  • 1
    Use some HTTP server library like [libonion](http://www.coralbits.com/libonion/) - which is a mostly C library. [HTTP](http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is quite a complex protocol. Better use existing libraries implementing it... – Basile Starynkevitch Oct 31 '13 at 09:11

1 Answers1

3

You can have a look at the web servers in C such as mongoose (https://github.com/cesanta/mongoose/blob/master/mongoose.c) and could use the same methodology to parse the http request. But what would I suggest is that just go through the HTTP RFC 2616 since that would help you in writing your own parser for http requests.

By the way which kind of HTTP requests your Server is handling (GET or POST or BOTH) ??

In http post requests the HTTP header & data are separated by "\r\n\r\n". In received data sscanf the Content-Length form the http header then start reading the data after you get "\r\n\r\n" until you get the same amount of data as mentioned in Content-Length.

Ankit Tripathi
  • 374
  • 1
  • 8