cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: linux curl_easy_perform error

From: Cornu Nicolas <nicolas.cornu_at_club-internet.fr>
Date: Sat, 30 Apr 2005 21:51:20 +0200

full source:

#include <stdio.h>
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <exception>
#include "CommSPG.h"
#include "Globals.h"
using namespace std;
namespace SMSD
{
namespace COMM
{
unsigned int CommSPG::timeOut;
std::string CommSPG::proxy = "";
std::string CommSPG::proxyUserPwd = "";
std::string CommSPG::certType;
std::string CommSPG::certFileName;
std::string CommSPG::keyPass;
std::string CommSPG::keyType;
std::string CommSPG::key;
CommSPG::MemoryStruct * CommSPG::mem;

void CommSPG::init(const std::string& leProxy, const std::string&
leProxyUserPwd,int leTimeOut)
{
    proxy=leProxy;
    proxyUserPwd=leProxyUserPwd;
    timeOut=leTimeOut;
}
void CommSPG::initSSL(char *leCertType, char *leCertFileName, char
*leKeyPass, char *leKeyType, char *laKey)
{
}
void * CommSPG::myrealloc(void *ptr, size_t size)
{
    /* There might be a realloc() out there that doesn't like reallocing
    NULL pointers, so we take care of it here */
    if(ptr)
    return realloc(ptr, size);
    else
    return malloc(size);
}

size_t CommSPG::WriteMemoryCallback(void *ptr, size_t size, size_t nmemb,
void *data)
{
    register int realsize = size * nmemb;
    mem = (struct MemoryStruct *)data;
    mem->memory = (char *)myrealloc(mem->memory, mem->size + realsize + 1);
    if (mem->memory)
    {
        memcpy(&(mem->memory[mem->size]), ptr, realsize);
        mem->size += realsize;
        mem->memory[mem->size] = 0;
    }
    return realsize;
}

std::string CommSPG::InterrogerSPG(const std::string& url, const
std::string& data)
{
    CURL *curl;
    CURLcode res;
    std::string reponse;
    struct MemoryStruct chunk;
    chunk.memory=NULL; /* we expect realloc(NULL, size) to work */
    chunk.size = 0; /* no data at this point */
    struct curl_slist *headerlist=NULL;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: " << url << " ,data:
" << data; GLOBALS::logger->write();

    if(curl)
    {
        if(proxy.c_str())
        {
            GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: defining
curl proxy."; GLOBALS::logger->write();
            curl_easy_setopt(curl, CURLOPT_PROXY, proxy.c_str());
        }
        if(proxyUserPwd.c_str())
       {
            GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: defining
proxy password"; GLOBALS::logger->write();
                curl_easy_setopt(curl,
CURLOPT_PROXYUSERPWD,proxyUserPwd.c_str());
        }
        curl_easy_setopt(curl, CURLOPT_URL, url.data());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS,data.data());
        GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: performing curl
request (curl_easy_perform)"; GLOBALS::logger->write();
        res = curl_easy_perform(curl);
        GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: performing curl
cleanup (curl_easy_cleanup)"; GLOBALS::logger->write();
        curl_easy_cleanup(curl);
    }

    if(chunk.size==0) {
    GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: exception thrown";
GLOBALS::logger->write();
      throw "Erreur de communication\n";
    }
    reponse = (string)chunk.memory;
    GLOBALS::logger->debug() << "CommSPG:InterrogerSPG: return next
operation"; GLOBALS::logger->write();
    return reponse;
}

In fact no error when the url is correct else this crash.

CN

----- Original Message -----
From: "Daniel Stenberg" <daniel-curl_at_haxx.se>
To: "libcurl development" <curl-library_at_cool.haxx.se>
Sent: Saturday, April 30, 2005 8:32 PM
Subject: Re: linux curl_easy_perform error

> On Sat, 30 Apr 2005, Cornu Nicolas wrote:
>
>> I am developping a linux windows portable server. I use libcurl and my
>> app crash on linux on curl_easy_perfrom
>
> If you are using a very recent libcurl the most likely causes for your
> problem, are 1) a problem in your write callback, or 2) you messed up the
> memory somehow before you called libcurl so that a malloc/free function
> within libcurl goes bananas.
>
> If you provide a full example source code (that compiles as-is) and that
> crashes when used with a given public URL, I'll be happy to verify my
> claim.
>
> --
> Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
>
>
Received on 2005-04-30