detecting end of http header with \\r\\n\\r\\n

2019-07-21 12:38发布

问题:

Using recv I want to get the http header so I can parse for a content length. However I'm having trouble detecting the line break. Or actually do I even have to detect line break or will the first time I read into the buffer always be the complete header (assuming I have a long enough buffer).

This is written in C.

edit: looking at some of the related questions one of the things I am worried about is "...the "\r\n" of the header break might be pulled into your buffer by two different calls to recv() which would prevent your code from recognizing the header break."

回答1:

You should call recv() repeatedly and each time it gives you x bytes you increase the buffer-pointer you give to it by x bytes (and decrease the cb it is allowed to write also by x bytes). You do this until your buffer either contains a \r\n\r\n or is completely full, in which case you just close the socket and ignore the malicious client from then on. Buffer-size should be about 3000 bytes.

But: this ignores the general problem that your server seems to be a polling-server. If you have some experience you should try to make an epoll-server instead.



回答2:

In addition to the problem of identifying "\r\n\r\n" across packet boundaries, you have the problem of identifying "Content-Length: xxxx\r\n" across packet boundaries. I suggest recieving and parsing one byte at a time. When you get a recv() of '\r' followed by a recv() of '\n', followed by a recv() of '\r' followed by a recv() of '\n', you can be sure the header has ended. Once you've grasped this, adapt your solution to recieve and parse n bytes at a time where n is a preprocessor definition defined to 1 initially, and change n.



回答3:

In the end I did something like this:

while ( recv... > 0 ) {
     if rnrn is inside the buffer using strstr
         look for content length, output error if content length doesn't exist
     else
         keep on reading into the buffer
}

and then once the header is found I keep on reading for the message body.

anyway thanks guys, ended up doing a really inefficient way to get my answer but what must be done is done.



标签: c http