Remove http header in socket C

Asked

Viewed 92 times

1

I made a socket in C. This program is command-line operated and compiled over Linux. It takes as parameter a complete URI and a file name, and then connects to the server, retrieves the page and saves it in the informed file. After obtaining the remote data. The file received by the function recv comes with a HEADER.
The question is: How to remove this HEADER before the file is written by the fwrite function().

1 answer

1

Do not start writing the final file before the header is finished

int headerterminado = 0;
char *p = NULL;
while (recv(socket, buffer, ...)) {
    p = strstr(buffer, "\r\n\r\n");
    if (p) headerterminado = 1;
}

'Of course you have to watch the case "\r\n\r\n" 'and party at 2 recv()s.

  • It didn’t work, I’m trying to do it with a state machine, byte by byte. Sorry ignorance, but nas I am able to post my code formatted here in this post.

  • creates the states HEADER0, HEADER1, HEADER2, and HEADER3. When you’re in HEADER0 and receive a '\r' becomes a state HEADER1; otherwise remains in stadium HEADER0; if you’re in a state HEADER1 and you get '\n' passes to HEADER2; or else go back to HEADER0; when state is HEADER2 and you get '\r' passes to HEADER3; when state is HEADER3 and you get '\n' that '\n' is the last byte of the header.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.