2.1 Procedures

These are the procedures provided by the library.

Procedure: curl-easy-init

Returns a curl handle that you must use as input to other functions. This handle represents one connection to a server. If it fails, it will throw an error.

If libcurl was built without an asynchronous DNS resolver, the new handle has the nosignal option set. Such a libcurl uses signals to time out DNS lookups, which is unsafe in a multithreaded program, and a Guile process always has several threads. The option can be turned off with (curl-easy-setopt handle 'nosignal #f).

Procedure: curl-easy-handle? x

Returns #t if x is a curl handle such as would be created by curl-easy-init. #f otherwise.

Procedure: curl-easy-cleanup handle

This forcibly closes all connections that this handle has used and have possibly kept open until now. Any use of the handle after it has been closed is invalid. The return value is unspecified.

Calling this procedure is optional: handles will automatically be freed when no longer in use.

Procedure: curl-easy-reset handle

Re-initializes all options previously set on a specified CURL handle to its default values. This puts back the handle to the same state as it was in when it was just created with [curl-easy-init].

It does not change the following information kept in the handle: live connections, the Session ID cache, the DNS cache, the cookies and shares.1

The return value is unspecified.

Procedure: curl-easy-perform handle #:optional bytevector? #:optional header?

From the C documentation

This function is called after the init and all the [curl-easy-setopt] calls are made, and will perform the transfer as described in the options. It must be called with the same handle as input as the [curl-easy-init] call returned.

You can do any amount of calls to [curl-easy-perform] while using the same handle. If you intend to transfer more than one file, you are even encouraged to do so. libcurl will then attempt to re-use the same connection for the following transfers, thus making the operations faster, less CPU intense and using less network resources. Just note that you will have to use [curl-easy-setopt] between the invokes to set options for the following [curl-easy-perform]

You must never call this function simultaneously from two places using the same handle. Let the function return first before invoking it another time. If you want parallel transfers, you must use several curl handles.2

The default behavior is to return a string containing the resource. There is no way to handle the encoding of the string: the binary data will mapped byte-by-byte to a string containing codepoints U+0000 through U+00FF. If the optional bytevector? parameter is given and is set to #t, the resource will be returned as a bytevector.

It returns either a string containing the resource, a bytevector containing the resource, or #f on failure. For information about the failure, you can call curl-error-string.

If the optional header? parameter is set to #t, it returns a list of two elements. The first is a string that contains any headers, such as HTTP headers, associated with this operation. The second is the body of the request, either as a string or as a bytevector, as described above.

If reading the data to upload from a readdata port raises an error, the transfer is stopped and curl-easy-perform raises that error.

Procedure: curl-easy-perform-to-fd handle fd #:optional offset

Perform the transfer like curl-easy-perform, but write the response body directly to the open file descriptor fd instead of returning it, so that a large download does not have to fit in memory. If offset is omitted or #f, the data is written at the descriptor’s current position. Otherwise offset must be a non-negative integer, and the data is written starting at that byte offset with positioned writes, without moving the descriptor’s position; this lets several transfers, for example of different ranges, write into one file at once. Writing at an offset is not available on platforms without pwrite.

Returns the number of bytes written, or #f on failure; as with curl-easy-perform, curl-error-code and curl-error-string then say why. If a write to fd fails, the error is CURLE_WRITE_ERROR and the error string gives the system error. The descriptor remains open and is owned by the caller.

While the transfer runs, other Guile threads keep running and may call curl-easy-progress and curl-easy-cancel! on handle.

Procedure: curl-easy-progress handle

Return a pair of the downloaded and total byte counts most recently reported by a curl-easy-perform-to-fd transfer on handle. The total is zero while the size of the download is not known. This may be called from another thread while the transfer runs.

Procedure: curl-easy-cancel! handle

Request that a running curl-easy-perform-to-fd transfer on handle stop, typically from another thread. The transfer then returns #f, with the error code CURLE_ABORTED_BY_CALLBACK.

Procedure: curl-easy-header handle name #:optional index

Return the value of the response header name of the most recent transfer on handle, as a string, or #f if the response had no such header. name is matched without regard to case. If the header occurs more than once, index (default 0) selects which occurrence. Requires libcurl 7.83.0 or later.

Procedure: curl-easy-headers handle

Return all the response headers of the most recent transfer on handle as a list of (name . value) pairs of strings, in the order they were received. Requires libcurl 7.83.0 or later.

Procedure: curl-easy-send handle data
Procedure: curl-easy-receive handle max-bytes

Send and receive raw data over a connection that libcurl set up but did not use, so that a program can speak a protocol libcurl does not support. Set the connect-only option and call curl-easy-perform to connect first.

curl-easy-send sends data, a string (sent as UTF-8) or a bytevector, and returns the number of bytes sent, which may be fewer than given. curl-easy-receive returns a bytevector of at most max-bytes bytes; it is empty when the other side has closed the connection. Both return #f on failure, including when the connection is not ready yet, in which case curl-error-code is CURLE_AGAIN and the call can be repeated later.

Procedure: curl-easy-upkeep handle

Perform connection upkeep on the connections of handle, such as sending HTTP/2 PING frames on idle connections that need them. Returns #t, or #f on failure.

Procedure: curl-error-string
Procedure: curl-error-code

When libcurl reports a failure to a procedure of this library, such as curl-easy-perform returning #f or curl-easy-setopt raising an error because libcurl rejected a value, these return information about it: curl-error-string as a message, and curl-error-code as one of the integer error codes from libcurl’s <curl/curl.h> (see Error Codes). Errors found before libcurl is called, such as an unknown option or an argument of the wrong type, raise an error without changing them. They are recorded per thread, so another thread’s failure does not change them either.

Procedure: bv->url-encoded-bv bv

Return a new bytevector in which every byte of the bytevector bv other than an ASCII letter, a digit or one of $ ' ( ) * + , - . / _ is replaced by % and two upper-case hexadecimal digits. Note that +, /, , and $ are not escaped; to encode a value for a URL query string, Guile’s uri-encode from (web uri) may be more suitable.

Procedure: %curl-dump-handle handle

A debugging aid: print the internal state of handle, such as the lists and form data stored with it, to the standard error stream.

Procedure: curl-easy-setopt handle option info

This procedure sets the options for this handle that will describe what will happen when curl-easy-perform is called. At a minimum, curl-easy-setopt must be called at least once per handle to set the url option. option is a symbol name of an option, and info is additional information needed for the option. See Options for setopt, for a table of options and their associated required type.

important: Options for a handle are not cleared after curl-easy-perform is called. Make sure to call curl-easy-reset when you want the options for this handle to be reset.

The return value is unspecified. An error is raised if handle is already closed, if option is unknown or not supported by the installed version of libcurl, if info is not of the option’s type, or if libcurl rejects the value; in the last case curl-error-code and curl-error-string say why.

Procedure: curl-easy-getinfo handle option

This procedure will extract internal information from a curl handle. The option is an symbol that indicates the value to be returned. The returned value is either the requested value or is #f on error.

You must execute an operation on a handle with curl-easy-perform before curl-easy-getinfo can be called on that handle.

important: Care must be taken not do disrupt internal values managed by curl. For example, this procedure can return a file descriptor which is managed by curl and should not be closed.

This may throw an error if handle is already closed or has not yet been used to perform an operation. Also, it may thrown an error if option is not available for the version of curl being used.

The curl-easy-getinfo procedure takes one of the option symbols give in the first column of the table below. These symbols are shorthand for the underlying CURL option given in the 2nd column.

Note that whether these options are available is dependent on the version of curl being used. To verify that a particular option is available, check that the defined constant is not #f. For example, to ensure the the 'xfer-id option is available, check that the CURLINFO_XFER_ID evaluates to an integer.

Option SymbolC Library NameOutput Type
activesocketCURLINFO_ACTIVESOCKETinteger file descriptor
appconnect-timeCURLINFO_APPCONNECT_TIMEreal seconds
appconnect-time-tCURLINFO_APPCONNECT_TIME_Tinteger microseconds
cainfoCURLINFO_CAINFOstring
capathCURLINFO_CAPATHstring
certinfoCURLINFO_CERTINFOlist of lists of strings3
condition-unmetCURLINFO_CONDITION_UNMETinteger 1 or zero
connect-timeCURLINFO_CONNECT_TIMEreal seconds
connect-time-tCURLINFO_CONNECT_TIME_Tinteger microseconds
conn-idCURLINFO_CONN_IDinteger
content-length-download-tCURLINFO_CONTENT_LENGTH_DOWNLOAD_Tinteger
content-length-upload-tCURLINFO_CONTENT_LENGTH_UPLOAD_Tinteger
content-typeCURLINFO_CONTENT_TYPEstring
cookielistCURLINFO_COOKIELISTlist of strings
earlydata-sent-tCURLINFO_EARLYDATA_SENT_Tinteger bytes
effective-methodCURLINFO_EFFECTIVE_METHODstring
effective-urlCURLINFO_EFFECTIVE_URLstring
filetimeCURLINFO_FILETIMEinteger
filetime-tCURLINFO_FILETIME_Tinteger
ftp-entry-pathCURLINFO_FTP_ENTRY_PATHstring
header-sizeCURLINFO_HEADER_SIZEinteger
httpauth-availCURLINFO_HTTPAUTH_AVAILbitfield of CURLAUTH constants See Integer Constants for httpauth
httpauth-usedCURLINFO_HTTPAUTH_USEDbitfield of CURLAUTH constants See Integer Constants for httpauth
http-connectcodeCURLINFO_HTTP_CONNECTCODEinteger
http-versionCURLINFO_HTTP_VERSIONinteger of CURL_HTTP_VERSION constants See Integer Constants for http-version
local-ipCURLINFO_LOCAL_IPstring
local-portCURLINFO_LOCAL_PORTinteger
namelookup-timeCURLINFO_NAMELOOKUP_TIMEreal seconds
namelookup-time-tCURLINFO_NAMELOOKUP_TIME_Tinteger microseconds
num-connectsCURLINFO_NUM_CONNECTSinteger
os-errnoCURLINFO_OS_ERRNOinteger
posttransfer-time-tCURLINFO_POSTTRANSFER_TIME_Tinteger microseconds
pretransfer-timeCURLINFO_PRETRANSFER_TIMEreal seconds
pretransfer-time-tCURLINFO_PRETRANSFER_TIME_Tinteger microseconds
primary-ipCURLINFO_PRIMARY_IPstring
primary-portCURLINFO_PRIMARY_PORTinteger
proxyauth-availCURLINFO_PROXYAUTH_AVAILbitfield of CURLAUTH constants
proxyauth-usedCURLINFO_PROXYAUTH_USEDbitfield of CURLAUTH constants
proxy-errorCURLINFO_PROXY_ERRORinteger of a CURLPX constant
proxy-ssl-verifyresultCURLINFO_PROXY_SSL_VERIFYRESULTinteger
queue-time-tCURLINFO_QUEUE_TIME_Tinteger microseconds
redirect-countCURLINFO_REDIRECT_COUNTinteger
redirect-timeCURLINFO_REDIRECT_TIMEreal seconds
redirect-time-tCURLINFO_REDIRECT_TIME_Tinteger microseconds
redirect-urlCURLINFO_REDIRECT_URLstring
refererCURLINFO_REFERERstring
request-sizeCURLINFO_REQUEST_SIZEinteger
response-codeCURLINFO_RESPONSE_CODEinteger
retry-afterCURLINFO_RETRY_AFTERinteger
rtsp-client-cseqCURLINFO_RTSP_CLIENT_CSEQinteger CSeq
rtsp-cseq-recvCURLINFO_RTSP_CSEQ_RECVinteger CSeq
rtsp-server-cseqCURLINFO_RTSP_SERVER_CSEQinteger CSeq
rtsp-session-idCURLINFO_RTSP_SESSION_IDstring
schemeCURLINFO_SCHEMEstring
size-download-tCURLINFO_SIZE_DOWNLOAD_Tinteger
size-upload-tCURLINFO_SIZE_UPLOAD_Tinteger
speed-download-tCURLINFO_SPEED_DOWNLOAD_Tinteger bytes per second
speed-upload-tCURLINFO_SPEED_UPLOAD_Tinteger bytes per second
ssl-enginesCURLINFO_SSL_ENGINESlist of strings
ssl-verifyresultCURLINFO_SSL_VERIFYRESULTinteger
starttransfer-timeCURLINFO_STARTTRANSFER_TIMEreal seconds
starttransfer-time-tCURLINFO_STARTTRANSFER_TIME_Tinteger microseconds
total-timeCURLINFO_TOTAL_TIMEreal seconds
total-time-tCURLINFO_TOTAL_TIME_Tinteger microseconds
used-proxyCURLINFO_USED_PROXYinteger form of boolean
xfer-idCURLINFO_XFER_IDinteger ID

Footnotes

(1)

from the libcurl 7.12.1 curl_easy_reset man page

(2)

from the libcurl 7.7 curl_easy_perform man page

(3)

One list per certificate in the server’s chain, each of "name:value" strings. The certinfo option must be set before the transfer; otherwise the list is empty.