curl-library
Lost data in chunk HTTP response using libcurl
Date: Wed, 17 Oct 2012 21:39:51 +0800
Dear all,
I'm using libcurl to build a HTTP proxy in C++ currently. It worked perfect
for non-chunk HTTP(those HTTP responses with "Content-length"). However,
when it needed to handle chunk HTTP, the responses always lost data(checked
through byte size and md5sum) compared with the responses I captured using
wireshark. The most obviously losing data are the chunked sizes at the
beginning of the HTTP responses' message body and the digital 0 to indicate
ending at the end.
I suspect my callback function got problem. Here are the relevant codes:
size_t httpClient::write_data(char *ptr, size_t size, size_t nmemb, void*
userdata)
{
// get total size
size_t realsize = size*nmemb;
// get object
struct MemoryStruct* mem = (struct MemoryStruct*)userdata;
// size of memory block pointerd by the mem->memory is changed to the
new bytes
mem->memory = (char*)realloc(mem->memory, (mem->size + realsize));
if(mem->memory == NULL)
{
printf("not enough memory (realloc returned NULL)\n");
exit(1);
}
// copy to memory structure
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
cout<<"Write ["<<realsize<<"] bytes Data to chunk"<<endl;
return realsize;
}
Hope someone can point out my mistakes. Thanks :D
The entire code and header file are in the attached. If anyone is
interested, please take a look.
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
- text/x-c++src attachment: httpClient.cc
- text/x-chdr attachment: httpClient.h