mirror of
https://github.com/curl/curl.git
synced 2026-04-14 22:31:41 +03:00
examples: improve global init, error checks and returning errors
- add `curl_global_init()` and `curl_global_cleanup()` where missing. - check the result of `curl_global_init()` where missing. - return the last curl error from `main()`. - drop Win32-specific socket initialization in favor of `curl_global_init()`. - rename some outliers to `res` for curl result code. - fix cleanup in some error cases. Inspired by Joshua's report on examples. Closes #19053
This commit is contained in:
parent
3049c8e0a0
commit
4c7507daf9
129 changed files with 990 additions and 485 deletions
|
|
@ -56,8 +56,6 @@ static size_t writefunction(void *ptr, size_t size, size_t nmemb, void *stream)
|
|||
|
||||
static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
|
||||
{
|
||||
CURLcode rv = CURLE_ABORTED_BY_CALLBACK;
|
||||
|
||||
/** This example uses two (fake) certificates **/
|
||||
/* replace the XXX with the actual CA certificates */
|
||||
static const char mypem[] =
|
||||
|
|
@ -89,14 +87,14 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
|
|||
(void)pointer;
|
||||
|
||||
if(!cts || !cbio) {
|
||||
return rv;
|
||||
return CURLE_ABORTED_BY_CALLBACK;
|
||||
}
|
||||
|
||||
inf = PEM_X509_INFO_read_bio(cbio, NULL, NULL, NULL);
|
||||
|
||||
if(!inf) {
|
||||
BIO_free(cbio);
|
||||
return rv;
|
||||
return CURLE_ABORTED_BY_CALLBACK;
|
||||
}
|
||||
|
||||
for(i = 0; i < sk_X509_INFO_num(inf); i++) {
|
||||
|
|
@ -112,16 +110,18 @@ static CURLcode sslctx_function(CURL *curl, void *sslctx, void *pointer)
|
|||
sk_X509_INFO_pop_free(inf, X509_INFO_free);
|
||||
BIO_free(cbio);
|
||||
|
||||
rv = CURLE_OK;
|
||||
return rv;
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
CURL *ch;
|
||||
CURLcode rv;
|
||||
CURLcode res;
|
||||
|
||||
res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if(res)
|
||||
return (int)res;
|
||||
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
ch = curl_easy_init();
|
||||
curl_easy_setopt(ch, CURLOPT_VERBOSE, 0L);
|
||||
curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
|
||||
|
|
@ -145,8 +145,8 @@ int main(void)
|
|||
/* first try: retrieve page without ca certificates -> should fail
|
||||
* unless libcurl was built --with-ca-fallback enabled at build-time
|
||||
*/
|
||||
rv = curl_easy_perform(ch);
|
||||
if(rv == CURLE_OK)
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
|
|
@ -166,13 +166,13 @@ int main(void)
|
|||
* "modifications" to the SSL CONTEXT just before link init
|
||||
*/
|
||||
curl_easy_setopt(ch, CURLOPT_SSL_CTX_FUNCTION, sslctx_function);
|
||||
rv = curl_easy_perform(ch);
|
||||
if(rv == CURLE_OK)
|
||||
res = curl_easy_perform(ch);
|
||||
if(res == CURLE_OK)
|
||||
printf("*** transfer succeeded ***\n");
|
||||
else
|
||||
printf("*** transfer failed ***\n");
|
||||
|
||||
curl_easy_cleanup(ch);
|
||||
curl_global_cleanup();
|
||||
return (int)rv;
|
||||
return (int)res;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue