Daniel Stenberg goes through some basic libcurl fundamentals and API design and explain how easily you can get your first transfers going in your own application. libcurl is the defacto standard library for Internet transfers and runs on virtually all platforms. The language focus will be on C/C++ but the concepts are generally applicable even if you use libcurl bindings for other languages.
5. @bagder
@bagder
cURL = curl && libcurl
The Open Source project itself is often called cURL or just curl
The command line tool is called curl
The internet transfer library is called libcurl
Today we focus on libcurl
libcurl is MIT licensed with commercial support available
@bagder
@bagder
6. @bagder
@bagder
Get libcurl
Install it from your linux distribution provider
Install it from home brew on macOS
…
Build it yourself from source (not detailed today)
@bagder
@bagder
7. @bagder
@bagder
Build your code to use libcurl
Tell the compiler where to find the includes
Tell the linker to use libcurl
@bagder
@bagder
$ gcc -lcurl example.c -o example
8. @bagder
@bagder
Different libcurl builds
Different versions
Different platforms
Different features enabled at build-time
Different backends enabled at build-time
Different third party library versions
@bagder
@bagder
9. @bagder
@bagder
Documentation!
Every function has a man page
Every option has a man page
>100 stand-alone examples
https://curl.se/ has them all (updated)
https://everything.curl.dev/ provides more resources
Tip: just google the function/option name
@bagder
@bagder
10. @bagder
@bagder
API and ABI
Libcurl doesn’t break API or ABI
Once introduced, features and behaviors remain
The SONAME was last bumped in 2006
The API works the same on all platforms
Build-time disabled protocols/features aside
@bagder
@bagder
13. @bagder
@bagder
Transfer oriented
With libcurl, you setup and perform transfers
Transfers operate with URLs
Get data from a URL or send data to a URL
The other end is always a URL
The default transfer is as simple as possible
The API is generally protocol agnostic
Change behavior by setting options
@bagder
@bagder
14. @bagder
@bagder
Easy handles
“CURL *handle” are often referred to as easy handles
Once created, it identifies a single transfer
Can and should be reused!
Options are sticky, they remain set until told otherwise
Options set copy data (with one notable exception)
CURLOPT_URL is the only mandatory option for a transfer
16. @bagder
@bagder
The easy interface
Synchronous transfers
Performed to the end, returns first once successful or failure
Done as fast as possible
Might take a long time; seconds, minutes, hours, weeks
You can limit how long time it may take
17. @bagder
@bagder
Create easy handle
CURL *easy = curl_easy_init();
Creates an easy handle and initiates it with default values.
This is just an opaque handle, there are no public struct fields.
Similar in spirit to FILE *
18. @bagder
@bagder
Create easy handle and set options
CURL *easy = curl_easy_init();
CURLcode rc = curl_easy_setopt(easy, CURLOPT_URL,
“https://curl.se”);
Options accept different argument types depending on the
option
Read the documentation for each option for details!
24. @bagder
@bagder
Get data
Provide a write callback: CURLOPT_WRITEFUNCTION
size_t writecb (void *contents, size_t size, size_t nmemb, void *userp)
{
size_t realsize = size * nmemb;
// the data is in ‘contents’
// ‘userp’ is what CURLOPT_WRITEDATA set
return realsize;
}
25. @bagder
@bagder
Send data
The three primary ways to provide data to libcurl for sending:
CURLOPT_READFUNCTION
CURLOPT_POSTFIELDS
CURLOPT_MIMEPOST
26. @bagder
@bagder
A minimal easy POST example
CURL *curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “hello”);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
printf("transfer fail: %sn", curl_easy_strerror(res));
curl_easy_cleanup(curl);
}
28. @bagder
@bagder
curl_easy_getinfo()
Pull data from an easy handle after a transfer.
CURLINFO_EFFECTIVE_URL
CURLINFO_RESPONSE_CODE
CURLINFO_TOTAL_TIME_T
CURLINFO_SPEED_DOWNLOAD_T
CURLINFO_CONTENT_TYPE
… and many more
33. @bagder
@bagder
The multi interface
Multiple concurrent non-blocking transfers – in a single thread
Create and setup easy handles just as before
Create a multi handle
Add easy handles to the multi handle
Drive all transfers in parallel
Leaves the event loop to the application!
35. @bagder
@bagder
Going further
You can add and remove transfers at any point
Mix in and also wait for your own file descriptors
curl_multi_setopt()
curl_multi_socket_action() for event-based logic
For hundreds or thousands of concurrent transfers
40. @bagder
@bagder
License
This presentation and its contents are
licensed under the Creative Commons
Attribution 4.0 license:
http://creativecommons.org/licenses/by/4.0/
@bagder
@bagder