[R] RCurl and cookies in POST requests

Duncan Temple Lang duncan at wald.ucdavis.edu
Sun Nov 21 17:19:20 CET 2010


Hi Christian

 There is a new version of the RCurl package on the Omegahat repository
and that handles this case.
The issue was running the finalizer to garbage
collect the curl handle, and it was correctly not being released as
the dynCurlReader() update function was precious and had a reference
to the curl handle.

The new package has finer grained control in curlSetOpt() to control
whether functions are made 'precious' or not and we can use this
when we know we will leave the curl handle in a correct and consistent
state even when the values of the function options are to be garbage
collected before the curl handle.

  D.



On 11/15/10 7:06 AM, Christian M. wrote:
> Hello Duncan.
> 
> Thanks for having a look at this. As soon as I get home I'll try
> your suggestion.
> 
> BTW, the link to the omega-help mailing list seems to be broken:
> http://www.omegahat.org/mailman/listinfo/
> 
> Thank you.
> 
> chr
> 
> 
> Duncan Temple Lang (Monday 15 November 2010, 01:02):
>> Hi Christian
>>
>>  Thanks for finding this. The problem seems to be that the finalizer
>> on the curl handle seems to disappear and so is not being called
>> when the handle is garbage collected.  So there is a bug somewhere
>> and I'll try to hunt it down quickly.
>>
>>   In the meantime, you can achieve the same effect by calling the
>> C routine curl_easy_cleanup.  You can't do this directly with a
>> .Call() or .C() as there is no explicit interface in the RCurl
>> package to this routine. However, you can use the Rffi package
>> (on the omegahat  repository)
>>
>>  library(Rffi)
>>  cif = CIF(voidType, list(pointerType))
>>  callCIF(cif, "curl_easy_cleanup", curl at ref)
>>
>>  I'll keep looking for why the finalizer is getting discarded.
>>
>>  Thanks again,
>>
>>  D.
>>
>> On 11/14/10 6:30 AM, Christian M. wrote:
>>> Hello.
>>>
>>> I know that it's usually possible to write cookies to a cookie
>>> file by removing the curl handle and doing a gc() call. I can do
>>> this with getURL(), but I just can't obtain the same results with
>>> postForm().
>>>
>>> If I use:
>>>
>>> curlHandle <- getCurlHandle(cookiefile=FILE, cookiejar=FILE)
>>>
>>> and then do:
>>>
>>> getURL(http://example.com/script.cgi, curl=curlHandle)
>>> rm(curlHandle)
>>> gc()
>>>
>>> it's OK, the cookie is there. But, if I do (same handle; the
>>> parameter is a dummy):
>>>
>>> postForm(site, .params=list(par="cookie"), curl=curlHandle,
>>>   style="POST")
>>> rm(curlHandle)
>>> gc()
>>>
>>> no cookie is written.
>>>
>>> Probably I'm doing something wrong, but don't know what.
>>>
>>> Is it possible to store cookies read from the output of a
>>> postForm() call? How?
>>>
>>> Thanks.
>>>
>>> Christian
>>>
>>> PS.: I'm attaching a script that can be sourced (and its .txt
>>> version). It contains an example. The expected result is a file
>>> (cookies.txt) with two cookies. The script currently uses
>>> getURL() and two cookies are stored. If postForm() is used
>>> (currently commented), only 1 cookie is written.
>>>
>



More information about the R-help mailing list