title: How to correctly parse incoming HTTP requests
body: <p>i've created an C++ application using WinSck, which has a small (handles just a few features which i need) http server implemented. This is used to communicate with the outside world using http requests. It works, but sometimes the requests are not handled correctly, because the parsing fails. Now i'm quite sure that the requests are correctly formed, since they are sent by major web browsers like firefox/chrome or perl/C# (which have http modules/dll's).</p>
  
  <p>After some debugging i found out that the problem is in fact in receiving the message. When the message comes in more than just one part (it is not read in one <code>recv()</code> call) then sometimes the parsing fails. I have gone through numerous tries on how to resolve this, but nothing seems to be reliable enough.</p>
  
  <p>What i do now is that i read in data until i find <code>"\r\n\r\n"</code> sequence which indicates end of header. If <code>WSAGetLastError()</code> reports something else than 10035 (connection closed/failed) before such a sequence is found i discard the message. When i know i have the whole header i parse it and look for information about the body length. However i'm not sure if this information is mandatory (i think not) and what should i do if there is no such information - does it mean there will be no body? Another problem is that i do not know if i should look for a <code>"\r\n\r\n"</code> after the body (if its length is greater than zero).</p>
  
  <p>Does anybody know how to reliably parse a http message?</p>
  
  <p>Note: i know there are implementations of http servers out there. I want my own for various reasons. And yes, reinventing the wheel is bad, i know that too.</p>