cURL / Mailing Lists / curl-library / Single Mail


Problem wirth libcurl and write_function

From: Thaorius <>
Date: Mon, 21 May 2007 00:38:05 +0200

Hi, in my last program I kinda needed libcurl, so I read the docs and
everything. In one part it says that curl needs me to implement a data
writting function. I wrote one. The problem is that if the HTTP server
output is less than 8 bytes(without the headers), one of the following
things happens:

1) Random characters appears til the 8 bytes al fulfilled.
2) The program crashes on the call to curl_easy_perform().

This is my write function:

size_t PlatformInterface::_curl_write(void *buffer, size_t size, size_t
nmemb, void *userp)
char *temp = (char *)malloc((size * nmemb) + 1);
memcpy(temp, buffer, (size * nmemb));
temp[(size * nmemb) + 1] = '\0';
((std::string *)userp)->append((char *)temp);

return size * nmemb;

Is an static class, where userp is a pointer to an std::string object.

This is the function that handles the curl execution:

std::string PlatformInterface::execute( std::string cid, std::string params,
bool onPlatformServer, bool zerolid )
// Clear the buffer

// Setup HTTP headers
struct curl_slist *headers=NULL;
headers = curl_slist_append(headers, "User-Agent: TDTClient/1.0
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);

// Setup url
char url[1024] = "http:// <http:///>";
strcat(url, gateway.c_str());
strcat(url, path.c_str());
strcat(url, (onPlatformServer ? platformServer.c_str() : loginServer.c_str
strcat(url, "?cid=");
strcat(url, cid.c_str());

strcat(url, "&lid=");
strcat(url, lid.c_str());
strcat(url, "zerolid");

if(params != "")
char *old = (char *)params.c_str();
replace(old, " ", "%20");
strcat(url, "&");
strcat(url, old);

curl_easy_setopt(handle, CURLOPT_URL, &url);

// Perform the request
CURLcode ret = curl_easy_perform(handle);

// Free headers

// Setup PlatformStatus and return data
if(ret == CURLE_OK) {
PlatformStatusFactory::getStatus(PL_SUCCESS, PL_NO_ERROR,
return tmp;
} else
PlatformStatusFactory::getStatus(PL_ERROR, PL_CURL_ERROR,
"PlatformInterface::execute", std::string(curlError));

return "";

Does anyone know whats wrong in _curl_write()?


Received on 2007-05-21