aboutsummaryrefslogtreecommitdiff
path: root/src/testcurl/test_large_put.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/testcurl/test_large_put.c')
-rw-r--r--src/testcurl/test_large_put.c514
1 files changed, 337 insertions, 177 deletions
diff --git a/src/testcurl/test_large_put.c b/src/testcurl/test_large_put.c
index 0a7040ba..b6666533 100644
--- a/src/testcurl/test_large_put.c
+++ b/src/testcurl/test_large_put.c
@@ -47,6 +47,111 @@
47#define MHD_CPU_COUNT 2 47#define MHD_CPU_COUNT 2
48#endif 48#endif
49 49
50
51#if defined(HAVE___FUNC__)
52#define externalErrorExit(ignore) \
53 _externalErrorExit_func(NULL, __func__, __LINE__)
54#define externalErrorExitDesc(errDesc) \
55 _externalErrorExit_func(errDesc, __func__, __LINE__)
56#define libcurlErrorExit(ignore) \
57 _libcurlErrorExit_func(NULL, __func__, __LINE__)
58#define libcurlErrorExitDesc(errDesc) \
59 _libcurlErrorExit_func(errDesc, __func__, __LINE__)
60#define mhdErrorExit(ignore) \
61 _mhdErrorExit_func(NULL, __func__, __LINE__)
62#define mhdErrorExitDesc(errDesc) \
63 _mhdErrorExit_func(errDesc, __func__, __LINE__)
64#elif defined(HAVE___FUNCTION__)
65#define externalErrorExit(ignore) \
66 _externalErrorExit_func(NULL, __FUNCTION__, __LINE__)
67#define externalErrorExitDesc(errDesc) \
68 _externalErrorExit_func(errDesc, __FUNCTION__, __LINE__)
69#define libcurlErrorExit(ignore) \
70 _libcurlErrorExit_func(NULL, __FUNCTION__, __LINE__)
71#define libcurlErrorExitDesc(errDesc) \
72 _libcurlErrorExit_func(errDesc, __FUNCTION__, __LINE__)
73#define mhdErrorExit(ignore) \
74 _mhdErrorExit_func(NULL, __FUNCTION__, __LINE__)
75#define mhdErrorExitDesc(errDesc) \
76 _mhdErrorExit_func(errDesc, __FUNCTION__, __LINE__)
77#else
78#define externalErrorExit(ignore) _externalErrorExit_func(NULL, NULL, __LINE__)
79#define externalErrorExitDesc(errDesc) \
80 _externalErrorExit_func(errDesc, NULL, __LINE__)
81#define libcurlErrorExit(ignore) _libcurlErrorExit_func(NULL, NULL, __LINE__)
82#define libcurlErrorExitDesc(errDesc) \
83 _libcurlErrorExit_func(errDesc, NULL, __LINE__)
84#define mhdErrorExit(ignore) _mhdErrorExit_func(NULL, NULL, __LINE__)
85#define mhdErrorExitDesc(errDesc) _mhdErrorExit_func(errDesc, NULL, __LINE__)
86#endif
87
88
89_MHD_NORETURN static void
90_externalErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
91{
92 if ((NULL != errDesc) && (0 != errDesc[0]))
93 fprintf (stderr, "%s", errDesc);
94 else
95 fprintf (stderr, "System or external library call failed");
96 if ((NULL != funcName) && (0 != funcName[0]))
97 fprintf (stderr, " in %s", funcName);
98 if (0 < lineNum)
99 fprintf (stderr, " at line %d", lineNum);
100
101 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
102 strerror (errno));
103#ifdef MHD_WINSOCK_SOCKETS
104 fprintf (stderr, "WSAGetLastError() value: %d\n", (int) WSAGetLastError ());
105#endif /* MHD_WINSOCK_SOCKETS */
106 fflush (stderr);
107 exit (99);
108}
109
110
111static char libcurl_errbuf[CURL_ERROR_SIZE] = "";
112
113_MHD_NORETURN static void
114_libcurlErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
115{
116 if ((NULL != errDesc) && (0 != errDesc[0]))
117 fprintf (stderr, "%s", errDesc);
118 else
119 fprintf (stderr, "CURL library call failed");
120 if ((NULL != funcName) && (0 != funcName[0]))
121 fprintf (stderr, " in %s", funcName);
122 if (0 < lineNum)
123 fprintf (stderr, " at line %d", lineNum);
124
125 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
126 strerror (errno));
127 if (0 != libcurl_errbuf[0])
128 fprintf (stderr, "Last libcurl error details: %s\n", libcurl_errbuf);
129
130 fflush (stderr);
131 exit (99);
132}
133
134
135_MHD_NORETURN static void
136_mhdErrorExit_func (const char *errDesc, const char *funcName, int lineNum)
137{
138 if ((NULL != errDesc) && (0 != errDesc[0]))
139 fprintf (stderr, "%s", errDesc);
140 else
141 fprintf (stderr, "MHD unexpected error");
142 if ((NULL != funcName) && (0 != funcName[0]))
143 fprintf (stderr, " in %s", funcName);
144 if (0 < lineNum)
145 fprintf (stderr, " at line %d", lineNum);
146
147 fprintf (stderr, ".\nLast errno value: %d (%s)\n", (int) errno,
148 strerror (errno));
149
150 fflush (stderr);
151 exit (8);
152}
153
154
50static int oneone; 155static int oneone;
51static int incr_read; /* Use incremental read */ 156static int incr_read; /* Use incremental read */
52static int verbose; /* Be verbose */ 157static int verbose; /* Be verbose */
@@ -74,7 +179,7 @@ alloc_init (size_t buf_size)
74 179
75 buf = malloc (buf_size); 180 buf = malloc (buf_size);
76 if (NULL == buf) 181 if (NULL == buf)
77 return NULL; 182 externalErrorExit ();
78 183
79 fill_ptr = buf; 184 fill_ptr = buf;
80 to_fill = buf_size; 185 to_fill = buf_size;
@@ -98,7 +203,7 @@ putBuffer (void *stream, size_t size, size_t nmemb, void *ptr)
98 wrt = size * nmemb; 203 wrt = size * nmemb;
99 /* Check for overflow. */ 204 /* Check for overflow. */
100 if (wrt / size != nmemb) 205 if (wrt / size != nmemb)
101 return 0; 206 libcurlErrorExitDesc ("Too large buffer size");
102 if (wrt > PUT_SIZE - (*pos)) 207 if (wrt > PUT_SIZE - (*pos))
103 wrt = PUT_SIZE - (*pos); 208 wrt = PUT_SIZE - (*pos);
104 memcpy (stream, &put_buffer[*pos], wrt); 209 memcpy (stream, &put_buffer[*pos], wrt);
@@ -113,7 +218,7 @@ copyBuffer (void *ptr, size_t size, size_t nmemb, void *ctx)
113 struct CBC *cbc = ctx; 218 struct CBC *cbc = ctx;
114 219
115 if (cbc->pos + size * nmemb > cbc->size) 220 if (cbc->pos + size * nmemb > cbc->size)
116 return 0; /* overflow */ 221 libcurlErrorExitDesc ("Too large buffer size");
117 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb); 222 memcpy (&cbc->buf[cbc->pos], ptr, size * nmemb);
118 cbc->pos += size * nmemb; 223 cbc->pos += size * nmemb;
119 return size * nmemb; 224 return size * nmemb;
@@ -133,17 +238,31 @@ ahc_echo (void *cls,
133 struct MHD_Response *response; 238 struct MHD_Response *response;
134 enum MHD_Result ret; 239 enum MHD_Result ret;
135 static size_t processed; 240 static size_t processed;
136 (void) version; /* Unused. Silent compiler warning. */ 241
242 if (NULL == cls)
243 mhdErrorExitDesc ("cls parameter is NULL");
244
245 if (0 != strcmp (version, oneone ?
246 MHD_HTTP_VERSION_1_1 : MHD_HTTP_VERSION_1_0))
247 mhdErrorExitDesc ("Unexpected HTTP version");
248
249 if (NULL == url)
250 mhdErrorExitDesc ("url parameter is NULL");
251
252 if (NULL == upload_data_size)
253 mhdErrorExitDesc ("'upload_data_size' pointer is NULL");
137 254
138 if (0 != strcmp ("PUT", method)) 255 if (0 != strcmp ("PUT", method))
139 return MHD_NO; /* unexpected method */ 256 mhdErrorExitDesc ("Unexpected request method"); /* unexpected method */
257
140 if ((*done) == 0) 258 if ((*done) == 0)
141 { 259 {
142 size_t *pproc; 260 size_t *pproc;
143 if (NULL == *pparam) 261 if (NULL == *pparam)
144 { 262 {
145 processed = 0; 263 processed = 0;
146 *pparam = &processed; /* Safe as long as only one parallel request served. */ 264 /* Safe as long as only one parallel request served. */
265 *pparam = &processed;
147 } 266 }
148 pproc = (size_t *) *pparam; 267 pproc = (size_t *) *pparam;
149 268
@@ -151,18 +270,14 @@ ahc_echo (void *cls,
151 return MHD_YES; /* No data to process. */ 270 return MHD_YES; /* No data to process. */
152 271
153 if (*pproc + *upload_data_size > PUT_SIZE) 272 if (*pproc + *upload_data_size > PUT_SIZE)
154 { 273 mhdErrorExitDesc ("Incoming data larger than expected");
155 fprintf (stderr, "Incoming data larger than expected.\n"); 274
156 return MHD_NO;
157 }
158 if ( (! incr_read) && (*upload_data_size != PUT_SIZE) ) 275 if ( (! incr_read) && (*upload_data_size != PUT_SIZE) )
159 return MHD_YES; /* Wait until whole request is received. */ 276 return MHD_YES; /* Wait until whole request is received. */
160 277
161 if (0 != memcmp (upload_data, put_buffer + (*pproc), *upload_data_size)) 278 if (0 != memcmp (upload_data, put_buffer + (*pproc), *upload_data_size))
162 { 279 mhdErrorExitDesc ("Incoming data does not match sent data");
163 fprintf (stderr, "Incoming data does not match sent data.\n"); 280
164 return MHD_NO;
165 }
166 *pproc += *upload_data_size; 281 *pproc += *upload_data_size;
167 *upload_data_size = 0; /* Current block of data is fully processed. */ 282 *upload_data_size = 0; /* Current block of data is fully processed. */
168 283
@@ -173,6 +288,8 @@ ahc_echo (void *cls,
173 response = MHD_create_response_from_buffer (strlen (url), 288 response = MHD_create_response_from_buffer (strlen (url),
174 (void *) url, 289 (void *) url,
175 MHD_RESPMEM_MUST_COPY); 290 MHD_RESPMEM_MUST_COPY);
291 if (NULL == response)
292 mhdErrorExitDesc ("Failed to create response");
176 ret = MHD_queue_response (connection, MHD_HTTP_OK, response); 293 ret = MHD_queue_response (connection, MHD_HTTP_OK, response);
177 MHD_destroy_response (response); 294 MHD_destroy_response (response);
178 return ret; 295 return ret;
@@ -213,37 +330,50 @@ testPutInternalThread (unsigned int add_flag)
213 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4 / 3)), 330 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4 / 3)),
214 MHD_OPTION_END); 331 MHD_OPTION_END);
215 if (d == NULL) 332 if (d == NULL)
216 return 1; 333 mhdErrorExit ();
217 if (0 == port) 334 if (0 == port)
218 { 335 {
219 const union MHD_DaemonInfo *dinfo; 336 const union MHD_DaemonInfo *dinfo;
220 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 337 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
221 if ((NULL == dinfo) || (0 == dinfo->port) ) 338 if ((NULL == dinfo) || (0 == dinfo->port) )
222 { 339 mhdErrorExit ();
223 MHD_stop_daemon (d); return 32;
224 }
225 port = (int) dinfo->port; 340 port = (int) dinfo->port;
226 } 341 }
342
227 c = curl_easy_init (); 343 c = curl_easy_init ();
228 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 344 if (NULL == c)
229 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 345 {
230 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 346 fprintf (stderr, "curl_easy_init() failed.\n");
231 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 347 externalErrorExit ();
232 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 348 }
233 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 349 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
234 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 350 (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
235 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); 351 "http://127.0.0.1/hello_world")) ||
236 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 352 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
237 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 353 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
238 if (oneone) 354 &copyBuffer)) ||
239 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 355 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
240 else 356 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
241 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 357 &putBuffer)) ||
242 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 358 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
243 /* NOTE: use of CONNECTTIMEOUT without also 359 (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
244 * setting NOSIGNAL results in really weird 360 (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
245 * crashes on my system! */ 361 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
246 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 362 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
363 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
364 (long) 150)) ||
365 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
366 (long) 150)) ||
367 ((oneone) ?
368 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
369 CURL_HTTP_VERSION_1_1)) :
370 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
371 CURL_HTTP_VERSION_1_0))))
372 {
373 fprintf (stderr, "curl_easy_setopt() failed.\n");
374 externalErrorExit ();
375 }
376
247 if (CURLE_OK != (errornum = curl_easy_perform (c))) 377 if (CURLE_OK != (errornum = curl_easy_perform (c)))
248 { 378 {
249 fprintf (stderr, 379 fprintf (stderr,
@@ -256,9 +386,17 @@ testPutInternalThread (unsigned int add_flag)
256 curl_easy_cleanup (c); 386 curl_easy_cleanup (c);
257 MHD_stop_daemon (d); 387 MHD_stop_daemon (d);
258 if (cbc.pos != strlen ("/hello_world")) 388 if (cbc.pos != strlen ("/hello_world"))
259 return 4; 389 {
390 fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
391 (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
392 (unsigned) strlen ("/hello_world"));
393 mhdErrorExitDesc ("Wrong returned data length");
394 }
260 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 395 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
261 return 8; 396 {
397 fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
398 mhdErrorExitDesc ("Wrong returned data length");
399 }
262 return 0; 400 return 0;
263} 401}
264 402
@@ -298,37 +436,50 @@ testPutThreadPerConn (unsigned int add_flag)
298 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)), 436 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
299 MHD_OPTION_END); 437 MHD_OPTION_END);
300 if (d == NULL) 438 if (d == NULL)
301 return 16; 439 mhdErrorExit ();
302 if (0 == port) 440 if (0 == port)
303 { 441 {
304 const union MHD_DaemonInfo *dinfo; 442 const union MHD_DaemonInfo *dinfo;
305 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 443 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
306 if ((NULL == dinfo) || (0 == dinfo->port) ) 444 if ((NULL == dinfo) || (0 == dinfo->port) )
307 { 445 mhdErrorExit ();
308 MHD_stop_daemon (d); return 32;
309 }
310 port = (int) dinfo->port; 446 port = (int) dinfo->port;
311 } 447 }
448
312 c = curl_easy_init (); 449 c = curl_easy_init ();
313 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 450 if (NULL == c)
314 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 451 {
315 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 452 fprintf (stderr, "curl_easy_init() failed.\n");
316 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 453 externalErrorExit ();
317 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 454 }
318 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 455 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
319 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 456 (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
320 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); 457 "http://127.0.0.1/hello_world")) ||
321 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 458 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
322 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 459 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
323 if (oneone) 460 &copyBuffer)) ||
324 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 461 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
325 else 462 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
326 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 463 &putBuffer)) ||
327 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 464 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
328 /* NOTE: use of CONNECTTIMEOUT without also 465 (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
329 * setting NOSIGNAL results in really weird 466 (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
330 * crashes on my system! */ 467 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
331 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 468 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
469 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
470 (long) 150)) ||
471 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
472 (long) 150)) ||
473 ((oneone) ?
474 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
475 CURL_HTTP_VERSION_1_1)) :
476 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
477 CURL_HTTP_VERSION_1_0))))
478 {
479 fprintf (stderr, "curl_easy_setopt() failed.\n");
480 externalErrorExit ();
481 }
482
332 if (CURLE_OK != (errornum = curl_easy_perform (c))) 483 if (CURLE_OK != (errornum = curl_easy_perform (c)))
333 { 484 {
334 fprintf (stderr, 485 fprintf (stderr,
@@ -342,11 +493,16 @@ testPutThreadPerConn (unsigned int add_flag)
342 MHD_stop_daemon (d); 493 MHD_stop_daemon (d);
343 if (cbc.pos != strlen ("/hello_world")) 494 if (cbc.pos != strlen ("/hello_world"))
344 { 495 {
345 fprintf (stderr, "Got invalid response `%.*s'\n", (int) cbc.pos, cbc.buf); 496 fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
346 return 64; 497 (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
498 (unsigned) strlen ("/hello_world"));
499 mhdErrorExitDesc ("Wrong returned data length");
347 } 500 }
348 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 501 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
349 return 128; 502 {
503 fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
504 mhdErrorExitDesc ("Wrong returned data length");
505 }
350 return 0; 506 return 0;
351} 507}
352 508
@@ -386,37 +542,49 @@ testPutThreadPool (unsigned int add_flag)
386 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)), 542 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
387 MHD_OPTION_END); 543 MHD_OPTION_END);
388 if (d == NULL) 544 if (d == NULL)
389 return 16; 545 mhdErrorExit ();
390 if (0 == port) 546 if (0 == port)
391 { 547 {
392 const union MHD_DaemonInfo *dinfo; 548 const union MHD_DaemonInfo *dinfo;
393 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 549 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
394 if ((NULL == dinfo) || (0 == dinfo->port) ) 550 if ((NULL == dinfo) || (0 == dinfo->port) )
395 { 551 mhdErrorExit ();
396 MHD_stop_daemon (d); return 32;
397 }
398 port = (int) dinfo->port; 552 port = (int) dinfo->port;
399 } 553 }
554
400 c = curl_easy_init (); 555 c = curl_easy_init ();
401 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world"); 556 if (NULL == c)
402 curl_easy_setopt (c, CURLOPT_PORT, (long) port); 557 {
403 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer); 558 fprintf (stderr, "curl_easy_init() failed.\n");
404 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); 559 externalErrorExit ();
405 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer); 560 }
406 curl_easy_setopt (c, CURLOPT_READDATA, &pos); 561 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
407 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L); 562 (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
408 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE); 563 "http://127.0.0.1/hello_world")) ||
409 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L); 564 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
410 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L); 565 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
411 if (oneone) 566 &copyBuffer)) ||
412 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); 567 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
413 else 568 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
414 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); 569 &putBuffer)) ||
415 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L); 570 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
416 /* NOTE: use of CONNECTTIMEOUT without also 571 (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
417 * setting NOSIGNAL results in really weird 572 (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
418 * crashes on my system! */ 573 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
419 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L); 574 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
575 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
576 (long) 150)) ||
577 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
578 (long) 150)) ||
579 ((oneone) ?
580 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
581 CURL_HTTP_VERSION_1_1)) :
582 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
583 CURL_HTTP_VERSION_1_0))))
584 {
585 fprintf (stderr, "curl_easy_setopt() failed.\n");
586 externalErrorExit ();
587 }
420 if (CURLE_OK != (errornum = curl_easy_perform (c))) 588 if (CURLE_OK != (errornum = curl_easy_perform (c)))
421 { 589 {
422 fprintf (stderr, 590 fprintf (stderr,
@@ -430,11 +598,16 @@ testPutThreadPool (unsigned int add_flag)
430 MHD_stop_daemon (d); 598 MHD_stop_daemon (d);
431 if (cbc.pos != strlen ("/hello_world")) 599 if (cbc.pos != strlen ("/hello_world"))
432 { 600 {
433 fprintf (stderr, "Got invalid response `%.*s'\n", (int) cbc.pos, cbc.buf); 601 fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
434 return 64; 602 (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
603 (unsigned) strlen ("/hello_world"));
604 mhdErrorExitDesc ("Wrong returned data length");
435 } 605 }
436 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 606 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
437 return 128; 607 {
608 fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
609 mhdErrorExitDesc ("Wrong returned data length");
610 }
438 return 0; 611 return 0;
439} 612}
440 613
@@ -450,12 +623,6 @@ testPutExternal (void)
450 fd_set rs; 623 fd_set rs;
451 fd_set ws; 624 fd_set ws;
452 fd_set es; 625 fd_set es;
453 MHD_socket maxsock;
454#ifdef MHD_WINSOCK_SOCKETS
455 int maxposixs; /* Max socket number unused on W32 */
456#else /* MHD_POSIX_SOCKETS */
457#define maxposixs maxsock
458#endif /* MHD_POSIX_SOCKETS */
459 int running; 626 int running;
460 struct CURLMsg *msg; 627 struct CURLMsg *msg;
461 time_t start; 628 time_t start;
@@ -487,105 +654,95 @@ testPutExternal (void)
487 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)), 654 (size_t) (incr_read ? 1024 : (PUT_SIZE * 4)),
488 MHD_OPTION_END); 655 MHD_OPTION_END);
489 if (d == NULL) 656 if (d == NULL)
490 return 256; 657 mhdErrorExit ();
491 if (0 == port) 658 if (0 == port)
492 { 659 {
493 const union MHD_DaemonInfo *dinfo; 660 const union MHD_DaemonInfo *dinfo;
494 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT); 661 dinfo = MHD_get_daemon_info (d, MHD_DAEMON_INFO_BIND_PORT);
495 if ((NULL == dinfo) || (0 == dinfo->port) ) 662 if ((NULL == dinfo) || (0 == dinfo->port) )
496 { 663 mhdErrorExit ();
497 MHD_stop_daemon (d); return 32;
498 }
499 port = (int) dinfo->port; 664 port = (int) dinfo->port;
500 } 665 }
501 c = curl_easy_init ();
502 curl_easy_setopt (c, CURLOPT_URL, "http://127.0.0.1/hello_world");
503 curl_easy_setopt (c, CURLOPT_PORT, (long) port);
504 curl_easy_setopt (c, CURLOPT_WRITEFUNCTION, &copyBuffer);
505 curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc);
506 curl_easy_setopt (c, CURLOPT_READFUNCTION, &putBuffer);
507 curl_easy_setopt (c, CURLOPT_READDATA, &pos);
508 curl_easy_setopt (c, CURLOPT_UPLOAD, 1L);
509 curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE);
510 curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L);
511 curl_easy_setopt (c, CURLOPT_TIMEOUT, 150L);
512 if (oneone)
513 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
514 else
515 curl_easy_setopt (c, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
516 curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT, 150L);
517 /* NOTE: use of CONNECTTIMEOUT without also
518 * setting NOSIGNAL results in really weird
519 * crashes on my system! */
520 curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L);
521 666
667 c = curl_easy_init ();
668 if (NULL == c)
669 {
670 fprintf (stderr, "curl_easy_init() failed.\n");
671 externalErrorExit ();
672 }
673 if ((CURLE_OK != curl_easy_setopt (c, CURLOPT_NOSIGNAL, 1L)) ||
674 (CURLE_OK != curl_easy_setopt (c, CURLOPT_URL,
675 "http://127.0.0.1/hello_world")) ||
676 (CURLE_OK != curl_easy_setopt (c, CURLOPT_PORT, (long) port)) ||
677 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEFUNCTION,
678 &copyBuffer)) ||
679 (CURLE_OK != curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc)) ||
680 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READFUNCTION,
681 &putBuffer)) ||
682 (CURLE_OK != curl_easy_setopt (c, CURLOPT_READDATA, &pos)) ||
683 (CURLE_OK != curl_easy_setopt (c, CURLOPT_UPLOAD, 1L)) ||
684 (CURLE_OK != curl_easy_setopt (c, CURLOPT_INFILESIZE, (long) PUT_SIZE)) ||
685 (CURLE_OK != curl_easy_setopt (c, CURLOPT_FAILONERROR, 1L)) ||
686 (CURLE_OK != curl_easy_setopt (c, CURLOPT_ERRORBUFFER, libcurl_errbuf)) ||
687 (CURLE_OK != curl_easy_setopt (c, CURLOPT_CONNECTTIMEOUT,
688 (long) 150)) ||
689 (CURLE_OK != curl_easy_setopt (c, CURLOPT_TIMEOUT,
690 (long) 150)) ||
691 ((oneone) ?
692 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
693 CURL_HTTP_VERSION_1_1)) :
694 (CURLE_OK != curl_easy_setopt (c, CURLOPT_HTTP_VERSION,
695 CURL_HTTP_VERSION_1_0))))
696 {
697 fprintf (stderr, "curl_easy_setopt() failed.\n");
698 externalErrorExit ();
699 }
522 700
523 multi = curl_multi_init (); 701 multi = curl_multi_init ();
524 if (multi == NULL) 702 if (multi == NULL)
525 { 703 libcurlErrorExit ();
526 curl_easy_cleanup (c);
527 MHD_stop_daemon (d);
528 return 512;
529 }
530 mret = curl_multi_add_handle (multi, c); 704 mret = curl_multi_add_handle (multi, c);
531 if (mret != CURLM_OK) 705 if (mret != CURLM_OK)
532 { 706 libcurlErrorExit ();
533 curl_multi_cleanup (multi); 707
534 curl_easy_cleanup (c);
535 MHD_stop_daemon (d);
536 return 1024;
537 }
538 start = time (NULL); 708 start = time (NULL);
539 while ((time (NULL) - start < 5) && (multi != NULL)) 709 while ((time (NULL) - start < 5) && (multi != NULL))
540 { 710 {
541 maxsock = MHD_INVALID_SOCKET; 711 MHD_socket maxMHDsock;
542 maxposixs = -1; 712 int maxcurlsock;
713 maxMHDsock = MHD_INVALID_SOCKET;
714 maxcurlsock = -1;
543 FD_ZERO (&rs); 715 FD_ZERO (&rs);
544 FD_ZERO (&ws); 716 FD_ZERO (&ws);
545 FD_ZERO (&es); 717 FD_ZERO (&es);
546 curl_multi_perform (multi, &running); 718 if (CURLM_OK != curl_multi_perform (multi, &running))
547 mret = curl_multi_fdset (multi, &rs, &ws, &es, &maxposixs); 719 libcurlErrorExitDesc ("curl_multi_perform() failed");
548 if (mret != CURLM_OK) 720 if (CURLM_OK != curl_multi_fdset (multi, &rs, &ws, &es, &maxcurlsock))
549 { 721 libcurlErrorExitDesc ("curl_multi_fdset() failed");
550 curl_multi_remove_handle (multi, c); 722 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxMHDsock))
551 curl_multi_cleanup (multi); 723 mhdErrorExit ();
552 curl_easy_cleanup (c); 724
553 MHD_stop_daemon (d);
554 return 2048;
555 }
556 if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &maxsock))
557 {
558 curl_multi_remove_handle (multi, c);
559 curl_multi_cleanup (multi);
560 curl_easy_cleanup (c);
561 MHD_stop_daemon (d);
562 return 4096;
563 }
564 tv.tv_sec = 0; 725 tv.tv_sec = 0;
565 tv.tv_usec = 1000; 726 tv.tv_usec = 1000;
566 if (-1 == select (maxposixs + 1, &rs, &ws, &es, &tv)) 727#ifndef MHD_WINSOCK_SOCKETS
728 if (maxMHDsock > maxcurlsock)
729 maxcurlsock = maxMHDsock;
730#endif /* MHD_WINSOCK_SOCKETS */
731 if (-1 == select (maxcurlsock + 1, &rs, &ws, &es, &tv))
567 { 732 {
568#ifdef MHD_POSIX_SOCKETS 733#ifdef MHD_POSIX_SOCKETS
569 if (EINTR != errno) 734 if (EINTR != errno)
570 { 735 externalErrorExitDesc ("Unexpected select() error");
571 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n",
572 (int) errno, __LINE__);
573 fflush (stderr);
574 exit (99);
575 }
576#else 736#else
577 if ((WSAEINVAL != WSAGetLastError ()) || 737 if ((WSAEINVAL != WSAGetLastError ()) ||
578 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) ) 738 (0 != rs.fd_count) || (0 != ws.fd_count) || (0 != es.fd_count) )
579 { 739 externalErrorExitDesc ("Unexpected select() error");
580 fprintf (stderr, "Unexpected select() error: %d. Line: %d\n", 740 Sleep (tv.tv_sec * 1000 + tv.tv_usec / 1000);
581 (int) WSAGetLastError (), __LINE__);
582 fflush (stderr);
583 exit (99);
584 }
585 Sleep (1);
586#endif 741#endif
587 } 742 }
588 curl_multi_perform (multi, &running); 743
744 if (CURLM_OK != curl_multi_perform (multi, &running))
745 libcurlErrorExitDesc ("curl_multi_perform() failed");
589 if (0 == running) 746 if (0 == running)
590 { 747 {
591 int pending; 748 int pending;
@@ -599,18 +756,16 @@ testPutExternal (void)
599 else 756 else
600 { 757 {
601 fprintf (stderr, 758 fprintf (stderr,
602 "%s failed at %s:%d: `%s'\n", 759 "curl_multi_perform() failed: '%s' ",
603 "curl_multi_perform", 760 curl_easy_strerror (msg->data.result));
604 __FILE__, 761 libcurlErrorExit ();
605 __LINE__, curl_easy_strerror (msg->data.result));
606 abort ();
607 } 762 }
608 } 763 }
609 } 764 }
610 if (! curl_fine) 765 if (! curl_fine)
611 { 766 {
612 fprintf (stderr, "libcurl haven't returned OK code\n"); 767 fprintf (stderr, "libcurl haven't returned OK code ");
613 abort (); 768 mhdErrorExit ();
614 } 769 }
615 curl_multi_remove_handle (multi, c); 770 curl_multi_remove_handle (multi, c);
616 curl_multi_cleanup (multi); 771 curl_multi_cleanup (multi);
@@ -629,11 +784,16 @@ testPutExternal (void)
629 MHD_stop_daemon (d); 784 MHD_stop_daemon (d);
630 if (cbc.pos != strlen ("/hello_world")) 785 if (cbc.pos != strlen ("/hello_world"))
631 { 786 {
632 fprintf (stderr, "Got invalid response `%.*s'\n", (int) cbc.pos, cbc.buf); 787 fprintf (stderr, "Got %u bytes ('%.*s'), expected %u bytes. ",
633 return 8192; 788 (unsigned) cbc.pos, (int) cbc.pos, cbc.buf,
789 (unsigned) strlen ("/hello_world"));
790 mhdErrorExitDesc ("Wrong returned data length");
634 } 791 }
635 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world"))) 792 if (0 != strncmp ("/hello_world", cbc.buf, strlen ("/hello_world")))
636 return 16384; 793 {
794 fprintf (stderr, "Got invalid response '%.*s'. ", (int) cbc.pos, cbc.buf);
795 mhdErrorExitDesc ("Wrong returned data length");
796 }
637 return 0; 797 return 0;
638} 798}
639 799