Reputation: 31
I made a socket in C. This program is operated by command line and compiled on Linux. It does the following:
My question is:
How to remove this HEADER before the file is recorded by the fwrite () function. like a byte to byte code, to find a double line "\ n \ n".
Upvotes: 1
Views: 3710
Reputation: 529
An HTTP header always ends in \r\n\r\n
. Therefore you just want to find the first string match of that, and increment the position by 4 bytes.
The strstr()
function (found in string.h
) does just this:
char * strstr(const char *str1, const char *str2);
Where str2
is the string you're searching for in str1
.
It returns the memory location to the beginning of the first match. If the string is not found it returns NULL
.
So, if you assume the buffer is larger than the header:
char *buf = getHTTPResponse(url); // Get HTTP response from URL
char *content = strstr(buf, "\r\n\r\n");
if (content != NULL) {
content += 4; // Offset by 4 bytes to start of content
}
else {
content = buf; // Didn't find end of header, write out everything
}
// Write out content to file
See here for a complete HTTP client example in C that uses strstr()
.
Upvotes: 6