cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Handle multiple requests within the same session using cookies

From: Angelo Di Iorio <diioangelo_at_gmail.com>
Date: Sun, 14 Oct 2012 21:34:33 +0200

2012/10/7 Daniel Stenberg <daniel_at_haxx.se>:
>
> You can get the HTTP traffic using many means, Wireshark being one but
> LiveHTTPTraffic or the browsers own debugging tools are other ways. You
> could focus on what exactly the differences are between what your browser
> does and what your libcurl program does (or doesn't) do.
>
>
> --
>
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

I've observed the behaviour of my code thanks to wireshark. With the
following version of my code, where I'm trying to use a global CURL
variable in order to perform a single connection to the server instead
of multiple ones, I don't have any traffic at all (please note all the
variable's names which starts with an underscore are global ones) :

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(!_isInitialized)
    {
        initialize(_curl);
        _isInitialized = true;
    }
    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;
}

this error shows up: curl_easy_perform() failed: A libcurl function
was given a bad argument

for the clarity's sake, here's the initialize() function:

void initialize(CURL* curl)
{
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
}

I've already sent the login code in my very first email (and, after I
had a look on wireshark, it seems to me the problem isn't in that
portion of code. At least, not this one).
With this version of my code, everything works fine (except the
connections thing: I have multiple connections, obviously)

int simpleHttpRequest(char* url)
{
    CURL* curl; //curl handler
    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;

    curl = curl_easy_init();

    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

    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;
}

The difference between the two versions of my code is basically I
don't have a global CURL variable (which I'd like to have).
Excuse me for my poor english and 'noobness', I appreciate your help
and suggestions.

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