So I'm writing this simple HTTP Client in C, and, well, I don't know C that well. Here's the gist of it. I got the HTTP client working, I can send the GET request and a get response back. I even can parse the HTTP headers to get the information I want (though my solution to that is admittedly inelegant). The problem is when it's binary information, like a picture file. I'm sure it's something simple, but ... well here's a slimmed down version of what I have.
FILE *fp = fopen("output", "wb"); // Open the file for writing
while (recv(s, buf, MAXDATASIZE - 1, 0) > 0) { //This just iterates over our HTTP response, it works don't worry
if (is_text == 1) { // Assume I already parsed the headers and know the Content-type
fprintf(fp, "%s", buf); // This works, it just writes the text to our output file
}
else { // Anything not text, this is where the problem is
fwrite(buf, sizeof buf, 1, fp); // <-- **This is the problem line**
}
memset(buf, 0, MAXADATASIZE -1, 0); // Reset the buffer, for the next iteration
}
flose(fp); // Close the file
So, the line I have marked as the problem line does write some binary stuff to my output file, it just never matches the original file. Not only do they differ, they differ in size by like orders of a hundred. It's just not the same file. Yes, I know need to figure out a way to strip the headers or the files will always be different, but clearly that's not even the problem here, yet.
--
Extra information:
Ubuntu 18.04.5
gcc version 7.5.0
"Hardware" is a virtualbox VM, 64-bit