cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: re-using connection with authentication

From: Sigrid Thijs <sigrid.thijs_at_androme.com>
Date: Wed, 14 Mar 2007 13:52:34 +0100

Daniel Stenberg wrote:
> On Tue, 13 Mar 2007, Sigrid Thijs wrote:
>
>> If this last assignment is removed, the conn->data does not point
>> anymore to the sessionHandle passed as a parameter to the function. As
>> a result, conn->data will contain the right digest, but the
>> (SessionHandle*)data isn't updated.
>
> I must admit I've gotten lost in this maze. If I setup a public URL for
> you that requires HTTP Digest, can you write a full example source code
> that reproduces the problem for me?
>

see attachment. To reproduce this, you should fill in the url and login
in the main function.
The body of the requests is retrieved from the following files:
request1_body.xml
request2_body.xml

kind regards,
Sigrid Thijs

#include <iostream>

#include "../libcurl/include/curl/curl.h"

int callback_debug ( CURL *pcurl, curl_infotype type, char *pmessage, size_t messageSize, void *puserData )
{
        if ( ( pcurl == NULL ) || ( pmessage == NULL ) || ( messageSize <= 0 ) )
                return 0;

        std::string test;
        test.append ( pmessage, messageSize );
        std::cout << "[" << messageSize << "] [" << test.c_str () << "]" << std::endl;

        return 0;
}

CURL * CreateCURL ( curl_slist *pCURLList, const std::string & url, const std::string & login, const std::string & filename )
{
        // Initialize CURL
        CURL * pCURL = curl_easy_init ();

        if ( !pCURL )
                return NULL;

        // add headers
        if ( pCURLList )
                curl_easy_setopt ( pCURL, CURLOPT_HTTPHEADER, pCURLList );

        // debug options
        curl_easy_setopt ( pCURL, CURLOPT_DEBUGDATA, (char *)"TEST" );
        curl_easy_setopt ( pCURL, CURLOPT_DEBUGFUNCTION, callback_debug );
        curl_easy_setopt ( pCURL, CURLOPT_VERBOSE, 1 );

        // url and login
        curl_easy_setopt ( pCURL, CURLOPT_URL, url.c_str () );
        curl_easy_setopt ( pCURL, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST );
        curl_easy_setopt ( pCURL, CURLOPT_USERPWD, login.c_str () );

        // Get data
        if ( !filename.empty () )
        {
                FILE * pFile = fopen ( filename.c_str (), "rb" );
                if ( pFile )
                {
                        int dataSize = 0;
                        if ( ( fseek ( pFile, 0, SEEK_END ) != 0 ) || ( ( dataSize = ftell ( pFile ) ) < 0 ) || ( fseek ( pFile, 0, SEEK_SET ) != 0 ) )
                                dataSize = -1;

                        if ( dataSize > 0 )
                        {
                                char * pData = new char [dataSize];

                                fread ( pData, 1, dataSize, pFile );
                                curl_easy_setopt ( pCURL, CURLOPT_POSTFIELDS, pData );
                                curl_easy_setopt ( pCURL, CURLOPT_POSTFIELDSIZE, dataSize );
                        }
                        fclose ( pFile );
                }
        }

        return pCURL;
}

void Perform ( CURLM * pMultiCURL )
{
        while ( true )
        {
                // Perform transfer
                int busyHandleCount = 0;
                CURLMcode code = curl_multi_perform ( pMultiCURL, &busyHandleCount );

                if ( code == CURLM_CALL_MULTI_PERFORM )
                        code = curl_multi_perform ( pMultiCURL, &busyHandleCount ); // Again

                // Process results
                int messageCount = 0;
                CURLMsg *pmessage = NULL;
                
                while ( ( pmessage = curl_multi_info_read ( pMultiCURL, &messageCount ) ) )
                {
                        if ( pmessage->msg == CURLMSG_DONE )
                        {
                                curl_multi_remove_handle ( pMultiCURL, pmessage->easy_handle );
                                
                                return;
                        }
                }
                Sleep ( 100 );
        }
}

int main ( void )
{
        std::string url = "http://your.url.here";
        std::string login = "username:password";

        CURLM * pMultiCURL = NULL;
        CURL *pCURL1 = NULL, *pCURL2 = NULL;
        curl_slist *pCURLList = NULL;
        try
        {
                // Create multi CURL handle
                CURLM *pMultiCURL = curl_multi_init ();

                if ( !pMultiCURL )
                        throw std::exception ( "curl_multi_init failed" );

                // Create headers
                pCURLList = curl_slist_append ( pCURLList, "Content-Type: text/xml; charset=utf-8" );

                // Create and perform first easy handle
                pCURL1 = CreateCURL ( pCURLList, url, login, "request1_body.xml" );
                if ( !pCURL1 )
                        throw std::exception ( "failed to create first easy handle" );
                curl_multi_add_handle ( pMultiCURL, pCURL1 );

                Perform ( pMultiCURL );

                // Create and perform second easy handle
                pCURL2 = CreateCURL ( pCURLList, url, login, "request2_body.xml" );
                if ( !pCURL2 )
                        throw std::exception ( "failed to create second easy handle" );
                curl_multi_add_handle ( pMultiCURL, pCURL2 );
                
                Perform ( pMultiCURL );

        }
        catch ( std::exception & e )
        {
                std::cout << "Error: " << e.what () << std::endl;
        }

        // Cleanup
        if ( pMultiCURL )
                curl_multi_cleanup ( pMultiCURL );

        if ( pCURL1 )
                curl_easy_cleanup ( pCURL1 );

        if ( pCURL2 )
                curl_easy_cleanup ( pCURL2 );

        if ( pCURLList )
                curl_slist_free_all ( pCURLList );
        
        return 0;
}
Received on 2007-03-14