lib1531.c (4357B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 #include "first.h" 25 26 #include "memdebug.h" 27 28 static CURLcode test_lib1531(char *URL) 29 { 30 static char const testData[] = ".abc\0xyz"; 31 static curl_off_t const testDataSize = sizeof(testData) - 1; 32 33 CURL *easy; 34 CURLM *multi_handle; 35 int still_running; /* keep number of running handles */ 36 CURLMsg *msg; /* for picking up messages with the transfer status */ 37 int msgs_left; /* how many messages are left */ 38 CURLcode res = CURLE_OK; 39 40 start_test_timing(); 41 42 global_init(CURL_GLOBAL_ALL); 43 44 /* Allocate one curl handle per transfer */ 45 easy = curl_easy_init(); 46 47 /* init a multi stack */ 48 multi_handle = curl_multi_init(); 49 50 /* add the individual transfer */ 51 curl_multi_add_handle(multi_handle, easy); 52 53 /* set the options (I left out a few, you'll get the point anyway) */ 54 curl_easy_setopt(easy, CURLOPT_URL, URL); 55 curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, testDataSize); 56 curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData); 57 58 /* we start some action by calling perform right away */ 59 curl_multi_perform(multi_handle, &still_running); 60 61 abort_on_test_timeout(); 62 63 do { 64 struct timeval timeout; 65 int rc; /* select() return code */ 66 CURLMcode mc; /* curl_multi_fdset() return code */ 67 68 fd_set fdread; 69 fd_set fdwrite; 70 fd_set fdexcep; 71 int maxfd = -1; 72 73 long curl_timeo = -1; 74 75 FD_ZERO(&fdread); 76 FD_ZERO(&fdwrite); 77 FD_ZERO(&fdexcep); 78 79 /* set a suitable timeout to play around with */ 80 timeout.tv_sec = 1; 81 timeout.tv_usec = 0; 82 83 curl_multi_timeout(multi_handle, &curl_timeo); 84 if(curl_timeo >= 0) { 85 curlx_mstotv(&timeout, curl_timeo); 86 if(timeout.tv_sec > 1) { 87 timeout.tv_sec = 1; 88 timeout.tv_usec = 0; 89 } 90 } 91 92 /* get file descriptors from the transfers */ 93 mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd); 94 95 if(mc != CURLM_OK) { 96 curl_mfprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc); 97 break; 98 } 99 100 /* On success the value of maxfd is guaranteed to be >= -1. We call 101 select(maxfd + 1, ...); specially in case of (maxfd == -1) there are 102 no fds ready yet so we call select(0, ...) --or Sleep() on Windows-- 103 to sleep 100ms, which is the minimum suggested value in the 104 curl_multi_fdset() doc. */ 105 106 if(maxfd == -1) { 107 rc = curlx_wait_ms(100); 108 } 109 else { 110 /* Note that on some platforms 'timeout' may be modified by select(). 111 If you need access to the original value save a copy beforehand. */ 112 rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout); 113 } 114 115 switch(rc) { 116 case -1: 117 /* select error */ 118 break; 119 case 0: /* timeout */ 120 default: /* action */ 121 curl_multi_perform(multi_handle, &still_running); 122 break; 123 } 124 125 abort_on_test_timeout(); 126 } while(still_running); 127 128 /* See how the transfers went */ 129 do { 130 msg = curl_multi_info_read(multi_handle, &msgs_left); 131 if(msg && msg->msg == CURLMSG_DONE) { 132 curl_mprintf("HTTP transfer completed with status %d\n", 133 msg->data.result); 134 break; 135 } 136 137 abort_on_test_timeout(); 138 } while(msg); 139 140 test_cleanup: 141 curl_multi_cleanup(multi_handle); 142 143 /* Free the curl handles */ 144 curl_easy_cleanup(easy); 145 curl_global_cleanup(); 146 147 return res; 148 }