I have made this code below to download large file and save it to file. (I removed not important parts)
int nDataLength;
int i = 0;
static char buffer[4096];
while ((nDataLength = recv(Socket, buffer, sizeof(buffer), NULL)) > 0)
{
//MessageBoxA(0, std::to_string(nDataLength).c_str(), "TEST", 0);
fwrite(buffer, nDataLength, 1, pFile);
}
Now It saves file, but it also saves HTTP header. Now I don't really know how to strip the header from received data.
If it was small enough I could read Content-Length from buffer and then open the file again and remove header, but thats not the option cause buffer will be overwritten with new data.
Also I cannot use other libraries like libcurl etc.
EDIT:
char* content = strstr(buffer, "\r\n\r\n");
if (content != NULL) {
content += 4;
fwrite(content, nDataLength, 1, pFile);
}
else
{
fwrite(buffer, nDataLength, 1, pFile);
}
OK, I came up with function that strips header before saving.
int nDataLength;
int i = 0;
static char buffer[4096];
while ((nDataLength = recv(Socket, buffer, sizeof(buffer), NULL)) > 0)
{
char* content = strstr(buffer, "\r\n\r\n");
if (content != NULL) {
std::string s2(buffer);
size_t p = s2.find("\r\n\r\n");
fwrite(buffer+p+4, nDataLength-p-4, 1, pFile);
}
else
{
fwrite(buffer, nDataLength, 1, pFile);
}
}