mirror of
https://github.com/curl/curl.git
synced 2026-08-25 07:23:31 +03:00
examples: check more errors, fix cleanups, scope variables
Inspired by Joshua's report on examples. Closes #19055
This commit is contained in:
parent
61dcb56743
commit
64ed2ea196
42 changed files with 874 additions and 785 deletions
|
|
@ -116,63 +116,64 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
|
|||
int main(void)
|
||||
{
|
||||
CURL *ch;
|
||||
CURLcode res;
|
||||
|
||||
res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
CURLcode res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(res)
|
||||
return (int)res;
|
||||
|
||||
ch = curl_easy_init();
|
||||
curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
|
||||
curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
|
||||
curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
|
||||
curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
|
||||
curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
|
||||
if(ch) {
|
||||
curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
|
||||
curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, writefunction);
|
||||
curl_easy_setopt(ch, CURLOPT_WRITEDATA, stdout);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, writefunction);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADERDATA, stderr);
|
||||
curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, "PEM");
|
||||
curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 1L);
|
||||
curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/");
|
||||
|
||||
/* Turn off the default CA locations, otherwise libcurl loads CA
|
||||
* certificates from the locations that were detected/specified at
|
||||
* build-time
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
|
||||
curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
|
||||
/* Turn off the default CA locations, otherwise libcurl loads CA
|
||||
* certificates from the locations that were detected/specified at
|
||||
* build-time
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_CAINFO, NULL);
|
||||
curl_easy_setopt(ch, CURLOPT_CAPATH, NULL);
|
||||
|
||||
/* first try: retrieve page without ca certificates -> should fail
|
||||
* unless libcurl was built --with-ca-fallback enabled at build-time
|
||||
*/
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
/* first try: retrieve page without ca certificates -> should fail
|
||||
* unless libcurl was built --with-ca-fallback enabled at build-time
|
||||
*/
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
|
||||
/* use a fresh connection (optional) this option seriously impacts
|
||||
* performance of multiple transfers but it is necessary order to
|
||||
* demonstrate this example. recall that the ssl ctx callback is only called
|
||||
* _before_ an SSL connection is established, therefore it does not affect
|
||||
* existing verified SSL connections already in the connection cache
|
||||
* associated with this handle. normally you would set the ssl ctx function
|
||||
* before making any transfers, and not use this option.
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
|
||||
/* use a fresh connection (optional) this option seriously impacts
|
||||
* performance of multiple transfers but it is necessary order to
|
||||
* demonstrate this example. recall that the ssl ctx callback is only
|
||||
* called _before_ an SSL connection is established, therefore it does not
|
||||
* affect existing verified SSL connections already in the connection cache
|
||||
* associated with this handle. normally you would set the ssl ctx function
|
||||
* before making any transfers, and not use this option.
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L);
|
||||
|
||||
/* second try: retrieve page using cacerts' certificate -> succeeds to load
|
||||
* the certificate by installing a function doing the necessary
|
||||
* "modifications" to the SSL CONTEXT just before link init
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
/* second try: retrieve page using cacerts' certificate -> succeeds to load
|
||||
* the certificate by installing a function doing the necessary
|
||||
* "modifications" to the SSL CONTEXT just before link init
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
|
||||
curl_easy_cleanup(ch);
|
||||
curl_easy_cleanup(ch);
|
||||
}
|
||||
curl_global_cleanup();
|
||||
return (int)res;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue