Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove HTTP Header Info

In C is there a way to exclude the HTTP header information that comes with the data when using recv() on a socket? I am trying to read some binary data and all I want is the actual binary information, not the HTTP header information. The current data received looks like this:

HTTP/1.1 200 OK
Content-Length: 3314
Content-Type: image/jpeg
Last-Modified: Tue, 20 Mar 2012 14:51:34 GMT
Accept-Ranges: bytes
ETag: "45da99f1a86cd1:6b9"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Mon, 20 Aug 2012 14:10:08 GMT
Connection: close 

╪ α

I would like only to read the binary portion of the file. (That's obviously not all the binary, only that much was displayed since I printed the output from my recv loop as a string and the first NULL char is after that small binary string).

I just need to get rid of the header portion, is there a simple way to do this?

like image 892
Keith Miller Avatar asked Aug 20 '12 14:08

Keith Miller


People also ask

How do I edit HTTP headers?

After installing the extension, Go to app.requestly.io/rules.Then create rule and select "Modify Headers". After that give the rule name, and from here you can add, override and remove headers by providing a Header name and value for that header.


1 Answers

You would be better of using some HTTP parsing library like curl

If you want to do it yourself:

You can search for '\r\n\r\n' (two \r\n) which separates HTTP headers and contents, and use string/buffer after that.

Also, you need to get Content-Length from header and read that many bytes as http content.

Something like:

/* http_resp has data read from recv */
httpbody = strstr(http_resp, "\r\n\r\n");
if(httpbody) 
    httpbody += 4; /* move ahead 4 chars
/* now httpbody  has just data, stripped down http headers */

Note: make sure strstr does not overrun the memory, may be using strnstr (not sure this exists or not) or similar functions.

like image 64
Rohan Avatar answered Nov 10 '22 00:11

Rohan