/***************************************************************************1* _ _ ____ _2* Project ___| | | | _ \| |3* / __| | | | |_) | |4* | (__| |_| | _ <| |___5* \___|\___/|_| \_\_____|6*7* Copyright (C) Daniel Stenberg, <[email protected]>, et al.8*9* This software is licensed as described in the file COPYING, which10* you should have received as part of this distribution. The terms11* are also available at https://curl.se/docs/copyright.html.12*13* You may opt to use, copy, modify, merge, publish, distribute and/or sell14* copies of the Software, and permit persons to whom the Software is15* furnished to do so, under the terms of the COPYING file.16*17* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY18* KIND, either express or implied.19*20* SPDX-License-Identifier: curl21*22***************************************************************************/23#include "test.h"2425#include "testutil.h"26#include "timediff.h"27#include "warnless.h"28#include "memdebug.h"2930#define TEST_HANG_TIMEOUT 60 * 10003132static char const testData[] = ".abc\0xyz";33static curl_off_t const testDataSize = sizeof(testData) - 1;3435CURLcode test(char *URL)36{37CURL *easy;38CURLM *multi_handle;39int still_running; /* keep number of running handles */40CURLMsg *msg; /* for picking up messages with the transfer status */41int msgs_left; /* how many messages are left */42CURLcode res = CURLE_OK;4344start_test_timing();4546global_init(CURL_GLOBAL_ALL);4748/* Allocate one curl handle per transfer */49easy = curl_easy_init();5051/* init a multi stack */52multi_handle = curl_multi_init();5354/* add the individual transfer */55curl_multi_add_handle(multi_handle, easy);5657/* set the options (I left out a few, you'll get the point anyway) */58curl_easy_setopt(easy, CURLOPT_URL, URL);59curl_easy_setopt(easy, CURLOPT_POSTFIELDSIZE_LARGE, testDataSize);60curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);6162/* we start some action by calling perform right away */63curl_multi_perform(multi_handle, &still_running);6465abort_on_test_timeout();6667do {68struct timeval timeout;69int rc; /* select() return code */70CURLMcode mc; /* curl_multi_fdset() return code */7172fd_set fdread;73fd_set fdwrite;74fd_set fdexcep;75int maxfd = -1;7677long curl_timeo = -1;7879FD_ZERO(&fdread);80FD_ZERO(&fdwrite);81FD_ZERO(&fdexcep);8283/* set a suitable timeout to play around with */84timeout.tv_sec = 1;85timeout.tv_usec = 0;8687curl_multi_timeout(multi_handle, &curl_timeo);88if(curl_timeo >= 0) {89curlx_mstotv(&timeout, curl_timeo);90if(timeout.tv_sec > 1) {91timeout.tv_sec = 1;92timeout.tv_usec = 0;93}94}9596/* get file descriptors from the transfers */97mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);9899if(mc != CURLM_OK) {100curl_mfprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);101break;102}103104/* On success the value of maxfd is guaranteed to be >= -1. We call105select(maxfd + 1, ...); specially in case of (maxfd == -1) there are106no fds ready yet so we call select(0, ...) --or Sleep() on Windows--107to sleep 100ms, which is the minimum suggested value in the108curl_multi_fdset() doc. */109110if(maxfd == -1) {111#ifdef _WIN32112Sleep(100);113rc = 0;114#else115/* Portable sleep for platforms other than Windows. */116struct timeval wait = {0};117wait.tv_usec = 100 * 1000; /* 100ms */118rc = select(0, NULL, NULL, NULL, &wait);119#endif120}121else {122/* Note that on some platforms 'timeout' may be modified by select().123If you need access to the original value save a copy beforehand. */124rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);125}126127switch(rc) {128case -1:129/* select error */130break;131case 0: /* timeout */132default: /* action */133curl_multi_perform(multi_handle, &still_running);134break;135}136137abort_on_test_timeout();138} while(still_running);139140/* See how the transfers went */141do {142msg = curl_multi_info_read(multi_handle, &msgs_left);143if(msg && msg->msg == CURLMSG_DONE) {144curl_mprintf("HTTP transfer completed with status %d\n",145msg->data.result);146break;147}148149abort_on_test_timeout();150} while(msg);151152test_cleanup:153curl_multi_cleanup(multi_handle);154155/* Free the curl handles */156curl_easy_cleanup(easy);157curl_global_cleanup();158159return res;160}161162163