[cURLpp] problème pour le téléchargement d'un fichier
Bonjour,
J'ai fait le code suivant dans le but de télécharger un fichier, mon problème est que si je télécharge un fichier de petite taille, tout fonctionne (checksum md5 ok) mais si je télécharge un fichier plus volumineux (ex:10Mb), la checksum md5 ne correspond plus...
Je ne sais pas d'oû vient mon problème mais j'ai un doute sur ma gestion du buffer...
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#define MAX_FILE_LENGTH 20000
using namespace cURLpp::Options;
class WriterMemoryClass
{
public:
WriterMemoryClass()
{
this->m_pBuffer = NULL;
this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char));
this->m_Size = 0;
};
~WriterMemoryClass()
{
if (this->m_pBuffer)
free(this->m_pBuffer);
};
void* Realloc(void* ptr, size_t size)
{
if(ptr)
return realloc(ptr, size);
else
return malloc(size);
};
// Callback must be declared static, otherwise it won't link...
size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb)
{
// Calculate the real size of the incoming buffer
size_t realsize = size * nmemb;
// (Re)Allocate memory for the buffer
m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize);
// Test if Buffer is initialized correctly & copy memory
if (m_pBuffer == NULL) {
realsize = 0;
}
memcpy(&(m_pBuffer[m_Size]), ptr, realsize);
m_Size += realsize;
std::ofstream fichier("file.test", std::ios::out | std::ios::trunc);
if(fichier)
{
fichier << m_pBuffer;
fichier.close();
}
// return the real size of the buffer...
return realsize;
};
char* m_pBuffer;
size_t m_Size;
};
int main ()
{
try
{
WriterMemoryClass mWriterChunk;
cURLpp::Cleanup myCleanup;
cURLpp::Easy myRequest;
cURLpp::Types::WriteFunctionFunctor functor(&mWriterChunk, &WriterMemoryClass::WriteMemoryCallback);
cURLpp::Options::WriteFunction *test = new cURLpp::Options::WriteFunction(functor);
myRequest.setOpt(test);
myRequest.setOpt(new cURLpp::Options::Url("http://urltest.test/file.test"));
myRequest.setOpt(new cURLpp::Options::Verbose(true));
myRequest.perform();
}
catch(cURLpp::RuntimeError & e)
{
std::cout << e.what() << std::endl;
}
catch(cURLpp::LogicError & e)
{
std::cout << e.what() << std::endl;
}
return 0;
} |
Est-ce que quelqu'un peut me donner des conseils sur ce code ou alors si quelqu'un connait un code qui fonctionne pour télécharger correctement n'importe quel fichier avec cURLpp?
Merci