cURL / Mailing Lists / curl-library / Single Mail

curl-library

Error while closing connection after receiving POST response

From: Varun Gupta <varungupta303_at_gmail.com>
Date: Wed, 20 Jun 2012 18:46:42 -0400

Hi,

I am using cURL library to send a POST request and receive a response back
from the server. I am able to send the request and and receive the response
but when cURL is trying to close the connection, I am receiving an
exception. I work for a company and we use cURL in our code base and there
is some error happening while closing the connection which is caught by the
company's code running the background and it throws out an Invalid Memory
Access exception. Here is the code that I am using to send the post request:

static size_t
    curlHttpClientWrite (void *buffer, size_t item_size, size_t num_items,
                            void *responsePtr ) {
        printf("%s\n", (char*)buffer);
        FRET(item_size * num_items);
    }

CurlHttpResponse*
CurlHttpClient::post(const char* brokerUrl, const char* content, const
char* contentType) {

    curl_global_init(CURL_GLOBAL_DEFAULT);

    CURL* curlHandle = curl_easy_init();

    curl_easy_setopt(curlHandle, CURLOPT_VERBOSE, 1);

    curl_easy_setopt(curlHandle, CURLOPT_URL, brokerUrl);

    // Set the POST request header fields
    //contentType should be "Content-Type: text/xml;charset=UTF-8"
    struct curl_slist *httpHeaderList = PRGC_NIL;
    httpHeaderList = curl_slist_append(httpHeaderList, contentType);
    curl_easy_setopt(curlHandle, CURLOPT_HTTPHEADER, httpHeaderList);

    // Set the POST request data
    curl_easy_setopt(curlHandle, CURLOPT_POSTFIELDS, content);

    // Receive the HTTP header as part of the response
    curl_easy_setopt(curlHandle, CURLOPT_HEADER, 1);

    curl_easy_setopt(curlHandle, CURLOPT_WRITEFUNCTION,
curlHttpClientWrite);

    CurlHttpResponse* curlHttpResponsePtr = new CurlHttpResponse();
    curl_easy_setopt(curlHandle, CURLOPT_WRITEDATA,
(void*)curlHttpResponsePtr);

    CURLcode result = curl_easy_perform(curlHandle);

    if(result != 0) {
        printf("POST internal error\n");
        throw
CurlHttpClientPostFailedException(std::string(curl_easy_strerror(result)));
    }

    if(!this->isValidStatusCode(curlHttpResponsePtr->getStatusCode())) {
        printf("Invalid status code received\n");
        std::string message("Received invalid status code. Status code: ");
        message.append(curlHttpResponsePtr->getStatusCode());
        throw CurlHttpClientPostFailedException(message);
    }

    FRET(curlHttpResponsePtr);
}

I get the memory exception after I have received the response for the POST
request. On enabling the VERBOSE flag, I saw that the last step mentioned
in the debug logs was

#Closing connection #0

So, I am guessing that something went wrong while closing the connection or
in some steps after that.

Please let me know if you see something wrong with the code or what could
be a good way to debug this problem.

Any kind of help will be much appreciated!

Thanks,
Varun

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2012-06-21