cURL / Mailing Lists / curl-library / Single Mail

curl-library

Handle multiple requests within the same session using cookies

From: Angelo Di Iorio <diioangelo_at_gmail.com>
Date: Sun, 23 Sep 2012 19:40:42 +0200

Hi all, I have a huge problem with cookies. I'm trying to write some
functions in C that share a global variable called _curl. I'm trying
to log into a certain page, and after that I'm trying to send some
data using a REST pattern. My data is a simple URL (that will be
analyzed and inserted into a database), followed by an optional POST
data (depending from what function I'm using). The problem is, if I
try to keep a session via cookies, and I run few times a function in
order to insert, for examples, 10 sets of data into the database, it
doesn't mantain a session at all, so I can insert only the first
request into the database, while the others get rejected due to the
login phase. I tried to change the code, without any luck. Can you
please help me? Here's the login code:

int login(CURL* curl)
{
    int result = -9;

    CURLcode res;

    MemoryStruct chunk; //defined in HttpRequestSupport.h

    chunk.memory = malloc(1); // will be grown as needed by the
realloc istruction in writeMemoryCallback
    chunk.size = 0; // no data at this point

    curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1 );
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1 );
    curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
    curl_easy_setopt(curl, CURLOPT_URL, _loginURL);

    // Next we tell LibCurl what HTTP POST data to submit

    char* data = malloc(500 * sizeof(char));
    strcpy(data, "username=");
    strncat(data, _username, strlen(_username));
    strncat(data,"&password=", strlen("&password="));
    strncat(data, _password, strlen(_password));

    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

    /* send all data to this function */
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeMemoryCallback);

    /* we pass our 'chunk' struct to the callback function */
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) &chunk);

    /* some servers don't like requests that are made without a
user-agent field, so we provide one */
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    result = check(res,chunk.memory); //checking if everything went fine

    if(chunk.memory) free(chunk.memory);
    free(data);

    return result;
}

Here's the function I'm trying to submit 10 times; it works fine only
in the first attempt:

int simpleHttpRequest(char* url)
{
    CURLcode res; //result of the request

    MemoryStruct chunk; //defined in HttpRequestSupport.h

    chunk.memory = malloc(1); // will be grown as needed by the
realloc istruction in writeMemoryCallback
    chunk.size = 0; // no data at this point

    int result = 1;

/* if(!_isInizialized) //this part should be used to inizialize
only the first time my _curl global variable, but I get -1 from the
curl_easy_perform
    {
        inizialize(_curl);
        _isInizialized = true;
    }*/

    _curl = curl_easy_init(); //obv it's not going to work this way

    if(!_isLogged)
    {
        result = login(_curl); //this function will return an error if
the login phase fails
        if(result != 0) return result; //an error occoured during the
login phase
        _isLogged = true;
    }

    if(_curl)
    {
        curl_easy_setopt(_curl, CURLOPT_URL, url);

        /* send all data to this function */
        curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, writeMemoryCallback);

        /* we pass our 'chunk' struct to the callback function */
        curl_easy_setopt(_curl, CURLOPT_WRITEDATA, (void*) &chunk);

        /* some servers don't like requests that are made without a
user-agent field, so we provide one */
        curl_easy_setopt(_curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");

        /* Perform the request, res will get the return code */
        res = curl_easy_perform(_curl);

        /* Check for errors */
        result = check(res,chunk.memory);
        /* always cleanup *
        curl_easy_cleanup(_curl);
        if(chunk.memory) free(chunk.memory);

        return result;
    }
    else return -9;
}

Please consider that this code got changed a lot of times, so you
could find some "weird attempts" to make everything work. Sorry for my
poor english, I'm from Italy. Thank you.

-- 
Angelo Di Iorio
-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette:  http://curl.haxx.se/mail/etiquette.html
Received on 2012-09-23