aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_upgrade.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_upgrade.c')
-rw-r--r--src/microhttpd/test_upgrade.c626
1 files changed, 320 insertions, 306 deletions
diff --git a/src/microhttpd/test_upgrade.c b/src/microhttpd/test_upgrade.c
index 9135187c..dbfa847d 100644
--- a/src/microhttpd/test_upgrade.c
+++ b/src/microhttpd/test_upgrade.c
@@ -82,7 +82,7 @@ enum tls_tool use_tls_tool;
82 */ 82 */
83static pid_t 83static pid_t
84gnutlscli_connect (int *sock, 84gnutlscli_connect (int *sock,
85 uint16_t port) 85 uint16_t port)
86{ 86{
87 pid_t chld; 87 pid_t chld;
88 int sp[2]; 88 int sp[2];
@@ -95,11 +95,11 @@ gnutlscli_connect (int *sock,
95 return -1; 95 return -1;
96 chld = fork (); 96 chld = fork ();
97 if (0 != chld) 97 if (0 != chld)
98 { 98 {
99 *sock = sp[1]; 99 *sock = sp[1];
100 MHD_socket_close_chk_ (sp[0]); 100 MHD_socket_close_chk_ (sp[0]);
101 return chld; 101 return chld;
102 } 102 }
103 MHD_socket_close_chk_ (sp[1]); 103 MHD_socket_close_chk_ (sp[1]);
104 (void) close (0); 104 (void) close (0);
105 (void) close (1); 105 (void) close (1);
@@ -109,34 +109,34 @@ gnutlscli_connect (int *sock,
109 abort (); 109 abort ();
110 MHD_socket_close_chk_ (sp[0]); 110 MHD_socket_close_chk_ (sp[0]);
111 if (TLS_CLI_GNUTLS == use_tls_tool) 111 if (TLS_CLI_GNUTLS == use_tls_tool)
112 { 112 {
113 snprintf (destination, 113 snprintf (destination,
114 sizeof(destination), 114 sizeof(destination),
115 "%u", 115 "%u",
116 (unsigned int) port); 116 (unsigned int) port);
117 execlp ("gnutls-cli", 117 execlp ("gnutls-cli",
118 "gnutls-cli", 118 "gnutls-cli",
119 "--insecure", 119 "--insecure",
120 "-p", 120 "-p",
121 destination, 121 destination,
122 "127.0.0.1", 122 "127.0.0.1",
123 (char *) NULL); 123 (char *) NULL);
124 } 124 }
125 else if (TLS_CLI_OPENSSL == use_tls_tool) 125 else if (TLS_CLI_OPENSSL == use_tls_tool)
126 { 126 {
127 snprintf (destination, 127 snprintf (destination,
128 sizeof(destination), 128 sizeof(destination),
129 "127.0.0.1:%u", 129 "127.0.0.1:%u",
130 (unsigned int) port); 130 (unsigned int) port);
131 execlp ("openssl", 131 execlp ("openssl",
132 "openssl", 132 "openssl",
133 "s_client", 133 "s_client",
134 "-connect", 134 "-connect",
135 destination, 135 destination,
136 "-verify", 136 "-verify",
137 "1", 137 "1",
138 (char *) NULL); 138 (char *) NULL);
139 } 139 }
140 _exit (1); 140 _exit (1);
141} 141}
142#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */ 142#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */
@@ -192,16 +192,16 @@ struct wr_socket
192 * @return created socket on success, NULL otherwise 192 * @return created socket on success, NULL otherwise
193 */ 193 */
194static struct wr_socket * 194static struct wr_socket *
195wr_create_plain_sckt(void) 195wr_create_plain_sckt (void)
196{ 196{
197 struct wr_socket *s = malloc(sizeof(struct wr_socket)); 197 struct wr_socket *s = malloc (sizeof(struct wr_socket));
198 if (NULL == s) 198 if (NULL == s)
199 return NULL; 199 return NULL;
200 s->t = wr_plain; 200 s->t = wr_plain;
201 s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 201 s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
202 if (MHD_INVALID_SOCKET != s->fd) 202 if (MHD_INVALID_SOCKET != s->fd)
203 return s; 203 return s;
204 free(s); 204 free (s);
205 return NULL; 205 return NULL;
206} 206}
207 207
@@ -211,40 +211,44 @@ wr_create_plain_sckt(void)
211 * @return created socket on success, NULL otherwise 211 * @return created socket on success, NULL otherwise
212 */ 212 */
213static struct wr_socket * 213static struct wr_socket *
214wr_create_tls_sckt(void) 214wr_create_tls_sckt (void)
215{ 215{
216#ifdef HTTPS_SUPPORT 216#ifdef HTTPS_SUPPORT
217 struct wr_socket *s = malloc(sizeof(struct wr_socket)); 217 struct wr_socket *s = malloc (sizeof(struct wr_socket));
218 if (NULL == s) 218 if (NULL == s)
219 return NULL; 219 return NULL;
220 s->t = wr_tls; 220 s->t = wr_tls;
221 s->tls_connected = 0; 221 s->tls_connected = 0;
222 s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); 222 s->fd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
223 if (MHD_INVALID_SOCKET != s->fd) 223 if (MHD_INVALID_SOCKET != s->fd)
224 {
225 if (GNUTLS_E_SUCCESS == gnutls_init (&(s->tls_s), GNUTLS_CLIENT))
224 { 226 {
225 if (GNUTLS_E_SUCCESS == gnutls_init (&(s->tls_s), GNUTLS_CLIENT)) 227 if (GNUTLS_E_SUCCESS == gnutls_set_default_priority (s->tls_s))
228 {
229 if (GNUTLS_E_SUCCESS == gnutls_certificate_allocate_credentials (
230 &(s->tls_crd)))
226 { 231 {
227 if (GNUTLS_E_SUCCESS == gnutls_set_default_priority (s->tls_s)) 232 if (GNUTLS_E_SUCCESS == gnutls_credentials_set (s->tls_s,
228 { 233 GNUTLS_CRD_CERTIFICATE,
229 if (GNUTLS_E_SUCCESS == gnutls_certificate_allocate_credentials (&(s->tls_crd))) 234 s->tls_crd))
230 { 235 {
231 if (GNUTLS_E_SUCCESS == gnutls_credentials_set (s->tls_s, GNUTLS_CRD_CERTIFICATE, s->tls_crd)) 236#if GNUTLS_VERSION_NUMBER + 0 >= 0x030109
232 { 237 gnutls_transport_set_int (s->tls_s, (int) (s->fd));
233#if GNUTLS_VERSION_NUMBER+0 >= 0x030109
234 gnutls_transport_set_int (s->tls_s, (int)(s->fd));
235#else /* GnuTLS before 3.1.9 */ 238#else /* GnuTLS before 3.1.9 */
236 gnutls_transport_set_ptr (s->tls_s, (gnutls_transport_ptr_t)(intptr_t)(s->fd)); 239 gnutls_transport_set_ptr (s->tls_s,
240 (gnutls_transport_ptr_t) (intptr_t) (s->fd));
237#endif /* GnuTLS before 3.1.9 */ 241#endif /* GnuTLS before 3.1.9 */
238 return s; 242 return s;
239 } 243 }
240 gnutls_certificate_free_credentials (s->tls_crd); 244 gnutls_certificate_free_credentials (s->tls_crd);
241 }
242 }
243 gnutls_deinit (s->tls_s);
244 } 245 }
245 (void)MHD_socket_close_ (s->fd); 246 }
247 gnutls_deinit (s->tls_s);
246 } 248 }
247 free(s); 249 (void) MHD_socket_close_ (s->fd);
250 }
251 free (s);
248#endif /* HTTPS_SUPPORT */ 252#endif /* HTTPS_SUPPORT */
249 return NULL; 253 return NULL;
250} 254}
@@ -257,9 +261,9 @@ wr_create_tls_sckt(void)
257 * @return created socket on success, NULL otherwise 261 * @return created socket on success, NULL otherwise
258 */ 262 */
259static struct wr_socket * 263static struct wr_socket *
260wr_create_from_plain_sckt(MHD_socket plain_sk) 264wr_create_from_plain_sckt (MHD_socket plain_sk)
261{ 265{
262 struct wr_socket *s = malloc(sizeof(struct wr_socket)); 266 struct wr_socket *s = malloc (sizeof(struct wr_socket));
263 267
264 if (NULL == s) 268 if (NULL == s)
265 return NULL; 269 return NULL;
@@ -277,9 +281,9 @@ wr_create_from_plain_sckt(MHD_socket plain_sk)
277 * @return zero on success, -1 otherwise. 281 * @return zero on success, -1 otherwise.
278 */ 282 */
279static int 283static int
280wr_connect(struct wr_socket *s, 284wr_connect (struct wr_socket *s,
281 const struct sockaddr *addr, 285 const struct sockaddr *addr,
282 int length) 286 int length)
283{ 287{
284 if (0 != connect (s->fd, addr, length)) 288 if (0 != connect (s->fd, addr, length))
285 return -1; 289 return -1;
@@ -287,15 +291,15 @@ wr_connect(struct wr_socket *s,
287 return 0; 291 return 0;
288#ifdef HTTPS_SUPPORT 292#ifdef HTTPS_SUPPORT
289 if (wr_tls == s->t) 293 if (wr_tls == s->t)
290 { 294 {
291 /* Do not try handshake here as 295 /* Do not try handshake here as
292 * it require processing on MHD side and 296 * it require processing on MHD side and
293 * when testing with "external" polling, 297 * when testing with "external" polling,
294 * test will call MHD processing only 298 * test will call MHD processing only
295 * after return from wr_connect(). */ 299 * after return from wr_connect(). */
296 s->tls_connected = 0; 300 s->tls_connected = 0;
297 return 0; 301 return 0;
298 } 302 }
299#endif /* HTTPS_SUPPORT */ 303#endif /* HTTPS_SUPPORT */
300 return -1; 304 return -1;
301} 305}
@@ -303,7 +307,7 @@ wr_connect(struct wr_socket *s,
303#ifdef HTTPS_SUPPORT 307#ifdef HTTPS_SUPPORT
304/* Only to be called from wr_send() and wr_recv() ! */ 308/* Only to be called from wr_send() and wr_recv() ! */
305static bool 309static bool
306wr_handshake(struct wr_socket *s) 310wr_handshake (struct wr_socket *s)
307{ 311{
308 int res = gnutls_handshake (s->tls_s); 312 int res = gnutls_handshake (s->tls_s);
309 if (GNUTLS_E_SUCCESS == res) 313 if (GNUTLS_E_SUCCESS == res)
@@ -332,22 +336,22 @@ wr_send (struct wr_socket *s,
332 size_t len) 336 size_t len)
333{ 337{
334 if (wr_plain == s->t) 338 if (wr_plain == s->t)
335 return MHD_send_(s->fd, buf, len); 339 return MHD_send_ (s->fd, buf, len);
336#ifdef HTTPS_SUPPORT 340#ifdef HTTPS_SUPPORT
337 if (wr_tls == s->t) 341 if (wr_tls == s->t)
338 { 342 {
339 ssize_t ret; 343 ssize_t ret;
340 if (!s->tls_connected && !wr_handshake (s)) 344 if (! s->tls_connected && ! wr_handshake (s))
341 return -1; 345 return -1;
342 346
343 ret = gnutls_record_send (s->tls_s, buf, len); 347 ret = gnutls_record_send (s->tls_s, buf, len);
344 if (ret > 0) 348 if (ret > 0)
345 return ret; 349 return ret;
346 if (GNUTLS_E_AGAIN == ret) 350 if (GNUTLS_E_AGAIN == ret)
347 MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); 351 MHD_socket_set_error_ (MHD_SCKT_EAGAIN_);
348 else 352 else
349 MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ 353 MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */
350 } 354 }
351#endif /* HTTPS_SUPPORT */ 355#endif /* HTTPS_SUPPORT */
352 return -1; 356 return -1;
353} 357}
@@ -371,19 +375,19 @@ wr_recv (struct wr_socket *s,
371 return MHD_recv_ (s->fd, buf, len); 375 return MHD_recv_ (s->fd, buf, len);
372#ifdef HTTPS_SUPPORT 376#ifdef HTTPS_SUPPORT
373 if (wr_tls == s->t) 377 if (wr_tls == s->t)
374 { 378 {
375 ssize_t ret; 379 ssize_t ret;
376 if (!s->tls_connected && !wr_handshake (s)) 380 if (! s->tls_connected && ! wr_handshake (s))
377 return -1; 381 return -1;
378 382
379 ret = gnutls_record_recv (s->tls_s, buf, len); 383 ret = gnutls_record_recv (s->tls_s, buf, len);
380 if (ret > 0) 384 if (ret > 0)
381 return ret; 385 return ret;
382 if (GNUTLS_E_AGAIN == ret) 386 if (GNUTLS_E_AGAIN == ret)
383 MHD_socket_set_error_ (MHD_SCKT_EAGAIN_); 387 MHD_socket_set_error_ (MHD_SCKT_EAGAIN_);
384 else 388 else
385 MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */ 389 MHD_socket_set_error_ (MHD_SCKT_ECONNABORTED_); /* hard error */
386 } 390 }
387#endif /* HTTPS_SUPPORT */ 391#endif /* HTTPS_SUPPORT */
388 return -1; 392 return -1;
389} 393}
@@ -397,13 +401,13 @@ wr_recv (struct wr_socket *s,
397static int 401static int
398wr_close (struct wr_socket *s) 402wr_close (struct wr_socket *s)
399{ 403{
400 int ret = (MHD_socket_close_(s->fd)) ? 0 : -1; 404 int ret = (MHD_socket_close_ (s->fd)) ? 0 : -1;
401#ifdef HTTPS_SUPPORT 405#ifdef HTTPS_SUPPORT
402 if (wr_tls == s->t) 406 if (wr_tls == s->t)
403 { 407 {
404 gnutls_deinit (s->tls_s); 408 gnutls_deinit (s->tls_s);
405 gnutls_certificate_free_credentials (s->tls_crd); 409 gnutls_certificate_free_credentials (s->tls_crd);
406 } 410 }
407#endif /* HTTPS_SUPPORT */ 411#endif /* HTTPS_SUPPORT */
408 free (s); 412 free (s);
409 return ret; 413 return ret;
@@ -447,7 +451,7 @@ notify_completed_cb (void *cls,
447 void **con_cls, 451 void **con_cls,
448 enum MHD_RequestTerminationCode toe) 452 enum MHD_RequestTerminationCode toe)
449{ 453{
450 pthread_t* ppth = *con_cls; 454 pthread_t*ppth = *con_cls;
451 455
452 (void) cls; 456 (void) cls;
453 (void) connection; /* Unused. Silent compiler warning. */ 457 (void) connection; /* Unused. Silent compiler warning. */
@@ -455,7 +459,7 @@ notify_completed_cb (void *cls,
455 (toe != MHD_REQUEST_TERMINATED_CLIENT_ABORT) && 459 (toe != MHD_REQUEST_TERMINATED_CLIENT_ABORT) &&
456 (toe != MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN) ) 460 (toe != MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN) )
457 abort (); 461 abort ();
458 if (! pthread_equal (**((pthread_t**)con_cls), 462 if (! pthread_equal (**((pthread_t**) con_cls),
459 pthread_self ())) 463 pthread_self ()))
460 abort (); 464 abort ();
461 if (NULL != ppth) 465 if (NULL != ppth)
@@ -486,7 +490,7 @@ log_cb (void *cls,
486 abort (); 490 abort ();
487 ppth = malloc (sizeof (pthread_t)); 491 ppth = malloc (sizeof (pthread_t));
488 if (NULL == ppth) 492 if (NULL == ppth)
489 abort(); 493 abort ();
490 *ppth = pthread_self (); 494 *ppth = pthread_self ();
491 return (void *) ppth; 495 return (void *) ppth;
492} 496}
@@ -578,20 +582,20 @@ send_all (struct wr_socket *sock,
578 582
579 make_blocking (wr_fd (sock)); 583 make_blocking (wr_fd (sock));
580 for (off = 0; off < len; off += ret) 584 for (off = 0; off < len; off += ret)
585 {
586 ret = wr_send (sock,
587 &text[off],
588 len - off);
589 if (0 > ret)
581 { 590 {
582 ret = wr_send (sock, 591 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
583 &text[off], 592 {
584 len - off); 593 ret = 0;
585 if (0 > ret) 594 continue;
586 { 595 }
587 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ())) 596 abort ();
588 {
589 ret = 0;
590 continue;
591 }
592 abort ();
593 }
594 } 597 }
598 }
595} 599}
596 600
597 601
@@ -611,36 +615,36 @@ recv_hdr (struct wr_socket *sock)
611 next = '\r'; 615 next = '\r';
612 i = 0; 616 i = 0;
613 while (i < 4) 617 while (i < 4)
618 {
619 ret = wr_recv (sock,
620 &c,
621 1);
622 if (0 > ret)
614 { 623 {
615 ret = wr_recv (sock, 624 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
616 &c,
617 1);
618 if (0 > ret)
619 {
620 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
621 continue;
622 abort ();
623 }
624 if (0 == ret)
625 continue; 625 continue;
626 if (c == next) 626 abort ();
627 {
628 i++;
629 if (next == '\r')
630 next = '\n';
631 else
632 next = '\r';
633 continue;
634 }
635 if (c == '\r')
636 {
637 i = 1;
638 next = '\n';
639 continue;
640 }
641 i = 0;
642 next = '\r';
643 } 627 }
628 if (0 == ret)
629 continue;
630 if (c == next)
631 {
632 i++;
633 if (next == '\r')
634 next = '\n';
635 else
636 next = '\r';
637 continue;
638 }
639 if (c == '\r')
640 {
641 i = 1;
642 next = '\n';
643 continue;
644 }
645 i = 0;
646 next = '\r';
647 }
644} 648}
645 649
646 650
@@ -655,22 +659,22 @@ recv_all (struct wr_socket *sock,
655 659
656 make_blocking (wr_fd (sock)); 660 make_blocking (wr_fd (sock));
657 for (off = 0; off < len; off += ret) 661 for (off = 0; off < len; off += ret)
662 {
663 ret = wr_recv (sock,
664 &buf[off],
665 len - off);
666 if (0 > ret)
658 { 667 {
659 ret = wr_recv (sock, 668 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ()))
660 &buf[off], 669 {
661 len - off); 670 ret = 0;
662 if (0 > ret) 671 continue;
663 { 672 }
664 if (MHD_SCKT_ERR_IS_EAGAIN_ (MHD_socket_get_error_ ())) 673 abort ();
665 {
666 ret = 0;
667 continue;
668 }
669 abort ();
670 }
671 } 674 }
675 }
672 if (0 != strncmp (text, buf, len)) 676 if (0 != strncmp (text, buf, len))
673 abort(); 677 abort ();
674} 678}
675 679
676 680
@@ -856,7 +860,7 @@ ahc_upgrade (void *cls,
856 860
857 if (NULL == *con_cls) 861 if (NULL == *con_cls)
858 abort (); 862 abort ();
859 if (! pthread_equal (**((pthread_t**)con_cls), pthread_self ())) 863 if (! pthread_equal (**((pthread_t**) con_cls), pthread_self ()))
860 abort (); 864 abort ();
861 resp = MHD_create_response_for_upgrade (&upgrade_cb, 865 resp = MHD_create_response_for_upgrade (&upgrade_cb,
862 NULL); 866 NULL);
@@ -887,37 +891,37 @@ run_mhd_select_loop (struct MHD_Daemon *daemon)
887 struct timeval tv; 891 struct timeval tv;
888 892
889 while (! done) 893 while (! done)
890 { 894 {
891 FD_ZERO (&rs); 895 FD_ZERO (&rs);
892 FD_ZERO (&ws); 896 FD_ZERO (&ws);
893 FD_ZERO (&es); 897 FD_ZERO (&es);
894 max_fd = -1; 898 max_fd = -1;
899 to = 1000;
900
901 if (MHD_YES !=
902 MHD_get_fdset (daemon,
903 &rs,
904 &ws,
905 &es,
906 &max_fd))
907 abort ();
908 (void) MHD_get_timeout (daemon,
909 &to);
910 if (1000 < to)
895 to = 1000; 911 to = 1000;
896 912 tv.tv_sec = to / 1000;
897 if (MHD_YES != 913 tv.tv_usec = 1000 * (to % 1000);
898 MHD_get_fdset (daemon, 914 if (0 > MHD_SYS_select_ (max_fd + 1,
915 &rs,
916 &ws,
917 &es,
918 &tv))
919 abort ();
920 MHD_run_from_select (daemon,
899 &rs, 921 &rs,
900 &ws, 922 &ws,
901 &es, 923 &es);
902 &max_fd)) 924 }
903 abort ();
904 (void) MHD_get_timeout (daemon,
905 &to);
906 if (1000 < to)
907 to = 1000;
908 tv.tv_sec = to / 1000;
909 tv.tv_usec = 1000 * (to % 1000);
910 if (0 > MHD_SYS_select_ (max_fd + 1,
911 &rs,
912 &ws,
913 &es,
914 &tv))
915 abort ();
916 MHD_run_from_select (daemon,
917 &rs,
918 &ws,
919 &es);
920 }
921} 925}
922 926
923#ifdef HAVE_POLL 927#ifdef HAVE_POLL
@@ -930,7 +934,7 @@ run_mhd_select_loop (struct MHD_Daemon *daemon)
930static void 934static void
931run_mhd_poll_loop (struct MHD_Daemon *daemon) 935run_mhd_poll_loop (struct MHD_Daemon *daemon)
932{ 936{
933 (void)daemon; /* Unused. Silent compiler warning. */ 937 (void) daemon; /* Unused. Silent compiler warning. */
934 abort (); /* currently not implementable with existing MHD API */ 938 abort (); /* currently not implementable with existing MHD API */
935} 939}
936#endif /* HAVE_POLL */ 940#endif /* HAVE_POLL */
@@ -956,28 +960,28 @@ run_mhd_epoll_loop (struct MHD_Daemon *daemon)
956 MHD_DAEMON_INFO_EPOLL_FD); 960 MHD_DAEMON_INFO_EPOLL_FD);
957 ep = di->listen_fd; 961 ep = di->listen_fd;
958 while (! done) 962 while (! done)
959 { 963 {
960 FD_ZERO (&rs); 964 FD_ZERO (&rs);
961 to = 1000; 965 to = 1000;
962 966
963 FD_SET (ep, &rs); 967 FD_SET (ep, &rs);
964 (void) MHD_get_timeout (daemon, 968 (void) MHD_get_timeout (daemon,
965 &to); 969 &to);
966 if (1000 < to) 970 if (1000 < to)
967 to = 1000; 971 to = 1000;
968 tv.tv_sec = to / 1000; 972 tv.tv_sec = to / 1000;
969 tv.tv_usec = 1000 * (to % 1000); 973 tv.tv_usec = 1000 * (to % 1000);
970 ret = select (ep + 1, 974 ret = select (ep + 1,
971 &rs, 975 &rs,
972 NULL, 976 NULL,
973 NULL, 977 NULL,
974 &tv); 978 &tv);
975 if ( (-1 == ret) && 979 if ( (-1 == ret) &&
976 (EAGAIN != errno) && 980 (EAGAIN != errno) &&
977 (EINTR != errno) ) 981 (EINTR != errno) )
978 abort (); 982 abort ();
979 MHD_run (daemon); 983 MHD_run (daemon);
980 } 984 }
981} 985}
982#endif /* EPOLL_SUPPORT */ 986#endif /* EPOLL_SUPPORT */
983 987
@@ -1030,25 +1034,32 @@ test_upgrade (int flags,
1030 1034
1031 if (! test_tls) 1035 if (! test_tls)
1032 d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE, 1036 d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE,
1033 MHD_is_feature_supported(MHD_FEATURE_AUTODETECT_BIND_PORT) ? 1037 MHD_is_feature_supported (
1034 0 : 1090, 1038 MHD_FEATURE_AUTODETECT_BIND_PORT) ?
1035 NULL, NULL, 1039 0 : 1090,
1036 &ahc_upgrade, NULL, 1040 NULL, NULL,
1037 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 1041 &ahc_upgrade, NULL,
1038 MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb, NULL, 1042 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
1039 MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb, NULL, 1043 MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb,
1040 MHD_OPTION_THREAD_POOL_SIZE, pool, 1044 NULL,
1041 MHD_OPTION_END); 1045 MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb,
1046 NULL,
1047 MHD_OPTION_THREAD_POOL_SIZE, pool,
1048 MHD_OPTION_END);
1042#ifdef HTTPS_SUPPORT 1049#ifdef HTTPS_SUPPORT
1043 else 1050 else
1044 d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE | MHD_USE_TLS, 1051 d = MHD_start_daemon (flags | MHD_USE_ERROR_LOG | MHD_ALLOW_UPGRADE
1045 MHD_is_feature_supported(MHD_FEATURE_AUTODETECT_BIND_PORT) ? 1052 | MHD_USE_TLS,
1046 0 : 1090, 1053 MHD_is_feature_supported (
1054 MHD_FEATURE_AUTODETECT_BIND_PORT) ?
1055 0 : 1090,
1047 NULL, NULL, 1056 NULL, NULL,
1048 &ahc_upgrade, NULL, 1057 &ahc_upgrade, NULL,
1049 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL, 1058 MHD_OPTION_URI_LOG_CALLBACK, &log_cb, NULL,
1050 MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb, NULL, 1059 MHD_OPTION_NOTIFY_COMPLETED, &notify_completed_cb,
1051 MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb, NULL, 1060 NULL,
1061 MHD_OPTION_NOTIFY_CONNECTION, &notify_connection_cb,
1062 NULL,
1052 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem, 1063 MHD_OPTION_HTTPS_MEM_KEY, srv_signed_key_pem,
1053 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem, 1064 MHD_OPTION_HTTPS_MEM_CERT, srv_signed_cert_pem,
1054 MHD_OPTION_THREAD_POOL_SIZE, pool, 1065 MHD_OPTION_THREAD_POOL_SIZE, pool,
@@ -1065,43 +1076,43 @@ test_upgrade (int flags,
1065 if ( (NULL == dinfo) || 1076 if ( (NULL == dinfo) ||
1066 (0 == dinfo->port) ) 1077 (0 == dinfo->port) )
1067 abort (); 1078 abort ();
1068 if (!test_tls || TLS_LIB_GNUTLS == use_tls_tool) 1079 if (! test_tls ||(TLS_LIB_GNUTLS == use_tls_tool))
1069 { 1080 {
1070 sock = test_tls ? wr_create_tls_sckt () : wr_create_plain_sckt (); 1081 sock = test_tls ? wr_create_tls_sckt () : wr_create_plain_sckt ();
1071 if (NULL == sock) 1082 if (NULL == sock)
1072 abort (); 1083 abort ();
1073 sa.sin_family = AF_INET; 1084 sa.sin_family = AF_INET;
1074 sa.sin_port = htons (dinfo->port); 1085 sa.sin_port = htons (dinfo->port);
1075 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 1086 sa.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1076 if (0 != wr_connect (sock, 1087 if (0 != wr_connect (sock,
1077 (struct sockaddr *) &sa, 1088 (struct sockaddr *) &sa,
1078 sizeof (sa))) 1089 sizeof (sa)))
1079 abort (); 1090 abort ();
1080 } 1091 }
1081 else 1092 else
1082 { 1093 {
1083#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID) 1094#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
1084 MHD_socket tls_fork_sock; 1095 MHD_socket tls_fork_sock;
1085 uint16_t port; 1096 uint16_t port;
1086 1097
1087 /* make address sanitizer happy */ 1098 /* make address sanitizer happy */
1088 memcpy (&port, 1099 memcpy (&port,
1089 dinfo /* ->port */, 1100 dinfo /* ->port */,
1090 sizeof (port)); 1101 sizeof (port));
1091 if (-1 == (pid = gnutlscli_connect (&tls_fork_sock, 1102 if (-1 == (pid = gnutlscli_connect (&tls_fork_sock,
1092 port))) 1103 port)))
1093 { 1104 {
1094 MHD_stop_daemon (d); 1105 MHD_stop_daemon (d);
1095 return 4; 1106 return 4;
1096 } 1107 }
1097 1108
1098 sock = wr_create_from_plain_sckt (tls_fork_sock); 1109 sock = wr_create_from_plain_sckt (tls_fork_sock);
1099 if (NULL == sock) 1110 if (NULL == sock)
1100 abort ();
1101#else /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */
1102 abort (); 1111 abort ();
1112#else /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */
1113 abort ();
1103#endif /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */ 1114#endif /* !HTTPS_SUPPORT || !HAVE_FORK || !HAVE_WAITPID */
1104 } 1115 }
1105 1116
1106 if (0 != pthread_create (&pt_client, 1117 if (0 != pthread_create (&pt_client,
1107 NULL, 1118 NULL,
@@ -1109,21 +1120,21 @@ test_upgrade (int flags,
1109 sock)) 1120 sock))
1110 abort (); 1121 abort ();
1111 if (0 == (flags & MHD_USE_INTERNAL_POLLING_THREAD) ) 1122 if (0 == (flags & MHD_USE_INTERNAL_POLLING_THREAD) )
1112 { 1123 {
1113 enum MHD_FLAG flags; 1124 enum MHD_FLAG flags;
1114 1125
1115 /* make address sanitizer happy */ 1126 /* make address sanitizer happy */
1116 memcpy (&flags, 1127 memcpy (&flags,
1117 real_flags /* ->flags */, 1128 real_flags /* ->flags */,
1118 sizeof (flags)); 1129 sizeof (flags));
1119 run_mhd_loop (d, flags); 1130 run_mhd_loop (d, flags);
1120 } 1131 }
1121 pthread_join (pt_client, 1132 pthread_join (pt_client,
1122 NULL); 1133 NULL);
1123 pthread_join (pt, 1134 pthread_join (pt,
1124 NULL); 1135 NULL);
1125#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID) 1136#if defined(HTTPS_SUPPORT) && defined(HAVE_FORK) && defined(HAVE_WAITPID)
1126 if (test_tls && TLS_LIB_GNUTLS != use_tls_tool) 1137 if (test_tls &&(TLS_LIB_GNUTLS != use_tls_tool))
1127 waitpid (pid, NULL, 0); 1138 waitpid (pid, NULL, 0);
1128#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */ 1139#endif /* HTTPS_SUPPORT && HAVE_FORK && HAVE_WAITPID */
1129 MHD_stop_daemon (d); 1140 MHD_stop_daemon (d);
@@ -1139,56 +1150,56 @@ main (int argc,
1139 int res; 1150 int res;
1140 1151
1141 use_tls_tool = TLS_CLI_NO_TOOL; 1152 use_tls_tool = TLS_CLI_NO_TOOL;
1142 test_tls = has_in_name(argv[0], "_tls"); 1153 test_tls = has_in_name (argv[0], "_tls");
1143 1154
1144 verbose = 1; 1155 verbose = 1;
1145 if (has_param(argc, argv, "-q") || 1156 if (has_param (argc, argv, "-q") ||
1146 has_param(argc, argv, "--quiet")) 1157 has_param (argc, argv, "--quiet"))
1147 verbose = 0; 1158 verbose = 0;
1148 1159
1149 if (test_tls) 1160 if (test_tls)
1150 { 1161 {
1151#ifdef HTTPS_SUPPORT 1162#ifdef HTTPS_SUPPORT
1152 if (has_param(argc, argv, "--use-gnutls-cli")) 1163 if (has_param (argc, argv, "--use-gnutls-cli"))
1153 use_tls_tool = TLS_CLI_GNUTLS; 1164 use_tls_tool = TLS_CLI_GNUTLS;
1154 else if (has_param(argc, argv, "--use-openssl")) 1165 else if (has_param (argc, argv, "--use-openssl"))
1155 use_tls_tool = TLS_CLI_OPENSSL; 1166 use_tls_tool = TLS_CLI_OPENSSL;
1156 else if (has_param(argc, argv, "--use-gnutls-lib")) 1167 else if (has_param (argc, argv, "--use-gnutls-lib"))
1157 use_tls_tool = TLS_LIB_GNUTLS; 1168 use_tls_tool = TLS_LIB_GNUTLS;
1158#if defined(HAVE_FORK) && defined(HAVE_WAITPID) 1169#if defined(HAVE_FORK) && defined(HAVE_WAITPID)
1159 else if (0 == system ("gnutls-cli --version 1> /dev/null 2> /dev/null")) 1170 else if (0 == system ("gnutls-cli --version 1> /dev/null 2> /dev/null"))
1160 use_tls_tool = TLS_CLI_GNUTLS; 1171 use_tls_tool = TLS_CLI_GNUTLS;
1161 else if (0 == system ("openssl version 1> /dev/null 2> /dev/null")) 1172 else if (0 == system ("openssl version 1> /dev/null 2> /dev/null"))
1162 use_tls_tool = TLS_CLI_OPENSSL; 1173 use_tls_tool = TLS_CLI_OPENSSL;
1163#endif /* HAVE_FORK && HAVE_WAITPID */ 1174#endif /* HAVE_FORK && HAVE_WAITPID */
1164 else 1175 else
1165 use_tls_tool = TLS_LIB_GNUTLS; /* Should be available as MHD use it. */ 1176 use_tls_tool = TLS_LIB_GNUTLS; /* Should be available as MHD use it. */
1166 if (verbose) 1177 if (verbose)
1167 { 1178 {
1168 switch (use_tls_tool) 1179 switch (use_tls_tool)
1169 { 1180 {
1170 case TLS_CLI_GNUTLS: 1181 case TLS_CLI_GNUTLS:
1171 printf ("GnuTLS-CLI will be used for testing.\n"); 1182 printf ("GnuTLS-CLI will be used for testing.\n");
1172 break; 1183 break;
1173 case TLS_CLI_OPENSSL: 1184 case TLS_CLI_OPENSSL:
1174 printf ("Command line version of OpenSSL will be used for testing.\n"); 1185 printf ("Command line version of OpenSSL will be used for testing.\n");
1175 break; 1186 break;
1176 case TLS_LIB_GNUTLS: 1187 case TLS_LIB_GNUTLS:
1177 printf ("GnuTLS library will be used for testing.\n"); 1188 printf ("GnuTLS library will be used for testing.\n");
1178 break; 1189 break;
1179 default: 1190 default:
1180 abort ();
1181 }
1182 }
1183 if ( (TLS_LIB_GNUTLS == use_tls_tool) &&
1184 (GNUTLS_E_SUCCESS != gnutls_global_init()) )
1185 abort (); 1191 abort ();
1192 }
1193 }
1194 if ( (TLS_LIB_GNUTLS == use_tls_tool) &&
1195 (GNUTLS_E_SUCCESS != gnutls_global_init ()) )
1196 abort ();
1186 1197
1187#else /* ! HTTPS_SUPPORT */ 1198#else /* ! HTTPS_SUPPORT */
1188 fprintf (stderr, "HTTPS support was disabled by configure.\n"); 1199 fprintf (stderr, "HTTPS support was disabled by configure.\n");
1189 return 77; 1200 return 77;
1190#endif /* ! HTTPS_SUPPORT */ 1201#endif /* ! HTTPS_SUPPORT */
1191 } 1202 }
1192 1203
1193 /* run tests */ 1204 /* run tests */
1194 if (verbose) 1205 if (verbose)
@@ -1229,7 +1240,8 @@ main (int argc,
1229#endif 1240#endif
1230 1241
1231 /* Test thread-per-connection */ 1242 /* Test thread-per-connection */
1232 res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_THREAD_PER_CONNECTION, 1243 res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD
1244 | MHD_USE_THREAD_PER_CONNECTION,
1233 0); 1245 0);
1234 error_count += res; 1246 error_count += res;
1235 if (res) 1247 if (res)
@@ -1239,7 +1251,8 @@ main (int argc,
1239 else if (verbose) 1251 else if (verbose)
1240 printf ("PASSED: Upgrade with thread per connection.\n"); 1252 printf ("PASSED: Upgrade with thread per connection.\n");
1241 1253
1242 res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_THREAD_PER_CONNECTION, 1254 res = test_upgrade (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD
1255 | MHD_USE_THREAD_PER_CONNECTION,
1243 0); 1256 0);
1244 error_count += res; 1257 error_count += res;
1245 if (res) 1258 if (res)
@@ -1249,7 +1262,8 @@ main (int argc,
1249 else if (verbose) 1262 else if (verbose)
1250 printf ("PASSED: Upgrade with thread per connection and 'auto'.\n"); 1263 printf ("PASSED: Upgrade with thread per connection and 'auto'.\n");
1251#ifdef HAVE_POLL 1264#ifdef HAVE_POLL
1252 res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD | MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL, 1265 res = test_upgrade (MHD_USE_INTERNAL_POLLING_THREAD
1266 | MHD_USE_THREAD_PER_CONNECTION | MHD_USE_POLL,
1253 0); 1267 0);
1254 error_count += res; 1268 error_count += res;
1255 if (res) 1269 if (res)
@@ -1341,7 +1355,7 @@ main (int argc,
1341 error_count); 1355 error_count);
1342#ifdef HTTPS_SUPPORT 1356#ifdef HTTPS_SUPPORT
1343 if (test_tls && (TLS_LIB_GNUTLS == use_tls_tool)) 1357 if (test_tls && (TLS_LIB_GNUTLS == use_tls_tool))
1344 gnutls_global_deinit(); 1358 gnutls_global_deinit ();
1345#endif /* HTTPS_SUPPORT */ 1359#endif /* HTTPS_SUPPORT */
1346 return error_count != 0; /* 0 == pass */ 1360 return error_count != 0; /* 0 == pass */
1347} 1361}