5

I'm currently writing my own C++ HTTP class for a certain project. And I'm trying to find a way to separate the response body from the header, because that's the only part I need to return.

Here's a sample of the raw http headers if you're not familiar with it:

HTTP/1.1 200 OK
Server: nginx/0.7.65
Date: Wed, 29 Dec 2010 06:13:07 GMT
Content-Type: text
Connection: keep-alive
Vary: Cookie
Content-Length: 82

Below that is the HTML/Response body. What would be the best way to do this? I'm only using Winsock library for the requests by the way (I don't even think this matters).

Thanks in advance.

Ruel
  • 15,438
  • 7
  • 38
  • 49

1 Answers1

29

HTTP headers are terminated by the sequence \r\n\r\n (a blank line). Just search for that, and return everything after. (It may not exist of course, e.g. if it was in response to a HEAD request.)

j_random_hacker
  • 50,331
  • 10
  • 105
  • 169
  • Thanks, but is there any chance that the response body has the same sequence? I'm planning to split it. – Ruel Dec 29 '10 at 06:23
  • @Ruel: I should have said "if it was in response to a `HEAD` request". In HTTP, `\r\n\r\n` terminates headers for both requests and responses. – j_random_hacker Dec 29 '10 at 06:31
  • Alright, solved. Thanks. `response.substr(response.find("\r\n\r\n"));` – Ruel Dec 29 '10 at 06:34