cURL / Mailing Lists / curl-library / Single Mail

curl-library

RE: CURLOPT_HTTPPOST documentation wrong?

From: Patrick Monnerat <Patrick.Monnerat_at_datasphere.ch>
Date: Tue, 15 Mar 2011 13:27:38 +0100

 
richardcavell_at_mail.com wrote:

> curl_easy_setopt ( curlhandle , CURLOPT_HTTPPOST , formptr ) ;
curl_easy_perform ( curlhandle ) ; curl_formfree ( formptr ) ;

> Now, the manual entry for CURLOPT_HTTPPOST says that the form data
should persist until the handle is released. Is this a mistake? It
seems logical to me that the data can be got rid of after the POST is
performed. I am reusing the one handle to do all my HTTP functions.
> My project ( a Wikipedia bot ) can run for days re-using the same curl
handle but the form data is always destroyed right after I get the
server's reply.

This is probably OK because you issue a new
curl_easy_setopt(CURLOPT_HTTPPOST) with a newly allocated pointer before
reusing the handle.
However, from a purist point of view, the code you show us leaves a
dangling pointer to the (released) form data attached to the handle.
If a subsequent curl_easy_perform() call were done for the same handle
without re-setting CURLOPT_HTTPPOST, data that do not exist anymore will
be accessed: this explicits what is clumsily, although properly
mentionned in the documentation.

To be "academic", I would change your code as follows:

curl_easy_setopt(curlhandle, CURLOPT_HTTPPOST, formptr);
curl_easy_perform(curlhandle);
curl_easy_setopt(curlhandle, CURLOPT_HTTPPOST, NULL); /* Avoid keeping
a dangling pointer. */
curl_formfree(formptr);

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-03-15