examples: check more errors, fix cleanups, scope variables
No binary (build failed)
—
(log yok)
| @@ -104,52 +104,54 @@ static void add_transfer(CURLM *cm, unsigned int i, int *left) | |
| int main(void) | |
| { | |
| CURLM *cm; | |
| - | CURLMsg *msg; |
| - | unsigned int transfers = 0; |
| - | int msgs_left = -1; |
| - | int left = 0; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| cm = curl_multi_init(); | |
| + | if(cm) { |
| + | CURLMsg *msg; |
| + | unsigned int transfers = 0; |
| + | int msgs_left = -1; |
| + | int left = 0; |
| - | /* Limit the amount of simultaneous connections curl should allow: */ |
| - | curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL); |
| + | /* Limit the amount of simultaneous connections curl should allow: */ |
| + | curl_multi_setopt(cm, CURLMOPT_MAXCONNECTS, (long)MAX_PARALLEL); |
| - | for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS; |
| - | transfers++) |
| - | add_transfer(cm, transfers, &left); |
| + | for(transfers = 0; transfers < MAX_PARALLEL && transfers < NUM_URLS; |
| + | transfers++) |
| + | add_transfer(cm, transfers, &left); |
| - | do { |
| - | int still_alive = 1; |
| - | curl_multi_perform(cm, &still_alive); |
| + | do { |
| + | int still_alive = 1; |
| + | curl_multi_perform(cm, &still_alive); |
| - | /* !checksrc! disable EQUALSNULL 1 */ |
| - | while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) { |
| - | if(msg->msg == CURLMSG_DONE) { |
| - | char *url; |
| - | CURL *e = msg->easy_handle; |
| - | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); |
| - | fprintf(stderr, "R: %d - %s <%s>\n", |
| - | msg->data.result, curl_easy_strerror(msg->data.result), url); |
| - | curl_multi_remove_handle(cm, e); |
| - | curl_easy_cleanup(e); |
| - | left--; |
| + | /* !checksrc! disable EQUALSNULL 1 */ |
| + | while((msg = curl_multi_info_read(cm, &msgs_left)) != NULL) { |
| + | if(msg->msg == CURLMSG_DONE) { |
| + | char *url; |
| + | CURL *e = msg->easy_handle; |
| + | curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, &url); |
| + | fprintf(stderr, "R: %d - %s <%s>\n", |
| + | msg->data.result, curl_easy_strerror(msg->data.result), url); |
| + | curl_multi_remove_handle(cm, e); |
| + | curl_easy_cleanup(e); |
| + | left--; |
| + | } |
| + | else { |
| + | fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); |
| + | } |
| + | if(transfers < NUM_URLS) |
| + | add_transfer(cm, transfers++, &left); |
| } | |
| - | else { |
| - | fprintf(stderr, "E: CURLMsg (%d)\n", msg->msg); |
| - | } |
| - | if(transfers < NUM_URLS) |
| - | add_transfer(cm, transfers++, &left); |
| - | } |
| - | if(left) |
| - | curl_multi_wait(cm, NULL, 0, 1000, NULL); |
| + | if(left) |
| + | curl_multi_wait(cm, NULL, 0, 1000, NULL); |
| - | } while(left); |
| + | } while(left); |
| - | curl_multi_cleanup(cm); |
| + | curl_multi_cleanup(cm); |
| + | } |
| curl_global_cleanup(); | |
| return EXIT_SUCCESS; | |
| @@ -105,10 +105,13 @@ int main(int argc, char **argv) | |
| #ifdef UNDER_CE | |
| /* !checksrc! disable BANNEDFUNC 1 */ | |
| - | stat(file, &file_info); |
| + | if(stat(file, &file_info) != 0) { |
| #else | |
| - | fstat(fileno(fp), &file_info); |
| + | if(fstat(fileno(fp), &file_info) != 0) { |
| #endif | |
| + | fclose(fp); |
| + | return 1; /* cannot continue */ |
| + | } |
| /* In Windows, this inits the Winsock stuff */ | |
| res = curl_global_init(CURL_GLOBAL_ALL); | |
| @@ -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/"); |
| - | |
| - | /* 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"); |
| - | |
| - | /* 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"); |
| - | |
| - | curl_easy_cleanup(ch); |
| + | 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); |
| + | |
| + | /* 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); |
| + | |
| + | /* 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_global_cleanup(); | |
| return (int)res; | |
| } | |
| @@ -162,62 +162,67 @@ int main(int argc, char *argv[]) | |
| /* init the curl session */ | |
| curl_handle = curl_easy_init(); | |
| + | if(curl_handle) { |
| - | /* specify URL to get */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_URL, url); |
| + | /* specify URL to get */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_URL, url); |
| - | /* send all data to this function */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback); |
| + | /* send all data to this function */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteCallback); |
| - | /* some servers do not like requests that are made without a user-agent |
| - | field, so we provide one */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, |
| - | "libcurl-speedchecker/" CHKSPEED_VERSION); |
| + | /* some servers do not like requests that are made without a user-agent |
| + | field, so we provide one */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, |
| + | "libcurl-speedchecker/" CHKSPEED_VERSION); |
| - | /* get it! */ |
| - | res = curl_easy_perform(curl_handle); |
| + | /* get it! */ |
| + | res = curl_easy_perform(curl_handle); |
| - | if(CURLE_OK == res) { |
| - | curl_off_t val; |
| + | if(CURLE_OK == res) { |
| + | curl_off_t val; |
| - | /* check for bytes downloaded */ |
| - | res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD_T, &val); |
| - | if((CURLE_OK == res) && (val > 0)) |
| - | printf("Data downloaded: %lu bytes.\n", (unsigned long)val); |
| - | |
| - | /* check for total download time */ |
| - | res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME_T, &val); |
| - | if((CURLE_OK == res) && (val > 0)) |
| - | printf("Total download time: %lu.%06lu sec.\n", |
| - | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); |
| - | |
| - | /* check for average download speed */ |
| - | res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD_T, &val); |
| - | if((CURLE_OK == res) && (val > 0)) |
| - | printf("Average download speed: %lu kbyte/sec.\n", |
| - | (unsigned long)(val / 1024)); |
| + | /* check for bytes downloaded */ |
| + | res = curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD_T, &val); |
| + | if((CURLE_OK == res) && (val > 0)) |
| + | printf("Data downloaded: %lu bytes.\n", (unsigned long)val); |
| - | if(prtall) { |
| - | /* check for name resolution time */ |
| - | res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME_T, &val); |
| + | /* check for total download time */ |
| + | res = curl_easy_getinfo(curl_handle, CURLINFO_TOTAL_TIME_T, &val); |
| if((CURLE_OK == res) && (val > 0)) | |
| - | printf("Name lookup time: %lu.%06lu sec.\n", |
| - | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); |
| + | printf("Total download time: %lu.%06lu sec.\n", |
| + | (unsigned long)(val / 1000000), |
| + | (unsigned long)(val % 1000000)); |
| - | /* check for connect time */ |
| - | res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME_T, &val); |
| + | /* check for average download speed */ |
| + | res = curl_easy_getinfo(curl_handle, CURLINFO_SPEED_DOWNLOAD_T, &val); |
| if((CURLE_OK == res) && (val > 0)) | |
| - | printf("Connect time: %lu.%06lu sec.\n", |
| - | (unsigned long)(val / 1000000), (unsigned long)(val % 1000000)); |
| + | printf("Average download speed: %lu kbyte/sec.\n", |
| + | (unsigned long)(val / 1024)); |
| + | |
| + | if(prtall) { |
| + | /* check for name resolution time */ |
| + | res = curl_easy_getinfo(curl_handle, CURLINFO_NAMELOOKUP_TIME_T, &val); |
| + | if((CURLE_OK == res) && (val > 0)) |
| + | printf("Name lookup time: %lu.%06lu sec.\n", |
| + | (unsigned long)(val / 1000000), |
| + | (unsigned long)(val % 1000000)); |
| + | |
| + | /* check for connect time */ |
| + | res = curl_easy_getinfo(curl_handle, CURLINFO_CONNECT_TIME_T, &val); |
| + | if((CURLE_OK == res) && (val > 0)) |
| + | printf("Connect time: %lu.%06lu sec.\n", |
| + | (unsigned long)(val / 1000000), |
| + | (unsigned long)(val % 1000000)); |
| + | } |
| + | } |
| + | else { |
| + | fprintf(stderr, "Error while fetching '%s' : %s\n", |
| + | url, curl_easy_strerror(res)); |
| } | |
| - | } |
| - | else { |
| - | fprintf(stderr, "Error while fetching '%s' : %s\n", |
| - | url, curl_easy_strerror(res)); |
| - | } |
| - | /* cleanup curl stuff */ |
| - | curl_easy_cleanup(curl_handle); |
| + | /* cleanup curl stuff */ |
| + | curl_easy_cleanup(curl_handle); |
| + | } |
| /* we are done with libcurl, so clean it up */ | |
| curl_global_cleanup(); | |
| @@ -191,67 +191,70 @@ int main(void) | |
| signal(SIGINT, sighandler); | |
| LIBXML_TEST_VERSION | |
| multi_handle = curl_multi_init(); | |
| - | curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con); |
| - | curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 6L); |
| + | if(multi_handle) { |
| + | curl_multi_setopt(multi_handle, CURLMOPT_MAX_TOTAL_CONNECTIONS, max_con); |
| + | curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 6L); |
| - | /* enables http/2 if available */ |
| + | /* enables http/2 if available */ |
| #ifdef CURLPIPE_MULTIPLEX | |
| - | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| + | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| #endif | |
| - | /* sets html start page */ |
| - | curl_multi_add_handle(multi_handle, make_handle(start_page)); |
| + | /* sets html start page */ |
| + | curl_multi_add_handle(multi_handle, make_handle(start_page)); |
| - | pending = 0; |
| - | complete = 0; |
| - | still_running = 1; |
| - | while(still_running && !pending_interrupt) { |
| - | int numfds; |
| - | CURLMsg *m; |
| + | pending = 0; |
| + | complete = 0; |
| + | still_running = 1; |
| + | while(still_running && !pending_interrupt) { |
| + | int numfds; |
| + | CURLMsg *m; |
| - | curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds); |
| - | curl_multi_perform(multi_handle, &still_running); |
| + | curl_multi_wait(multi_handle, NULL, 0, 1000, &numfds); |
| + | curl_multi_perform(multi_handle, &still_running); |
| - | /* See how the transfers went */ |
| - | m = NULL; |
| - | while((m = curl_multi_info_read(multi_handle, &msgs_left))) { |
| - | if(m->msg == CURLMSG_DONE) { |
| - | CURL *handle = m->easy_handle; |
| - | char *url; |
| - | struct memory *mem; |
| - | curl_easy_getinfo(handle, CURLINFO_PRIVATE, &mem); |
| - | curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); |
| - | if(m->data.result == CURLE_OK) { |
| - | long res_status; |
| - | curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &res_status); |
| - | if(res_status == 200) { |
| - | char *ctype; |
| - | curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &ctype); |
| - | printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url); |
| - | if(is_html(ctype) && mem->size > 100) { |
| - | if(pending < max_requests && (complete + pending) < max_total) { |
| - | pending += follow_links(multi_handle, mem, url); |
| - | still_running = 1; |
| + | /* See how the transfers went */ |
| + | m = NULL; |
| + | while((m = curl_multi_info_read(multi_handle, &msgs_left))) { |
| + | if(m->msg == CURLMSG_DONE) { |
| + | CURL *handle = m->easy_handle; |
| + | char *url; |
| + | struct memory *mem; |
| + | curl_easy_getinfo(handle, CURLINFO_PRIVATE, &mem); |
| + | curl_easy_getinfo(handle, CURLINFO_EFFECTIVE_URL, &url); |
| + | if(m->data.result == CURLE_OK) { |
| + | long res_status; |
| + | curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &res_status); |
| + | if(res_status == 200) { |
| + | char *ctype; |
| + | curl_easy_getinfo(handle, CURLINFO_CONTENT_TYPE, &ctype); |
| + | printf("[%d] HTTP 200 (%s): %s\n", complete, ctype, url); |
| + | if(is_html(ctype) && mem->size > 100) { |
| + | if(pending < max_requests && |
| + | (complete + pending) < max_total) { |
| + | pending += follow_links(multi_handle, mem, url); |
| + | still_running = 1; |
| + | } |
| } | |
| } | |
| + | else { |
| + | printf("[%d] HTTP %d: %s\n", complete, (int) res_status, url); |
| + | } |
| } | |
| else { | |
| - | printf("[%d] HTTP %d: %s\n", complete, (int) res_status, url); |
| + | printf("[%d] Connection failure: %s\n", complete, url); |
| } | |
| + | curl_multi_remove_handle(multi_handle, handle); |
| + | curl_easy_cleanup(handle); |
| + | free(mem->buf); |
| + | free(mem); |
| + | complete++; |
| + | pending--; |
| } | |
| - | else { |
| - | printf("[%d] Connection failure: %s\n", complete, url); |
| - | } |
| - | curl_multi_remove_handle(multi_handle, handle); |
| - | curl_easy_cleanup(handle); |
| - | free(mem->buf); |
| - | free(mem); |
| - | complete++; |
| - | pending--; |
| } | |
| } | |
| + | curl_multi_cleanup(multi_handle); |
| } | |
| - | curl_multi_cleanup(multi_handle); |
| curl_global_cleanup(); | |
| return 0; | |
| } | |
| @@ -32,10 +32,8 @@ struct data { | |
| char trace_ascii; /* 1 or 0 */ | |
| }; | |
| - | static |
| - | void dump(const char *text, |
| - | FILE *stream, unsigned char *ptr, size_t size, |
| - | char nohex) |
| + | static void dump(const char *text, FILE *stream, unsigned char *ptr, |
| + | size_t size, char nohex) |
| { | |
| size_t i; | |
| size_t c; | |
| @@ -83,10 +81,8 @@ void dump(const char *text, | |
| fflush(stream); | |
| } | |
| - | static |
| - | int my_trace(CURL *handle, curl_infotype type, |
| - | char *data, size_t size, |
| - | void *userp) |
| + | static int my_trace(CURL *handle, curl_infotype type, |
| + | char *data, size_t size, void *userp) |
| { | |
| struct data *config = (struct data *)userp; | |
| const char *text; | |
| @@ -30,8 +30,7 @@ | |
| * in a separate file using our own callback! | |
| * </DESC> | |
| */ | |
| - | static size_t |
| - | write_response(void *ptr, size_t size, size_t nmemb, void *data) |
| + | static size_t write_response(void *ptr, size_t size, size_t nmemb, void *data) |
| { | |
| FILE *writehere = (FILE *)data; | |
| return fwrite(ptr, size, nmemb, writehere); | |
| @@ -88,5 +87,7 @@ int main(void) | |
| fclose(ftpfile); /* close the local file */ | |
| fclose(respfile); /* close the response file */ | |
| + | curl_global_cleanup(); |
| + | |
| return (int)res; | |
| } | |
| @@ -84,8 +84,8 @@ int main(void) | |
| curl_off_t fsize; | |
| struct curl_slist *headerlist = NULL; | |
| - | static const char buf_1 [] = "RNFR " UPLOAD_FILE_AS; |
| - | static const char buf_2 [] = "RNTO " RENAME_FILE_TO; |
| + | static const char buf_1[] = "RNFR " UPLOAD_FILE_AS; |
| + | static const char buf_2[] = "RNTO " RENAME_FILE_TO; |
| /* get a FILE * of the file */ | |
| hd_src = fopen(LOCAL_FILE, "rb"); | |
| @@ -159,11 +159,13 @@ int main(void) | |
| return (int)res; | |
| curlhandle = curl_easy_init(); | |
| + | if(curlhandle) { |
| - | upload(curlhandle, "ftp://user:[email protected]/path/file", "C:\\file", |
| - | 0, 3); |
| + | upload(curlhandle, "ftp://user:[email protected]/path/file", "C:\\file", |
| + | 0, 3); |
| - | curl_easy_cleanup(curlhandle); |
| + | curl_easy_cleanup(curlhandle); |
| + | } |
| curl_global_cleanup(); | |
| return 0; | |
| @@ -38,8 +38,8 @@ struct MemoryStruct { | |
| size_t size; | |
| }; | |
| - | static size_t |
| - | WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) |
| + | static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, |
| + | void *userp) |
| { | |
| size_t realsize = size * nmemb; | |
| struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
| @@ -75,41 +75,43 @@ int main(void) | |
| /* init the curl session */ | |
| curl_handle = curl_easy_init(); | |
| - | |
| - | /* specify URL to get */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/"); |
| - | |
| - | /* send all data to this function */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); |
| - | |
| - | /* we pass our 'chunk' struct to the callback function */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); |
| - | |
| - | /* some servers do not like requests that are made without a user-agent |
| - | field, so we provide one */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); |
| - | |
| - | /* get it! */ |
| - | res = curl_easy_perform(curl_handle); |
| - | |
| - | /* check for errors */ |
| - | if(res != CURLE_OK) { |
| - | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| - | curl_easy_strerror(res)); |
| + | if(curl_handle) { |
| + | |
| + | /* specify URL to get */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.example.com/"); |
| + | |
| + | /* send all data to this function */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); |
| + | |
| + | /* we pass our 'chunk' struct to the callback function */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); |
| + | |
| + | /* some servers do not like requests that are made without a user-agent |
| + | field, so we provide one */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); |
| + | |
| + | /* get it! */ |
| + | res = curl_easy_perform(curl_handle); |
| + | |
| + | /* check for errors */ |
| + | if(res != CURLE_OK) { |
| + | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| + | curl_easy_strerror(res)); |
| + | } |
| + | else { |
| + | /* |
| + | * Now, our chunk.memory points to a memory block that is chunk.size |
| + | * bytes big and contains the remote file. |
| + | * |
| + | * Do something nice with it! |
| + | */ |
| + | |
| + | printf("%lu bytes retrieved\n", (unsigned long)chunk.size); |
| + | } |
| + | |
| + | /* cleanup curl stuff */ |
| + | curl_easy_cleanup(curl_handle); |
| } | |
| - | else { |
| - | /* |
| - | * Now, our chunk.memory points to a memory block that is chunk.size |
| - | * bytes big and contains the remote file. |
| - | * |
| - | * Do something nice with it! |
| - | */ |
| - | |
| - | printf("%lu bytes retrieved\n", (unsigned long)chunk.size); |
| - | } |
| - | |
| - | /* cleanup curl stuff */ |
| - | curl_easy_cleanup(curl_handle); |
| free(chunk.memory); | |
| @@ -434,16 +434,18 @@ int main(void) | |
| g_io_add_watch(ch, G_IO_IN, fifo_cb, g); | |
| gmain = g_main_loop_new(NULL, FALSE); | |
| g->multi = curl_multi_init(); | |
| - | curl_multi_setopt(g->multi, CURLMOPT_SOCKETFUNCTION, sock_cb); |
| - | curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g); |
| - | curl_multi_setopt(g->multi, CURLMOPT_TIMERFUNCTION, update_timeout_cb); |
| - | curl_multi_setopt(g->multi, CURLMOPT_TIMERDATA, g); |
| + | if(g->multi) { |
| + | curl_multi_setopt(g->multi, CURLMOPT_SOCKETFUNCTION, sock_cb); |
| + | curl_multi_setopt(g->multi, CURLMOPT_SOCKETDATA, g); |
| + | curl_multi_setopt(g->multi, CURLMOPT_TIMERFUNCTION, update_timeout_cb); |
| + | curl_multi_setopt(g->multi, CURLMOPT_TIMERDATA, g); |
| - | /* we do not call any curl_multi_socket*() function yet as we have no handles |
| - | added! */ |
| + | /* we do not call any curl_multi_socket*() function yet as we have no |
| + | handles added! */ |
| - | g_main_loop_run(gmain); |
| - | curl_multi_cleanup(g->multi); |
| + | g_main_loop_run(gmain); |
| + | curl_multi_cleanup(g->multi); |
| + | } |
| curl_global_cleanup(); | |
| return 0; | |
| } | |
| @@ -92,42 +92,47 @@ int main(int argc, char **argv) | |
| if(res) | |
| return (int)res; | |
| - | curl = curl_easy_init(); |
| - | curl_easy_setopt(curl, CURLOPT_URL, argv[1]); |
| - | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf); |
| - | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); |
| - | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| - | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); |
| - | |
| tdoc = tidyCreate(); | |
| tidyOptSetBool(tdoc, TidyForceOutput, yes); /* try harder */ | |
| tidyOptSetInt(tdoc, TidyWrapLen, 4096); | |
| tidySetErrorBuffer(tdoc, &tidy_errbuf); | |
| tidyBufInit(&docbuf); | |
| - | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf); |
| - | res = curl_easy_perform(curl); |
| - | if(!res) { |
| - | res = tidyParseBuffer(tdoc, &docbuf); /* parse the input */ |
| - | if(res >= 0) { |
| - | res = tidyCleanAndRepair(tdoc); /* fix any problems */ |
| + | curl = curl_easy_init(); |
| + | if(curl) { |
| + | curl_easy_setopt(curl, CURLOPT_URL, argv[1]); |
| + | curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf); |
| + | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); |
| + | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| + | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_cb); |
| + | |
| + | curl_easy_setopt(curl, CURLOPT_WRITEDATA, &docbuf); |
| + | res = curl_easy_perform(curl); |
| + | if(!res) { |
| + | res = tidyParseBuffer(tdoc, &docbuf); /* parse the input */ |
| if(res >= 0) { | |
| - | res = tidyRunDiagnostics(tdoc); /* load tidy error buffer */ |
| + | res = tidyCleanAndRepair(tdoc); /* fix any problems */ |
| if(res >= 0) { | |
| - | dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */ |
| - | fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */ |
| + | res = tidyRunDiagnostics(tdoc); /* load tidy error buffer */ |
| + | if(res >= 0) { |
| + | dumpNode(tdoc, tidyGetRoot(tdoc), 0); /* walk the tree */ |
| + | fprintf(stderr, "%s\n", tidy_errbuf.bp); /* show errors */ |
| + | } |
| } | |
| } | |
| } | |
| + | else |
| + | fprintf(stderr, "%s\n", curl_errbuf); |
| + | |
| + | /* clean-up */ |
| + | curl_easy_cleanup(curl); |
| } | |
| - | else |
| - | fprintf(stderr, "%s\n", curl_errbuf); |
| - | /* clean-up */ |
| - | curl_easy_cleanup(curl); |
| - | curl_global_cleanup(); |
| tidyBufFree(&docbuf); | |
| tidyBufFree(&tidy_errbuf); | |
| tidyRelease(tdoc); | |
| + | |
| + | curl_global_cleanup(); |
| + | |
| return (int)res; | |
| } | |
| @@ -56,9 +56,8 @@ struct transfer { | |
| #define NUM_HANDLES 1000 | |
| - | static |
| - | void dump(const char *text, unsigned int num, unsigned char *ptr, size_t size, |
| - | char nohex) |
| + | static void dump(const char *text, unsigned int num, unsigned char *ptr, |
| + | size_t size, char nohex) |
| { | |
| size_t i; | |
| size_t c; | |
| @@ -105,10 +104,8 @@ void dump(const char *text, unsigned int num, unsigned char *ptr, size_t size, | |
| } | |
| } | |
| - | static |
| - | int my_trace(CURL *handle, curl_infotype type, |
| - | char *data, size_t size, |
| - | void *userp) |
| + | static int my_trace(CURL *handle, curl_infotype type, |
| + | char *data, size_t size, void *userp) |
| { | |
| const char *text; | |
| struct transfer *t = (struct transfer *)userp; | |
| @@ -210,6 +207,8 @@ int main(int argc, char **argv) | |
| if(res) | |
| return (int)res; | |
| + | memset(trans, 0, sizeof(trans)); |
| + | |
| /* init a multi stack */ | |
| multi_handle = curl_multi_init(); | |
| @@ -242,6 +241,7 @@ int main(int argc, char **argv) | |
| } | |
| curl_multi_cleanup(multi_handle); | |
| + | curl_global_cleanup(); |
| return 0; | |
| } | |
| @@ -37,8 +37,7 @@ struct Memory { | |
| size_t size; | |
| }; | |
| - | static size_t |
| - | write_cb(void *contents, size_t size, size_t nmemb, void *userp) |
| + | static size_t write_cb(void *contents, size_t size, size_t nmemb, void *userp) |
| { | |
| size_t realsize = size * nmemb; | |
| struct Memory *mem = (struct Memory *)userp; | |
| @@ -125,10 +124,8 @@ int main(void) | |
| { | |
| CURL *easy; | |
| CURLM *multi; | |
| - | int still_running; /* keep number of running handles */ |
| int transfers = 1; /* we start with one */ | |
| int i; | |
| - | struct CURLMsg *m; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -150,7 +147,10 @@ int main(void) | |
| curl_multi_setopt(multi, CURLMOPT_PUSHDATA, &transfers); | |
| while(transfers) { | |
| + | struct CURLMsg *m; |
| + | int still_running; /* keep number of running handles */ |
| int rc; | |
| + | |
| CURLMcode mcode = curl_multi_perform(multi, &still_running); | |
| if(mcode) | |
| break; | |
| @@ -173,7 +173,6 @@ int main(void) | |
| curl_easy_cleanup(e); | |
| } | |
| } while(m); | |
| - | |
| } | |
| curl_multi_cleanup(multi); | |
| @@ -40,9 +40,7 @@ | |
| #error "too old libcurl, cannot do HTTP/2 server push!" | |
| #endif | |
| - | static |
| - | void dump(const char *text, unsigned char *ptr, size_t size, |
| - | char nohex) |
| + | static void dump(const char *text, unsigned char *ptr, size_t size, char nohex) |
| { | |
| size_t i; | |
| size_t c; | |
| @@ -89,10 +87,8 @@ void dump(const char *text, unsigned char *ptr, size_t size, | |
| } | |
| } | |
| - | static |
| - | int my_trace(CURL *handle, curl_infotype type, |
| - | char *data, size_t size, |
| - | void *userp) |
| + | static int my_trace(CURL *handle, curl_infotype type, |
| + | char *data, size_t size, void *userp) |
| { | |
| const char *text; | |
| (void)handle; | |
| @@ -216,7 +212,6 @@ int main(int argc, char *argv[]) | |
| CURL *easy; | |
| CURLM *multi_handle; | |
| int transfers = 1; /* we start with one */ | |
| - | struct CURLMsg *m; |
| const char *url = "https://localhost:8443/index.html"; | |
| if(argc == 2) | |
| @@ -246,6 +241,7 @@ int main(int argc, char *argv[]) | |
| curl_multi_setopt(multi_handle, CURLMOPT_PUSHDATA, &transfers); | |
| do { | |
| + | struct CURLMsg *m; |
| int still_running; /* keep number of running handles */ | |
| CURLMcode mc = curl_multi_perform(multi_handle, &still_running); | |
| @@ -68,8 +68,7 @@ | |
| #ifdef _MSC_VER | |
| #define gettimeofday(a, b) my_gettimeofday((a), (b)) | |
| - | static |
| - | int my_gettimeofday(struct timeval *tp, void *tzp) |
| + | static int my_gettimeofday(struct timeval *tp, void *tzp) |
| { | |
| (void)tzp; | |
| if(tp) { | |
| @@ -89,14 +88,14 @@ int my_gettimeofday(struct timeval *tp, void *tzp) | |
| struct input { | |
| FILE *in; | |
| + | FILE *out; |
| size_t bytes_read; /* count up */ | |
| CURL *hnd; | |
| int num; | |
| }; | |
| - | static |
| - | void dump(const char *text, int num, unsigned char *ptr, size_t size, |
| - | char nohex) |
| + | static void dump(const char *text, int num, unsigned char *ptr, size_t size, |
| + | char nohex) |
| { | |
| size_t i; | |
| size_t c; | |
| @@ -142,10 +141,8 @@ void dump(const char *text, int num, unsigned char *ptr, size_t size, | |
| } | |
| } | |
| - | static |
| - | int my_trace(CURL *handle, curl_infotype type, |
| - | char *data, size_t size, |
| - | void *userp) |
| + | static int my_trace(CURL *handle, curl_infotype type, char *data, |
| + | size_t size, void *userp) |
| { | |
| char timebuf[60]; | |
| const char *text; | |
| @@ -208,7 +205,6 @@ static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *userp) | |
| static int setup(struct input *i, int num, const char *upload) | |
| { | |
| - | FILE *out; |
| char url[256]; | |
| char filename[128]; | |
| struct stat file_info; | |
| @@ -219,8 +215,8 @@ static int setup(struct input *i, int num, const char *upload) | |
| i->num = num; | |
| snprintf(filename, sizeof(filename), "dl-%d", num); | |
| - | out = fopen(filename, "wb"); |
| - | if(!out) { |
| + | i->out = fopen(filename, "wb"); |
| + | if(!i->out) { |
| fprintf(stderr, "error: could not open file %s for writing: %s\n", upload, | |
| strerror(errno)); | |
| return 1; | |
| @@ -232,7 +228,8 @@ static int setup(struct input *i, int num, const char *upload) | |
| if(!i->in) { | |
| fprintf(stderr, "error: could not open file %s for reading: %s\n", upload, | |
| strerror(errno)); | |
| - | fclose(out); |
| + | fclose(i->out); |
| + | i->out = NULL; |
| return 1; | |
| } | |
| @@ -244,46 +241,49 @@ static int setup(struct input *i, int num, const char *upload) | |
| #endif | |
| fprintf(stderr, "error: could not stat file %s: %s\n", upload, | |
| strerror(errno)); | |
| - | fclose(out); |
| + | fclose(i->out); |
| + | i->out = NULL; |
| return 1; | |
| } | |
| uploadsize = file_info.st_size; | |
| hnd = i->hnd = curl_easy_init(); | |
| + | if(hnd) { |
| - | /* write to this file */ |
| - | curl_easy_setopt(hnd, CURLOPT_WRITEDATA, out); |
| + | /* write to this file */ |
| + | curl_easy_setopt(hnd, CURLOPT_WRITEDATA, i->out); |
| - | /* we want to use our own read function */ |
| - | curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback); |
| - | /* read from this file */ |
| - | curl_easy_setopt(hnd, CURLOPT_READDATA, i); |
| - | /* provide the size of the upload */ |
| - | curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize); |
| + | /* we want to use our own read function */ |
| + | curl_easy_setopt(hnd, CURLOPT_READFUNCTION, read_callback); |
| + | /* read from this file */ |
| + | curl_easy_setopt(hnd, CURLOPT_READDATA, i); |
| + | /* provide the size of the upload */ |
| + | curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, uploadsize); |
| - | /* send in the URL to store the upload as */ |
| - | curl_easy_setopt(hnd, CURLOPT_URL, url); |
| + | /* send in the URL to store the upload as */ |
| + | curl_easy_setopt(hnd, CURLOPT_URL, url); |
| - | /* upload please */ |
| - | curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L); |
| + | /* upload please */ |
| + | curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1L); |
| - | /* please be verbose */ |
| - | curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L); |
| - | curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace); |
| - | curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i); |
| + | /* please be verbose */ |
| + | curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1L); |
| + | curl_easy_setopt(hnd, CURLOPT_DEBUGFUNCTION, my_trace); |
| + | curl_easy_setopt(hnd, CURLOPT_DEBUGDATA, i); |
| - | /* HTTP/2 please */ |
| - | curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| + | /* HTTP/2 please */ |
| + | curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0); |
| - | /* we use a self-signed test server, skip verification during debugging */ |
| - | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L); |
| - | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L); |
| + | /* we use a self-signed test server, skip verification during debugging */ |
| + | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L); |
| + | curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 0L); |
| #if (CURLPIPE_MULTIPLEX > 0) | |
| - | /* wait for pipe connection to confirm */ |
| - | curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L); |
| + | /* wait for pipe connection to confirm */ |
| + | curl_easy_setopt(hnd, CURLOPT_PIPEWAIT, 1L); |
| #endif | |
| + | } |
| return 0; | |
| } | |
| @@ -296,7 +296,6 @@ int main(int argc, char **argv) | |
| struct input trans[NUM_HANDLES]; | |
| CURLM *multi_handle; | |
| int i; | |
| - | int still_running = 0; /* keep number of running handles */ |
| const char *filename = "index.html"; | |
| int num_transfers; | |
| @@ -318,41 +317,54 @@ int main(int argc, char **argv) | |
| if(res) | |
| return (int)res; | |
| + | memset(trans, 0, sizeof(trans)); |
| + | |
| /* init a multi stack */ | |
| multi_handle = curl_multi_init(); | |
| + | if(multi_handle) { |
| - | for(i = 0; i < num_transfers; i++) { |
| - | if(setup(&trans[i], i, filename)) { |
| - | curl_global_cleanup(); |
| - | return 1; |
| + | int still_running = 0; /* keep number of running handles */ |
| + | |
| + | for(i = 0; i < num_transfers; i++) { |
| + | if(setup(&trans[i], i, filename)) { |
| + | curl_global_cleanup(); |
| + | return 1; |
| + | } |
| + | |
| + | /* add the individual transfer */ |
| + | curl_multi_add_handle(multi_handle, trans[i].hnd); |
| } | |
| - | /* add the individual transfer */ |
| - | curl_multi_add_handle(multi_handle, trans[i].hnd); |
| - | } |
| + | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| - | curl_multi_setopt(multi_handle, CURLMOPT_PIPELINING, CURLPIPE_MULTIPLEX); |
| + | /* We do HTTP/2 so let's stick to one connection per host */ |
| + | curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L); |
| - | /* We do HTTP/2 so let's stick to one connection per host */ |
| - | curl_multi_setopt(multi_handle, CURLMOPT_MAX_HOST_CONNECTIONS, 1L); |
| + | do { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | do { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | if(mc) |
| + | break; |
| - | if(mc) |
| - | break; |
| + | } while(still_running); |
| - | } while(still_running); |
| + | for(i = 0; i < num_transfers; i++) |
| + | curl_multi_remove_handle(multi_handle, trans[i].hnd); |
| - | curl_multi_cleanup(multi_handle); |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| for(i = 0; i < num_transfers; i++) { | |
| - | curl_multi_remove_handle(multi_handle, trans[i].hnd); |
| curl_easy_cleanup(trans[i].hnd); | |
| + | |
| + | if(trans[i].in) |
| + | fclose(trans[i].in); |
| + | if(trans[i].out) |
| + | fclose(trans[i].out); |
| } | |
| curl_global_cleanup(); | |
| @@ -37,21 +37,17 @@ | |
| * Download an HTTP file and upload an FTP file simultaneously. | |
| */ | |
| - | #define HANDLECOUNT 2 /* Number of simultaneous transfers */ |
| #define HTTP_HANDLE 0 /* Index for the HTTP transfer */ | |
| #define FTP_HANDLE 1 /* Index for the FTP transfer */ | |
| + | #define HANDLECOUNT 2 /* Number of simultaneous transfers */ |
| int main(void) | |
| { | |
| CURL *handles[HANDLECOUNT]; | |
| CURLM *multi_handle; | |
| - | int still_running = 1; /* keep number of running handles */ |
| int i; | |
| - | CURLMsg *msg; /* for picking up messages with the transfer status */ |
| - | int msgs_left; /* how many messages are left */ |
| - | |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| @@ -68,52 +64,63 @@ int main(void) | |
| /* init a multi stack */ | |
| multi_handle = curl_multi_init(); | |
| + | if(multi_handle) { |
| - | /* add the individual transfers */ |
| - | for(i = 0; i < HANDLECOUNT; i++) |
| - | curl_multi_add_handle(multi_handle, handles[i]); |
| + | int still_running = 1; /* keep number of running handles */ |
| - | while(still_running) { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | CURLMsg *msg; /* for picking up messages with the transfer status */ |
| + | int msgs_left; /* how many messages are left */ |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | /* add the individual transfers */ |
| + | for(i = 0; i < HANDLECOUNT; i++) |
| + | curl_multi_add_handle(multi_handle, handles[i]); |
| - | if(mc) |
| - | break; |
| - | } |
| - | /* See how the transfers went */ |
| - | /* !checksrc! disable EQUALSNULL 1 */ |
| - | while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { |
| - | if(msg->msg == CURLMSG_DONE) { |
| - | int idx; |
| - | |
| - | /* Find out which handle this message is about */ |
| - | for(idx = 0; idx < HANDLECOUNT; idx++) { |
| - | int found = (msg->easy_handle == handles[idx]); |
| - | if(found) |
| - | break; |
| - | } |
| + | while(still_running) { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | switch(idx) { |
| - | case HTTP_HANDLE: |
| - | printf("HTTP transfer completed with status %d\n", msg->data.result); |
| - | break; |
| - | case FTP_HANDLE: |
| - | printf("FTP transfer completed with status %d\n", msg->data.result); |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | |
| + | if(mc) |
| break; | |
| + | } |
| + | |
| + | /* See how the transfers went */ |
| + | /* !checksrc! disable EQUALSNULL 1 */ |
| + | while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { |
| + | if(msg->msg == CURLMSG_DONE) { |
| + | int idx; |
| + | |
| + | /* Find out which handle this message is about */ |
| + | for(idx = 0; idx < HANDLECOUNT; idx++) { |
| + | int found = (msg->easy_handle == handles[idx]); |
| + | if(found) |
| + | break; |
| + | } |
| + | |
| + | switch(idx) { |
| + | case HTTP_HANDLE: |
| + | printf("HTTP transfer completed with status %d\n", msg->data.result); |
| + | break; |
| + | case FTP_HANDLE: |
| + | printf("FTP transfer completed with status %d\n", msg->data.result); |
| + | break; |
| + | } |
| } | |
| } | |
| + | |
| + | /* remove the transfers */ |
| + | for(i = 0; i < HANDLECOUNT; i++) |
| + | curl_multi_remove_handle(multi_handle, handles[i]); |
| + | |
| + | curl_multi_cleanup(multi_handle); |
| } | |
| - | /* remove the transfers and cleanup the handles */ |
| - | for(i = 0; i < HANDLECOUNT; i++) { |
| - | curl_multi_remove_handle(multi_handle, handles[i]); |
| + | /* Free the curl handles */ |
| + | for(i = 0; i < HANDLECOUNT; i++) |
| curl_easy_cleanup(handles[i]); | |
| - | } |
| - | curl_multi_cleanup(multi_handle); |
| curl_global_cleanup(); | |
| return 0; | |
| @@ -83,10 +83,9 @@ static void dump(const char *text, FILE *stream, unsigned char *ptr, | |
| fflush(stream); | |
| } | |
| - | static |
| - | int my_trace(CURL *handle, curl_infotype type, |
| - | unsigned char *data, size_t size, |
| - | void *userp) |
| + | static int my_trace(CURL *handle, curl_infotype type, |
| + | unsigned char *data, size_t size, |
| + | void *userp) |
| { | |
| const char *text; | |
| @@ -123,43 +122,48 @@ int my_trace(CURL *handle, curl_infotype type, | |
| int main(void) | |
| { | |
| CURL *http_handle; | |
| - | CURLM *multi_handle; |
| - | |
| - | int still_running = 0; /* keep number of running handles */ |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| http_handle = curl_easy_init(); | |
| + | if(http_handle) { |
| + | |
| + | CURLM *multi_handle; |
| - | /* set the options (I left out a few, you get the point anyway) */ |
| - | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| + | /* set the options (I left out a few, you get the point anyway) */ |
| + | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| - | curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace); |
| - | curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L); |
| + | curl_easy_setopt(http_handle, CURLOPT_DEBUGFUNCTION, my_trace); |
| + | curl_easy_setopt(http_handle, CURLOPT_VERBOSE, 1L); |
| - | /* init a multi stack */ |
| - | multi_handle = curl_multi_init(); |
| + | /* init a multi stack */ |
| + | multi_handle = curl_multi_init(); |
| + | if(multi_handle) { |
| - | /* add the individual transfers */ |
| - | curl_multi_add_handle(multi_handle, http_handle); |
| + | int still_running = 0; /* keep number of running handles */ |
| - | do { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | /* add the individual transfers */ |
| + | curl_multi_add_handle(multi_handle, http_handle); |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | do { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | if(mc) |
| - | break; |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| - | } while(still_running); |
| + | if(mc) |
| + | break; |
| - | curl_multi_cleanup(multi_handle); |
| + | } while(still_running); |
| - | curl_easy_cleanup(http_handle); |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| + | |
| + | curl_easy_cleanup(http_handle); |
| + | } |
| curl_global_cleanup(); | |
| @@ -38,9 +38,6 @@ int main(void) | |
| { | |
| CURL *http_handle; | |
| CURL *http_handle2; | |
| - | CURLM *multi_handle; |
| - | |
| - | int still_running = 1; /* keep number of running handles */ |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -49,46 +46,57 @@ int main(void) | |
| http_handle = curl_easy_init(); | |
| http_handle2 = curl_easy_init(); | |
| - | /* set options */ |
| - | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| + | if(http_handle && |
| + | http_handle2) { |
| + | |
| + | CURLM *multi_handle; |
| + | |
| + | /* set options */ |
| + | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| + | |
| + | /* set options */ |
| + | curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); |
| - | /* set options */ |
| - | curl_easy_setopt(http_handle2, CURLOPT_URL, "http://localhost/"); |
| + | /* init a multi stack */ |
| + | multi_handle = curl_multi_init(); |
| + | if(multi_handle) { |
| - | /* init a multi stack */ |
| - | multi_handle = curl_multi_init(); |
| + | int still_running = 1; /* keep number of running handles */ |
| - | /* add the individual transfers */ |
| - | curl_multi_add_handle(multi_handle, http_handle); |
| - | curl_multi_add_handle(multi_handle, http_handle2); |
| + | /* add the individual transfers */ |
| + | curl_multi_add_handle(multi_handle, http_handle); |
| + | curl_multi_add_handle(multi_handle, http_handle2); |
| - | while(still_running) { |
| - | CURLMsg *msg; |
| - | int queued; |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | while(still_running) { |
| + | CURLMsg *msg; |
| + | int queued; |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | if(mc) |
| - | break; |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| - | do { |
| - | msg = curl_multi_info_read(multi_handle, &queued); |
| - | if(msg) { |
| - | if(msg->msg == CURLMSG_DONE) { |
| - | /* a transfer ended */ |
| - | fprintf(stderr, "Transfer completed\n"); |
| - | } |
| + | if(mc) |
| + | break; |
| + | |
| + | do { |
| + | msg = curl_multi_info_read(multi_handle, &queued); |
| + | if(msg) { |
| + | if(msg->msg == CURLMSG_DONE) { |
| + | /* a transfer ended */ |
| + | fprintf(stderr, "Transfer completed\n"); |
| + | } |
| + | } |
| + | } while(msg); |
| } | |
| - | } while(msg); |
| - | } |
| - | curl_multi_remove_handle(multi_handle, http_handle); |
| - | curl_multi_remove_handle(multi_handle, http_handle2); |
| + | curl_multi_remove_handle(multi_handle, http_handle); |
| + | curl_multi_remove_handle(multi_handle, http_handle2); |
| - | curl_multi_cleanup(multi_handle); |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| + | } |
| curl_easy_cleanup(http_handle); | |
| curl_easy_cleanup(http_handle2); | |
| @@ -234,16 +234,18 @@ int main(int argc, char **argv) | |
| timeout = evtimer_new(base, on_timeout, NULL); | |
| curl_handle = curl_multi_init(); | |
| - | curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket); |
| - | curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout); |
| + | if(curl_handle) { |
| + | curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket); |
| + | curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout); |
| - | while(argc-- > 1) { |
| - | add_download(argv[argc], argc); |
| - | } |
| + | while(argc-- > 1) { |
| + | add_download(argv[argc], argc); |
| + | } |
| - | event_base_dispatch(base); |
| + | event_base_dispatch(base); |
| - | curl_multi_cleanup(curl_handle); |
| + | curl_multi_cleanup(curl_handle); |
| + | } |
| event_free(timeout); | |
| event_base_free(base); | |
| @@ -40,9 +40,6 @@ int main(void) | |
| { | |
| CURL *curl; | |
| - | CURLM *multi_handle; |
| - | int still_running = 0; |
| - | |
| struct curl_httppost *formpost = NULL; | |
| struct curl_httppost *lastptr = NULL; | |
| struct curl_slist *headerlist = NULL; | |
| @@ -76,50 +73,59 @@ int main(void) | |
| CURLFORM_END); | |
| ) | |
| - | curl = curl_easy_init(); |
| - | multi_handle = curl_multi_init(); |
| - | |
| /* initialize custom header list (stating that Expect: 100-continue is not | |
| wanted */ | |
| headerlist = curl_slist_append(headerlist, buf); | |
| - | if(curl && multi_handle) { |
| - | /* what URL that receives this POST */ |
| - | curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi"); |
| - | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| + | curl = curl_easy_init(); |
| + | if(curl) { |
| + | CURLM *multi_handle; |
| + | |
| + | multi_handle = curl_multi_init(); |
| + | if(multi_handle) { |
| + | |
| + | int still_running = 0; |
| + | |
| + | /* what URL that receives this POST */ |
| + | curl_easy_setopt(curl, CURLOPT_URL, |
| + | "https://www.example.com/upload.cgi"); |
| + | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| - | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); |
| - | CURL_IGNORE_DEPRECATION( |
| - | curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
| - | ) |
| + | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); |
| + | CURL_IGNORE_DEPRECATION( |
| + | curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost); |
| + | ) |
| - | curl_multi_add_handle(multi_handle, curl); |
| + | curl_multi_add_handle(multi_handle, curl); |
| - | do { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | do { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| - | if(mc) |
| - | break; |
| + | if(mc) |
| + | break; |
| - | } while(still_running); |
| + | } while(still_running); |
| - | curl_multi_cleanup(multi_handle); |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| /* always cleanup */ | |
| curl_easy_cleanup(curl); | |
| + | } |
| + | |
| + | CURL_IGNORE_DEPRECATION( |
| + | /* then cleanup the formpost chain */ |
| + | curl_formfree(formpost); |
| + | ) |
| - | CURL_IGNORE_DEPRECATION( |
| - | /* then cleanup the formpost chain */ |
| - | curl_formfree(formpost); |
| - | ) |
| + | /* free slist */ |
| + | curl_slist_free_all(headerlist); |
| - | /* free slist */ |
| - | curl_slist_free_all(headerlist); |
| - | } |
| curl_global_cleanup(); | |
| + | |
| return 0; | |
| } | |
| @@ -43,21 +43,17 @@ | |
| * Download an HTTP file and upload an FTP file simultaneously. | |
| */ | |
| - | #define HANDLECOUNT 2 /* Number of simultaneous transfers */ |
| #define HTTP_HANDLE 0 /* Index for the HTTP transfer */ | |
| #define FTP_HANDLE 1 /* Index for the FTP transfer */ | |
| + | #define HANDLECOUNT 2 /* Number of simultaneous transfers */ |
| int main(void) | |
| { | |
| CURL *handles[HANDLECOUNT]; | |
| CURLM *multi_handle; | |
| - | int still_running = 0; /* keep number of running handles */ |
| int i; | |
| - | CURLMsg *msg; /* for picking up messages with the transfer status */ |
| - | int msgs_left; /* how many messages are left */ |
| - | |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| @@ -74,124 +70,132 @@ int main(void) | |
| /* init a multi stack */ | |
| multi_handle = curl_multi_init(); | |
| + | if(multi_handle) { |
| - | /* add the individual transfers */ |
| - | for(i = 0; i < HANDLECOUNT; i++) |
| - | curl_multi_add_handle(multi_handle, handles[i]); |
| + | int still_running = 0; /* keep number of running handles */ |
| + | |
| + | CURLMsg *msg; /* for picking up messages with the transfer status */ |
| + | int msgs_left; /* how many messages are left */ |
| + | |
| + | /* add the individual transfers */ |
| + | for(i = 0; i < HANDLECOUNT; i++) |
| + | curl_multi_add_handle(multi_handle, handles[i]); |
| + | |
| + | /* we start some action by calling perform right away */ |
| + | curl_multi_perform(multi_handle, &still_running); |
| - | /* we start some action by calling perform right away */ |
| - | curl_multi_perform(multi_handle, &still_running); |
| + | while(still_running) { |
| - | while(still_running) { |
| - | struct timeval timeout; |
| - | int rc; /* select() return code */ |
| - | CURLMcode mc; /* curl_multi_fdset() return code */ |
| + | struct timeval timeout; |
| + | int rc; /* select() return code */ |
| + | CURLMcode mc; /* curl_multi_fdset() return code */ |
| - | fd_set fdread; |
| - | fd_set fdwrite; |
| - | fd_set fdexcep; |
| - | int maxfd = -1; |
| + | fd_set fdread; |
| + | fd_set fdwrite; |
| + | fd_set fdexcep; |
| + | int maxfd = -1; |
| - | long curl_timeo = -1; |
| + | long curl_timeo = -1; |
| - | FD_ZERO(&fdread); |
| - | FD_ZERO(&fdwrite); |
| - | FD_ZERO(&fdexcep); |
| + | FD_ZERO(&fdread); |
| + | FD_ZERO(&fdwrite); |
| + | FD_ZERO(&fdexcep); |
| - | /* set a suitable timeout to play around with */ |
| - | timeout.tv_sec = 1; |
| - | timeout.tv_usec = 0; |
| + | /* set a suitable timeout to play around with */ |
| + | timeout.tv_sec = 1; |
| + | timeout.tv_usec = 0; |
| - | curl_multi_timeout(multi_handle, &curl_timeo); |
| - | if(curl_timeo >= 0) { |
| + | curl_multi_timeout(multi_handle, &curl_timeo); |
| + | if(curl_timeo >= 0) { |
| #if defined(MSDOS) || defined(__AMIGA__) | |
| - | timeout.tv_sec = (time_t)(curl_timeo / 1000); |
| + | timeout.tv_sec = (time_t)(curl_timeo / 1000); |
| #else | |
| - | timeout.tv_sec = curl_timeo / 1000; |
| + | timeout.tv_sec = curl_timeo / 1000; |
| #endif | |
| - | if(timeout.tv_sec > 1) |
| - | timeout.tv_sec = 1; |
| - | else |
| + | if(timeout.tv_sec > 1) |
| + | timeout.tv_sec = 1; |
| + | else |
| #if defined(MSDOS) || defined(__AMIGA__) | |
| - | timeout.tv_usec = (time_t)(curl_timeo % 1000) * 1000; |
| + | timeout.tv_usec = (time_t)(curl_timeo % 1000) * 1000; |
| #else | |
| - | timeout.tv_usec = (int)(curl_timeo % 1000) * 1000; |
| + | timeout.tv_usec = (int)(curl_timeo % 1000) * 1000; |
| #endif | |
| - | } |
| + | } |
| - | /* get file descriptors from the transfers */ |
| - | mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
| + | /* get file descriptors from the transfers */ |
| + | mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); |
| - | if(mc != CURLM_OK) { |
| - | fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); |
| - | break; |
| - | } |
| + | if(mc != CURLM_OK) { |
| + | fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); |
| + | break; |
| + | } |
| - | /* On success the value of maxfd is guaranteed to be >= -1. We call |
| - | select(maxfd + 1, ...); specially in case of (maxfd == -1) there are |
| - | no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- |
| - | to sleep 100ms, which is the minimum suggested value in the |
| - | curl_multi_fdset() doc. */ |
| + | /* On success the value of maxfd is guaranteed to be >= -1. We call |
| + | select(maxfd + 1, ...); specially in case of (maxfd == -1) there are |
| + | no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- |
| + | to sleep 100ms, which is the minimum suggested value in the |
| + | curl_multi_fdset() doc. */ |
| - | if(maxfd == -1) { |
| + | if(maxfd == -1) { |
| #ifdef _WIN32 | |
| - | Sleep(100); |
| - | rc = 0; |
| + | Sleep(100); |
| + | rc = 0; |
| #else | |
| - | /* Portable sleep for platforms other than Windows. */ |
| - | struct timeval wait = {0}; |
| - | wait.tv_usec = 100 * 1000; /* 100ms */ |
| - | rc = select(0, NULL, NULL, NULL, &wait); |
| + | /* Portable sleep for platforms other than Windows. */ |
| + | struct timeval wait = {0}; |
| + | wait.tv_usec = 100 * 1000; /* 100ms */ |
| + | rc = select(0, NULL, NULL, NULL, &wait); |
| #endif | |
| - | } |
| - | else { |
| - | /* Note that on some platforms 'timeout' may be modified by select(). |
| - | If you need access to the original value save a copy beforehand. */ |
| - | rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); |
| - | } |
| - | |
| - | switch(rc) { |
| - | case -1: |
| - | /* select error */ |
| - | break; |
| - | case 0: /* timeout */ |
| - | default: /* action */ |
| - | curl_multi_perform(multi_handle, &still_running); |
| - | break; |
| - | } |
| - | } |
| - | |
| - | /* See how the transfers went */ |
| - | /* !checksrc! disable EQUALSNULL 1 */ |
| - | while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { |
| - | if(msg->msg == CURLMSG_DONE) { |
| - | int idx; |
| - | |
| - | /* Find out which handle this message is about */ |
| - | for(idx = 0; idx < HANDLECOUNT; idx++) { |
| - | int found = (msg->easy_handle == handles[idx]); |
| - | if(found) |
| - | break; |
| + | } |
| + | else { |
| + | /* Note that on some platforms 'timeout' may be modified by select(). |
| + | If you need access to the original value save a copy beforehand. */ |
| + | rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); |
| } | |
| - | switch(idx) { |
| - | case HTTP_HANDLE: |
| - | printf("HTTP transfer completed with status %d\n", msg->data.result); |
| + | switch(rc) { |
| + | case -1: |
| + | /* select error */ |
| break; | |
| - | case FTP_HANDLE: |
| - | printf("FTP transfer completed with status %d\n", msg->data.result); |
| + | case 0: /* timeout */ |
| + | default: /* action */ |
| + | curl_multi_perform(multi_handle, &still_running); |
| break; | |
| } | |
| } | |
| - | } |
| - | curl_multi_cleanup(multi_handle); |
| + | /* See how the transfers went */ |
| + | /* !checksrc! disable EQUALSNULL 1 */ |
| + | while((msg = curl_multi_info_read(multi_handle, &msgs_left)) != NULL) { |
| + | if(msg->msg == CURLMSG_DONE) { |
| + | int idx; |
| + | |
| + | /* Find out which handle this message is about */ |
| + | for(idx = 0; idx < HANDLECOUNT; idx++) { |
| + | int found = (msg->easy_handle == handles[idx]); |
| + | if(found) |
| + | break; |
| + | } |
| + | |
| + | switch(idx) { |
| + | case HTTP_HANDLE: |
| + | printf("HTTP transfer completed with status %d\n", msg->data.result); |
| + | break; |
| + | case FTP_HANDLE: |
| + | printf("FTP transfer completed with status %d\n", msg->data.result); |
| + | break; |
| + | } |
| + | } |
| + | } |
| + | |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| /* Free the curl handles */ | |
| for(i = 0; i < HANDLECOUNT; i++) | |
| curl_easy_cleanup(handles[i]); | |
| - | curl_global_cleanup(); |
| + | curl_global_cleanup(); |
| return 0; | |
| } | |
| @@ -33,77 +33,82 @@ | |
| int main(void) | |
| { | |
| - | CURL *curl; |
| - | |
| - | CURLM *multi_handle; |
| - | int still_running = 0; |
| - | |
| curl_mime *form = NULL; | |
| curl_mimepart *field = NULL; | |
| struct curl_slist *headerlist = NULL; | |
| static const char buf[] = "Expect:"; | |
| + | CURL *curl; |
| + | |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| curl = curl_easy_init(); | |
| - | multi_handle = curl_multi_init(); |
| + | if(curl) { |
| + | CURLM *multi_handle; |
| - | if(curl && multi_handle) { |
| - | /* Create the form */ |
| - | form = curl_mime_init(curl); |
| + | multi_handle = curl_multi_init(); |
| + | if(multi_handle) { |
| + | int still_running = 0; |
| - | /* Fill in the file upload field */ |
| - | field = curl_mime_addpart(form); |
| - | curl_mime_name(field, "sendfile"); |
| - | curl_mime_filedata(field, "multi-post.c"); |
| + | /* Create the form */ |
| + | form = curl_mime_init(curl); |
| - | /* Fill in the filename field */ |
| - | field = curl_mime_addpart(form); |
| - | curl_mime_name(field, "filename"); |
| - | curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED); |
| + | /* Fill in the file upload field */ |
| + | field = curl_mime_addpart(form); |
| + | curl_mime_name(field, "sendfile"); |
| + | curl_mime_filedata(field, "multi-post.c"); |
| - | /* Fill in the submit field too, even if this is rarely needed */ |
| - | field = curl_mime_addpart(form); |
| - | curl_mime_name(field, "submit"); |
| - | curl_mime_data(field, "send", CURL_ZERO_TERMINATED); |
| + | /* Fill in the filename field */ |
| + | field = curl_mime_addpart(form); |
| + | curl_mime_name(field, "filename"); |
| + | curl_mime_data(field, "multi-post.c", CURL_ZERO_TERMINATED); |
| - | /* initialize custom header list (stating that Expect: 100-continue is not |
| - | wanted */ |
| - | headerlist = curl_slist_append(headerlist, buf); |
| + | /* Fill in the submit field too, even if this is rarely needed */ |
| + | field = curl_mime_addpart(form); |
| + | curl_mime_name(field, "submit"); |
| + | curl_mime_data(field, "send", CURL_ZERO_TERMINATED); |
| - | /* what URL that receives this POST */ |
| - | curl_easy_setopt(curl, CURLOPT_URL, "https://www.example.com/upload.cgi"); |
| - | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| + | /* initialize custom header list (stating that Expect: 100-continue is |
| + | not wanted */ |
| + | headerlist = curl_slist_append(headerlist, buf); |
| - | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); |
| - | curl_easy_setopt(curl, CURLOPT_MIMEPOST, form); |
| + | /* what URL that receives this POST */ |
| + | curl_easy_setopt(curl, CURLOPT_URL, |
| + | "https://www.example.com/upload.cgi"); |
| + | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
| - | curl_multi_add_handle(multi_handle, curl); |
| + | curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist); |
| + | curl_easy_setopt(curl, CURLOPT_MIMEPOST, form); |
| - | do { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | curl_multi_add_handle(multi_handle, curl); |
| - | if(still_running) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | do { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | if(mc) |
| - | break; |
| - | } while(still_running); |
| + | if(still_running) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| - | curl_multi_cleanup(multi_handle); |
| + | if(mc) |
| + | break; |
| + | } while(still_running); |
| + | |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| /* always cleanup */ | |
| curl_easy_cleanup(curl); | |
| + | } |
| - | /* then cleanup the form */ |
| - | curl_mime_free(form); |
| + | /* then cleanup the form */ |
| + | curl_mime_free(form); |
| + | |
| + | /* free slist */ |
| + | curl_slist_free_all(headerlist); |
| - | /* free slist */ |
| - | curl_slist_free_all(headerlist); |
| - | } |
| curl_global_cleanup(); | |
| + | |
| return 0; | |
| } | |
| @@ -38,43 +38,48 @@ | |
| int main(void) | |
| { | |
| CURL *http_handle; | |
| - | CURLM *multi_handle; |
| - | int still_running = 1; /* keep number of running handles */ |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| return (int)res; | |
| http_handle = curl_easy_init(); | |
| + | if(http_handle) { |
| - | /* set the options (I left out a few, you get the point anyway) */ |
| - | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| + | CURLM *multi_handle; |
| + | int still_running = 1; /* keep number of running handles */ |
| - | /* init a multi stack */ |
| - | multi_handle = curl_multi_init(); |
| + | /* set the options (I left out a few, you get the point anyway) */ |
| + | curl_easy_setopt(http_handle, CURLOPT_URL, "https://www.example.com/"); |
| - | /* add the individual transfers */ |
| - | curl_multi_add_handle(multi_handle, http_handle); |
| + | /* init a multi stack */ |
| + | multi_handle = curl_multi_init(); |
| + | if(multi_handle) { |
| - | do { |
| - | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| + | /* add the individual transfers */ |
| + | curl_multi_add_handle(multi_handle, http_handle); |
| - | if(!mc) |
| - | /* wait for activity, timeout or "nothing" */ |
| - | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | do { |
| + | CURLMcode mc = curl_multi_perform(multi_handle, &still_running); |
| - | if(mc) { |
| - | fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc); |
| - | break; |
| - | } |
| + | if(!mc) |
| + | /* wait for activity, timeout or "nothing" */ |
| + | mc = curl_multi_poll(multi_handle, NULL, 0, 1000, NULL); |
| + | |
| + | if(mc) { |
| + | fprintf(stderr, "curl_multi_poll() failed, code %d.\n", (int)mc); |
| + | break; |
| + | } |
| - | } while(still_running); |
| + | } while(still_running); |
| - | curl_multi_remove_handle(multi_handle, http_handle); |
| + | curl_multi_remove_handle(multi_handle, http_handle); |
| - | curl_easy_cleanup(http_handle); |
| + | curl_multi_cleanup(multi_handle); |
| + | } |
| - | curl_multi_cleanup(multi_handle); |
| + | curl_easy_cleanup(http_handle); |
| + | } |
| curl_global_cleanup(); | |
| @@ -241,21 +241,22 @@ int main(int argc, char **argv) | |
| uv_timer_init(uv.loop, &uv.timeout); | |
| uv.multi = curl_multi_init(); | |
| - | curl_multi_setopt(uv.multi, CURLMOPT_SOCKETFUNCTION, cb_socket); |
| - | curl_multi_setopt(uv.multi, CURLMOPT_SOCKETDATA, &uv); |
| - | curl_multi_setopt(uv.multi, CURLMOPT_TIMERFUNCTION, cb_timeout); |
| - | curl_multi_setopt(uv.multi, CURLMOPT_TIMERDATA, &uv); |
| + | if(uv.multi) { |
| + | curl_multi_setopt(uv.multi, CURLMOPT_SOCKETFUNCTION, cb_socket); |
| + | curl_multi_setopt(uv.multi, CURLMOPT_SOCKETDATA, &uv); |
| + | curl_multi_setopt(uv.multi, CURLMOPT_TIMERFUNCTION, cb_timeout); |
| + | curl_multi_setopt(uv.multi, CURLMOPT_TIMERDATA, &uv); |
| + | |
| + | while(argc-- > 1) { |
| + | add_download(argv[argc], argc, uv.multi); |
| + | } |
| - | while(argc-- > 1) { |
| - | add_download(argv[argc], argc, uv.multi); |
| + | /* kickstart the thing */ |
| + | curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0, |
| + | &running_handles); |
| + | uv_run(uv.loop, UV_RUN_DEFAULT); |
| + | curl_multi_cleanup(uv.multi); |
| } | |
| - | |
| - | /* kickstart the thing */ |
| - | curl_multi_socket_action(uv.multi, CURL_SOCKET_TIMEOUT, 0, &running_handles); |
| - | uv_run(uv.loop, UV_RUN_DEFAULT); |
| - | curl_multi_cleanup(uv.multi); |
| - | curl_global_cleanup(); |
| - | |
| curl_global_cleanup(); | |
| return 0; | |
| @@ -52,13 +52,15 @@ static const char * const urls[NUMT]= { | |
| static void *pull_one_url(void *pindex) | |
| { | |
| - | int i = *(int *)pindex; |
| CURL *curl; | |
| curl = curl_easy_init(); | |
| - | curl_easy_setopt(curl, CURLOPT_URL, urls[i]); |
| - | (void)curl_easy_perform(curl); /* ignores error */ |
| - | curl_easy_cleanup(curl); |
| + | if(curl) { |
| + | int i = *(int *)pindex; |
| + | curl_easy_setopt(curl, CURLOPT_URL, urls[i]); |
| + | (void)curl_easy_perform(curl); /* ignores error */ |
| + | curl_easy_cleanup(curl); |
| + | } |
| return NULL; | |
| } | |
| @@ -35,8 +35,8 @@ struct MemoryStruct { | |
| size_t size; | |
| }; | |
| - | static size_t |
| - | WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) |
| + | static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, |
| + | void *userp) |
| { | |
| size_t realsize = size * nmemb; | |
| struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
| @@ -46,19 +46,18 @@ | |
| int main(int argc, char *argv[]) | |
| { | |
| CURL *curl; | |
| - | CURLcode res; |
| - | curl_mime *form = NULL; |
| - | curl_mimepart *field = NULL; |
| - | struct curl_slist *headerlist = NULL; |
| - | static const char buf[] = "Expect:"; |
| - | |
| - | res = curl_global_init(CURL_GLOBAL_ALL); |
| + | CURLcode res = curl_global_init(CURL_GLOBAL_ALL); |
| if(res) | |
| return (int)res; | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | curl_mime *form = NULL; |
| + | curl_mimepart *field = NULL; |
| + | struct curl_slist *headerlist = NULL; |
| + | static const char buf[] = "Expect:"; |
| + | |
| /* Create the form */ | |
| form = curl_mime_init(curl); | |
| @@ -70,7 +70,6 @@ static int xferinfo(void *p, | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct myprogress prog; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -78,6 +77,8 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct myprogress prog; |
| + | |
| prog.lastruntime = 0; | |
| prog.curl = curl; | |
| @@ -121,8 +121,6 @@ static int sftpResumeUpload(CURL *curlhandle, const char *remotepath, | |
| int main(void) | |
| { | |
| - | const char *remote = "sftp://user:[email protected]/path/filename"; |
| - | const char *filename = "filename"; |
| CURL *curlhandle = NULL; | |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| @@ -130,12 +128,16 @@ int main(void) | |
| return (int)res; | |
| curlhandle = curl_easy_init(); | |
| + | if(curlhandle) { |
| + | const char *remote = "sftp://user:[email protected]/path/filename"; |
| + | const char *filename = "filename"; |
| - | if(!sftpResumeUpload(curlhandle, remote, filename)) { |
| - | printf("resumed upload using curl %s failed\n", curl_version()); |
| - | } |
| + | if(!sftpResumeUpload(curlhandle, remote, filename)) { |
| + | printf("resumed upload using curl %s failed\n", curl_version()); |
| + | } |
| - | curl_easy_cleanup(curlhandle); |
| + | curl_easy_cleanup(curlhandle); |
| + | } |
| curl_global_cleanup(); | |
| return 0; | |
| @@ -67,22 +67,24 @@ size_t write_file(void *ptr, size_t size, size_t nmemb, FILE *stream) | |
| static void run_one(gchar *http, int j) | |
| { | |
| - | FILE *outfile = fopen(urls[j], "wb"); |
| CURL *curl; | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| printf("j = %d\n", j); | |
| - | /* Set the URL and transfer type */ |
| - | curl_easy_setopt(curl, CURLOPT_URL, http); |
| + | FILE *outfile = fopen(urls[j], "wb"); |
| + | if(outfile) { |
| + | /* Set the URL and transfer type */ |
| + | curl_easy_setopt(curl, CURLOPT_URL, http); |
| - | /* Write to the file */ |
| - | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
| - | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); |
| - | (void)curl_easy_perform(curl); |
| + | /* Write to the file */ |
| + | curl_easy_setopt(curl, CURLOPT_WRITEDATA, outfile); |
| + | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_file); |
| + | (void)curl_easy_perform(curl); |
| - | fclose(outfile); |
| + | fclose(outfile); |
| + | } |
| curl_easy_cleanup(curl); | |
| } | |
| } | |
| @@ -95,8 +95,6 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| - | struct upload_status upload_ctx = { 0 }; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -104,6 +102,9 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | struct upload_status upload_ctx = { 0 }; |
| + | |
| /* This is the URL for your mailserver. In this example we connect to the | |
| smtp-submission port as we require an authenticated connection. */ | |
| curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com:587"); | |
| @@ -42,7 +42,6 @@ | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -50,6 +49,8 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | |
| /* This is the URL for your mailserver */ | |
| curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); | |
| @@ -92,8 +92,6 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| - | struct upload_status upload_ctx = { 0 }; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -101,6 +99,9 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | struct upload_status upload_ctx = { 0 }; |
| + | |
| /* This is the URL for your mailserver */ | |
| curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); | |
| @@ -89,8 +89,6 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| - | struct upload_status upload_ctx = { 0 }; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -98,6 +96,9 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | struct upload_status upload_ctx = { 0 }; |
| + | |
| /* Set username and password */ | |
| curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); | |
| curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); | |
| @@ -89,8 +89,6 @@ static size_t payload_source(char *ptr, size_t size, size_t nmemb, void *userp) | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| - | struct upload_status upload_ctx = { 0 }; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -98,6 +96,9 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | struct upload_status upload_ctx = { 0 }; |
| + | |
| /* Set username and password */ | |
| curl_easy_setopt(curl, CURLOPT_USERNAME, "user"); | |
| curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret"); | |
| @@ -45,7 +45,6 @@ | |
| int main(void) | |
| { | |
| CURL *curl; | |
| - | struct curl_slist *recipients = NULL; |
| CURLcode res = curl_global_init(CURL_GLOBAL_ALL); | |
| if(res) | |
| @@ -53,6 +52,8 @@ int main(void) | |
| curl = curl_easy_init(); | |
| if(curl) { | |
| + | struct curl_slist *recipients = NULL; |
| + | |
| /* This is the URL for your mailserver */ | |
| curl_easy_setopt(curl, CURLOPT_URL, "smtp://mail.example.com"); | |
| @@ -55,17 +55,19 @@ static const char * const urls[]= { | |
| static void *pull_one_url(void *pindex) | |
| { | |
| - | int i = *(int *)pindex; |
| CURL *curl; | |
| curl = curl_easy_init(); | |
| - | curl_easy_setopt(curl, CURLOPT_URL, urls[i]); |
| - | /* this example does not verify the server's certificate, which means we |
| - | might be downloading stuff from an impostor */ |
| - | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
| - | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
| - | (void)curl_easy_perform(curl); /* ignores error */ |
| - | curl_easy_cleanup(curl); |
| + | if(curl) { |
| + | int i = *(int *)pindex; |
| + | curl_easy_setopt(curl, CURLOPT_URL, urls[i]); |
| + | /* this example does not verify the server's certificate, which means we |
| + | might be downloading stuff from an impostor */ |
| + | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
| + | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
| + | (void)curl_easy_perform(curl); /* ignores error */ |
| + | curl_easy_cleanup(curl); |
| + | } |
| return NULL; | |
| } | |
| @@ -38,10 +38,10 @@ static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) | |
| int main(int argc, char *argv[]) | |
| { | |
| + | static const char *pagefilename = "page.out"; |
| + | |
| CURLcode res; | |
| CURL *curl_handle; | |
| - | static const char *pagefilename = "page.out"; |
| - | FILE *pagefile; |
| if(argc < 2) { | |
| printf("Usage: %s <URL>\n", argv[0]); | |
| @@ -56,35 +56,38 @@ int main(int argc, char *argv[]) | |
| /* init the curl session */ | |
| curl_handle = curl_easy_init(); | |
| + | if(curl_handle) { |
| + | FILE *pagefile; |
| - | /* set URL to get here */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]); |
| + | /* set URL to get here */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_URL, argv[1]); |
| - | /* Switch on full protocol/debug output while testing */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); |
| + | /* Switch on full protocol/debug output while testing */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1L); |
| - | /* disable progress meter, set to 0L to enable it */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); |
| + | /* disable progress meter, set to 0L to enable it */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L); |
| - | /* send all data to this function */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); |
| + | /* send all data to this function */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data); |
| - | /* open the file */ |
| - | pagefile = fopen(pagefilename, "wb"); |
| - | if(pagefile) { |
| + | /* open the file */ |
| + | pagefile = fopen(pagefilename, "wb"); |
| + | if(pagefile) { |
| - | /* write the page body to this file handle */ |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); |
| + | /* write the page body to this file handle */ |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile); |
| - | /* get it! */ |
| - | res = curl_easy_perform(curl_handle); |
| + | /* get it! */ |
| + | res = curl_easy_perform(curl_handle); |
| - | /* close the header file */ |
| - | fclose(pagefile); |
| - | } |
| + | /* close the header file */ |
| + | fclose(pagefile); |
| + | } |
| - | /* cleanup curl stuff */ |
| - | curl_easy_cleanup(curl_handle); |
| + | /* cleanup curl stuff */ |
| + | curl_easy_cleanup(curl_handle); |
| + | } |
| curl_global_cleanup(); | |
| @@ -34,8 +34,7 @@ | |
| int main(void) | |
| { | |
| - | CURL *curl; |
| - | |
| + | CURL *curl = NULL; |
| CURLU *urlp; | |
| CURLUcode uc; | |
| @@ -43,9 +42,6 @@ int main(void) | |
| if(res) | |
| return (int)res; | |
| - | /* get a curl handle */ |
| - | curl = curl_easy_init(); |
| - | |
| /* init Curl URL */ | |
| urlp = curl_url(); | |
| uc = curl_url_set(urlp, CURLUPART_URL, | |
| @@ -56,6 +52,8 @@ int main(void) | |
| goto cleanup; | |
| } | |
| + | /* get a curl handle */ |
| + | curl = curl_easy_init(); |
| if(curl) { | |
| /* set urlp to use as working URL */ | |
| curl_easy_setopt(curl, CURLOPT_CURLU, urlp); | |
| @@ -69,8 +67,6 @@ int main(void) | |
| if(res != CURLE_OK) | |
| fprintf(stderr, "curl_easy_perform() failed: %s\n", | |
| curl_easy_strerror(res)); | |
| - | |
| - | goto cleanup; |
| } | |
| cleanup: | |
| @@ -162,45 +162,46 @@ int main(void) | |
| 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); |
| - | 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"); |
| - | |
| - | /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there is |
| - | no CA certificate */ |
| - | |
| - | curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); |
| - | curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); |
| - | curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/"); |
| - | curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM"); |
| - | |
| - | /* first try: retrieve page without user certificate and key -> fails */ |
| - | res = curl_easy_perform(ch); |
| - | if(res == CURLE_OK) |
| - | printf("*** transfer succeeded ***\n"); |
| - | else |
| - | printf("*** transfer failed ***\n"); |
| - | |
| - | /* second try: retrieve page using user certificate and key -> succeeds |
| - | * load the certificate and key 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); |
| + | 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"); |
| + | |
| + | /* both VERIFYPEER and VERIFYHOST are set to 0 in this case because there |
| + | is no CA certificate */ |
| + | |
| + | curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); |
| + | curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); |
| + | curl_easy_setopt(ch, CURLOPT_URL, "https://www.example.com/"); |
| + | curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, "PEM"); |
| + | |
| + | /* first try: retrieve page without user certificate and key -> fails */ |
| + | res = curl_easy_perform(ch); |
| + | if(res == CURLE_OK) |
| + | printf("*** transfer succeeded ***\n"); |
| + | else |
| + | printf("*** transfer failed ***\n"); |
| + | |
| + | /* second try: retrieve page using user certificate and key -> succeeds |
| + | * load the certificate and key 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_global_cleanup(); | |
| return (int)res; | |
| } | |
| @@ -117,56 +117,60 @@ static size_t parseStreamCallback(void *contents, size_t length, size_t nmemb, | |
| int main(void) | |
| { | |
| CURL *curl_handle; | |
| - | CURLcode res; |
| - | XML_Parser parser; |
| - | struct ParserStruct state; |
| - | /* Initialize the state structure for parsing. */ |
| - | memset(&state, 0, sizeof(state)); |
| - | state.ok = 1; |
| - | |
| - | /* Initialize a namespace-aware parser. */ |
| - | parser = XML_ParserCreateNS(NULL, '\0'); |
| - | XML_SetUserData(parser, &state); |
| - | XML_SetElementHandler(parser, startElement, endElement); |
| - | XML_SetCharacterDataHandler(parser, characterDataHandler); |
| - | |
| - | res = curl_global_init(CURL_GLOBAL_ALL); |
| + | CURLcode res = curl_global_init(CURL_GLOBAL_ALL); |
| if(res) | |
| return (int)res; | |
| /* Initialize a libcurl handle. */ | |
| curl_handle = curl_easy_init(); | |
| - | curl_easy_setopt(curl_handle, CURLOPT_URL, |
| - | "https://www.w3schools.com/xml/simple.xml"); |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback); |
| - | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser); |
| - | |
| - | printf("Depth Characters Closing Tag\n"); |
| - | |
| - | /* Perform the request and any follow-up parsing. */ |
| - | res = curl_easy_perform(curl_handle); |
| - | if(res != CURLE_OK) { |
| - | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| - | curl_easy_strerror(res)); |
| - | } |
| - | else if(state.ok) { |
| - | /* Expat requires one final call to finalize parsing. */ |
| - | if(XML_Parse(parser, NULL, 0, 1) == 0) { |
| - | enum XML_Error error_code = XML_GetErrorCode(parser); |
| - | fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n", |
| - | error_code, XML_ErrorString(error_code)); |
| + | if(curl) { |
| + | XML_Parser parser; |
| + | struct ParserStruct state; |
| + | |
| + | /* Initialize the state structure for parsing. */ |
| + | memset(&state, 0, sizeof(state)); |
| + | state.ok = 1; |
| + | |
| + | /* Initialize a namespace-aware parser. */ |
| + | parser = XML_ParserCreateNS(NULL, '\0'); |
| + | XML_SetUserData(parser, &state); |
| + | XML_SetElementHandler(parser, startElement, endElement); |
| + | XML_SetCharacterDataHandler(parser, characterDataHandler); |
| + | |
| + | curl_easy_setopt(curl_handle, CURLOPT_URL, |
| + | "https://www.w3schools.com/xml/simple.xml"); |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, parseStreamCallback); |
| + | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)parser); |
| + | |
| + | printf("Depth Characters Closing Tag\n"); |
| + | |
| + | /* Perform the request and any follow-up parsing. */ |
| + | res = curl_easy_perform(curl_handle); |
| + | if(res != CURLE_OK) { |
| + | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
| + | curl_easy_strerror(res)); |
| } | |
| - | else { |
| - | printf(" --------------\n"); |
| - | printf(" %lu tags total\n", state.tags); |
| + | else if(state.ok) { |
| + | /* Expat requires one final call to finalize parsing. */ |
| + | if(XML_Parse(parser, NULL, 0, 1) == 0) { |
| + | enum XML_Error error_code = XML_GetErrorCode(parser); |
| + | fprintf(stderr, "Finalizing parsing failed with error code %d (%s).\n", |
| + | error_code, XML_ErrorString(error_code)); |
| + | } |
| + | else { |
| + | printf(" --------------\n"); |
| + | printf(" %lu tags total\n", state.tags); |
| + | } |
| } | |
| + | |
| + | /* Clean up. */ |
| + | free(state.characters.memory); |
| + | XML_ParserFree(parser); |
| + | |
| + | curl_easy_cleanup(curl_handle); |
| } | |
| - | /* Clean up. */ |
| - | free(state.characters.memory); |
| - | XML_ParserFree(parser); |
| - | curl_easy_cleanup(curl_handle); |
| curl_global_cleanup(); | |
| return (int)res; | |