diff options
author | Christian Grothoff <christian@grothoff.org> | 2008-09-08 06:32:16 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2008-09-08 06:32:16 +0000 |
commit | 4f5e8029080c325197950cbea29ac24fd074b90f (patch) | |
tree | 3a08db55b79a67e3dbbc94cb824cd75acc50b6a5 /src/daemon/connection.c | |
parent | dc29c2914479a8fa2f423c7abf81890c5685cb2c (diff) | |
download | libmicrohttpd-4f5e8029080c325197950cbea29ac24fd074b90f.tar.gz libmicrohttpd-4f5e8029080c325197950cbea29ac24fd074b90f.zip |
more code cleanup
Diffstat (limited to 'src/daemon/connection.c')
-rw-r--r-- | src/daemon/connection.c | 114 |
1 files changed, 51 insertions, 63 deletions
diff --git a/src/daemon/connection.c b/src/daemon/connection.c index dd75771d..8477a9d0 100644 --- a/src/daemon/connection.c +++ b/src/daemon/connection.c | |||
@@ -410,7 +410,8 @@ try_ready_chunked_body (struct MHD_Connection *connection) | |||
410 | } | 410 | } |
411 | if (ret > 0xFFFFFF) | 411 | if (ret > 0xFFFFFF) |
412 | ret = 0xFFFFFF; | 412 | ret = 0xFFFFFF; |
413 | cblen = snprintf (cbuf, sizeof (cbuf), "%X\r\n", ret); | 413 | SPRINTF (cbuf, "%X\r\n", ret); |
414 | cblen = strlen(cbuf); | ||
414 | EXTRA_CHECK (cblen <= sizeof (cbuf)); | 415 | EXTRA_CHECK (cblen <= sizeof (cbuf)); |
415 | memcpy (&connection->write_buffer[sizeof (cbuf) - cblen], cbuf, cblen); | 416 | memcpy (&connection->write_buffer[sizeof (cbuf) - cblen], cbuf, cblen); |
416 | memcpy (&connection->write_buffer[sizeof (cbuf) + ret], "\r\n", 2); | 417 | memcpy (&connection->write_buffer[sizeof (cbuf) + ret], "\r\n", 2); |
@@ -458,10 +459,9 @@ add_extra_headers (struct MHD_Connection *connection) | |||
458 | else if (NULL == MHD_get_response_header (connection->response, | 459 | else if (NULL == MHD_get_response_header (connection->response, |
459 | MHD_HTTP_HEADER_CONTENT_LENGTH)) | 460 | MHD_HTTP_HEADER_CONTENT_LENGTH)) |
460 | { | 461 | { |
461 | _REAL_SNPRINTF (buf, | 462 | SPRINTF (buf, |
462 | 128, | 463 | "%llu", |
463 | "%llu", | 464 | (unsigned long long) connection->response->total_size); |
464 | (unsigned long long) connection->response->total_size); | ||
465 | MHD_add_response_header (connection->response, | 465 | MHD_add_response_header (connection->response, |
466 | MHD_HTTP_HEADER_CONTENT_LENGTH, buf); | 466 | MHD_HTTP_HEADER_CONTENT_LENGTH, buf); |
467 | } | 467 | } |
@@ -469,11 +469,12 @@ add_extra_headers (struct MHD_Connection *connection) | |||
469 | 469 | ||
470 | /** | 470 | /** |
471 | * Produce HTTP "Date:" header. | 471 | * Produce HTTP "Date:" header. |
472 | * @param date where to write the header | 472 | * |
473 | * @param max maximum number of characters to write | 473 | * @param date where to write the header, with |
474 | * at least 128 bytes available space. | ||
474 | */ | 475 | */ |
475 | static void | 476 | static void |
476 | get_date_string (char *date, unsigned int max) | 477 | get_date_string (char *date) |
477 | { | 478 | { |
478 | static const char *days[] = | 479 | static const char *days[] = |
479 | { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; | 480 | { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; |
@@ -486,17 +487,17 @@ get_date_string (char *date, unsigned int max) | |||
486 | 487 | ||
487 | time (&t); | 488 | time (&t); |
488 | gmtime_r (&t, &now); | 489 | gmtime_r (&t, &now); |
489 | snprintf (date, | 490 | SPRINTF (date, |
490 | max - 1, | 491 | "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", |
491 | "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n", | 492 | days[now.tm_wday % 7], |
492 | days[now.tm_wday % 7], | 493 | now.tm_mday, |
493 | now.tm_mday, | 494 | mons[now.tm_mon % 12], |
494 | mons[now.tm_mon % 12], | 495 | 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec); |
495 | 1900 + now.tm_year, now.tm_hour, now.tm_min, now.tm_sec); | ||
496 | } | 496 | } |
497 | 497 | ||
498 | /** | 498 | /** |
499 | * try growing the read buffer | 499 | * Try growing the read buffer |
500 | * | ||
500 | * @return MHD_YES on success, MHD_NO on failure | 501 | * @return MHD_YES on success, MHD_NO on failure |
501 | */ | 502 | */ |
502 | static int | 503 | static int |
@@ -530,7 +531,7 @@ build_header_response (struct MHD_Connection *connection) | |||
530 | size_t size; | 531 | size_t size; |
531 | size_t off; | 532 | size_t off; |
532 | struct MHD_HTTP_Header *pos; | 533 | struct MHD_HTTP_Header *pos; |
533 | char code[128]; | 534 | char code[256]; |
534 | char date[128]; | 535 | char date[128]; |
535 | char *data; | 536 | char *data; |
536 | enum MHD_ValueKind kind; | 537 | enum MHD_ValueKind kind; |
@@ -540,15 +541,18 @@ build_header_response (struct MHD_Connection *connection) | |||
540 | { | 541 | { |
541 | add_extra_headers (connection); | 542 | add_extra_headers (connection); |
542 | reason_phrase = MHD_get_reason_phrase_for (connection->responseCode); | 543 | reason_phrase = MHD_get_reason_phrase_for (connection->responseCode); |
543 | _REAL_SNPRINTF (code, 128, "%s %u %s\r\n", MHD_HTTP_VERSION_1_1, | 544 | SPRINTF (code, |
544 | connection->responseCode, reason_phrase); | 545 | "%s %u %s\r\n", |
546 | MHD_HTTP_VERSION_1_1, | ||
547 | connection->responseCode, | ||
548 | reason_phrase); | ||
545 | off = strlen (code); | 549 | off = strlen (code); |
546 | /* estimate size */ | 550 | /* estimate size */ |
547 | size = off + 2; /* extra \r\n at the end */ | 551 | size = off + 2; /* extra \r\n at the end */ |
548 | kind = MHD_HEADER_KIND; | 552 | kind = MHD_HEADER_KIND; |
549 | if (NULL == MHD_get_response_header (connection->response, | 553 | if (NULL == MHD_get_response_header (connection->response, |
550 | MHD_HTTP_HEADER_DATE)) | 554 | MHD_HTTP_HEADER_DATE)) |
551 | get_date_string (date, sizeof (date)); | 555 | get_date_string (date); |
552 | else | 556 | else |
553 | date[0] = '\0'; | 557 | date[0] = '\0'; |
554 | size += strlen (date); | 558 | size += strlen (date); |
@@ -1138,9 +1142,9 @@ call_connection_handler (struct MHD_Connection *connection) | |||
1138 | { | 1142 | { |
1139 | buffer_head[i] = '\0'; | 1143 | buffer_head[i] = '\0'; |
1140 | malformed = | 1144 | malformed = |
1141 | (1 != sscanf (buffer_head, "%X", | 1145 | (1 != SSCANF (buffer_head, "%X", |
1142 | &connection->current_chunk_size)) && | 1146 | &connection->current_chunk_size)) && |
1143 | (1 != sscanf (buffer_head, "%x", | 1147 | (1 != SSCANF (buffer_head, "%x", |
1144 | &connection->current_chunk_size)); | 1148 | &connection->current_chunk_size)); |
1145 | } | 1149 | } |
1146 | if (malformed) | 1150 | if (malformed) |
@@ -1234,8 +1238,9 @@ do_read (struct MHD_Connection *connection) | |||
1234 | if (connection->read_buffer_size == connection->read_buffer_offset) | 1238 | if (connection->read_buffer_size == connection->read_buffer_offset) |
1235 | return MHD_NO; | 1239 | return MHD_NO; |
1236 | 1240 | ||
1237 | bytes_read = connection->recv_cls (connection); | 1241 | bytes_read = connection->recv_cls (connection, |
1238 | 1242 | &connection->read_buffer[connection->read_buffer_offset], | |
1243 | connection->read_buffer_size - connection->read_buffer_offset); | ||
1239 | if (bytes_read < 0) | 1244 | if (bytes_read < 0) |
1240 | { | 1245 | { |
1241 | if (errno == EINTR) | 1246 | if (errno == EINTR) |
@@ -1270,7 +1275,11 @@ do_write (struct MHD_Connection *connection) | |||
1270 | { | 1275 | { |
1271 | int ret; | 1276 | int ret; |
1272 | 1277 | ||
1273 | ret = connection->send_cls (connection); | 1278 | ret = connection->send_cls (connection, |
1279 | &connection->write_buffer | ||
1280 | [connection->write_buffer_send_offset], | ||
1281 | connection->write_buffer_append_offset | ||
1282 | - connection->write_buffer_send_offset); | ||
1274 | 1283 | ||
1275 | if (ret < 0) | 1284 | if (ret < 0) |
1276 | { | 1285 | { |
@@ -1284,7 +1293,7 @@ do_write (struct MHD_Connection *connection) | |||
1284 | return MHD_YES; | 1293 | return MHD_YES; |
1285 | } | 1294 | } |
1286 | #if DEBUG_SEND_DATA | 1295 | #if DEBUG_SEND_DATA |
1287 | fprintf (stderr, | 1296 | FPRINTF (stderr, |
1288 | "Sent response: `%.*s'\n", | 1297 | "Sent response: `%.*s'\n", |
1289 | ret, | 1298 | ret, |
1290 | &connection->write_buffer[connection->write_buffer_send_offset]); | 1299 | &connection->write_buffer[connection->write_buffer_send_offset]); |
@@ -1293,15 +1302,6 @@ do_write (struct MHD_Connection *connection) | |||
1293 | return MHD_YES; | 1302 | return MHD_YES; |
1294 | } | 1303 | } |
1295 | 1304 | ||
1296 | static ssize_t | ||
1297 | MHD_con_read (struct MHD_Connection *connection) | ||
1298 | { | ||
1299 | return RECV (connection->socket_fd, | ||
1300 | &connection->read_buffer[connection->read_buffer_offset], | ||
1301 | connection->read_buffer_size - | ||
1302 | connection->read_buffer_offset, MSG_NOSIGNAL); | ||
1303 | } | ||
1304 | |||
1305 | /** | 1305 | /** |
1306 | * Check if we are done sending the write-buffer. | 1306 | * Check if we are done sending the write-buffer. |
1307 | * If so, transition into "next_state". | 1307 | * If so, transition into "next_state". |
@@ -1462,7 +1462,7 @@ parse_connection_headers (struct MHD_Connection *connection) | |||
1462 | MHD_HTTP_HEADER_CONTENT_LENGTH); | 1462 | MHD_HTTP_HEADER_CONTENT_LENGTH); |
1463 | if (clen != NULL) | 1463 | if (clen != NULL) |
1464 | { | 1464 | { |
1465 | if (1 != sscanf (clen, "%llu", &cval)) | 1465 | if (1 != SSCANF (clen, "%llu", &cval)) |
1466 | { | 1466 | { |
1467 | #if HAVE_MESSAGES | 1467 | #if HAVE_MESSAGES |
1468 | MHD_DLOG (connection->daemon, | 1468 | MHD_DLOG (connection->daemon, |
@@ -1553,16 +1553,6 @@ MHD_connection_handle_read (struct MHD_Connection *connection) | |||
1553 | return MHD_YES; | 1553 | return MHD_YES; |
1554 | } | 1554 | } |
1555 | 1555 | ||
1556 | static ssize_t | ||
1557 | MHD_con_write (struct MHD_Connection *connection) | ||
1558 | { | ||
1559 | return SEND (connection->socket_fd, | ||
1560 | &connection->write_buffer[connection-> | ||
1561 | write_buffer_send_offset], | ||
1562 | connection->write_buffer_append_offset - | ||
1563 | connection->write_buffer_send_offset, MSG_NOSIGNAL); | ||
1564 | } | ||
1565 | |||
1566 | /** | 1556 | /** |
1567 | * This function was created to handle writes to sockets when it has | 1557 | * This function was created to handle writes to sockets when it has |
1568 | * been determined that the socket can be written to. All | 1558 | * been determined that the socket can be written to. All |
@@ -1595,12 +1585,11 @@ MHD_connection_handle_write (struct MHD_Connection *connection) | |||
1595 | case MHD_CONNECTION_HEADERS_PROCESSED: | 1585 | case MHD_CONNECTION_HEADERS_PROCESSED: |
1596 | break; | 1586 | break; |
1597 | case MHD_CONNECTION_CONTINUE_SENDING: | 1587 | case MHD_CONNECTION_CONTINUE_SENDING: |
1598 | ret = SEND (connection->socket_fd, | 1588 | ret = connection->send_cls (connection, |
1599 | &HTTP_100_CONTINUE | 1589 | &HTTP_100_CONTINUE |
1600 | [connection->continue_message_write_offset], | 1590 | [connection->continue_message_write_offset], |
1601 | strlen (HTTP_100_CONTINUE) - | 1591 | strlen (HTTP_100_CONTINUE) - |
1602 | connection->continue_message_write_offset, | 1592 | connection->continue_message_write_offset); |
1603 | MSG_NOSIGNAL); | ||
1604 | if (ret < 0) | 1593 | if (ret < 0) |
1605 | { | 1594 | { |
1606 | if (errno == EINTR) | 1595 | if (errno == EINTR) |
@@ -1613,7 +1602,7 @@ MHD_connection_handle_write (struct MHD_Connection *connection) | |||
1613 | return MHD_NO; | 1602 | return MHD_NO; |
1614 | } | 1603 | } |
1615 | #if DEBUG_SEND_DATA | 1604 | #if DEBUG_SEND_DATA |
1616 | fprintf (stderr, | 1605 | FPRINTF (stderr, |
1617 | "Sent 100 continue response: `%.*s'\n", | 1606 | "Sent 100 continue response: `%.*s'\n", |
1618 | ret, | 1607 | ret, |
1619 | &HTTP_100_CONTINUE | 1608 | &HTTP_100_CONTINUE |
@@ -1661,17 +1650,17 @@ MHD_connection_handle_write (struct MHD_Connection *connection) | |||
1661 | else | 1650 | else |
1662 | #endif | 1651 | #endif |
1663 | { | 1652 | { |
1664 | ret = SEND (connection->socket_fd, | 1653 | ret = connection->send_cls (connection, |
1665 | &response->data[connection-> | 1654 | &response->data[connection-> |
1666 | response_write_position - | 1655 | response_write_position - |
1667 | response->data_start], | 1656 | response->data_start], |
1668 | response->data_size - | 1657 | response->data_size - |
1669 | (connection->response_write_position - | 1658 | (connection->response_write_position - |
1670 | response->data_start), MSG_NOSIGNAL); | 1659 | response->data_start)); |
1671 | } | 1660 | } |
1672 | #if DEBUG_SEND_DATA | 1661 | #if DEBUG_SEND_DATA |
1673 | if (ret > 0) | 1662 | if (ret > 0) |
1674 | fprintf (stderr, | 1663 | FPRINTF (stderr, |
1675 | "Sent DATA response: `%.*s'\n", | 1664 | "Sent DATA response: `%.*s'\n", |
1676 | ret, | 1665 | ret, |
1677 | &response->data[connection->response_write_position - | 1666 | &response->data[connection->response_write_position - |
@@ -1748,6 +1737,7 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) | |||
1748 | unsigned int timeout; | 1737 | unsigned int timeout; |
1749 | const char *end; | 1738 | const char *end; |
1750 | char *line; | 1739 | char *line; |
1740 | |||
1751 | while (1) | 1741 | while (1) |
1752 | { | 1742 | { |
1753 | #if DEBUG_STATES | 1743 | #if DEBUG_STATES |
@@ -2072,8 +2062,6 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) | |||
2072 | void | 2062 | void |
2073 | MHD_set_http_calbacks (struct MHD_Connection *connection) | 2063 | MHD_set_http_calbacks (struct MHD_Connection *connection) |
2074 | { | 2064 | { |
2075 | connection->recv_cls = &MHD_con_read; | ||
2076 | connection->send_cls = &MHD_con_write; | ||
2077 | connection->read_handler = &MHD_connection_handle_read; | 2065 | connection->read_handler = &MHD_connection_handle_read; |
2078 | connection->write_handler = &MHD_connection_handle_write; | 2066 | connection->write_handler = &MHD_connection_handle_write; |
2079 | connection->idle_handler = &MHD_connection_handle_idle; | 2067 | connection->idle_handler = &MHD_connection_handle_idle; |