Instantiate a new Curl_Type object
Curl_Type curl_new(String_Type url)
This function instantiates and returns a Curl_Type
and
returns it. It is a wrapper around the
cURL library function
curl_easy_init
. The Curl_Type
object is created with
the CURLOPT_NOPROGRESS
option set to 1, and
CURLOPT_VERBOSE
set to 0.
Upon failure, the function throws a CurlError
exception.
Set an option for a specified Curl_Type object
curl_setopt (Curl_Type c, Int_Type opt, ...)
The curl_setopt
function is a wrapper around the
cURL
library function
curl_easy_setopt. For more information
about the options that this function takes, see
curl_easy_setopt. Only the differences between the
module function and the corresponding API function will be explained
here.
The current version of the curl
module does not support the
CURLOPT_*DATA
options. Instead, support for these options
has been integrated into the corresponding callback functions. For
example, the CURLOPT_WRITEDATA
option has been merged with
the CURLOPT_WRITEFUNCTION
. Moreover the prototypes for the
callbacks have been changed to simplify their use, as described below.
This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed two arguments: the specified user-defined object, and a binary string to write. Upon failure, the function must return -1, and any other value will indicate success.
This option requires two parameters: a
reference to the callback function, and a user-defined object to
pass to that function. The callback function will be passed two
arguments: the specified user-defined object, and the maximum number
of bytes to be read by the function. Upon success, the function
should return a (binary) string, otherwise it should return NULL
to
indicate failure.
This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed two arguments: the specified user-defined object, and a header string write. The function must return -1 upon failure, or any other integer to indicate success.
This option requires two parameters: a reference to the callback function, and a user-defined object to pass to that function. The callback function will be passed five arguments: the specified user-defined object, and four double precision floating point values that represent the total number of bytes to be downloaded, the total downloaded so far, the total to be uploaded, and the total currently uploaded. This function must return 0 to indicate success, or non-zero to indicate failure.
A number of the options in the cURL API take a linked list of strings. Instead of a linked list, the module requires an array of strings for such options, e.g.,
curl_setopt (c, CURLOPT_HTTPHEADER,
["User-Agent: S-Lang curl module",
"Content-Type: text/xml; charset=UTF-8",
"Content-Length: 1234"]);
The following example illustrates how to write the contents of a
specified URL to a file, its download progress to stdout
, and
the contents of its header to variable:
define write_callback (fp, str)
{
return fputs (str, fp);
}
define progress_callback (fp, dltotal, dlnow, ultotal, ulnow)
{
if (-1 == fprintf (fp, "Bytes Received: %d\n", int(dlnow)))
return -1;
if (dltotal > 0.0)
{
if (-1 == fprintf (fp, "Percent Received: %g\n",
dlnow/dltotal * 100.0))
return -1;
}
return 0;
}
define header_callback (strp, str)
{
@strp += str;
return 0;
}
define download_url (url, file)
{
variable fp = fopen (file, "w");
variable c = curl_new (url);
curl_setopt (c, CURLOPT_FOLLOWLOCATION);
curl_setopt (c, CURLOPT_WRITEFUNCTION, &write_callback, fp);
curl_setopt (c, CURLOPT_PROGRESSFUNCTION, &progress_callback, stdout);
variable var = "";
curl_setopt (c, CURLOPT_HEADERFUNCTION, &header_callback, &var);
curl_perform (c);
() = fprintf (stdout, "Header: %s\n", var);
}
Initialize the Curl library
curl_global_init (flags)
This function is a wrapper around the corresponding cURL library function. See its documentation for more information.
Finalize the Curl library
curl_global_cleanup
This function is a wrapper around the corresponding cURL library function. See its documentation for more information.
Transfer a file
curl_perform
This function is a wrapper around the curl_easy_perform cURL library function. See its documentation for more information.
Get information about a Curl_Type object
info = curl_get_info (Curl_Type c, Int_Type type)
This function returns information of the requested type from a
Curl_Type
object. The data returned depends upon the value
of the type
argument. For more information, see see the
documentation for the
cURL library
curl_easy_getinfo function.
This example shows how to use the curl_get_info
function to
obtain the effective URL used for the transfer.
url = curl_get_info (c, CURLINFO_EFFECTIVE_URL);
Close a Curl_Type object
curl_close
This function is a wrapper around the curl_easy_cleanup cURL library function. See its documentation for more information.
Normally there is no need to call this function because the module
automatically frees any memory associated with the Curl_Type
object when it is no longer referenced.
Instantiate a new Curl_Multi_Type object
Curl_Multi_Type curl_multi_new ()
This function is a wrapper around the
curl_multi_init
cURL library function. It creates a new instance of a
Curl_Multi_Type
object and returns it.
Process a Curl_Multi_Type object
Int_Type curl_multi_perform (Curl_Multi_Type m [,Double_Type dt])
This function is a wrapper around the
curl_multi_perform
cURL library function. However, the curl
module function
takes an additional argument (dt
) that causes the function to
wait up to that many seconds for one of the underlying
Curl_Type
objects to become ready for reading or writing.
The function returns the number of Curl_Type
.
Remove a Curl_Type object from a Curl_Multi_Type
curl_multi_remove_handle (Curl_Multi_Type m, Curl_Type c)
This function removes the specified Curl_Type
object from
the Curl_Multi_Type
object. For more information, see the
documentation for the
corresponding
cURL library function.
Add a Curl_Type object to a Curl_Multi_Type
curl_multi_add_handle
This function adds the specified Curl_Type
object to
the Curl_Multi_Type
object. For more information, see the
documentation for the
corresponding
cURL library function.
curl_multi_remove_handle,
curl_multi_new,
curl_multi_perform
Close a Curl_Multi_Type object
curl_multi_close (Curl_Multi_Type m)
This function is a wrapper around the
curl_multi_cleanup
cURL library function. Any Curl_Multi_Type
objects
associated with the specified Curl_Multi_Type
object will be
removed from it.
curl_multi_new,
curl_multi_remove_handle,
curl_multi_info_read
Get information about a Curl_Multi_Type transfer
Curl_Type curl_multi_info_read (Curl_Multi_Type m [,Ref_Type info])
This function retrieves information from the specified
Curl_Multi_Type
object about an individual completed
transfer by one of its associated Curl_Type
objects. If all
of the associated Curl_Type
objects are still being
processed, the function will return NULL
. Otherwise it returns the
completed Curl_Type
object. If an optional Ref_Type
parameter is passed to the function, then upon return the
the associated variable be set to an integer representing the
completion status of the Curl_Type
object. If the
completion status is 0, then the transfer was successful, otherwise
the individual transfer failed and the completion status gives the
error code associated with the transfer. More infomation about the
transfer may be obtained by calling the curl_get_info
function.
The curl_multi_info_read
function should be called after a
call to curl_multi_perform
has indicated that a transfer has
taken place. The following example repeatedly calls
curl_multi_info_read
until it returns NULL
. Each time
through the loop, the completed Curl_Type
object is removed
from the Curl_Multi_Type
object.
while (c = curl_multi_info_read (m, &status), c!=NULL)
{
curl_multi_remove_handle (m, c);
url = curl_get_url (c);
if (status == 0)
vmessage ("Retrieved %s", url);
else
vmessage ("Unable to retrieve %s", url);
}
Get the URL associated with a Curl_Type object
String_Type curl_get_url (Curl_Type c)
This function returns the name of the URL that was used to
instantiate the specified Curl_Type
object.
Get the number of Curl_Type objects in a Curl_Multi_Type
Int_Type curl_multi_length (Curl_Multi_Type m)
This function returns the number of Curl_Type
objects
contained in the specified Curl_Multi_Type
object.