aboutsummaryrefslogtreecommitdiff
path: root/src/daemon
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-07-17 21:52:26 +0000
committerChristian Grothoff <christian@grothoff.org>2012-07-17 21:52:26 +0000
commita232740396820ffb48f240f87db4b39735ad2b52 (patch)
tree8285e2fb736bc55efcbaa317460075e1f9fa2af4 /src/daemon
parentea8a8fe4558a23f504363ff7390d4411de3873e5 (diff)
downloadlibmicrohttpd-a232740396820ffb48f240f87db4b39735ad2b52.tar.gz
libmicrohttpd-a232740396820ffb48f240f87db4b39735ad2b52.zip
code cleanup, minor bug fixes, allow lookup up trailing values with key of NULL
Diffstat (limited to 'src/daemon')
-rw-r--r--src/daemon/connection.c248
-rw-r--r--src/daemon/connection.h80
-rw-r--r--src/daemon/daemon.c560
-rw-r--r--src/daemon/reason_phrase.c4
-rw-r--r--src/daemon/response.c155
5 files changed, 567 insertions, 480 deletions
diff --git a/src/daemon/connection.c b/src/daemon/connection.c
index 13c465b5..abdf62ca 100644
--- a/src/daemon/connection.c
+++ b/src/daemon/connection.c
@@ -123,22 +123,18 @@ MHD_get_connection_values (struct MHD_Connection *connection,
123 int ret; 123 int ret;
124 struct MHD_HTTP_Header *pos; 124 struct MHD_HTTP_Header *pos;
125 125
126 if (connection == NULL) 126 if (NULL == connection)
127 return -1; 127 return -1;
128 ret = 0; 128 ret = 0;
129 pos = connection->headers_received; 129 for (pos = connection->headers_received; NULL != pos; pos = pos->next)
130 while (pos != NULL) 130 if (0 != (pos->kind & kind))
131 { 131 {
132 if (0 != (pos->kind & kind)) 132 ret++;
133 { 133 if ((NULL != iterator) &&
134 ret++; 134 (MHD_YES != iterator (iterator_cls,
135 if ((iterator != NULL) && 135 kind, pos->header, pos->value)))
136 (MHD_YES != iterator (iterator_cls, 136 return ret;
137 kind, pos->header, pos->value))) 137 }
138 return ret;
139 }
140 pos = pos->next;
141 }
142 return ret; 138 return ret;
143} 139}
144 140
@@ -181,7 +177,7 @@ MHD_set_connection_value (struct MHD_Connection *connection,
181 177
182 pos = MHD_pool_allocate (connection->pool, 178 pos = MHD_pool_allocate (connection->pool,
183 sizeof (struct MHD_HTTP_Header), MHD_NO); 179 sizeof (struct MHD_HTTP_Header), MHD_NO);
184 if (pos == NULL) 180 if (NULL == pos)
185 return MHD_NO; 181 return MHD_NO;
186 pos->header = (char *) key; 182 pos->header = (char *) key;
187 pos->value = (char *) value; 183 pos->value = (char *) value;
@@ -208,7 +204,7 @@ MHD_set_connection_value (struct MHD_Connection *connection,
208 * 204 *
209 * @param connection connection to get values from 205 * @param connection connection to get values from
210 * @param kind what kind of value are we looking for 206 * @param kind what kind of value are we looking for
211 * @param key the header to look for 207 * @param key the header to look for, NULL to lookup 'trailing' value without a key
212 * @return NULL if no such item was found 208 * @return NULL if no such item was found
213 */ 209 */
214const char * 210const char *
@@ -220,7 +216,11 @@ MHD_lookup_connection_value (struct MHD_Connection *connection,
220 if (NULL == connection) 216 if (NULL == connection)
221 return NULL; 217 return NULL;
222 for (pos = connection->headers_received; NULL != pos; pos = pos->next) 218 for (pos = connection->headers_received; NULL != pos; pos = pos->next)
223 if ((0 != (pos->kind & kind)) && (0 == strcasecmp (key, pos->header))) 219 if ((0 != (pos->kind & kind)) &&
220 ( (key == pos->header) ||
221 ( (NULL != pos->header) &&
222 (NULL != key) &&
223 (0 == strcasecmp (key, pos->header))) ))
224 return pos->value; 224 return pos->value;
225 return NULL; 225 return NULL;
226} 226}
@@ -240,28 +240,28 @@ int
240MHD_queue_response (struct MHD_Connection *connection, 240MHD_queue_response (struct MHD_Connection *connection,
241 unsigned int status_code, struct MHD_Response *response) 241 unsigned int status_code, struct MHD_Response *response)
242{ 242{
243 if ((connection == NULL) || 243 if ( (NULL == connection) ||
244 (response == NULL) || 244 (NULL == response) ||
245 (connection->response != NULL) || 245 (NULL != connection->response) ||
246 ((connection->state != MHD_CONNECTION_HEADERS_PROCESSED) && 246 ( (MHD_CONNECTION_HEADERS_PROCESSED != connection->state) &&
247 (connection->state != MHD_CONNECTION_FOOTERS_RECEIVED))) 247 (MHD_CONNECTION_FOOTERS_RECEIVED != connection->state) ) )
248 return MHD_NO; 248 return MHD_NO;
249 MHD_increment_response_rc (response); 249 MHD_increment_response_rc (response);
250 connection->response = response; 250 connection->response = response;
251 connection->responseCode = status_code; 251 connection->responseCode = status_code;
252 if ((connection->method != NULL) && 252 if ( (NULL != connection->method) &&
253 (0 == strcasecmp (connection->method, MHD_HTTP_METHOD_HEAD))) 253 (0 == strcasecmp (connection->method, MHD_HTTP_METHOD_HEAD)) )
254 { 254 {
255 /* if this is a "HEAD" request, pretend that we 255 /* if this is a "HEAD" request, pretend that we
256 have already sent the full message body */ 256 have already sent the full message body */
257 connection->response_write_position = response->total_size; 257 connection->response_write_position = response->total_size;
258 } 258 }
259 if (connection->state == MHD_CONNECTION_HEADERS_PROCESSED) 259 if (MHD_CONNECTION_HEADERS_PROCESSED == connection->state)
260 { 260 {
261 /* response was queued "early", 261 /* response was queued "early",
262 refuse to read body / footers or further 262 refuse to read body / footers or further
263 requests! */ 263 requests! */
264 SHUTDOWN (connection->socket_fd, SHUT_RD); 264 (void) SHUTDOWN (connection->socket_fd, SHUT_RD);
265 connection->read_closed = MHD_YES; 265 connection->read_closed = MHD_YES;
266 connection->state = MHD_CONNECTION_FOOTERS_RECEIVED; 266 connection->state = MHD_CONNECTION_FOOTERS_RECEIVED;
267 } 267 }
@@ -281,16 +281,16 @@ need_100_continue (struct MHD_Connection *connection)
281{ 281{
282 const char *expect; 282 const char *expect;
283 283
284 return ((connection->response == NULL) && 284 return ( (NULL == connection->response) &&
285 (connection->version != NULL) && 285 (NULL != connection->version) &&
286 (0 == strcasecmp (connection->version, 286 (0 == strcasecmp (connection->version,
287 MHD_HTTP_VERSION_1_1)) && 287 MHD_HTTP_VERSION_1_1)) &&
288 (NULL != (expect = MHD_lookup_connection_value (connection, 288 (NULL != (expect = MHD_lookup_connection_value (connection,
289 MHD_HEADER_KIND, 289 MHD_HEADER_KIND,
290 MHD_HTTP_HEADER_EXPECT))) 290 MHD_HTTP_HEADER_EXPECT))) &&
291 && (0 == strcasecmp (expect, "100-continue")) 291 (0 == strcasecmp (expect, "100-continue")) &&
292 && (connection->continue_message_write_offset < 292 (connection->continue_message_write_offset <
293 strlen (HTTP_100_CONTINUE))); 293 strlen (HTTP_100_CONTINUE)) );
294} 294}
295 295
296 296
@@ -358,6 +358,7 @@ connection_close_error (struct MHD_Connection *connection,
358 * this function may close the socket (and return 358 * this function may close the socket (and return
359 * MHD_NO). 359 * MHD_NO).
360 * 360 *
361 * @param connection the connection
361 * @return MHD_NO if readying the response failed 362 * @return MHD_NO if readying the response failed
362 */ 363 */
363static int 364static int
@@ -367,7 +368,7 @@ try_ready_normal_body (struct MHD_Connection *connection)
367 struct MHD_Response *response; 368 struct MHD_Response *response;
368 369
369 response = connection->response; 370 response = connection->response;
370 if (response->crc == NULL) 371 if (NULL == response->crc)
371 return MHD_YES; 372 return MHD_YES;
372 if ( (response->data_start <= 373 if ( (response->data_start <=
373 connection->response_write_position) && 374 connection->response_write_position) &&
@@ -375,7 +376,7 @@ try_ready_normal_body (struct MHD_Connection *connection)
375 connection->response_write_position) ) 376 connection->response_write_position) )
376 return MHD_YES; /* response already ready */ 377 return MHD_YES; /* response already ready */
377#if LINUX 378#if LINUX
378 if ( (response->fd != -1) && 379 if ( (-1 != response->fd) &&
379 (0 == (connection->daemon->options & MHD_USE_SSL)) ) 380 (0 == (connection->daemon->options & MHD_USE_SSL)) )
380 { 381 {
381 /* will use sendfile, no need to bother response crc */ 382 /* will use sendfile, no need to bother response crc */
@@ -389,7 +390,7 @@ try_ready_normal_body (struct MHD_Connection *connection)
389 MHD_MIN (response->data_buffer_size, 390 MHD_MIN (response->data_buffer_size,
390 response->total_size - 391 response->total_size -
391 connection->response_write_position)); 392 connection->response_write_position));
392 if ((ret == 0) && 393 if ((0 == ret) &&
393 (0 != (connection->daemon->options & MHD_USE_SELECT_INTERNALLY))) 394 (0 != (connection->daemon->options & MHD_USE_SELECT_INTERNALLY)))
394 mhd_panic (mhd_panic_cls, __FILE__, __LINE__, 395 mhd_panic (mhd_panic_cls, __FILE__, __LINE__,
395#if HAVE_MESSAGES 396#if HAVE_MESSAGES
@@ -398,8 +399,8 @@ try_ready_normal_body (struct MHD_Connection *connection)
398 NULL 399 NULL
399#endif 400#endif
400 ); 401 );
401 if ( (ret == MHD_CONTENT_READER_END_OF_STREAM) || 402 if ( (MHD_CONTENT_READER_END_OF_STREAM == ret) ||
402 (ret == MHD_CONTENT_READER_END_WITH_ERROR) ) 403 (MHD_CONTENT_READER_END_WITH_ERROR == ret) )
403 { 404 {
404 /* either error or http 1.0 transfer, close socket! */ 405 /* either error or http 1.0 transfer, close socket! */
405 response->total_size = connection->response_write_position; 406 response->total_size = connection->response_write_position;
@@ -411,7 +412,7 @@ try_ready_normal_body (struct MHD_Connection *connection)
411 } 412 }
412 response->data_start = connection->response_write_position; 413 response->data_start = connection->response_write_position;
413 response->data_size = ret; 414 response->data_size = ret;
414 if (ret == 0) 415 if (0 == ret)
415 { 416 {
416 connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY; 417 connection->state = MHD_CONNECTION_NORMAL_BODY_UNREADY;
417 return MHD_NO; 418 return MHD_NO;
@@ -426,6 +427,7 @@ try_ready_normal_body (struct MHD_Connection *connection)
426 * transmission is complete, this function may close the socket (and 427 * transmission is complete, this function may close the socket (and
427 * return MHD_NO). 428 * return MHD_NO).
428 * 429 *
430 * @param connection the connection
429 * @return MHD_NO if readying the response failed 431 * @return MHD_NO if readying the response failed
430 */ 432 */
431static int 433static int
@@ -439,7 +441,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
439 int cblen; 441 int cblen;
440 442
441 response = connection->response; 443 response = connection->response;
442 if (connection->write_buffer_size == 0) 444 if (0 == connection->write_buffer_size)
443 { 445 {
444 size = connection->daemon->pool_size; 446 size = connection->daemon->pool_size;
445 do 447 do
@@ -454,7 +456,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
454 } 456 }
455 buf = MHD_pool_allocate (connection->pool, size, MHD_NO); 457 buf = MHD_pool_allocate (connection->pool, size, MHD_NO);
456 } 458 }
457 while (buf == NULL); 459 while (NULL == buf);
458 connection->write_buffer_size = size; 460 connection->write_buffer_size = size;
459 connection->write_buffer = buf; 461 connection->write_buffer = buf;
460 } 462 }
@@ -480,7 +482,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
480 &connection->write_buffer[sizeof (cbuf)], 482 &connection->write_buffer[sizeof (cbuf)],
481 connection->write_buffer_size - sizeof (cbuf) - 2); 483 connection->write_buffer_size - sizeof (cbuf) - 2);
482 } 484 }
483 if (ret == MHD_CONTENT_READER_END_WITH_ERROR) 485 if (MHD_CONTENT_READER_END_WITH_ERROR == ret)
484 { 486 {
485 /* error, close socket! */ 487 /* error, close socket! */
486 response->total_size = connection->response_write_position; 488 response->total_size = connection->response_write_position;
@@ -488,7 +490,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
488 "Closing connection (error generating response)\n"); 490 "Closing connection (error generating response)\n");
489 return MHD_NO; 491 return MHD_NO;
490 } 492 }
491 if (ret == MHD_CONTENT_READER_END_OF_STREAM) 493 if (MHD_CONTENT_READER_END_OF_STREAM == ret)
492 { 494 {
493 /* end of message, signal other side! */ 495 /* end of message, signal other side! */
494 strcpy (connection->write_buffer, "0\r\n"); 496 strcpy (connection->write_buffer, "0\r\n");
@@ -497,7 +499,7 @@ try_ready_chunked_body (struct MHD_Connection *connection)
497 response->total_size = connection->response_write_position; 499 response->total_size = connection->response_write_position;
498 return MHD_YES; 500 return MHD_YES;
499 } 501 }
500 if (ret == 0) 502 if (0 == ret)
501 { 503 {
502 connection->state = MHD_CONNECTION_CHUNKED_BODY_UNREADY; 504 connection->state = MHD_CONNECTION_CHUNKED_BODY_UNREADY;
503 return MHD_NO; 505 return MHD_NO;
@@ -517,9 +519,12 @@ try_ready_chunked_body (struct MHD_Connection *connection)
517 return MHD_YES; 519 return MHD_YES;
518} 520}
519 521
522
520/** 523/**
521 * Check if we need to set some additional headers 524 * Check if we need to set some additional headers
522 * for http-compiliance. 525 * for http-compiliance.
526 *
527 * @param connection connection to check (and possibly modify)
523 */ 528 */
524static void 529static void
525add_extra_headers (struct MHD_Connection *connection) 530add_extra_headers (struct MHD_Connection *connection)
@@ -528,19 +533,19 @@ add_extra_headers (struct MHD_Connection *connection)
528 char buf[128]; 533 char buf[128];
529 534
530 connection->have_chunked_upload = MHD_NO; 535 connection->have_chunked_upload = MHD_NO;
531 if (connection->response->total_size == MHD_SIZE_UNKNOWN) 536 if (MHD_SIZE_UNKNOWN == connection->response->total_size)
532 { 537 {
533 have = MHD_get_response_header (connection->response, 538 have = MHD_get_response_header (connection->response,
534 MHD_HTTP_HEADER_CONNECTION); 539 MHD_HTTP_HEADER_CONNECTION);
535 if ((have == NULL) || (0 != strcasecmp (have, "close"))) 540 if ((NULL == have) || (0 != strcasecmp (have, "close")))
536 { 541 {
537 if ((connection->version != NULL) && 542 if ((NULL != connection->version) &&
538 (0 == strcasecmp (connection->version, MHD_HTTP_VERSION_1_1))) 543 (0 == strcasecmp (connection->version, MHD_HTTP_VERSION_1_1)))
539 { 544 {
540 connection->have_chunked_upload = MHD_YES; 545 connection->have_chunked_upload = MHD_YES;
541 have = MHD_get_response_header (connection->response, 546 have = MHD_get_response_header (connection->response,
542 MHD_HTTP_HEADER_TRANSFER_ENCODING); 547 MHD_HTTP_HEADER_TRANSFER_ENCODING);
543 if (have == NULL) 548 if (NULL == have)
544 MHD_add_response_header (connection->response, 549 MHD_add_response_header (connection->response,
545 MHD_HTTP_HEADER_TRANSFER_ENCODING, 550 MHD_HTTP_HEADER_TRANSFER_ENCODING,
546 "chunked"); 551 "chunked");
@@ -557,12 +562,13 @@ add_extra_headers (struct MHD_Connection *connection)
557 { 562 {
558 SPRINTF (buf, 563 SPRINTF (buf,
559 "%" MHD_LONG_LONG_PRINTF "u", 564 "%" MHD_LONG_LONG_PRINTF "u",
560 (unsigned MHD_LONG_LONG)connection->response->total_size); 565 (unsigned MHD_LONG_LONG) connection->response->total_size);
561 MHD_add_response_header (connection->response, 566 MHD_add_response_header (connection->response,
562 MHD_HTTP_HEADER_CONTENT_LENGTH, buf); 567 MHD_HTTP_HEADER_CONTENT_LENGTH, buf);
563 } 568 }
564} 569}
565 570
571
566/** 572/**
567 * Produce HTTP "Date:" header. 573 * Produce HTTP "Date:" header.
568 * 574 *
@@ -591,9 +597,11 @@ get_date_string (char *date)
591 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec); 597 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec);
592} 598}
593 599
600
594/** 601/**
595 * Try growing the read buffer 602 * Try growing the read buffer
596 * 603 *
604 * @param connection the connection
597 * @return MHD_YES on success, MHD_NO on failure 605 * @return MHD_YES on success, MHD_NO on failure
598 */ 606 */
599static int 607static int
@@ -606,7 +614,7 @@ try_grow_read_buffer (struct MHD_Connection *connection)
606 connection->read_buffer_size, 614 connection->read_buffer_size,
607 connection->read_buffer_size * 2 + 615 connection->read_buffer_size * 2 +
608 MHD_BUF_INC_SIZE + 1); 616 MHD_BUF_INC_SIZE + 1);
609 if (buf == NULL) 617 if (NULL == buf)
610 return MHD_NO; 618 return MHD_NO;
611 /* we can actually grow the buffer, do it! */ 619 /* we can actually grow the buffer, do it! */
612 connection->read_buffer = buf; 620 connection->read_buffer = buf;
@@ -620,6 +628,8 @@ try_grow_read_buffer (struct MHD_Connection *connection)
620 * Allocate the connection's write buffer and fill it with all of the 628 * Allocate the connection's write buffer and fill it with all of the
621 * headers (or footers, if we have already sent the body) from the 629 * headers (or footers, if we have already sent the body) from the
622 * HTTPd's response. 630 * HTTPd's response.
631 *
632 * @param connection the connection
623 */ 633 */
624static int 634static int
625build_header_response (struct MHD_Connection *connection) 635build_header_response (struct MHD_Connection *connection)
@@ -645,7 +655,7 @@ build_header_response (struct MHD_Connection *connection)
645 connection->write_buffer_size = 0; 655 connection->write_buffer_size = 0;
646 return MHD_YES; 656 return MHD_YES;
647 } 657 }
648 if (connection->state == MHD_CONNECTION_FOOTERS_RECEIVED) 658 if (MHD_CONNECTION_FOOTERS_RECEIVED == connection->state)
649 { 659 {
650 add_extra_headers (connection); 660 add_extra_headers (connection);
651 rc = connection->responseCode & (~MHD_ICY_FLAG); 661 rc = connection->responseCode & (~MHD_ICY_FLAG);
@@ -686,23 +696,19 @@ build_header_response (struct MHD_Connection *connection)
686 MHD_HTTP_HEADER_CONNECTION)) ); 696 MHD_HTTP_HEADER_CONNECTION)) );
687 if (must_add_close) 697 if (must_add_close)
688 size += strlen ("Connection: close\r\n"); 698 size += strlen ("Connection: close\r\n");
689 pos = connection->response->first_header; 699 for (pos = connection->response->first_header; NULL != pos; pos = pos->next)
690 while (pos != NULL) 700 if (pos->kind == kind)
691 { 701 size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */
692 if (pos->kind == kind)
693 size += strlen (pos->header) + strlen (pos->value) + 4; /* colon, space, linefeeds */
694 pos = pos->next;
695 }
696 /* produce data */ 702 /* produce data */
697 data = MHD_pool_allocate (connection->pool, size + 1, MHD_YES); 703 data = MHD_pool_allocate (connection->pool, size + 1, MHD_YES);
698 if (data == NULL) 704 if (NULL == data)
699 { 705 {
700#if HAVE_MESSAGES 706#if HAVE_MESSAGES
701 MHD_DLOG (connection->daemon, "Not enough memory for write!\n"); 707 MHD_DLOG (connection->daemon, "Not enough memory for write!\n");
702#endif 708#endif
703 return MHD_NO; 709 return MHD_NO;
704 } 710 }
705 if (connection->state == MHD_CONNECTION_FOOTERS_RECEIVED) 711 if (MHD_CONNECTION_FOOTERS_RECEIVED == connection->state)
706 { 712 {
707 memcpy (data, code, off); 713 memcpy (data, code, off);
708 } 714 }
@@ -716,13 +722,12 @@ build_header_response (struct MHD_Connection *connection)
716 strlen ("Connection: close\r\n")); 722 strlen ("Connection: close\r\n"));
717 off += strlen ("Connection: close\r\n"); 723 off += strlen ("Connection: close\r\n");
718 } 724 }
719 pos = connection->response->first_header; 725 for (pos = connection->response->first_header; NULL != pos; pos = pos->next)
720 while (pos != NULL) 726 if (pos->kind == kind)
721 { 727 off += SPRINTF (&data[off],
722 if (pos->kind == kind) 728 "%s: %s\r\n",
723 off += SPRINTF (&data[off], "%s: %s\r\n", pos->header, pos->value); 729 pos->header,
724 pos = pos->next; 730 pos->value);
725 }
726 if (connection->state == MHD_CONNECTION_FOOTERS_RECEIVED) 731 if (connection->state == MHD_CONNECTION_FOOTERS_RECEIVED)
727 { 732 {
728 strcpy (&data[off], date); 733 strcpy (&data[off], date);
@@ -746,15 +751,18 @@ build_header_response (struct MHD_Connection *connection)
746 * Handle it properly by stopping to read data 751 * Handle it properly by stopping to read data
747 * and sending the indicated response code and message. 752 * and sending the indicated response code and message.
748 * 753 *
754 * @param connection the connection
749 * @param status_code the response code to send (400, 413 or 414) 755 * @param status_code the response code to send (400, 413 or 414)
756 * @param message the error message to send
750 */ 757 */
751static void 758static void
752transmit_error_response (struct MHD_Connection *connection, 759transmit_error_response (struct MHD_Connection *connection,
753 unsigned int status_code, const char *message) 760 unsigned int status_code,
761 const char *message)
754{ 762{
755 struct MHD_Response *response; 763 struct MHD_Response *response;
756 764
757 if (connection->version == NULL) 765 if (NULL == connection->version)
758 { 766 {
759 /* we were unable to process the full header line, so we don't 767 /* we were unable to process the full header line, so we don't
760 really know what version the client speaks; assume 1.0 */ 768 really know what version the client speaks; assume 1.0 */
@@ -786,12 +794,19 @@ transmit_error_response (struct MHD_Connection *connection,
786 } 794 }
787} 795}
788 796
797
789/** 798/**
790 * Add "fd" to the "fd_set". If "fd" is 799 * Add "fd" to the "fd_set". If "fd" is
791 * greater than "*max", set "*max" to fd. 800 * greater than "*max", set "*max" to fd.
801 *
802 * @param fd file descriptor to add to the set
803 * @param set set to modify
804 * @param max_fd maximum value to potentially update
792 */ 805 */
793static void 806static void
794do_fd_set (int fd, fd_set * set, int *max_fd) 807add_to_fd_set (int fd,
808 fd_set *set,
809 int *max_fd)
795{ 810{
796 FD_SET (fd, set); 811 FD_SET (fd, set);
797 if ( (NULL != max_fd) && 812 if ( (NULL != max_fd) &&
@@ -799,27 +814,38 @@ do_fd_set (int fd, fd_set * set, int *max_fd)
799 *max_fd = fd; 814 *max_fd = fd;
800} 815}
801 816
817
802/** 818/**
803 * Obtain the select sets for this connection 819 * Obtain the select sets for this connection. The given
820 * sets (and the maximum) are updated and must have
821 * already been initialized.
804 * 822 *
823 * @param connection connetion to get select sets for
824 * @param read_fd_set read set to initialize
825 * @param write_fd_set write set to initialize
826 * @param except_fd_set except set to initialize (never changed)
827 * @param max_fd where to store largest FD put into any set
805 * @return MHD_YES on success 828 * @return MHD_YES on success
806 */ 829 */
807int 830int
808MHD_connection_get_fdset (struct MHD_Connection *connection, 831MHD_connection_get_fdset (struct MHD_Connection *connection,
809 fd_set * read_fd_set, 832 fd_set *read_fd_set,
810 fd_set * write_fd_set, 833 fd_set *write_fd_set,
811 fd_set * except_fd_set, int *max_fd) 834 fd_set *except_fd_set,
835 int *max_fd)
812{ 836{
813 int ret; 837 int ret;
814 struct MHD_Pollfd p; 838 struct MHD_Pollfd p;
815 839
816 memset(&p, 0, sizeof(struct MHD_Pollfd)); 840 /* we use the 'poll fd' as a convenient way to re-use code
817 ret = MHD_connection_get_pollfd(connection, &p); 841 when determining the select sets */
818 if ( (ret == MHD_YES) && (p.fd >= 0) ) { 842 memset (&p, 0, sizeof(struct MHD_Pollfd));
843 ret = MHD_connection_get_pollfd (connection, &p);
844 if ( (MHD_YES == ret) && (p.fd >= 0) ) {
819 if (0 != (p.events & MHD_POLL_ACTION_IN)) 845 if (0 != (p.events & MHD_POLL_ACTION_IN))
820 do_fd_set(p.fd, read_fd_set, max_fd); 846 add_to_fd_set(p.fd, read_fd_set, max_fd);
821 if (0 != (p.events & MHD_POLL_ACTION_OUT)) 847 if (0 != (p.events & MHD_POLL_ACTION_OUT))
822 do_fd_set(p.fd, write_fd_set, max_fd); 848 add_to_fd_set(p.fd, write_fd_set, max_fd);
823 } 849 }
824 return ret; 850 return ret;
825} 851}
@@ -828,17 +854,20 @@ MHD_connection_get_fdset (struct MHD_Connection *connection,
828/** 854/**
829 * Obtain the pollfd for this connection 855 * Obtain the pollfd for this connection
830 * 856 *
857 * @param connection connetion to get poll set for
858 * @param p where to store the polling information
831 * @return MHD_YES on success. If return MHD_YES and p->fd < 0, this 859 * @return MHD_YES on success. If return MHD_YES and p->fd < 0, this
832 * connection is not waiting for any read or write events 860 * connection is not waiting for any read or write events
833 */ 861 */
834int 862int
835MHD_connection_get_pollfd (struct MHD_Connection *connection, struct MHD_Pollfd *p) 863MHD_connection_get_pollfd (struct MHD_Connection *connection,
864 struct MHD_Pollfd *p)
836{ 865{
837 int fd; 866 int fd;
838 867
839 if (connection->pool == NULL) 868 if (NULL == connection->pool)
840 connection->pool = MHD_pool_create (connection->daemon->pool_size); 869 connection->pool = MHD_pool_create (connection->daemon->pool_size);
841 if (connection->pool == NULL) 870 if (NULL == connection->pool)
842 { 871 {
843 CONNECTION_CLOSE_ERROR (connection, 872 CONNECTION_CLOSE_ERROR (connection,
844 "Failed to create memory pool!\n"); 873 "Failed to create memory pool!\n");
@@ -846,7 +875,7 @@ MHD_connection_get_pollfd (struct MHD_Connection *connection, struct MHD_Pollfd
846 } 875 }
847 fd = connection->socket_fd; 876 fd = connection->socket_fd;
848 p->fd = fd; 877 p->fd = fd;
849 if (fd == -1) 878 if (-1 == fd)
850 return MHD_YES; 879 return MHD_YES;
851 while (1) 880 while (1)
852 { 881 {
@@ -870,7 +899,7 @@ MHD_connection_get_pollfd (struct MHD_Connection *connection, struct MHD_Pollfd
870 /* while reading headers, we always grow the 899 /* while reading headers, we always grow the
871 read buffer if needed, no size-check required */ 900 read buffer if needed, no size-check required */
872 if ((connection->read_closed) && 901 if ((connection->read_closed) &&
873 (connection->read_buffer_offset == 0)) 902 (0 == connection->read_buffer_offset))
874 { 903 {
875 CONNECTION_CLOSE_ERROR (connection, 904 CONNECTION_CLOSE_ERROR (connection,
876 "Connection buffer to small for request\n"); 905 "Connection buffer to small for request\n");
@@ -984,6 +1013,7 @@ MHD_connection_get_pollfd (struct MHD_Connection *connection, struct MHD_Pollfd
984 return MHD_YES; 1013 return MHD_YES;
985} 1014}
986 1015
1016
987/** 1017/**
988 * Parse a single line of the HTTP header. Advance 1018 * Parse a single line of the HTTP header. Advance
989 * read_buffer (!) appropriately. If the current line does not 1019 * read_buffer (!) appropriately. If the current line does not
@@ -998,12 +1028,12 @@ get_next_header_line (struct MHD_Connection *connection)
998 char *rbuf; 1028 char *rbuf;
999 size_t pos; 1029 size_t pos;
1000 1030
1001 if (connection->read_buffer_offset == 0) 1031 if (0 == connection->read_buffer_offset)
1002 return NULL; 1032 return NULL;
1003 pos = 0; 1033 pos = 0;
1004 rbuf = connection->read_buffer; 1034 rbuf = connection->read_buffer;
1005 while ((pos < connection->read_buffer_offset - 1) && 1035 while ((pos < connection->read_buffer_offset - 1) &&
1006 (rbuf[pos] != '\r') && (rbuf[pos] != '\n')) 1036 ('\r' != rbuf[pos]) && ('\n' != rbuf[pos]))
1007 pos++; 1037 pos++;
1008 if (pos == connection->read_buffer_offset - 1) 1038 if (pos == connection->read_buffer_offset - 1)
1009 { 1039 {
@@ -1015,10 +1045,10 @@ get_next_header_line (struct MHD_Connection *connection)
1015 connection->read_buffer_size, 1045 connection->read_buffer_size,
1016 connection->read_buffer_size * 2 + 1046 connection->read_buffer_size * 2 +
1017 MHD_BUF_INC_SIZE); 1047 MHD_BUF_INC_SIZE);
1018 if (rbuf == NULL) 1048 if (NULL == rbuf)
1019 { 1049 {
1020 transmit_error_response (connection, 1050 transmit_error_response (connection,
1021 (connection->url != NULL) 1051 (NULL != connection->url)
1022 ? MHD_HTTP_REQUEST_ENTITY_TOO_LARGE 1052 ? MHD_HTTP_REQUEST_ENTITY_TOO_LARGE
1023 : MHD_HTTP_REQUEST_URI_TOO_LONG, 1053 : MHD_HTTP_REQUEST_URI_TOO_LONG,
1024 REQUEST_TOO_BIG); 1054 REQUEST_TOO_BIG);
@@ -1033,7 +1063,7 @@ get_next_header_line (struct MHD_Connection *connection)
1033 return NULL; 1063 return NULL;
1034 } 1064 }
1035 /* found, check if we have proper CRLF */ 1065 /* found, check if we have proper CRLF */
1036 if ((rbuf[pos] == '\r') && (rbuf[pos + 1] == '\n')) 1066 if (('\r' == rbuf[pos]) && ('\n' == rbuf[pos + 1]))
1037 rbuf[pos++] = '\0'; /* skip both r and n */ 1067 rbuf[pos++] = '\0'; /* skip both r and n */
1038 rbuf[pos++] = '\0'; 1068 rbuf[pos++] = '\0';
1039 connection->read_buffer += pos; 1069 connection->read_buffer += pos;
@@ -1085,15 +1115,15 @@ connection_add_header (struct MHD_Connection *connection,
1085 */ 1115 */
1086static int 1116static int
1087parse_arguments (enum MHD_ValueKind kind, 1117parse_arguments (enum MHD_ValueKind kind,
1088 struct MHD_Connection *connection, char *args) 1118 struct MHD_Connection *connection,
1119 char *args)
1089{ 1120{
1090 char *equals; 1121 char *equals;
1091 char *amper; 1122 char *amper;
1092 1123
1093 while (args != NULL) 1124 while (NULL != args)
1094 { 1125 {
1095 equals = strstr (args, "="); 1126 if (NULL == (equals = strstr (args, "=")))
1096 if (equals == NULL)
1097 { 1127 {
1098 /* add with 'value' NULL */ 1128 /* add with 'value' NULL */
1099 connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls, 1129 connection->daemon->unescape_callback (connection->daemon->unescape_callback_cls,
@@ -1220,6 +1250,7 @@ parse_cookie_header (struct MHD_Connection *connection)
1220 return MHD_YES; 1250 return MHD_YES;
1221} 1251}
1222 1252
1253
1223/** 1254/**
1224 * Parse the first line of the HTTP HEADER. 1255 * Parse the first line of the HTTP HEADER.
1225 * 1256 *
@@ -1234,8 +1265,7 @@ parse_initial_message_line (struct MHD_Connection *connection, char *line)
1234 char *httpVersion; 1265 char *httpVersion;
1235 char *args; 1266 char *args;
1236 1267
1237 uri = strstr (line, " "); 1268 if (NULL == (uri = strstr (line, " ")))
1238 if (uri == NULL)
1239 return MHD_NO; /* serious error */ 1269 return MHD_NO; /* serious error */
1240 uri[0] = '\0'; 1270 uri[0] = '\0';
1241 connection->method = line; 1271 connection->method = line;
@@ -1282,7 +1312,7 @@ call_connection_handler (struct MHD_Connection *connection)
1282{ 1312{
1283 size_t processed; 1313 size_t processed;
1284 1314
1285 if (connection->response != NULL) 1315 if (NULL != connection->response)
1286 return; /* already queued a response */ 1316 return; /* already queued a response */
1287 processed = 0; 1317 processed = 0;
1288 connection->client_aware = MHD_YES; 1318 connection->client_aware = MHD_YES;
@@ -1321,7 +1351,7 @@ process_request_body (struct MHD_Connection *connection)
1321 char *buffer_head; 1351 char *buffer_head;
1322 char *end; 1352 char *end;
1323 1353
1324 if (connection->response != NULL) 1354 if (NULL != connection->response)
1325 return; /* already queued a response */ 1355 return; /* already queued a response */
1326 1356
1327 buffer_head = connection->read_buffer; 1357 buffer_head = connection->read_buffer;
@@ -1473,7 +1503,7 @@ process_request_body (struct MHD_Connection *connection)
1473 if (connection->remaining_upload_size != MHD_SIZE_UNKNOWN) 1503 if (connection->remaining_upload_size != MHD_SIZE_UNKNOWN)
1474 connection->remaining_upload_size -= used; 1504 connection->remaining_upload_size -= used;
1475 } 1505 }
1476 while (instant_retry == MHD_YES); 1506 while (MHD_YES == instant_retry);
1477 if (available > 0) 1507 if (available > 0)
1478 memmove (connection->read_buffer, buffer_head, available); 1508 memmove (connection->read_buffer, buffer_head, available);
1479 connection->read_buffer_offset = available; 1509 connection->read_buffer_offset = available;
@@ -1502,7 +1532,7 @@ do_read (struct MHD_Connection *connection)
1502 connection->read_buffer_offset); 1532 connection->read_buffer_offset);
1503 if (bytes_read < 0) 1533 if (bytes_read < 0)
1504 { 1534 {
1505 if ((errno == EINTR) || (errno == EAGAIN)) 1535 if ((EINTR == errno) || (EAGAIN == errno))
1506 return MHD_NO; 1536 return MHD_NO;
1507#if HAVE_MESSAGES 1537#if HAVE_MESSAGES
1508#if HTTPS_SUPPORT 1538#if HTTPS_SUPPORT
@@ -1518,7 +1548,7 @@ do_read (struct MHD_Connection *connection)
1518 CONNECTION_CLOSE_ERROR (connection, NULL); 1548 CONNECTION_CLOSE_ERROR (connection, NULL);
1519 return MHD_YES; 1549 return MHD_YES;
1520 } 1550 }
1521 if (bytes_read == 0) 1551 if (0 == bytes_read)
1522 { 1552 {
1523 /* other side closed connection */ 1553 /* other side closed connection */
1524 connection->read_closed = MHD_YES; 1554 connection->read_closed = MHD_YES;
@@ -1549,7 +1579,7 @@ do_write (struct MHD_Connection *connection)
1549 1579
1550 if (ret < 0) 1580 if (ret < 0)
1551 { 1581 {
1552 if ((errno == EINTR) || (errno == EAGAIN)) 1582 if ((EINTR == errno) || (EAGAIN == errno))
1553 return MHD_NO; 1583 return MHD_NO;
1554#if HAVE_MESSAGES 1584#if HAVE_MESSAGES
1555#if HTTPS_SUPPORT 1585#if HTTPS_SUPPORT
@@ -1774,12 +1804,14 @@ parse_connection_headers (struct MHD_Connection *connection)
1774 } 1804 }
1775} 1805}
1776 1806
1807
1777/** 1808/**
1778 * This function handles a particular connection when it has been 1809 * This function handles a particular connection when it has been
1779 * determined that there is data to be read off a socket. All 1810 * determined that there is data to be read off a socket. All
1780 * implementations (multithreaded, external select, internal select) 1811 * implementations (multithreaded, external select, internal select)
1781 * call this function to handle reads. 1812 * call this function to handle reads.
1782 * 1813 *
1814 * @param connection connection to handle
1783 * @return always MHD_YES (we should continue to process the 1815 * @return always MHD_YES (we should continue to process the
1784 * connection) 1816 * connection)
1785 */ 1817 */
@@ -1843,6 +1875,7 @@ MHD_connection_handle_read (struct MHD_Connection *connection)
1843 * implementations (multithreaded, external select, internal select) 1875 * implementations (multithreaded, external select, internal select)
1844 * call this function 1876 * call this function
1845 * 1877 *
1878 * @param connection connection to handle
1846 * @return always MHD_YES (we should continue to process the 1879 * @return always MHD_YES (we should continue to process the
1847 * connection) 1880 * connection)
1848 */ 1881 */
@@ -1993,12 +2026,14 @@ MHD_connection_handle_write (struct MHD_Connection *connection)
1993 return MHD_YES; 2026 return MHD_YES;
1994} 2027}
1995 2028
2029
1996/** 2030/**
1997 * This function was created to handle per-connection processing that 2031 * This function was created to handle per-connection processing that
1998 * has to happen even if the socket cannot be read or written to. All 2032 * has to happen even if the socket cannot be read or written to. All
1999 * implementations (multithreaded, external select, internal select) 2033 * implementations (multithreaded, external select, internal select)
2000 * call this function. 2034 * call this function.
2001 * 2035 *
2036 * @param connection connection to handle
2002 * @return MHD_YES if we should continue to process the 2037 * @return MHD_YES if we should continue to process the
2003 * connection (not dead yet), MHD_NO if it died 2038 * connection (not dead yet), MHD_NO if it died
2004 */ 2039 */
@@ -2381,6 +2416,11 @@ MHD_connection_handle_idle (struct MHD_Connection *connection)
2381} 2416}
2382 2417
2383 2418
2419/**
2420 * Set callbacks for this connection to those for HTTP.
2421 *
2422 * @param connection connection to initialize
2423 */
2384void 2424void
2385MHD_set_http_callbacks_ (struct MHD_Connection *connection) 2425MHD_set_http_callbacks_ (struct MHD_Connection *connection)
2386{ 2426{
diff --git a/src/daemon/connection.h b/src/daemon/connection.h
index 28b18e4a..e639691f 100644
--- a/src/daemon/connection.h
+++ b/src/daemon/connection.h
@@ -29,9 +29,17 @@
29 29
30#include "internal.h" 30#include "internal.h"
31 31
32
32/** 33/**
33 * Obtain the select sets for this connection. 34 * Obtain the select sets for this connection. The given
35 * sets (and the maximum) are updated and must have
36 * already been initialized.
34 * 37 *
38 * @param connection connetion to get select sets for
39 * @param read_fd_set read set to initialize
40 * @param write_fd_set write set to initialize
41 * @param except_fd_set except set to initialize (never changed)
42 * @param max_fd where to store largest FD put into any set
35 * @return MHD_YES on success 43 * @return MHD_YES on success
36 */ 44 */
37int 45int
@@ -40,27 +48,81 @@ MHD_connection_get_fdset (struct MHD_Connection *connection,
40 fd_set * write_fd_set, 48 fd_set * write_fd_set,
41 fd_set * except_fd_set, int *max_fd); 49 fd_set * except_fd_set, int *max_fd);
42 50
51
43/** 52/**
44 * Obtain the pollfd for this connection. The poll interface allows large 53 * Obtain the pollfd for this connection. The poll interface allows large
45 * file descriptors. Select goes stupid when the fd overflows fdset (which 54 * file descriptors. Select goes stupid when the fd overflows fdset (which
46 * is fixed). 55 * is fixed).
56 *
57 * @param connection connetion to get poll set for
58 * @param p where to store the polling information
59 */
60int
61MHD_connection_get_pollfd (struct MHD_Connection *connection,
62 struct MHD_Pollfd *p);
63
64
65/**
66 * Set callbacks for this connection to those for HTTP.
67 *
68 * @param connection connection to initialize
69 */
70void
71MHD_set_http_callbacks_ (struct MHD_Connection *connection);
72
73
74/**
75 * This function handles a particular connection when it has been
76 * determined that there is data to be read off a socket. All
77 * implementations (multithreaded, external select, internal select)
78 * call this function to handle reads.
79 *
80 * @param connection connection to handle
81 * @return always MHD_YES (we should continue to process the
82 * connection)
47 */ 83 */
48int MHD_connection_get_pollfd(struct MHD_Connection *connection, 84int
49 struct MHD_Pollfd *p); 85MHD_connection_handle_read (struct MHD_Connection *connection);
50 86
51void MHD_set_http_callbacks_ (struct MHD_Connection *connection);
52 87
53int MHD_connection_handle_read (struct MHD_Connection *connection); 88/**
89 * This function was created to handle writes to sockets when it has
90 * been determined that the socket can be written to. All
91 * implementations (multithreaded, external select, internal select)
92 * call this function
93 *
94 * @param connection connection to handle
95 * @return always MHD_YES (we should continue to process the
96 * connection)
97 */
98int
99MHD_connection_handle_write (struct MHD_Connection *connection);
54 100
55int MHD_connection_handle_write (struct MHD_Connection *connection);
56 101
57int MHD_connection_handle_idle (struct MHD_Connection *connection); 102/**
103 * This function was created to handle per-connection processing that
104 * has to happen even if the socket cannot be read or written to. All
105 * implementations (multithreaded, external select, internal select)
106 * call this function.
107 *
108 * @param connection connection to handle
109 * @return MHD_YES if we should continue to process the
110 * connection (not dead yet), MHD_NO if it died
111 */
112int
113MHD_connection_handle_idle (struct MHD_Connection *connection);
114
58 115
59/** 116/**
60 * Close the given connection and give the 117 * Close the given connection and give the
61 * specified termination code to the user. 118 * specified termination code to the user.
119 *
120 * @param connection connection to close
121 * @param termination_code termination reason to give
62 */ 122 */
63void MHD_connection_close (struct MHD_Connection *connection, 123void
64 enum MHD_RequestTerminationCode termination_code); 124MHD_connection_close (struct MHD_Connection *connection,
125 enum MHD_RequestTerminationCode termination_code);
126
65 127
66#endif 128#endif
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 2f367f25..dd49014f 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -191,6 +191,7 @@ MHD_ip_count_unlock(struct MHD_Daemon *daemon)
191 } 191 }
192} 192}
193 193
194
194/** 195/**
195 * Tree comparison function for IP addresses (supplied to tsearch() family). 196 * Tree comparison function for IP addresses (supplied to tsearch() family).
196 * We compare everything in the struct up through the beginning of the 197 * We compare everything in the struct up through the beginning of the
@@ -203,7 +204,7 @@ MHD_ip_count_unlock(struct MHD_Daemon *daemon)
203static int 204static int
204MHD_ip_addr_compare(const void *a1, const void *a2) 205MHD_ip_addr_compare(const void *a1, const void *a2)
205{ 206{
206 return memcmp (a1, a2, offsetof(struct MHD_IPCount, count)); 207 return memcmp (a1, a2, offsetof (struct MHD_IPCount, count));
207} 208}
208 209
209 210
@@ -216,15 +217,16 @@ MHD_ip_addr_compare(const void *a1, const void *a2)
216 * @return MHD_YES on success and MHD_NO otherwise (e.g., invalid address type) 217 * @return MHD_YES on success and MHD_NO otherwise (e.g., invalid address type)
217 */ 218 */
218static int 219static int
219MHD_ip_addr_to_key(const struct sockaddr *addr, socklen_t addrlen, 220MHD_ip_addr_to_key(const struct sockaddr *addr,
221 socklen_t addrlen,
220 struct MHD_IPCount *key) 222 struct MHD_IPCount *key)
221{ 223{
222 memset(key, 0, sizeof(*key)); 224 memset(key, 0, sizeof(*key));
223 225
224 /* IPv4 addresses */ 226 /* IPv4 addresses */
225 if (addrlen == sizeof(struct sockaddr_in)) 227 if (sizeof (struct sockaddr_in) == addrlen)
226 { 228 {
227 const struct sockaddr_in *addr4 = (const struct sockaddr_in*)addr; 229 const struct sockaddr_in *addr4 = (const struct sockaddr_in*) addr;
228 key->family = AF_INET; 230 key->family = AF_INET;
229 memcpy (&key->addr.ipv4, &addr4->sin_addr, sizeof(addr4->sin_addr)); 231 memcpy (&key->addr.ipv4, &addr4->sin_addr, sizeof(addr4->sin_addr));
230 return MHD_YES; 232 return MHD_YES;
@@ -232,9 +234,9 @@ MHD_ip_addr_to_key(const struct sockaddr *addr, socklen_t addrlen,
232 234
233#if HAVE_IPV6 235#if HAVE_IPV6
234 /* IPv6 addresses */ 236 /* IPv6 addresses */
235 if (addrlen == sizeof (struct sockaddr_in6)) 237 if (sizeof (struct sockaddr_in6) == addrlen)
236 { 238 {
237 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*)addr; 239 const struct sockaddr_in6 *addr6 = (const struct sockaddr_in6*) addr;
238 key->family = AF_INET6; 240 key->family = AF_INET6;
239 memcpy (&key->addr.ipv6, &addr6->sin6_addr, sizeof(addr6->sin6_addr)); 241 memcpy (&key->addr.ipv6, &addr6->sin6_addr, sizeof(addr6->sin6_addr));
240 return MHD_YES; 242 return MHD_YES;
@@ -261,17 +263,16 @@ MHD_ip_limit_add(struct MHD_Daemon *daemon,
261 socklen_t addrlen) 263 socklen_t addrlen)
262{ 264{
263 struct MHD_IPCount *key; 265 struct MHD_IPCount *key;
266 void **nodep;
264 void *node; 267 void *node;
265 int result; 268 int result;
266 269
267 daemon = MHD_get_master (daemon); 270 daemon = MHD_get_master (daemon);
268
269 /* Ignore if no connection limit assigned */ 271 /* Ignore if no connection limit assigned */
270 if (daemon->per_ip_connection_limit == 0) 272 if (0 == daemon->per_ip_connection_limit)
271 return MHD_YES; 273 return MHD_YES;
272 274
273 key = malloc (sizeof(*key)); 275 if (NULL == (key = malloc (sizeof(*key))))
274 if (NULL == key)
275 return MHD_NO; 276 return MHD_NO;
276 277
277 /* Initialize key */ 278 /* Initialize key */
@@ -281,32 +282,30 @@ MHD_ip_limit_add(struct MHD_Daemon *daemon,
281 free (key); 282 free (key);
282 return MHD_YES; 283 return MHD_YES;
283 } 284 }
284
285 MHD_ip_count_lock (daemon); 285 MHD_ip_count_lock (daemon);
286 286
287 /* Search for the IP address */ 287 /* Search for the IP address */
288 node = (void*)TSEARCH (key, &daemon->per_ip_connection_count, MHD_ip_addr_compare); 288 if (NULL == (nodep = TSEARCH (key,
289 if (!node) 289 &daemon->per_ip_connection_count,
290 &MHD_ip_addr_compare)))
290 { 291 {
291#if HAVE_MESSAGES 292#if HAVE_MESSAGES
292 MHD_DLOG(daemon, 293 MHD_DLOG (daemon,
293 "Failed to add IP connection count node\n"); 294 "Failed to add IP connection count node\n");
294#endif 295#endif
295 MHD_ip_count_unlock (daemon); 296 MHD_ip_count_unlock (daemon);
296 free (key); 297 free (key);
297 return MHD_NO; 298 return MHD_NO;
298 } 299 }
299 node = *(void**)node; 300 node = *nodep;
300
301 /* If we got an existing node back, free the one we created */ 301 /* If we got an existing node back, free the one we created */
302 if (node != key) 302 if (node != key)
303 free(key); 303 free(key);
304 key = (struct MHD_IPCount*)node; 304 key = (struct MHD_IPCount *) node;
305
306 /* Test if there is room for another connection; if so, 305 /* Test if there is room for another connection; if so,
307 * increment count */ 306 * increment count */
308 result = (key->count < daemon->per_ip_connection_limit); 307 result = (key->count < daemon->per_ip_connection_limit);
309 if (result == MHD_YES) 308 if (MHD_YES == result)
310 ++key->count; 309 ++key->count;
311 310
312 MHD_ip_count_unlock (daemon); 311 MHD_ip_count_unlock (daemon);
@@ -329,14 +328,12 @@ MHD_ip_limit_del(struct MHD_Daemon *daemon,
329{ 328{
330 struct MHD_IPCount search_key; 329 struct MHD_IPCount search_key;
331 struct MHD_IPCount *found_key; 330 struct MHD_IPCount *found_key;
332 void *node; 331 void **nodep;
333 332
334 daemon = MHD_get_master (daemon); 333 daemon = MHD_get_master (daemon);
335
336 /* Ignore if no connection limit assigned */ 334 /* Ignore if no connection limit assigned */
337 if (daemon->per_ip_connection_limit == 0) 335 if (0 == daemon->per_ip_connection_limit)
338 return; 336 return;
339
340 /* Initialize search key */ 337 /* Initialize search key */
341 if (MHD_NO == MHD_ip_addr_to_key (addr, addrlen, &search_key)) 338 if (MHD_NO == MHD_ip_addr_to_key (addr, addrlen, &search_key))
342 return; 339 return;
@@ -344,22 +341,21 @@ MHD_ip_limit_del(struct MHD_Daemon *daemon,
344 MHD_ip_count_lock (daemon); 341 MHD_ip_count_lock (daemon);
345 342
346 /* Search for the IP address */ 343 /* Search for the IP address */
347 node = (void*)TFIND (&search_key, &daemon->per_ip_connection_count, MHD_ip_addr_compare); 344 if (NULL == (nodep = TFIND (&search_key,
348 345 &daemon->per_ip_connection_count,
349 /* Something's wrong if we couldn't find an IP address 346 &MHD_ip_addr_compare)))
350 * that was previously added */ 347 {
351 if (!node) 348 /* Something's wrong if we couldn't find an IP address
352 { 349 * that was previously added */
353#if HAVE_MESSAGES 350#if HAVE_MESSAGES
354 MHD_DLOG (daemon, 351 MHD_DLOG (daemon,
355 "Failed to find previously-added IP address\n"); 352 "Failed to find previously-added IP address\n");
356#endif 353#endif
357 abort(); 354 abort();
358 } 355 }
359 found_key = (struct MHD_IPCount*)*(void**)node; 356 found_key = (struct MHD_IPCount *) *nodep;
360
361 /* Validate existing count for IP address */ 357 /* Validate existing count for IP address */
362 if (found_key->count == 0) 358 if (0 == found_key->count)
363 { 359 {
364#if HAVE_MESSAGES 360#if HAVE_MESSAGES
365 MHD_DLOG (daemon, 361 MHD_DLOG (daemon,
@@ -367,11 +363,12 @@ MHD_ip_limit_del(struct MHD_Daemon *daemon,
367#endif 363#endif
368 abort(); 364 abort();
369 } 365 }
370
371 /* Remove the node entirely if count reduces to 0 */ 366 /* Remove the node entirely if count reduces to 0 */
372 if (--found_key->count == 0) 367 if (0 == --found_key->count)
373 { 368 {
374 TDELETE (found_key, &daemon->per_ip_connection_count, MHD_ip_addr_compare); 369 TDELETE (found_key,
370 &daemon->per_ip_connection_count,
371 &MHD_ip_addr_compare);
375 free (found_key); 372 free (found_key);
376 } 373 }
377 374
@@ -395,9 +392,10 @@ static ssize_t
395recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i) 392recv_tls_adapter (struct MHD_Connection *connection, void *other, size_t i)
396{ 393{
397 int res; 394 int res;
395
398 res = gnutls_record_recv (connection->tls_session, other, i); 396 res = gnutls_record_recv (connection->tls_session, other, i);
399 if ( (res == GNUTLS_E_AGAIN) || 397 if ( (GNUTLS_E_AGAIN == res) ||
400 (res == GNUTLS_E_INTERRUPTED) ) 398 (GNUTLS_E_INTERRUPTED == res) )
401 { 399 {
402 errno = EINTR; 400 errno = EINTR;
403 return -1; 401 return -1;
@@ -427,9 +425,10 @@ send_tls_adapter (struct MHD_Connection *connection,
427 const void *other, size_t i) 425 const void *other, size_t i)
428{ 426{
429 int res; 427 int res;
428
430 res = gnutls_record_send (connection->tls_session, other, i); 429 res = gnutls_record_send (connection->tls_session, other, i);
431 if ( (res == GNUTLS_E_AGAIN) || 430 if ( (GNUTLS_E_AGAIN == res) ||
432 (res == GNUTLS_E_INTERRUPTED) ) 431 (GNUTLS_E_INTERRUPTED == res) )
433 { 432 {
434 errno = EINTR; 433 errno = EINTR;
435 return -1; 434 return -1;
@@ -450,7 +449,7 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
450 gnutls_datum_t key; 449 gnutls_datum_t key;
451 gnutls_datum_t cert; 450 gnutls_datum_t cert;
452 451
453 if (daemon->https_mem_trust) 452 if (NULL != daemon->https_mem_trust)
454 { 453 {
455 cert.data = (unsigned char *) daemon->https_mem_trust; 454 cert.data = (unsigned char *) daemon->https_mem_trust;
456 cert.size = strlen (daemon->https_mem_trust); 455 cert.size = strlen (daemon->https_mem_trust);
@@ -466,7 +465,8 @@ MHD_init_daemon_certificate (struct MHD_Daemon *daemon)
466 } 465 }
467 466
468 /* certificate & key loaded from memory */ 467 /* certificate & key loaded from memory */
469 if (daemon->https_mem_cert && daemon->https_mem_key) 468 if ( (NULL != daemon->https_mem_cert) &&
469 (NULL != daemon->https_mem_key) )
470 { 470 {
471 key.data = (unsigned char *) daemon->https_mem_key; 471 key.data = (unsigned char *) daemon->https_mem_key;
472 key.size = strlen (daemon->https_mem_key); 472 key.size = strlen (daemon->https_mem_key);
@@ -605,8 +605,8 @@ MHD_handle_connection (void *data)
605 tv.tv_usec = 0; 605 tv.tv_usec = 0;
606 tvp = &tv; 606 tvp = &tv;
607 } 607 }
608 if ((con->state == MHD_CONNECTION_NORMAL_BODY_UNREADY) || 608 if ( (MHD_CONNECTION_NORMAL_BODY_UNREADY == con->state) ||
609 (con->state == MHD_CONNECTION_CHUNKED_BODY_UNREADY)) 609 (MHD_CONNECTION_CHUNKED_BODY_UNREADY == con->state) )
610 { 610 {
611 /* do not block (we're waiting for our callback to succeed) */ 611 /* do not block (we're waiting for our callback to succeed) */
612 tv.tv_sec = 0; 612 tv.tv_sec = 0;
@@ -628,10 +628,12 @@ MHD_handle_connection (void *data)
628 num_ready = SELECT (max + 1, &rs, &ws, &es, tvp); 628 num_ready = SELECT (max + 1, &rs, &ws, &es, tvp);
629 if (num_ready < 0) 629 if (num_ready < 0)
630 { 630 {
631 if (errno == EINTR) 631 if (EINTR == errno)
632 continue; 632 continue;
633#if HAVE_MESSAGES 633#if HAVE_MESSAGES
634 MHD_DLOG (con->daemon, "Error during select (%d): `%s'\n", max, 634 MHD_DLOG (con->daemon,
635 "Error during select (%d): `%s'\n",
636 max,
635 STRERROR (errno)); 637 STRERROR (errno));
636#endif 638#endif
637 break; 639 break;
@@ -658,11 +660,9 @@ MHD_handle_connection (void *data)
658 p[0].events |= POLLOUT; 660 p[0].events |= POLLOUT;
659 if (poll (p, 661 if (poll (p,
660 1, 662 1,
661 (tvp == NULL) 663 (NULL == tvp) ? -1 : tv.tv_sec * 1000) < 0)
662 ? -1
663 : tv.tv_sec * 1000) < 0)
664 { 664 {
665 if (errno == EINTR) 665 if (EINTR == errno)
666 continue; 666 continue;
667#if HAVE_MESSAGES 667#if HAVE_MESSAGES
668 MHD_DLOG (con->daemon, "Error during poll: `%s'\n", 668 MHD_DLOG (con->daemon, "Error during poll: `%s'\n",
@@ -681,7 +681,7 @@ MHD_handle_connection (void *data)
681 } 681 }
682#endif 682#endif
683 } 683 }
684 if (con->state != MHD_CONNECTION_IN_CLEANUP) 684 if (MHD_CONNECTION_IN_CLEANUP != con->state)
685 { 685 {
686#if DEBUG_CLOSE 686#if DEBUG_CLOSE
687#if HAVE_MESSAGES 687#if HAVE_MESSAGES
@@ -689,12 +689,13 @@ MHD_handle_connection (void *data)
689 "Processing thread terminating, closing connection\n"); 689 "Processing thread terminating, closing connection\n");
690#endif 690#endif
691#endif 691#endif
692 if (con->state != MHD_CONNECTION_CLOSED) 692 if (MHD_CONNECTION_CLOSED != con->state)
693 MHD_connection_close (con, MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN); 693 MHD_connection_close (con,
694 MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN);
694 con->idle_handler (con); 695 con->idle_handler (con);
695 } 696 }
696exit: 697exit:
697 if (con->response != NULL) 698 if (NULL != con->response)
698 { 699 {
699 MHD_destroy_response (con->response); 700 MHD_destroy_response (con->response);
700 con->response = NULL; 701 con->response = NULL;
@@ -747,8 +748,8 @@ send_param_adapter (struct MHD_Connection *connection,
747 off_t left; 748 off_t left;
748 ssize_t ret; 749 ssize_t ret;
749#endif 750#endif
750 if ( (connection->socket_fd == -1) || 751 if ( (-1 == connection->socket_fd) ||
751 (connection->state == MHD_CONNECTION_CLOSED) ) 752 (MHD_CONNECTION_CLOSED == connection->state) )
752 { 753 {
753 errno = ENOTCONN; 754 errno = ENOTCONN;
754 return -1; 755 return -1;
@@ -766,16 +767,14 @@ send_param_adapter (struct MHD_Connection *connection,
766 left = connection->response->total_size - connection->response_write_position; 767 left = connection->response->total_size - connection->response_write_position;
767 if (left > SSIZE_MAX) 768 if (left > SSIZE_MAX)
768 left = SSIZE_MAX; /* cap at return value limit */ 769 left = SSIZE_MAX; /* cap at return value limit */
769 ret = sendfile (connection->socket_fd, 770 if (-1 != (ret = sendfile (connection->socket_fd,
770 fd, 771 fd,
771 &offset, 772 &offset,
772 (size_t) left); 773 (size_t) left)))
773 if (ret != -1)
774 return ret; 774 return ret;
775 if ((EINTR == errno) || (EAGAIN == errno)) 775 if ( (EINTR == errno) || (EAGAIN == errno) )
776 return 0; 776 return 0;
777 if ( (EINVAL == errno) || 777 if ( (EINVAL == errno) || (EBADF == errno) )
778 (EBADF == errno) )
779 return -1; 778 return -1;
780 /* None of the 'usual' sendfile errors occurred, so we should try 779 /* None of the 'usual' sendfile errors occurred, so we should try
781 to fall back to 'SEND'; see also this thread for info on 780 to fall back to 'SEND'; see also this thread for info on
@@ -806,7 +805,7 @@ create_thread (pthread_t * thread,
806 pthread_attr_t *pattr; 805 pthread_attr_t *pattr;
807 int ret; 806 int ret;
808 807
809 if (daemon->thread_stack_size != 0) 808 if (0 != daemon->thread_stack_size)
810 { 809 {
811 if (0 != (ret = pthread_attr_init (&attr))) 810 if (0 != (ret = pthread_attr_init (&attr)))
812 goto ERR; 811 goto ERR;
@@ -823,7 +822,7 @@ create_thread (pthread_t * thread,
823 } 822 }
824 ret = pthread_create (thread, pattr, 823 ret = pthread_create (thread, pattr,
825 start_routine, arg); 824 start_routine, arg);
826 if (daemon->thread_stack_size != 0) 825 if (0 != daemon->thread_stack_size)
827 pthread_attr_destroy (&attr); 826 pthread_attr_destroy (&attr);
828 return ret; 827 return ret;
829 ERR: 828 ERR:
@@ -893,8 +892,8 @@ MHD_add_connection (struct MHD_Daemon *daemon,
893 MHD_DLOG (daemon, "Accepted connection on socket %d\n", s); 892 MHD_DLOG (daemon, "Accepted connection on socket %d\n", s);
894#endif 893#endif
895#endif 894#endif
896 if ((daemon->max_connections == 0) 895 if ( (0 == daemon->max_connections) ||
897 || (MHD_ip_limit_add (daemon, addr, addrlen) == MHD_NO)) 896 (MHD_NO == MHD_ip_limit_add (daemon, addr, addrlen)) )
898 { 897 {
899 /* above connection limit - reject */ 898 /* above connection limit - reject */
900#if HAVE_MESSAGES 899#if HAVE_MESSAGES
@@ -907,8 +906,9 @@ MHD_add_connection (struct MHD_Daemon *daemon,
907 } 906 }
908 907
909 /* apply connection acceptance policy if present */ 908 /* apply connection acceptance policy if present */
910 if ((daemon->apc != NULL) 909 if ( (NULL != daemon->apc) &&
911 && (MHD_NO == daemon->apc (daemon->apc_cls, addr, addrlen))) 910 (MHD_NO == daemon->apc (daemon->apc_cls,
911 addr, addrlen)) )
912 { 912 {
913#if DEBUG_CLOSE 913#if DEBUG_CLOSE
914#if HAVE_MESSAGES 914#if HAVE_MESSAGES
@@ -924,16 +924,19 @@ MHD_add_connection (struct MHD_Daemon *daemon,
924#if OSX 924#if OSX
925#ifdef SOL_SOCKET 925#ifdef SOL_SOCKET
926#ifdef SO_NOSIGPIPE 926#ifdef SO_NOSIGPIPE
927 setsockopt (client_socket, SOL_SOCKET, SO_NOSIGPIPE, &on, sizeof (on)); 927 setsockopt (client_socket,
928 SOL_SOCKET, SO_NOSIGPIPE,
929 &on, sizeof (on));
928#endif 930#endif
929#endif 931#endif
930#endif 932#endif
931 933
932 connection = malloc (sizeof (struct MHD_Connection)); 934 if (NULL == (connection = malloc (sizeof (struct MHD_Connection))))
933 if (NULL == connection)
934 { 935 {
935#if HAVE_MESSAGES 936#if HAVE_MESSAGES
936 MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); 937 MHD_DLOG (daemon,
938 "Error allocating memory: %s\n",
939 STRERROR (errno));
937#endif 940#endif
938 SHUTDOWN (client_socket, SHUT_RDWR); 941 SHUTDOWN (client_socket, SHUT_RDWR);
939 CLOSE (client_socket); 942 CLOSE (client_socket);
@@ -943,11 +946,12 @@ MHD_add_connection (struct MHD_Daemon *daemon,
943 memset (connection, 0, sizeof (struct MHD_Connection)); 946 memset (connection, 0, sizeof (struct MHD_Connection));
944 connection->connection_timeout = daemon->connection_timeout; 947 connection->connection_timeout = daemon->connection_timeout;
945 connection->pool = NULL; 948 connection->pool = NULL;
946 connection->addr = malloc (addrlen); 949 if (NULL == (connection->addr = malloc (addrlen)))
947 if (connection->addr == NULL)
948 { 950 {
949#if HAVE_MESSAGES 951#if HAVE_MESSAGES
950 MHD_DLOG (daemon, "Error allocating memory: %s\n", STRERROR (errno)); 952 MHD_DLOG (daemon,
953 "Error allocating memory: %s\n",
954 STRERROR (errno));
951#endif 955#endif
952 SHUTDOWN (client_socket, SHUT_RDWR); 956 SHUTDOWN (client_socket, SHUT_RDWR);
953 CLOSE (client_socket); 957 CLOSE (client_socket);
@@ -979,13 +983,14 @@ MHD_add_connection (struct MHD_Daemon *daemon,
979 /* make socket non-blocking */ 983 /* make socket non-blocking */
980#ifndef MINGW 984#ifndef MINGW
981 int flags = fcntl (connection->socket_fd, F_GETFL); 985 int flags = fcntl (connection->socket_fd, F_GETFL);
982 if ( (flags == -1) || 986 if ( (-1 == flags) ||
983 (0 != fcntl (connection->socket_fd, F_SETFL, flags | O_NONBLOCK)) ) 987 (0 != fcntl (connection->socket_fd, F_SETFL, flags | O_NONBLOCK)) )
984 { 988 {
985#if HAVE_MESSAGES 989#if HAVE_MESSAGES
986 FPRINTF(stderr, "Failed to make socket %d non-blocking: %s\n", 990 MHD_DLOG (daemon,
987 connection->socket_fd, 991 "Failed to make socket %d non-blocking: %s\n",
988 STRERROR (errno)); 992 connection->socket_fd,
993 STRERROR (errno));
989#endif 994#endif
990 } 995 }
991#else 996#else
@@ -993,8 +998,9 @@ MHD_add_connection (struct MHD_Daemon *daemon,
993 if (0 != ioctlsocket (connection->socket_fd, FIONBIO, &flags)) 998 if (0 != ioctlsocket (connection->socket_fd, FIONBIO, &flags))
994 { 999 {
995#if HAVE_MESSAGES 1000#if HAVE_MESSAGES
996 FPRINTF(stderr, "Failed to make socket non-blocking: %s\n", 1001 MHD_DLOG (daemon,
997 STRERROR (errno)); 1002 "Failed to make socket non-blocking: %s\n",
1003 STRERROR (errno));
998#endif 1004#endif
999 } 1005 }
1000#endif 1006#endif
@@ -1076,7 +1082,7 @@ MHD_add_connection (struct MHD_Daemon *daemon,
1076 { 1082 {
1077 res_thread_create = create_thread (&connection->pid, daemon, 1083 res_thread_create = create_thread (&connection->pid, daemon,
1078 &MHD_handle_connection, connection); 1084 &MHD_handle_connection, connection);
1079 if (res_thread_create != 0) 1085 if (0 != res_thread_create)
1080 { 1086 {
1081#if HAVE_MESSAGES 1087#if HAVE_MESSAGES
1082 MHD_DLOG (daemon, "Failed to create a thread: %s\n", 1088 MHD_DLOG (daemon, "Failed to create a thread: %s\n",
@@ -1148,14 +1154,16 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1148 s = ACCEPT (daemon->socket_fd, addr, &addrlen); 1154 s = ACCEPT (daemon->socket_fd, addr, &addrlen);
1149 need_fcntl = MHD_YES; 1155 need_fcntl = MHD_YES;
1150 } 1156 }
1151 if ((s == -1) || (addrlen <= 0)) 1157 if ((-1 == s) || (addrlen <= 0))
1152 { 1158 {
1153#if HAVE_MESSAGES 1159#if HAVE_MESSAGES
1154 /* This could be a common occurance with multiple worker threads */ 1160 /* This could be a common occurance with multiple worker threads */
1155 if ((EAGAIN != errno) && (EWOULDBLOCK != errno)) 1161 if ((EAGAIN != errno) && (EWOULDBLOCK != errno))
1156 MHD_DLOG (daemon, "Error accepting connection: %s\n", STRERROR (errno)); 1162 MHD_DLOG (daemon,
1163 "Error accepting connection: %s\n",
1164 STRERROR (errno));
1157#endif 1165#endif
1158 if (s != -1) 1166 if (-1 != s)
1159 { 1167 {
1160 SHUTDOWN (s, SHUT_RDWR); 1168 SHUTDOWN (s, SHUT_RDWR);
1161 CLOSE (s); 1169 CLOSE (s);
@@ -1182,8 +1190,9 @@ MHD_accept_connection (struct MHD_Daemon *daemon)
1182#if WINDOWS 1190#if WINDOWS
1183 SetErrnoFromWinError (GetLastError ()); 1191 SetErrnoFromWinError (GetLastError ());
1184#endif 1192#endif
1185 FPRINTF(stderr, "Failed to make socket non-inheritable: %s\n", 1193 MHD_DLOG (daemon,
1186 STRERROR (errno)); 1194 "Failed to make socket non-inheritable: %s\n",
1195 STRERROR (errno));
1187#endif 1196#endif
1188 } 1197 }
1189 } 1198 }
@@ -1241,7 +1250,7 @@ MHD_cleanup_connections (struct MHD_Daemon *daemon)
1241 gnutls_deinit (pos->tls_session); 1250 gnutls_deinit (pos->tls_session);
1242#endif 1251#endif
1243 MHD_ip_limit_del (daemon, (struct sockaddr*)pos->addr, pos->addr_len); 1252 MHD_ip_limit_del (daemon, (struct sockaddr*)pos->addr, pos->addr_len);
1244 if (pos->response != NULL) 1253 if (NULL != pos->response)
1245 { 1254 {
1246 MHD_destroy_response (pos->response); 1255 MHD_destroy_response (pos->response);
1247 pos->response = NULL; 1256 pos->response = NULL;
@@ -1282,7 +1291,7 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
1282 time_t earliest_deadline; 1291 time_t earliest_deadline;
1283 time_t now; 1292 time_t now;
1284 struct MHD_Connection *pos; 1293 struct MHD_Connection *pos;
1285 int have_timeout = MHD_NO; 1294 int have_timeout;
1286 1295
1287 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1296 if (0 != (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1288 { 1297 {
@@ -1291,10 +1300,8 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
1291#endif 1300#endif
1292 return MHD_NO; 1301 return MHD_NO;
1293 } 1302 }
1294 pos = daemon->connections_head; 1303 have_timeout = MHD_NO;
1295 if (pos == NULL) 1304 for (pos = daemon->connections_head; NULL != pos; pos = pos->next)
1296 return MHD_NO; /* no connections */
1297 while (pos != NULL)
1298 { 1305 {
1299 if (0 != pos->connection_timeout) { 1306 if (0 != pos->connection_timeout) {
1300 if (!have_timeout || 1307 if (!have_timeout ||
@@ -1307,9 +1314,8 @@ MHD_get_timeout (struct MHD_Daemon *daemon,
1307#endif 1314#endif
1308 have_timeout = MHD_YES; 1315 have_timeout = MHD_YES;
1309 } 1316 }
1310 pos = pos->next;
1311 } 1317 }
1312 if (!have_timeout) 1318 if (MHD_NO == have_timeout)
1313 return MHD_NO; 1319 return MHD_NO;
1314 now = MHD_monotonic_time(); 1320 now = MHD_monotonic_time();
1315 if (earliest_deadline < now) 1321 if (earliest_deadline < now)
@@ -1345,12 +1351,12 @@ MHD_select (struct MHD_Daemon *daemon,
1345 1351
1346 timeout.tv_sec = 0; 1352 timeout.tv_sec = 0;
1347 timeout.tv_usec = 0; 1353 timeout.tv_usec = 0;
1348 if (daemon->shutdown == MHD_YES) 1354 if (MHD_YES == daemon->shutdown)
1349 return MHD_NO; 1355 return MHD_NO;
1350 FD_ZERO (&rs); 1356 FD_ZERO (&rs);
1351 FD_ZERO (&ws); 1357 FD_ZERO (&ws);
1352 FD_ZERO (&es); 1358 FD_ZERO (&es);
1353 max = 0; 1359 max = -1;
1354 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1360 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1355 { 1361 {
1356 /* single-threaded, go over everything */ 1362 /* single-threaded, go over everything */
@@ -1366,12 +1372,12 @@ MHD_select (struct MHD_Daemon *daemon,
1366 else 1372 else
1367 { 1373 {
1368 /* accept only, have one thread per connection */ 1374 /* accept only, have one thread per connection */
1369 max = daemon->socket_fd; 1375 if (-1 != daemon->socket_fd)
1370 if (max == -1) 1376 {
1371 return MHD_NO; 1377 max = daemon->socket_fd;
1372 FD_SET (max, &rs); 1378 FD_SET (daemon->socket_fd, &rs);
1379 }
1373 } 1380 }
1374
1375 if (-1 != daemon->wpipe[0]) 1381 if (-1 != daemon->wpipe[0])
1376 { 1382 {
1377 FD_SET (daemon->wpipe[0], &rs); 1383 FD_SET (daemon->wpipe[0], &rs);
@@ -1381,7 +1387,7 @@ MHD_select (struct MHD_Daemon *daemon,
1381 } 1387 }
1382 1388
1383 tv = NULL; 1389 tv = NULL;
1384 if (may_block == MHD_NO) 1390 if (MHD_NO == may_block)
1385 { 1391 {
1386 timeout.tv_usec = 0; 1392 timeout.tv_usec = 0;
1387 timeout.tv_sec = 0; 1393 timeout.tv_sec = 0;
@@ -1397,20 +1403,17 @@ MHD_select (struct MHD_Daemon *daemon,
1397 } 1403 }
1398 num_ready = SELECT (max + 1, &rs, &ws, &es, tv); 1404 num_ready = SELECT (max + 1, &rs, &ws, &es, tv);
1399 1405
1400 if (daemon->shutdown == MHD_YES) 1406 if (MHD_YES == daemon->shutdown)
1401 return MHD_NO; 1407 return MHD_NO;
1402 if (num_ready < 0) 1408 if (num_ready < 0)
1403 { 1409 {
1404 if (errno == EINTR) 1410 if (EINTR == errno)
1405 return MHD_YES; 1411 return MHD_YES;
1406#if HAVE_MESSAGES 1412#if HAVE_MESSAGES
1407 MHD_DLOG (daemon, "select failed: %s\n", STRERROR (errno)); 1413 MHD_DLOG (daemon, "select failed: %s\n", STRERROR (errno));
1408#endif 1414#endif
1409 return MHD_NO; 1415 return MHD_NO;
1410 } 1416 }
1411 if (MHD_YES == daemon->shutdown)
1412 return MHD_YES;
1413
1414 /* select connection thread handling type */ 1417 /* select connection thread handling type */
1415 if ( (-1 != (ds = daemon->socket_fd)) && 1418 if ( (-1 != (ds = daemon->socket_fd)) &&
1416 (FD_ISSET (ds, &rs)) ) 1419 (FD_ISSET (ds, &rs)) )
@@ -1454,13 +1457,11 @@ MHD_poll_all (struct MHD_Daemon *daemon,
1454 struct MHD_Connection *pos; 1457 struct MHD_Connection *pos;
1455 struct MHD_Connection *next; 1458 struct MHD_Connection *next;
1456 1459
1460 /* count number of connections and thus determine poll set size */
1457 num_connections = 0; 1461 num_connections = 0;
1458 pos = daemon->connections_head; 1462 for (pos = daemon->connections_head; NULL != pos; pos = pos->next)
1459 while (pos != NULL) 1463 num_connections++;
1460 { 1464
1461 num_connections++;
1462 pos = pos->next;
1463 }
1464 { 1465 {
1465 struct pollfd p[2 + num_connections]; 1466 struct pollfd p[2 + num_connections];
1466 struct MHD_Pollfd mp; 1467 struct MHD_Pollfd mp;
@@ -1515,7 +1516,9 @@ MHD_poll_all (struct MHD_Daemon *daemon,
1515 if (EINTR == errno) 1516 if (EINTR == errno)
1516 return MHD_YES; 1517 return MHD_YES;
1517#if HAVE_MESSAGES 1518#if HAVE_MESSAGES
1518 MHD_DLOG (daemon, "poll failed: %s\n", STRERROR (errno)); 1519 MHD_DLOG (daemon,
1520 "poll failed: %s\n",
1521 STRERROR (errno));
1519#endif 1522#endif
1520 return MHD_NO; 1523 return MHD_NO;
1521 } 1524 }
@@ -1620,7 +1623,7 @@ MHD_poll (struct MHD_Daemon *daemon,
1620 int may_block) 1623 int may_block)
1621{ 1624{
1622#ifdef HAVE_POLL_H 1625#ifdef HAVE_POLL_H
1623 if (daemon->shutdown == MHD_YES) 1626 if (MHD_YES == daemon->shutdown)
1624 return MHD_NO; 1627 return MHD_NO;
1625 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION)) 1628 if (0 == (daemon->options & MHD_USE_THREAD_PER_CONNECTION))
1626 return MHD_poll_all (daemon, may_block); 1629 return MHD_poll_all (daemon, may_block);
@@ -1814,9 +1817,9 @@ parse_options_va (struct MHD_Daemon *daemon,
1814 if (daemon->worker_pool_size >= SIZE_MAX / sizeof (struct MHD_Daemon)) 1817 if (daemon->worker_pool_size >= SIZE_MAX / sizeof (struct MHD_Daemon))
1815 { 1818 {
1816#if HAVE_MESSAGES 1819#if HAVE_MESSAGES
1817 FPRINTF (stderr, 1820 MHD_DLOG (daemon,
1818 "Specified thread pool size (%u) too big\n", 1821 "Specified thread pool size (%u) too big\n",
1819 daemon->worker_pool_size); 1822 daemon->worker_pool_size);
1820#endif 1823#endif
1821 return MHD_NO; 1824 return MHD_NO;
1822 } 1825 }
@@ -1827,9 +1830,9 @@ parse_options_va (struct MHD_Daemon *daemon,
1827 daemon->https_mem_key = va_arg (ap, const char *); 1830 daemon->https_mem_key = va_arg (ap, const char *);
1828#if HAVE_MESSAGES 1831#if HAVE_MESSAGES
1829 else 1832 else
1830 FPRINTF (stderr, 1833 MHD_DLOG (daemon,
1831 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n", 1834 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n",
1832 opt); 1835 opt);
1833#endif 1836#endif
1834 break; 1837 break;
1835 case MHD_OPTION_HTTPS_MEM_CERT: 1838 case MHD_OPTION_HTTPS_MEM_CERT:
@@ -1837,9 +1840,9 @@ parse_options_va (struct MHD_Daemon *daemon,
1837 daemon->https_mem_cert = va_arg (ap, const char *); 1840 daemon->https_mem_cert = va_arg (ap, const char *);
1838#if HAVE_MESSAGES 1841#if HAVE_MESSAGES
1839 else 1842 else
1840 FPRINTF (stderr, 1843 MHD_DLOG (daemon,
1841 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n", 1844 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n",
1842 opt); 1845 opt);
1843#endif 1846#endif
1844 break; 1847 break;
1845 case MHD_OPTION_HTTPS_MEM_TRUST: 1848 case MHD_OPTION_HTTPS_MEM_TRUST:
@@ -1847,16 +1850,16 @@ parse_options_va (struct MHD_Daemon *daemon,
1847 daemon->https_mem_trust = va_arg (ap, const char *); 1850 daemon->https_mem_trust = va_arg (ap, const char *);
1848#if HAVE_MESSAGES 1851#if HAVE_MESSAGES
1849 else 1852 else
1850 FPRINTF (stderr, 1853 MHD_DLOG (daemon,
1851 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n", 1854 "MHD HTTPS option %d passed to MHD but MHD_USE_SSL not set\n",
1852 opt); 1855 opt);
1853#endif 1856#endif
1854 break; 1857 break;
1855 case MHD_OPTION_HTTPS_CRED_TYPE: 1858 case MHD_OPTION_HTTPS_CRED_TYPE:
1856 daemon->cred_type = va_arg (ap, gnutls_credentials_type_t); 1859 daemon->cred_type = va_arg (ap, gnutls_credentials_type_t);
1857 break; 1860 break;
1858 case MHD_OPTION_HTTPS_PRIORITIES: 1861 case MHD_OPTION_HTTPS_PRIORITIES:
1859 if (daemon->options & MHD_USE_SSL) 1862 if (0 != (daemon->options & MHD_USE_SSL))
1860 { 1863 {
1861 gnutls_priority_deinit (daemon->priority_cache); 1864 gnutls_priority_deinit (daemon->priority_cache);
1862 ret = gnutls_priority_init (&daemon->priority_cache, 1865 ret = gnutls_priority_init (&daemon->priority_cache,
@@ -1865,10 +1868,10 @@ parse_options_va (struct MHD_Daemon *daemon,
1865 if (ret != GNUTLS_E_SUCCESS) 1868 if (ret != GNUTLS_E_SUCCESS)
1866 { 1869 {
1867#if HAVE_MESSAGES 1870#if HAVE_MESSAGES
1868 FPRINTF (stderr, 1871 MHD_DLOG (daemon,
1869 "Setting priorities to `%s' failed: %s\n", 1872 "Setting priorities to `%s' failed: %s\n",
1870 pstr, 1873 pstr,
1871 gnutls_strerror (ret)); 1874 gnutls_strerror (ret));
1872#endif 1875#endif
1873 daemon->priority_cache = NULL; 1876 daemon->priority_cache = NULL;
1874 return MHD_NO; 1877 return MHD_NO;
@@ -1994,15 +1997,15 @@ parse_options_va (struct MHD_Daemon *daemon,
1994 if (((opt >= MHD_OPTION_HTTPS_MEM_KEY) && 1997 if (((opt >= MHD_OPTION_HTTPS_MEM_KEY) &&
1995 (opt <= MHD_OPTION_HTTPS_PRIORITIES)) || (opt == MHD_OPTION_HTTPS_MEM_TRUST)) 1998 (opt <= MHD_OPTION_HTTPS_PRIORITIES)) || (opt == MHD_OPTION_HTTPS_MEM_TRUST))
1996 { 1999 {
1997 FPRINTF (stderr, 2000 MHD_DLOG (daemon,
1998 "MHD HTTPS option %d passed to MHD compiled without HTTPS support\n", 2001 "MHD HTTPS option %d passed to MHD compiled without HTTPS support\n",
1999 opt); 2002 opt);
2000 } 2003 }
2001 else 2004 else
2002 { 2005 {
2003 FPRINTF (stderr, 2006 MHD_DLOG (daemon,
2004 "Invalid option %d! (Did you terminate the list with MHD_OPTION_END?)\n", 2007 "Invalid option %d! (Did you terminate the list with MHD_OPTION_END?)\n",
2005 opt); 2008 opt);
2006 } 2009 }
2007#endif 2010#endif
2008 return MHD_NO; 2011 return MHD_NO;
@@ -2050,13 +2053,9 @@ create_socket (int domain, int type, int protocol)
2050 if (!GetHandleInformation ((HANDLE) fd, &dwFlags)) 2053 if (!GetHandleInformation ((HANDLE) fd, &dwFlags))
2051#endif 2054#endif
2052 { 2055 {
2053#if HAVE_MESSAGES
2054#if WINDOWS 2056#if WINDOWS
2055 SetErrnoFromWinError (GetLastError ()); 2057 SetErrnoFromWinError (GetLastError ());
2056#endif 2058#endif
2057 FPRINTF(stderr, "Failed to get socket options to make socket non-inheritable: %s\n",
2058 STRERROR (errno));
2059#endif
2060 return fd; /* good luck */ 2059 return fd; /* good luck */
2061 } 2060 }
2062#if !WINDOWS 2061#if !WINDOWS
@@ -2065,18 +2064,14 @@ create_socket (int domain, int type, int protocol)
2065 flags |= FD_CLOEXEC; 2064 flags |= FD_CLOEXEC;
2066 if (0 != fcntl (fd, F_SETFD, flags)) 2065 if (0 != fcntl (fd, F_SETFD, flags))
2067#else 2066#else
2068 if (dwFlags != dwFlags | HANDLE_FLAG_INHERIT) 2067 if (dwFlags != (dwFlags | HANDLE_FLAG_INHERIT))
2069 return fd; /* already unset */ 2068 return fd; /* already unset */
2070 if (!SetHandleInformation ((HANDLE) fd, HANDLE_FLAG_INHERIT, 0)) 2069 if (!SetHandleInformation ((HANDLE) fd, HANDLE_FLAG_INHERIT, 0))
2071#endif 2070#endif
2072 { 2071 {
2073#if HAVE_MESSAGES
2074#if WINDOWS 2072#if WINDOWS
2075 SetErrnoFromWinError (GetLastError ()); 2073 SetErrnoFromWinError (GetLastError ());
2076#endif 2074#endif
2077 FPRINTF(stderr, "Failed to make socket non-inheritable: %s\n",
2078 STRERROR (errno));
2079#endif
2080 return fd; /* good luck */ 2075 return fd; /* good luck */
2081 } 2076 }
2082 return fd; 2077 return fd;
@@ -2103,7 +2098,7 @@ MHD_start_daemon_va (unsigned int options,
2103 va_list ap) 2098 va_list ap)
2104{ 2099{
2105 const int on = 1; 2100 const int on = 1;
2106 struct MHD_Daemon *retVal; 2101 struct MHD_Daemon *daemon;
2107 int socket_fd; 2102 int socket_fd;
2108 struct sockaddr_in servaddr4; 2103 struct sockaddr_in servaddr4;
2109#if HAVE_INET6 2104#if HAVE_INET6
@@ -2115,72 +2110,71 @@ MHD_start_daemon_va (unsigned int options,
2115 int res_thread_create; 2110 int res_thread_create;
2116 int use_pipe; 2111 int use_pipe;
2117 2112
2118 if ((port == 0) || (dh == NULL)) 2113 if (NULL == dh)
2119 return NULL; 2114 return NULL;
2120 retVal = malloc (sizeof (struct MHD_Daemon)); 2115 if (NULL == (daemon = malloc (sizeof (struct MHD_Daemon))))
2121 if (retVal == NULL)
2122 return NULL; 2116 return NULL;
2123 memset (retVal, 0, sizeof (struct MHD_Daemon)); 2117 memset (daemon, 0, sizeof (struct MHD_Daemon));
2124#if HTTPS_SUPPORT 2118#if HTTPS_SUPPORT
2125 if (options & MHD_USE_SSL) 2119 if (0 != (options & MHD_USE_SSL))
2126 { 2120 {
2127 gnutls_priority_init (&retVal->priority_cache, 2121 gnutls_priority_init (&daemon->priority_cache,
2128 "NORMAL", 2122 "NORMAL",
2129 NULL); 2123 NULL);
2130 } 2124 }
2131#endif 2125#endif
2132 retVal->socket_fd = -1; 2126 daemon->socket_fd = -1;
2133 retVal->options = (enum MHD_OPTION) options; 2127 daemon->options = (enum MHD_OPTION) options;
2134 retVal->port = port; 2128 daemon->port = port;
2135 retVal->apc = apc; 2129 daemon->apc = apc;
2136 retVal->apc_cls = apc_cls; 2130 daemon->apc_cls = apc_cls;
2137 retVal->default_handler = dh; 2131 daemon->default_handler = dh;
2138 retVal->default_handler_cls = dh_cls; 2132 daemon->default_handler_cls = dh_cls;
2139 retVal->max_connections = MHD_MAX_CONNECTIONS_DEFAULT; 2133 daemon->max_connections = MHD_MAX_CONNECTIONS_DEFAULT;
2140 retVal->pool_size = MHD_POOL_SIZE_DEFAULT; 2134 daemon->pool_size = MHD_POOL_SIZE_DEFAULT;
2141 retVal->unescape_callback = &MHD_http_unescape; 2135 daemon->unescape_callback = &MHD_http_unescape;
2142 retVal->connection_timeout = 0; /* no timeout */ 2136 daemon->connection_timeout = 0; /* no timeout */
2143 retVal->wpipe[0] = -1; 2137 daemon->wpipe[0] = -1;
2144 retVal->wpipe[1] = -1; 2138 daemon->wpipe[1] = -1;
2139#if HAVE_MESSAGES
2140 daemon->custom_error_log =
2141 (void (*)(void *, const char *, va_list)) &vfprintf;
2142 daemon->custom_error_log_cls = stderr;
2143#endif
2145#ifdef HAVE_LISTEN_SHUTDOWN 2144#ifdef HAVE_LISTEN_SHUTDOWN
2146 use_pipe = (0 != (retVal->options & MHD_USE_NO_LISTEN_SOCKET)); 2145 use_pipe = (0 != (daemon->options & MHD_USE_NO_LISTEN_SOCKET));
2147#else 2146#else
2148 use_pipe = 1; /* yes, must use pipe to signal shutdown */ 2147 use_pipe = 1; /* yes, must use pipe to signal shutdown */
2149#endif 2148#endif
2150 if ( (use_pipe) && 2149 if ( (use_pipe) &&
2151 (0 != PIPE (retVal->wpipe)) ) 2150 (0 != PIPE (daemon->wpipe)) )
2152 { 2151 {
2153#if HAVE_MESSAGES 2152#if HAVE_MESSAGES
2154 FPRINTF(stderr, 2153 MHD_DLOG (daemon,
2155 "Failed to create control pipe: %s\n", 2154 "Failed to create control pipe: %s\n",
2156 STRERROR (errno)); 2155 STRERROR (errno));
2157#endif 2156#endif
2158 free (retVal); 2157 free (daemon);
2159 return NULL; 2158 return NULL;
2160 } 2159 }
2161#ifndef WINDOWS 2160#ifndef WINDOWS
2162 if ( (0 == (options & MHD_USE_POLL)) && 2161 if ( (0 == (options & MHD_USE_POLL)) &&
2163 (retVal->wpipe[0] >= FD_SETSIZE) ) 2162 (daemon->wpipe[0] >= FD_SETSIZE) )
2164 { 2163 {
2165#if HAVE_MESSAGES 2164#if HAVE_MESSAGES
2166 FPRINTF(stderr, 2165 MHD_DLOG (daemon,
2167 "file descriptor for control pipe exceeds maximum value\n"); 2166 "file descriptor for control pipe exceeds maximum value\n");
2168#endif 2167#endif
2169 CLOSE (retVal->wpipe[0]); 2168 CLOSE (daemon->wpipe[0]);
2170 CLOSE (retVal->wpipe[1]); 2169 CLOSE (daemon->wpipe[1]);
2171 free (retVal); 2170 free (daemon);
2172 return NULL; 2171 return NULL;
2173 } 2172 }
2174#endif 2173#endif
2175#ifdef DAUTH_SUPPORT 2174#ifdef DAUTH_SUPPORT
2176 retVal->digest_auth_rand_size = 0; 2175 daemon->digest_auth_rand_size = 0;
2177 retVal->digest_auth_random = NULL; 2176 daemon->digest_auth_random = NULL;
2178 retVal->nonce_nc_size = 4; /* tiny */ 2177 daemon->nonce_nc_size = 4; /* tiny */
2179#endif
2180#if HAVE_MESSAGES
2181 retVal->custom_error_log =
2182 (void (*)(void *, const char *, va_list)) &vfprintf;
2183 retVal->custom_error_log_cls = stderr;
2184#endif 2178#endif
2185#if HTTPS_SUPPORT 2179#if HTTPS_SUPPORT
2186 if (0 != (options & MHD_USE_SSL)) 2180 if (0 != (options & MHD_USE_SSL))
@@ -2189,88 +2183,88 @@ MHD_start_daemon_va (unsigned int options,
2189 if (0 != pthread_mutex_lock (&MHD_gnutls_init_mutex)) 2183 if (0 != pthread_mutex_lock (&MHD_gnutls_init_mutex))
2190 { 2184 {
2191#if HAVE_MESSAGES 2185#if HAVE_MESSAGES
2192 MHD_DLOG (retVal, "Failed to acquire gnutls mutex\n"); 2186 MHD_DLOG (daemon, "Failed to acquire gnutls mutex\n");
2193#endif 2187#endif
2194 mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); 2188 mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL);
2195 } 2189 }
2196 if (0 != pthread_mutex_unlock (&MHD_gnutls_init_mutex)) 2190 if (0 != pthread_mutex_unlock (&MHD_gnutls_init_mutex))
2197 { 2191 {
2198#if HAVE_MESSAGES 2192#if HAVE_MESSAGES
2199 MHD_DLOG (retVal, "Failed to release gnutls mutex\n"); 2193 MHD_DLOG (daemon, "Failed to release gnutls mutex\n");
2200#endif 2194#endif
2201 mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL); 2195 mhd_panic (mhd_panic_cls, __FILE__, __LINE__, NULL);
2202 } 2196 }
2203 retVal->cred_type = GNUTLS_CRD_CERTIFICATE; 2197 daemon->cred_type = GNUTLS_CRD_CERTIFICATE;
2204 } 2198 }
2205#endif 2199#endif
2206 2200
2207 if (MHD_YES != parse_options_va (retVal, &servaddr, ap)) 2201 if (MHD_YES != parse_options_va (daemon, &servaddr, ap))
2208 { 2202 {
2209#if HTTPS_SUPPORT 2203#if HTTPS_SUPPORT
2210 if ( (0 != (options & MHD_USE_SSL)) && 2204 if ( (0 != (options & MHD_USE_SSL)) &&
2211 (NULL != retVal->priority_cache) ) 2205 (NULL != daemon->priority_cache) )
2212 gnutls_priority_deinit (retVal->priority_cache); 2206 gnutls_priority_deinit (daemon->priority_cache);
2213#endif 2207#endif
2214 free (retVal); 2208 free (daemon);
2215 return NULL; 2209 return NULL;
2216 } 2210 }
2217 2211
2218#ifdef DAUTH_SUPPORT 2212#ifdef DAUTH_SUPPORT
2219 if (retVal->nonce_nc_size > 0) 2213 if (daemon->nonce_nc_size > 0)
2220 { 2214 {
2221 if ( ( (size_t) (retVal->nonce_nc_size * sizeof(struct MHD_NonceNc))) / 2215 if ( ( (size_t) (daemon->nonce_nc_size * sizeof(struct MHD_NonceNc))) /
2222 sizeof(struct MHD_NonceNc) != retVal->nonce_nc_size) 2216 sizeof(struct MHD_NonceNc) != daemon->nonce_nc_size)
2223 { 2217 {
2224#if HAVE_MESSAGES 2218#if HAVE_MESSAGES
2225 MHD_DLOG (retVal, 2219 MHD_DLOG (daemon,
2226 "Specified value for NC_SIZE too large\n"); 2220 "Specified value for NC_SIZE too large\n");
2227#endif 2221#endif
2228#if HTTPS_SUPPORT 2222#if HTTPS_SUPPORT
2229 if (options & MHD_USE_SSL) 2223 if (options & MHD_USE_SSL)
2230 gnutls_priority_deinit (retVal->priority_cache); 2224 gnutls_priority_deinit (daemon->priority_cache);
2231#endif 2225#endif
2232 free (retVal); 2226 free (daemon);
2233 return NULL; 2227 return NULL;
2234 } 2228 }
2235 retVal->nnc = malloc (retVal->nonce_nc_size * sizeof(struct MHD_NonceNc)); 2229 daemon->nnc = malloc (daemon->nonce_nc_size * sizeof(struct MHD_NonceNc));
2236 if (NULL == retVal->nnc) 2230 if (NULL == daemon->nnc)
2237 { 2231 {
2238#if HAVE_MESSAGES 2232#if HAVE_MESSAGES
2239 MHD_DLOG (retVal, 2233 MHD_DLOG (daemon,
2240 "Failed to allocate memory for nonce-nc map: %s\n", 2234 "Failed to allocate memory for nonce-nc map: %s\n",
2241 STRERROR (errno)); 2235 STRERROR (errno));
2242#endif 2236#endif
2243#if HTTPS_SUPPORT 2237#if HTTPS_SUPPORT
2244 if (options & MHD_USE_SSL) 2238 if (options & MHD_USE_SSL)
2245 gnutls_priority_deinit (retVal->priority_cache); 2239 gnutls_priority_deinit (daemon->priority_cache);
2246#endif 2240#endif
2247 free (retVal); 2241 free (daemon);
2248 return NULL; 2242 return NULL;
2249 } 2243 }
2250 } 2244 }
2251 2245
2252 if (0 != pthread_mutex_init (&retVal->nnc_lock, NULL)) 2246 if (0 != pthread_mutex_init (&daemon->nnc_lock, NULL))
2253 { 2247 {
2254#if HAVE_MESSAGES 2248#if HAVE_MESSAGES
2255 MHD_DLOG (retVal, 2249 MHD_DLOG (daemon,
2256 "MHD failed to initialize nonce-nc mutex\n"); 2250 "MHD failed to initialize nonce-nc mutex\n");
2257#endif 2251#endif
2258#if HTTPS_SUPPORT 2252#if HTTPS_SUPPORT
2259 if (options & MHD_USE_SSL) 2253 if (options & MHD_USE_SSL)
2260 gnutls_priority_deinit (retVal->priority_cache); 2254 gnutls_priority_deinit (daemon->priority_cache);
2261#endif 2255#endif
2262 free (retVal->nnc); 2256 free (daemon->nnc);
2263 free (retVal); 2257 free (daemon);
2264 return NULL; 2258 return NULL;
2265 } 2259 }
2266#endif 2260#endif
2267 2261
2268 /* Thread pooling currently works only with internal select thread model */ 2262 /* Thread pooling currently works only with internal select thread model */
2269 if ( (0 == (options & MHD_USE_SELECT_INTERNALLY)) && 2263 if ( (0 == (options & MHD_USE_SELECT_INTERNALLY)) &&
2270 (retVal->worker_pool_size > 0) ) 2264 (daemon->worker_pool_size > 0) )
2271 { 2265 {
2272#if HAVE_MESSAGES 2266#if HAVE_MESSAGES
2273 MHD_DLOG (retVal, 2267 MHD_DLOG (daemon,
2274 "MHD thread pooling only works with MHD_USE_SELECT_INTERNALLY\n"); 2268 "MHD thread pooling only works with MHD_USE_SELECT_INTERNALLY\n");
2275#endif 2269#endif
2276 goto free_and_fail; 2270 goto free_and_fail;
@@ -2280,14 +2274,14 @@ MHD_start_daemon_va (unsigned int options,
2280 if (0 != (options & (MHD_USE_SELECT_INTERNALLY | MHD_USE_THREAD_PER_CONNECTION))) 2274 if (0 != (options & (MHD_USE_SELECT_INTERNALLY | MHD_USE_THREAD_PER_CONNECTION)))
2281 { 2275 {
2282#if HAVE_MESSAGES 2276#if HAVE_MESSAGES
2283 MHD_DLOG (retVal, 2277 MHD_DLOG (daemon,
2284 "Threaded operations are not supported on Symbian.\n"); 2278 "Threaded operations are not supported on Symbian.\n");
2285#endif 2279#endif
2286 goto free_and_fail; 2280 goto free_and_fail;
2287 } 2281 }
2288#endif 2282#endif
2289 if ( (-1 == retVal->socket_fd) && 2283 if ( (-1 == daemon->socket_fd) &&
2290 (0 == (retVal->options & MHD_USE_NO_LISTEN_SOCKET)) ) 2284 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) )
2291 { 2285 {
2292 /* try to open listen socket */ 2286 /* try to open listen socket */
2293 if ((options & MHD_USE_IPv6) != 0) 2287 if ((options & MHD_USE_IPv6) != 0)
@@ -2296,7 +2290,7 @@ MHD_start_daemon_va (unsigned int options,
2296#else 2290#else
2297 { 2291 {
2298#if HAVE_MESSAGES 2292#if HAVE_MESSAGES
2299 MHD_DLOG (retVal, 2293 MHD_DLOG (daemon,
2300 "AF_INET6 not supported\n"); 2294 "AF_INET6 not supported\n");
2301#endif 2295#endif
2302 goto free_and_fail; 2296 goto free_and_fail;
@@ -2304,11 +2298,11 @@ MHD_start_daemon_va (unsigned int options,
2304#endif 2298#endif
2305 else 2299 else
2306 socket_fd = create_socket (PF_INET, SOCK_STREAM, 0); 2300 socket_fd = create_socket (PF_INET, SOCK_STREAM, 0);
2307 if (socket_fd == -1) 2301 if (-1 == socket_fd)
2308 { 2302 {
2309#if HAVE_MESSAGES 2303#if HAVE_MESSAGES
2310 if ((options & MHD_USE_DEBUG) != 0) 2304 if (0 != (options & MHD_USE_DEBUG))
2311 MHD_DLOG (retVal, 2305 MHD_DLOG (daemon,
2312 "Call to socket failed: %s\n", 2306 "Call to socket failed: %s\n",
2313 STRERROR (errno)); 2307 STRERROR (errno));
2314#endif 2308#endif
@@ -2320,7 +2314,7 @@ MHD_start_daemon_va (unsigned int options,
2320 &on, sizeof (on)) < 0) && ((options & MHD_USE_DEBUG) != 0)) 2314 &on, sizeof (on)) < 0) && ((options & MHD_USE_DEBUG) != 0))
2321 { 2315 {
2322#if HAVE_MESSAGES 2316#if HAVE_MESSAGES
2323 MHD_DLOG (retVal, 2317 MHD_DLOG (daemon,
2324 "setsockopt failed: %s\n", 2318 "setsockopt failed: %s\n",
2325 STRERROR (errno)); 2319 STRERROR (errno));
2326#endif 2320#endif
@@ -2328,7 +2322,7 @@ MHD_start_daemon_va (unsigned int options,
2328 2322
2329 /* check for user supplied sockaddr */ 2323 /* check for user supplied sockaddr */
2330#if HAVE_INET6 2324#if HAVE_INET6
2331 if ((options & MHD_USE_IPv6) != 0) 2325 if (0 != (options & MHD_USE_IPv6))
2332 addrlen = sizeof (struct sockaddr_in6); 2326 addrlen = sizeof (struct sockaddr_in6);
2333 else 2327 else
2334#endif 2328#endif
@@ -2336,7 +2330,7 @@ MHD_start_daemon_va (unsigned int options,
2336 if (NULL == servaddr) 2330 if (NULL == servaddr)
2337 { 2331 {
2338#if HAVE_INET6 2332#if HAVE_INET6
2339 if ((options & MHD_USE_IPv6) != 0) 2333 if (0 != (options & MHD_USE_IPv6))
2340 { 2334 {
2341 memset (&servaddr6, 0, sizeof (struct sockaddr_in6)); 2335 memset (&servaddr6, 0, sizeof (struct sockaddr_in6));
2342 servaddr6.sin6_family = AF_INET6; 2336 servaddr6.sin6_family = AF_INET6;
@@ -2358,9 +2352,9 @@ MHD_start_daemon_va (unsigned int options,
2358 servaddr = (struct sockaddr *) &servaddr4; 2352 servaddr = (struct sockaddr *) &servaddr4;
2359 } 2353 }
2360 } 2354 }
2361 retVal->socket_fd = socket_fd; 2355 daemon->socket_fd = socket_fd;
2362 2356
2363 if ((options & MHD_USE_IPv6) != 0) 2357 if (0 != (options & MHD_USE_IPv6))
2364 { 2358 {
2365#ifdef IPPROTO_IPV6 2359#ifdef IPPROTO_IPV6
2366#ifdef IPV6_V6ONLY 2360#ifdef IPV6_V6ONLY
@@ -2382,11 +2376,11 @@ MHD_start_daemon_va (unsigned int options,
2382#endif 2376#endif
2383#endif 2377#endif
2384 } 2378 }
2385 if (BIND (socket_fd, servaddr, addrlen) == -1) 2379 if (-1 == BIND (socket_fd, servaddr, addrlen))
2386 { 2380 {
2387#if HAVE_MESSAGES 2381#if HAVE_MESSAGES
2388 if ((options & MHD_USE_DEBUG) != 0) 2382 if (0 != (options & MHD_USE_DEBUG))
2389 MHD_DLOG (retVal, 2383 MHD_DLOG (daemon,
2390 "Failed to bind to port %u: %s\n", 2384 "Failed to bind to port %u: %s\n",
2391 (unsigned int) port, 2385 (unsigned int) port,
2392 STRERROR (errno)); 2386 STRERROR (errno));
@@ -2398,8 +2392,8 @@ MHD_start_daemon_va (unsigned int options,
2398 if (LISTEN (socket_fd, 20) < 0) 2392 if (LISTEN (socket_fd, 20) < 0)
2399 { 2393 {
2400#if HAVE_MESSAGES 2394#if HAVE_MESSAGES
2401 if ((options & MHD_USE_DEBUG) != 0) 2395 if (0 != (options & MHD_USE_DEBUG))
2402 MHD_DLOG (retVal, 2396 MHD_DLOG (daemon,
2403 "Failed to listen for connections: %s\n", 2397 "Failed to listen for connections: %s\n",
2404 STRERROR (errno)); 2398 STRERROR (errno));
2405#endif 2399#endif
@@ -2409,7 +2403,7 @@ MHD_start_daemon_va (unsigned int options,
2409 } 2403 }
2410 else 2404 else
2411 { 2405 {
2412 socket_fd = retVal->socket_fd; 2406 socket_fd = daemon->socket_fd;
2413 } 2407 }
2414#ifndef WINDOWS 2408#ifndef WINDOWS
2415 if ( (socket_fd >= FD_SETSIZE) && 2409 if ( (socket_fd >= FD_SETSIZE) &&
@@ -2417,7 +2411,7 @@ MHD_start_daemon_va (unsigned int options,
2417 { 2411 {
2418#if HAVE_MESSAGES 2412#if HAVE_MESSAGES
2419 if ((options & MHD_USE_DEBUG) != 0) 2413 if ((options & MHD_USE_DEBUG) != 0)
2420 MHD_DLOG (retVal, 2414 MHD_DLOG (daemon,
2421 "Socket descriptor larger than FD_SETSIZE: %d > %d\n", 2415 "Socket descriptor larger than FD_SETSIZE: %d > %d\n",
2422 socket_fd, 2416 socket_fd,
2423 FD_SETSIZE); 2417 FD_SETSIZE);
@@ -2427,23 +2421,23 @@ MHD_start_daemon_va (unsigned int options,
2427 } 2421 }
2428#endif 2422#endif
2429 2423
2430 if (0 != pthread_mutex_init (&retVal->per_ip_connection_mutex, NULL)) 2424 if (0 != pthread_mutex_init (&daemon->per_ip_connection_mutex, NULL))
2431 { 2425 {
2432#if HAVE_MESSAGES 2426#if HAVE_MESSAGES
2433 MHD_DLOG (retVal, 2427 MHD_DLOG (daemon,
2434 "MHD failed to initialize IP connection limit mutex\n"); 2428 "MHD failed to initialize IP connection limit mutex\n");
2435#endif 2429#endif
2436 if (-1 != socket_fd) 2430 if (-1 != socket_fd)
2437 CLOSE (socket_fd); 2431 CLOSE (socket_fd);
2438 goto free_and_fail; 2432 goto free_and_fail;
2439 } 2433 }
2440 if (0 != pthread_mutex_init (&retVal->cleanup_connection_mutex, NULL)) 2434 if (0 != pthread_mutex_init (&daemon->cleanup_connection_mutex, NULL))
2441 { 2435 {
2442#if HAVE_MESSAGES 2436#if HAVE_MESSAGES
2443 MHD_DLOG (retVal, 2437 MHD_DLOG (daemon,
2444 "MHD failed to initialize IP connection limit mutex\n"); 2438 "MHD failed to initialize IP connection limit mutex\n");
2445#endif 2439#endif
2446 pthread_mutex_destroy (&retVal->cleanup_connection_mutex); 2440 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
2447 if (-1 != socket_fd) 2441 if (-1 != socket_fd)
2448 CLOSE (socket_fd); 2442 CLOSE (socket_fd);
2449 goto free_and_fail; 2443 goto free_and_fail;
@@ -2451,39 +2445,39 @@ MHD_start_daemon_va (unsigned int options,
2451 2445
2452#if HTTPS_SUPPORT 2446#if HTTPS_SUPPORT
2453 /* initialize HTTPS daemon certificate aspects & send / recv functions */ 2447 /* initialize HTTPS daemon certificate aspects & send / recv functions */
2454 if ((0 != (options & MHD_USE_SSL)) && (0 != MHD_TLS_init (retVal))) 2448 if ((0 != (options & MHD_USE_SSL)) && (0 != MHD_TLS_init (daemon)))
2455 { 2449 {
2456#if HAVE_MESSAGES 2450#if HAVE_MESSAGES
2457 MHD_DLOG (retVal, 2451 MHD_DLOG (daemon,
2458 "Failed to initialize TLS support\n"); 2452 "Failed to initialize TLS support\n");
2459#endif 2453#endif
2460 if (-1 != socket_fd) 2454 if (-1 != socket_fd)
2461 CLOSE (socket_fd); 2455 CLOSE (socket_fd);
2462 pthread_mutex_destroy (&retVal->cleanup_connection_mutex); 2456 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
2463 pthread_mutex_destroy (&retVal->per_ip_connection_mutex); 2457 pthread_mutex_destroy (&daemon->per_ip_connection_mutex);
2464 goto free_and_fail; 2458 goto free_and_fail;
2465 } 2459 }
2466#endif 2460#endif
2467 if ( ( (0 != (options & MHD_USE_THREAD_PER_CONNECTION)) || 2461 if ( ( (0 != (options & MHD_USE_THREAD_PER_CONNECTION)) ||
2468 ( (0 != (options & MHD_USE_SELECT_INTERNALLY)) && 2462 ( (0 != (options & MHD_USE_SELECT_INTERNALLY)) &&
2469 (0 == retVal->worker_pool_size)) ) && 2463 (0 == daemon->worker_pool_size)) ) &&
2470 (0 == (retVal->options & MHD_USE_NO_LISTEN_SOCKET)) && 2464 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) &&
2471 (0 != (res_thread_create = 2465 (0 != (res_thread_create =
2472 create_thread (&retVal->pid, retVal, &MHD_select_thread, retVal)))) 2466 create_thread (&daemon->pid, daemon, &MHD_select_thread, daemon))))
2473 { 2467 {
2474#if HAVE_MESSAGES 2468#if HAVE_MESSAGES
2475 MHD_DLOG (retVal, 2469 MHD_DLOG (daemon,
2476 "Failed to create listen thread: %s\n", 2470 "Failed to create listen thread: %s\n",
2477 STRERROR (res_thread_create)); 2471 STRERROR (res_thread_create));
2478#endif 2472#endif
2479 pthread_mutex_destroy (&retVal->cleanup_connection_mutex); 2473 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
2480 pthread_mutex_destroy (&retVal->per_ip_connection_mutex); 2474 pthread_mutex_destroy (&daemon->per_ip_connection_mutex);
2481 if (-1 != socket_fd) 2475 if (-1 != socket_fd)
2482 CLOSE (socket_fd); 2476 CLOSE (socket_fd);
2483 goto free_and_fail; 2477 goto free_and_fail;
2484 } 2478 }
2485 if ( (retVal->worker_pool_size > 0) && 2479 if ( (daemon->worker_pool_size > 0) &&
2486 (0 == (retVal->options & MHD_USE_NO_LISTEN_SOCKET)) ) 2480 (0 == (daemon->options & MHD_USE_NO_LISTEN_SOCKET)) )
2487 { 2481 {
2488#ifndef MINGW 2482#ifndef MINGW
2489 int sk_flags; 2483 int sk_flags;
@@ -2494,10 +2488,10 @@ MHD_start_daemon_va (unsigned int options,
2494 /* Coarse-grained count of connections per thread (note error 2488 /* Coarse-grained count of connections per thread (note error
2495 * due to integer division). Also keep track of how many 2489 * due to integer division). Also keep track of how many
2496 * connections are leftover after an equal split. */ 2490 * connections are leftover after an equal split. */
2497 unsigned int conns_per_thread = retVal->max_connections 2491 unsigned int conns_per_thread = daemon->max_connections
2498 / retVal->worker_pool_size; 2492 / daemon->worker_pool_size;
2499 unsigned int leftover_conns = retVal->max_connections 2493 unsigned int leftover_conns = daemon->max_connections
2500 % retVal->worker_pool_size; 2494 % daemon->worker_pool_size;
2501 2495
2502 i = 0; /* we need this in case fcntl or malloc fails */ 2496 i = 0; /* we need this in case fcntl or malloc fails */
2503 2497
@@ -2513,8 +2507,8 @@ MHD_start_daemon_va (unsigned int options,
2513#else 2507#else
2514 sk_flags = 1; 2508 sk_flags = 1;
2515#if HAVE_PLIBC_FD 2509#if HAVE_PLIBC_FD
2516 if (ioctlsocket (plibc_fd_get_handle (socket_fd), FIONBIO, &sk_flags) == 2510 if (SOCKET_ERROR ==
2517 SOCKET_ERROR) 2511 ioctlsocket (plibc_fd_get_handle (socket_fd), FIONBIO, &sk_flags))
2518#else 2512#else
2519 if (ioctlsocket (socket_fd, FIONBIO, &sk_flags) == SOCKET_ERROR) 2513 if (ioctlsocket (socket_fd, FIONBIO, &sk_flags) == SOCKET_ERROR)
2520#endif // PLIBC_FD 2514#endif // PLIBC_FD
@@ -2522,22 +2516,22 @@ MHD_start_daemon_va (unsigned int options,
2522#endif // MINGW 2516#endif // MINGW
2523 2517
2524 /* Allocate memory for pooled objects */ 2518 /* Allocate memory for pooled objects */
2525 retVal->worker_pool = malloc (sizeof (struct MHD_Daemon) 2519 daemon->worker_pool = malloc (sizeof (struct MHD_Daemon)
2526 * retVal->worker_pool_size); 2520 * daemon->worker_pool_size);
2527 if (NULL == retVal->worker_pool) 2521 if (NULL == daemon->worker_pool)
2528 goto thread_failed; 2522 goto thread_failed;
2529 2523
2530 /* Start the workers in the pool */ 2524 /* Start the workers in the pool */
2531 for (i = 0; i < retVal->worker_pool_size; ++i) 2525 for (i = 0; i < daemon->worker_pool_size; ++i)
2532 { 2526 {
2533 /* Create copy of the Daemon object for each worker */ 2527 /* Create copy of the Daemon object for each worker */
2534 struct MHD_Daemon *d = &retVal->worker_pool[i]; 2528 struct MHD_Daemon *d = &daemon->worker_pool[i];
2535 memcpy (d, retVal, sizeof (struct MHD_Daemon)); 2529 memcpy (d, daemon, sizeof (struct MHD_Daemon));
2536 2530
2537 /* Adjust pooling params for worker daemons; note that memcpy() 2531 /* Adjust pooling params for worker daemons; note that memcpy()
2538 has already copied MHD_USE_SELECT_INTERNALLY thread model into 2532 has already copied MHD_USE_SELECT_INTERNALLY thread model into
2539 the worker threads. */ 2533 the worker threads. */
2540 d->master = retVal; 2534 d->master = daemon;
2541 d->worker_pool_size = 0; 2535 d->worker_pool_size = 0;
2542 d->worker_pool = NULL; 2536 d->worker_pool = NULL;
2543 2537
@@ -2549,10 +2543,10 @@ MHD_start_daemon_va (unsigned int options,
2549 ++d->max_connections; 2543 ++d->max_connections;
2550 2544
2551 /* Spawn the worker thread */ 2545 /* Spawn the worker thread */
2552 if (0 != (res_thread_create = create_thread (&d->pid, retVal, &MHD_select_thread, d))) 2546 if (0 != (res_thread_create = create_thread (&d->pid, daemon, &MHD_select_thread, d)))
2553 { 2547 {
2554#if HAVE_MESSAGES 2548#if HAVE_MESSAGES
2555 MHD_DLOG (retVal, 2549 MHD_DLOG (daemon,
2556 "Failed to create pool thread: %s\n", 2550 "Failed to create pool thread: %s\n",
2557 STRERROR (res_thread_create)); 2551 STRERROR (res_thread_create));
2558#endif 2552#endif
@@ -2562,21 +2556,21 @@ MHD_start_daemon_va (unsigned int options,
2562 } 2556 }
2563 } 2557 }
2564 } 2558 }
2565 return retVal; 2559 return daemon;
2566 2560
2567thread_failed: 2561thread_failed:
2568 /* If no worker threads created, then shut down normally. Calling 2562 /* If no worker threads created, then shut down normally. Calling
2569 MHD_stop_daemon (as we do below) doesn't work here since it 2563 MHD_stop_daemon (as we do below) doesn't work here since it
2570 assumes a 0-sized thread pool means we had been in the default 2564 assumes a 0-sized thread pool means we had been in the default
2571 MHD_USE_SELECT_INTERNALLY mode. */ 2565 MHD_USE_SELECT_INTERNALLY mode. */
2572 if (i == 0) 2566 if (0 == i)
2573 { 2567 {
2574 if (-1 != socket_fd) 2568 if (-1 != socket_fd)
2575 CLOSE (socket_fd); 2569 CLOSE (socket_fd);
2576 pthread_mutex_destroy (&retVal->cleanup_connection_mutex); 2570 pthread_mutex_destroy (&daemon->cleanup_connection_mutex);
2577 pthread_mutex_destroy (&retVal->per_ip_connection_mutex); 2571 pthread_mutex_destroy (&daemon->per_ip_connection_mutex);
2578 if (NULL != retVal->worker_pool) 2572 if (NULL != daemon->worker_pool)
2579 free (retVal->worker_pool); 2573 free (daemon->worker_pool);
2580 goto free_and_fail; 2574 goto free_and_fail;
2581 } 2575 }
2582 2576
@@ -2584,22 +2578,22 @@ thread_failed:
2584 as though we had fully initialized our daemon, but 2578 as though we had fully initialized our daemon, but
2585 with a smaller number of threads than had been 2579 with a smaller number of threads than had been
2586 requested. */ 2580 requested. */
2587 retVal->worker_pool_size = i - 1; 2581 daemon->worker_pool_size = i - 1;
2588 MHD_stop_daemon (retVal); 2582 MHD_stop_daemon (daemon);
2589 return NULL; 2583 return NULL;
2590 2584
2591 free_and_fail: 2585 free_and_fail:
2592 /* clean up basic memory state in 'retVal' and return NULL to 2586 /* clean up basic memory state in 'daemon' and return NULL to
2593 indicate failure */ 2587 indicate failure */
2594#ifdef DAUTH_SUPPORT 2588#ifdef DAUTH_SUPPORT
2595 free (retVal->nnc); 2589 free (daemon->nnc);
2596 pthread_mutex_destroy (&retVal->nnc_lock); 2590 pthread_mutex_destroy (&daemon->nnc_lock);
2597#endif 2591#endif
2598#if HTTPS_SUPPORT 2592#if HTTPS_SUPPORT
2599 if (options & MHD_USE_SSL) 2593 if (0 != (options & MHD_USE_SSL))
2600 gnutls_priority_deinit (retVal->priority_cache); 2594 gnutls_priority_deinit (daemon->priority_cache);
2601#endif 2595#endif
2602 free (retVal); 2596 free (daemon);
2603 return NULL; 2597 return NULL;
2604} 2598}
2605 2599
@@ -2627,7 +2621,7 @@ close_all_connections (struct MHD_Daemon *daemon)
2627#endif 2621#endif
2628 abort(); 2622 abort();
2629 } 2623 }
2630 for (pos = daemon->connections_head; pos != NULL; pos = pos->next) 2624 for (pos = daemon->connections_head; NULL != pos; pos = pos->next)
2631 SHUTDOWN (pos->socket_fd, 2625 SHUTDOWN (pos->socket_fd,
2632 (pos->read_closed == MHD_YES) ? SHUT_WR : SHUT_RDWR); 2626 (pos->read_closed == MHD_YES) ? SHUT_WR : SHUT_RDWR);
2633 if (0 != pthread_mutex_unlock(&daemon->cleanup_connection_mutex)) 2627 if (0 != pthread_mutex_unlock(&daemon->cleanup_connection_mutex))
@@ -2684,7 +2678,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
2684 unsigned int i; 2678 unsigned int i;
2685 int rc; 2679 int rc;
2686 2680
2687 if (daemon == NULL) 2681 if (NULL == daemon)
2688 return; 2682 return;
2689 daemon->shutdown = MHD_YES; 2683 daemon->shutdown = MHD_YES;
2690 fd = daemon->socket_fd; 2684 fd = daemon->socket_fd;
@@ -2699,7 +2693,7 @@ MHD_stop_daemon (struct MHD_Daemon *daemon)
2699 daemon->worker_pool[i].socket_fd = -1; 2693 daemon->worker_pool[i].socket_fd = -1;
2700 } 2694 }
2701 } 2695 }
2702 if (daemon->wpipe[1] != -1) 2696 if (-1 != daemon->wpipe[1])
2703 { 2697 {
2704 WRITE (daemon->wpipe[1], "e", 1); 2698 WRITE (daemon->wpipe[1], "e", 1);
2705 } 2699 }
diff --git a/src/daemon/reason_phrase.c b/src/daemon/reason_phrase.c
index 9c103649..6e7f4bcc 100644
--- a/src/daemon/reason_phrase.c
+++ b/src/daemon/reason_phrase.c
@@ -151,7 +151,9 @@ static const struct MHD_Reason_Block reasons[] = {
151const char * 151const char *
152MHD_get_reason_phrase_for (unsigned int code) 152MHD_get_reason_phrase_for (unsigned int code)
153{ 153{
154 if ((code >= 100 && code < 600) && (reasons[code / 100].max > code % 100)) 154 if ( (code >= 100) &&
155 (code < 600) &&
156 (reasons[code / 100].max > (code % 100)) )
155 return reasons[code / 100].data[code % 100]; 157 return reasons[code / 100].data[code % 100];
156 return "Unknown"; 158 return "Unknown";
157} 159}
diff --git a/src/daemon/response.c b/src/daemon/response.c
index 8e5b6629..aa1e16d7 100644
--- a/src/daemon/response.c
+++ b/src/daemon/response.c
@@ -45,28 +45,26 @@ add_response_entry (struct MHD_Response *response,
45{ 45{
46 struct MHD_HTTP_Header *hdr; 46 struct MHD_HTTP_Header *hdr;
47 47
48 if ((response == NULL) || 48 if ( (NULL == response) ||
49 (header == NULL) || 49 (NULL == header) ||
50 (content == NULL) || 50 (NULL == content) ||
51 (strlen (header) == 0) || 51 (0 == strlen (header)) ||
52 (strlen (content) == 0) || 52 (0 == strlen (content)) ||
53 (NULL != strstr (header, "\t")) || 53 (NULL != strstr (header, "\t")) ||
54 (NULL != strstr (header, "\r")) || 54 (NULL != strstr (header, "\r")) ||
55 (NULL != strstr (header, "\n")) || 55 (NULL != strstr (header, "\n")) ||
56 (NULL != strstr (content, "\t")) || 56 (NULL != strstr (content, "\t")) ||
57 (NULL != strstr (content, "\r")) || (NULL != strstr (content, "\n"))) 57 (NULL != strstr (content, "\r")) ||
58 (NULL != strstr (content, "\n")) )
58 return MHD_NO; 59 return MHD_NO;
59 hdr = malloc (sizeof (struct MHD_HTTP_Header)); 60 if (NULL == (hdr = malloc (sizeof (struct MHD_HTTP_Header))))
60 if (hdr == NULL)
61 return MHD_NO; 61 return MHD_NO;
62 hdr->header = strdup (header); 62 if (NULL == (hdr->header = strdup (header)))
63 if (hdr->header == NULL)
64 { 63 {
65 free (hdr); 64 free (hdr);
66 return MHD_NO; 65 return MHD_NO;
67 } 66 }
68 hdr->value = strdup (content); 67 if (NULL == (hdr->value = strdup (content)))
69 if (hdr->value == NULL)
70 { 68 {
71 free (hdr->header); 69 free (hdr->header);
72 free (hdr); 70 free (hdr);
@@ -132,7 +130,7 @@ MHD_del_response_header (struct MHD_Response *response,
132 struct MHD_HTTP_Header *pos; 130 struct MHD_HTTP_Header *pos;
133 struct MHD_HTTP_Header *prev; 131 struct MHD_HTTP_Header *prev;
134 132
135 if ((header == NULL) || (content == NULL)) 133 if ( (NULL == header) || (NULL == content) )
136 return MHD_NO; 134 return MHD_NO;
137 prev = NULL; 135 prev = NULL;
138 pos = response->first_header; 136 pos = response->first_header;
@@ -143,7 +141,7 @@ MHD_del_response_header (struct MHD_Response *response,
143 { 141 {
144 free (pos->header); 142 free (pos->header);
145 free (pos->value); 143 free (pos->value);
146 if (prev == NULL) 144 if (NULL == prev)
147 response->first_header = pos->next; 145 response->first_header = pos->next;
148 else 146 else
149 prev->next = pos->next; 147 prev->next = pos->next;
@@ -171,15 +169,14 @@ MHD_get_response_headers (struct MHD_Response *response,
171{ 169{
172 struct MHD_HTTP_Header *pos; 170 struct MHD_HTTP_Header *pos;
173 int numHeaders = 0; 171 int numHeaders = 0;
174 pos = response->first_header; 172
175 while (pos != NULL) 173 for (pos = response->first_header; NULL != pos; pos = pos->next)
176 { 174 {
177 numHeaders++; 175 numHeaders++;
178 if ((iterator != NULL) && 176 if ((NULL != iterator) &&
179 (MHD_YES != iterator (iterator_cls, 177 (MHD_YES != iterator (iterator_cls,
180 pos->kind, pos->header, pos->value))) 178 pos->kind, pos->header, pos->value)))
181 break; 179 break;
182 pos = pos->next;
183 } 180 }
184 return numHeaders; 181 return numHeaders;
185} 182}
@@ -196,15 +193,11 @@ MHD_get_response_header (struct MHD_Response *response, const char *key)
196{ 193{
197 struct MHD_HTTP_Header *pos; 194 struct MHD_HTTP_Header *pos;
198 195
199 if (key == NULL) 196 if (NULL == key)
200 return NULL; 197 return NULL;
201 pos = response->first_header; 198 for (pos = response->first_header; NULL != pos; pos = pos->next)
202 while (pos != NULL) 199 if (0 == strcmp (key, pos->header))
203 { 200 return pos->value;
204 if (0 == strcmp (key, pos->header))
205 return pos->value;
206 pos = pos->next;
207 }
208 return NULL; 201 return NULL;
209} 202}
210 203
@@ -231,28 +224,27 @@ MHD_create_response_from_callback (uint64_t size,
231 void *crc_cls, 224 void *crc_cls,
232 MHD_ContentReaderFreeCallback crfc) 225 MHD_ContentReaderFreeCallback crfc)
233{ 226{
234 struct MHD_Response *retVal; 227 struct MHD_Response *response;
235 228
236 if ((crc == NULL) || (block_size == 0)) 229 if ((NULL == crc) || (0 == block_size))
237 return NULL; 230 return NULL;
238 retVal = malloc (sizeof (struct MHD_Response) + block_size); 231 if (NULL == (response = malloc (sizeof (struct MHD_Response) + block_size)))
239 if (retVal == NULL)
240 return NULL; 232 return NULL;
241 memset (retVal, 0, sizeof (struct MHD_Response)); 233 memset (response, 0, sizeof (struct MHD_Response));
242 retVal->fd = -1; 234 response->fd = -1;
243 retVal->data = (void *) &retVal[1]; 235 response->data = (void *) &response[1];
244 retVal->data_buffer_size = block_size; 236 response->data_buffer_size = block_size;
245 if (pthread_mutex_init (&retVal->mutex, NULL) != 0) 237 if (0 != pthread_mutex_init (&response->mutex, NULL))
246 { 238 {
247 free (retVal); 239 free (response);
248 return NULL; 240 return NULL;
249 } 241 }
250 retVal->crc = crc; 242 response->crc = crc;
251 retVal->crfc = crfc; 243 response->crfc = crfc;
252 retVal->crc_cls = crc_cls; 244 response->crc_cls = crc_cls;
253 retVal->reference_count = 1; 245 response->reference_count = 1;
254 retVal->total_size = size; 246 response->total_size = size;
255 return retVal; 247 return response;
256} 248}
257 249
258 250
@@ -274,7 +266,7 @@ file_reader (void *cls, uint64_t pos, char *buf, size_t max)
274 266
275 (void) lseek (response->fd, pos + response->fd_off, SEEK_SET); 267 (void) lseek (response->fd, pos + response->fd_off, SEEK_SET);
276 n = read (response->fd, buf, max); 268 n = read (response->fd, buf, max);
277 if (n == 0) 269 if (0 == n)
278 return MHD_CONTENT_READER_END_OF_STREAM; 270 return MHD_CONTENT_READER_END_OF_STREAM;
279 if (n < 0) 271 if (n < 0)
280 return MHD_CONTENT_READER_END_WITH_ERROR; 272 return MHD_CONTENT_READER_END_WITH_ERROR;
@@ -292,6 +284,7 @@ static void
292free_callback (void *cls) 284free_callback (void *cls)
293{ 285{
294 struct MHD_Response *response = cls; 286 struct MHD_Response *response = cls;
287
295 (void) close (response->fd); 288 (void) close (response->fd);
296 response->fd = -1; 289 response->fd = -1;
297} 290}
@@ -310,24 +303,22 @@ struct MHD_Response *MHD_create_response_from_fd_at_offset (size_t size,
310 int fd, 303 int fd,
311 off_t offset) 304 off_t offset)
312{ 305{
313 struct MHD_Response *ret; 306 struct MHD_Response *response;
314 307
315 ret = MHD_create_response_from_callback (size, 308 response = MHD_create_response_from_callback (size,
316 4 * 1024, 309 4 * 1024,
317 &file_reader, 310 &file_reader,
318 NULL, 311 NULL,
319 &free_callback); 312 &free_callback);
320 if (ret == NULL) 313 if (NULL == response)
321 return NULL; 314 return NULL;
322 ret->fd = fd; 315 response->fd = fd;
323 ret->fd_off = offset; 316 response->fd_off = offset;
324 ret->crc_cls = ret; 317 response->crc_cls = response;
325 return ret; 318 return response;
326} 319}
327 320
328 321
329
330
331/** 322/**
332 * Create a response object. The response object can be extended with 323 * Create a response object. The response object can be extended with
333 * header information and then be used any number of times. 324 * header information and then be used any number of times.
@@ -360,42 +351,40 @@ struct MHD_Response *
360MHD_create_response_from_data (size_t size, 351MHD_create_response_from_data (size_t size,
361 void *data, int must_free, int must_copy) 352 void *data, int must_free, int must_copy)
362{ 353{
363 struct MHD_Response *retVal; 354 struct MHD_Response *response;
364 void *tmp; 355 void *tmp;
365 356
366 if ((data == NULL) && (size > 0)) 357 if ((NULL == data) && (size > 0))
367 return NULL; 358 return NULL;
368 retVal = malloc (sizeof (struct MHD_Response)); 359 if (NULL == (response = malloc (sizeof (struct MHD_Response))))
369 if (retVal == NULL)
370 return NULL; 360 return NULL;
371 memset (retVal, 0, sizeof (struct MHD_Response)); 361 memset (response, 0, sizeof (struct MHD_Response));
372 retVal->fd = -1; 362 response->fd = -1;
373 if (pthread_mutex_init (&retVal->mutex, NULL) != 0) 363 if (0 != pthread_mutex_init (&response->mutex, NULL))
374 { 364 {
375 free (retVal); 365 free (response);
376 return NULL; 366 return NULL;
377 } 367 }
378 if ((must_copy) && (size > 0)) 368 if ((must_copy) && (size > 0))
379 { 369 {
380 tmp = malloc (size); 370 if (NULL == (tmp = malloc (size)))
381 if (tmp == NULL)
382 { 371 {
383 pthread_mutex_destroy (&retVal->mutex); 372 pthread_mutex_destroy (&response->mutex);
384 free (retVal); 373 free (response);
385 return NULL; 374 return NULL;
386 } 375 }
387 memcpy (tmp, data, size); 376 memcpy (tmp, data, size);
388 must_free = MHD_YES; 377 must_free = MHD_YES;
389 data = tmp; 378 data = tmp;
390 } 379 }
391 retVal->crc = NULL; 380 response->crc = NULL;
392 retVal->crfc = must_free ? &free : NULL; 381 response->crfc = must_free ? &free : NULL;
393 retVal->crc_cls = must_free ? data : NULL; 382 response->crc_cls = must_free ? data : NULL;
394 retVal->reference_count = 1; 383 response->reference_count = 1;
395 retVal->total_size = size; 384 response->total_size = size;
396 retVal->data = data; 385 response->data = data;
397 retVal->data_size = size; 386 response->data_size = size;
398 return retVal; 387 return response;
399} 388}
400 389
401 390
@@ -431,7 +420,7 @@ MHD_destroy_response (struct MHD_Response *response)
431{ 420{
432 struct MHD_HTTP_Header *pos; 421 struct MHD_HTTP_Header *pos;
433 422
434 if (response == NULL) 423 if (NULL == response)
435 return; 424 return;
436 pthread_mutex_lock (&response->mutex); 425 pthread_mutex_lock (&response->mutex);
437 if (0 != --(response->reference_count)) 426 if (0 != --(response->reference_count))
@@ -443,7 +432,7 @@ MHD_destroy_response (struct MHD_Response *response)
443 pthread_mutex_destroy (&response->mutex); 432 pthread_mutex_destroy (&response->mutex);
444 if (response->crfc != NULL) 433 if (response->crfc != NULL)
445 response->crfc (response->crc_cls); 434 response->crfc (response->crc_cls);
446 while (response->first_header != NULL) 435 while (NULL != response->first_header)
447 { 436 {
448 pos = response->first_header; 437 pos = response->first_header;
449 response->first_header = pos->next; 438 response->first_header = pos->next;