lib1531.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.haxx.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  ***************************************************************************/
22 #include "test.h"
23 
24 #include "testutil.h"
25 #include "warnless.h"
26 #include "memdebug.h"
27 
28 #define TEST_HANG_TIMEOUT 60 * 1000
29 
30 static char const testData[] = ".abc\0xyz";
31 static off_t const testDataSize = sizeof(testData) - 1;
32 
33 int test(char *URL)
34 {
35  CURL *easy;
37  int still_running; /* keep number of running handles */
38  CURLMsg *msg; /* for picking up messages with the transfer status */
39  int msgs_left; /* how many messages are left */
40  int res = CURLE_OK;
41 
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,
57  curl_easy_setopt(easy, CURLOPT_POSTFIELDS, testData);
58 
59  /* we start some action by calling perform right away */
60  curl_multi_perform(multi_handle, &still_running);
61 
62  do {
63  struct timeval timeout;
64  int rc; /* select() return code */
65  CURLMcode mc; /* curl_multi_fdset() return code */
66 
67  fd_set fdread;
68  fd_set fdwrite;
69  fd_set fdexcep;
70  int maxfd = -1;
71 
72  long curl_timeo = -1;
73 
74  FD_ZERO(&fdread);
75  FD_ZERO(&fdwrite);
76  FD_ZERO(&fdexcep);
77 
78  /* set a suitable timeout to play around with */
79  timeout.tv_sec = 1;
80  timeout.tv_usec = 0;
81 
82  curl_multi_timeout(multi_handle, &curl_timeo);
83  if(curl_timeo >= 0) {
84  timeout.tv_sec = curl_timeo / 1000;
85  if(timeout.tv_sec > 1)
86  timeout.tv_sec = 1;
87  else
88  timeout.tv_usec = (curl_timeo % 1000) * 1000;
89  }
90 
91  /* get file descriptors from the transfers */
92  mc = curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
93 
94  if(mc != CURLM_OK) {
95  fprintf(stderr, "curl_multi_fdset() failed, code %d.\n", mc);
96  break;
97  }
98 
99  /* On success the value of maxfd is guaranteed to be >= -1. We call
100  select(maxfd + 1, ...); specially in case of (maxfd == -1) there are
101  no fds ready yet so we call select(0, ...) --or Sleep() on Windows--
102  to sleep 100ms, which is the minimum suggested value in the
103  curl_multi_fdset() doc. */
104 
105  if(maxfd == -1) {
106 #ifdef _WIN32
107  Sleep(100);
108  rc = 0;
109 #else
110  /* Portable sleep for platforms other than Windows. */
111  struct timeval wait = { 0, 100 * 1000 }; /* 100ms */
112  rc = select(0, NULL, NULL, NULL, &wait);
113 #endif
114  }
115  else {
116  /* Note that on some platforms 'timeout' may be modified by select().
117  If you need access to the original value save a copy beforehand. */
118  rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
119  }
120 
121  switch(rc) {
122  case -1:
123  /* select error */
124  break;
125  case 0: /* timeout */
126  default: /* action */
127  curl_multi_perform(multi_handle, &still_running);
128  break;
129  }
130  } while(still_running);
131 
132  /* See how the transfers went */
133  do {
134  msg = curl_multi_info_read(multi_handle, &msgs_left);
135  if(msg && msg->msg == CURLMSG_DONE) {
136  printf("HTTP transfer completed with status %d\n", msg->data.result);
137  break;
138  }
139  } while(msg);
140 
141  curl_multi_cleanup(multi_handle);
142 
143  /* Free the CURL handles */
144  curl_easy_cleanup(easy);
146 
147  return 0;
148 }
149 
CURL_EXTERN CURLMcode curl_multi_fdset(CURLM *multi_handle, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *exc_fd_set, int *max_fd)
CURL_EXTERN CURLMcode curl_multi_add_handle(CURLM *multi_handle, CURL *curl_handle)
#define global_init(A)
Definition: test.h:431
CURL_EXTERN CURLMcode curl_multi_perform(CURLM *multi_handle, int *running_handles)
CURL_EXTERN CURLMcode curl_multi_timeout(CURLM *multi_handle, long *milliseconds)
Definition: multi.h:93
static int res
static off_t const testDataSize
Definition: lib1531.c:31
#define curl_easy_setopt(handle, option, value)
Definition: typecheck-gcc.h:41
CURL_EXTERN CURLM * curl_multi_init(void)
Definition: multi.c:355
static CURLM * multi_handle
Definition: fopen.c:91
UNITTEST_START int rc
Definition: unit1301.c:31
int test(char *URL)
Definition: lib1531.c:33
#define printf
Definition: curl_printf.h:40
CURL_EXTERN CURL * curl_easy_init(void)
Definition: easy.c:343
CURLMSG msg
Definition: multi.h:94
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
CURL_TYPEOF_CURL_OFF_T curl_off_t
Definition: system.h:420
Definition: curl.h:455
CURLMcode
Definition: multi.h:61
void CURLM
Definition: multi.h:58
void CURL
Definition: curl.h:102
CURL_EXTERN CURLMsg * curl_multi_info_read(CURLM *multi_handle, int *msgs_in_queue)
Definition: multi.h:64
union CURLMsg::@6 data
#define fprintf
Definition: curl_printf.h:41
static CURL * easy[MAX_EASY_HANDLES]
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
CURLcode result
Definition: multi.h:98
static char const testData[]
Definition: lib1531.c:30
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15