cURL / Mailing Lists / curl-library / Single Mail

curl-library

(no subject)

From: Ryan Earl <heretic_at_mail.utexas.edu>
Date: Sun, 20 May 2001 18:24:35 -0500

I recently found the curl library, and was especially interested in it for use with SSL in an Win32 application I'm developing. I got curl-7.7.3 and openssl-0.9.6a compiled on Win32 just fine.

However... I've been playing with it for quite some time and have run into runtime errors all over the place dealing with curl simply not copying over specified data to internal curl data structures. It would be nice if the documentation was more specific about how long I have to keep around memory I use for curl arguments for all functions/options. ie mention that I -don't- have to keep my copy around, etc. For instance, in curl_easy_setopt, CURLOPT_HTTPPOST doesn't mention you have to keep the post field list around until after you call curl_easy_perform.

I ended up moving -all- arguments off the stack just trying to get this to run, but I still am running into a weird error. With my debug build, the program behaves well. It does a POST with all the correct data. However, with a release build, everytime I get a memory access violation error in curl_easy_perform. Without debugging symbols, it's pain to figure out what this is. Had to resort to "printf" methodolgy just to find the function where it died. Can anyone spot anything wrong in this code? (yea, it's ugly)

void CHttptestDlg::setDefaultCurlOpts( CURL *pCURL )
{
        // prepare headers once, reuse through rest of program
        const static char* headers[] = {
                "Accept: text/*",
                "Cache-Control: no-cache",
        };
        const static int numHeaders = sizeof( headers ) / sizeof( *headers );
        
        static struct curl_slist *headerList = NULL;
        static int bHeaderlistInitialized = 0;

        if( !bHeaderlistInitialized ) {
                // add our headers
                for( int i = 0; i < numHeaders; ++i )
                        headerList = curl_slist_append( headerList, headers[ i ] );

                bHeaderlistInitialized = 1;
        }

        // setup standard options
        curl_easy_setopt( pCURL, CURLOPT_ERRORBUFFER, m_strCURLError );
        curl_easy_setopt( pCURL, CURLOPT_NOPROGRESS , 1 );
        curl_easy_setopt( pCURL, CURLOPT_HEADER , 1 );
        curl_easy_setopt( pCURL, CURLOPT_NOBODY , 0 );
        curl_easy_setopt( pCURL, CURLOPT_CLOSEPOLICY,
                                                                                CURLCLOSEPOLICY_LEAST_RECENTLY_USED );
        curl_easy_setopt( pCURL, CURLOPT_USERAGENT,
                                                                                "Mozilla/4.0 (compatible; autosubmitter 0.01a)" );
// curl_easy_setopt( pCURL, CURLOPT_WRITEFUNCTION, logOutput );
        curl_easy_setopt( pCURL, CURLOPT_FILE, m_logFile );
        curl_easy_setopt( pCURL, CURLOPT_HTTPHEADER, headerList );

        
// curl_easy_setopt( pCURL,
// curl_easy_setopt( pCURL,
// curl_easy_setopt( pCURL,
}

void CHttptestDlg::PostFromMap( CURL *pCurl, const map< string, string > &postMap ) {
        if( !postMap.size() )
                return;

        HttpPost* post = NULL;
    HttpPost* last = NULL;

        m_strPlainText = "";

        char **stringArr = new char*[ postMap.size() ];
        int i = 0;

        for( std::map< string, string >::const_iterator it = postMap.begin();
                it != postMap.end(); ++it, ++i ) {

                string postStr = it->first + "=";
                postStr += it->second;
                m_strPlainText += postStr.c_str();
                m_strPlainText += "\r\n";

                //curl_formparse( (char *)postStr.c_str(), &post, &last );
                stringArr[ i ] = new char[ postStr.length() + 1 ];
                strcpy( stringArr[ i ], postStr.c_str() );
                curl_formparse( stringArr[ i ], &post, &last );
        }

        UpdateData( FALSE ); // display post strings to GUI

        MessageBox( "before CURLOPT_HTTPPOST" );

        curl_easy_setopt( pCurl, CURLOPT_HTTPPOST, post );
        UpdateError(); m_cProgress.StepIt();
        
        MessageBox( "before curl_easy_perform" );
        curl_easy_perform( pCurl ); // some weird memory error here
        MessageBox( "after curl_easy_perform" );
        UpdateError(); m_cProgress.StepIt();

        MessageBox( "before formfree" );
        curl_formfree( post );

        while( i-- )
                delete [] stringArr[ i ];
        delete [] stringArr;
}

void CHttptestDlg::doConnection()
{
        CWaitCursor cursor; // setup waiting cursor

        m_pCURLConnection = curl_easy_init();
        m_cProgress.StepIt();

        setDefaultCurlOpts( m_pCURLConnection );
        m_cProgress.StepIt();

        int upper, lower;
        m_cProgress.GetRange( lower, upper );
        m_cProgress.SetPos( lower );

        CString cURL = m_strServerName + m_strObject;
        char *blah2 = new char[ cURL.GetLength() + 1 ];
        memcpy( blah2, cURL.GetBuffer( 1 ), cURL.GetLength() );
        blah2[ cURL.GetLength() ] = '\0';
        //char *pURL = new char[ cURL.GetLength() + 1 ];
        //memcpy( pURL, cURL.GetBuffer(), cURL.GetLength() + 1 );

        curl_easy_setopt( m_pCURLConnection, CURLOPT_URL, blah2 );
        UpdateError(); m_cProgress.StepIt();

        map< string, string > post;
        post[ "name" ] = "dope";
        post[ "verb" ] = "as";
        post[ "reason" ] = "me";

        PostFromMap( m_pCURLConnection, post );
        UpdateError(); m_cProgress.StepIt();

// m_cEncodeDisplay = strPostData;
// UpdateData( FALSE );

// curl_easy_perform( m_pCURLConnection );
// m_cProgress.StepIt();
        MessageBox( "before curl_easy_cleanup" );
        curl_easy_cleanup( m_pCURLConnection );
        m_cProgress.StepIt();

        delete [] blah2;

        //m_cUpdateReady.SetEvent();
}

Thanks in advance,
j. ryan earl

_______________________________________________
Curl-library mailing list
Curl-library_at_lists.sourceforge.net
http://lists.sourceforge.net/lists/listinfo/curl-library
Received on 2001-05-21