How to parse HTTP requests from a C based web serv

2019-03-22 08:50发布

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?

标签: c http
1条回答
老娘就宠你
2楼-- · 2019-03-22 09:17

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.

查看更多
登录 后发表回答