diff options
Diffstat (limited to 'src/microhttpd/connection.c')
-rw-r--r-- | src/microhttpd/connection.c | 108 |
1 files changed, 61 insertions, 47 deletions
diff --git a/src/microhttpd/connection.c b/src/microhttpd/connection.c index b304ca25..4bbe2683 100644 --- a/src/microhttpd/connection.c +++ b/src/microhttpd/connection.c | |||
@@ -1669,11 +1669,13 @@ build_header_response (struct MHD_Connection *connection) | |||
1669 | * @param connection the connection | 1669 | * @param connection the connection |
1670 | * @param status_code the response code to send (400, 413 or 414) | 1670 | * @param status_code the response code to send (400, 413 or 414) |
1671 | * @param message the error message to send | 1671 | * @param message the error message to send |
1672 | * @param message_len the length of the @a message | ||
1672 | */ | 1673 | */ |
1673 | static void | 1674 | static void |
1674 | transmit_error_response (struct MHD_Connection *connection, | 1675 | transmit_error_response_len (struct MHD_Connection *connection, |
1675 | unsigned int status_code, | 1676 | unsigned int status_code, |
1676 | const char *message) | 1677 | const char *message, |
1678 | size_t message_len) | ||
1677 | { | 1679 | { |
1678 | struct MHD_Response *response; | 1680 | struct MHD_Response *response; |
1679 | enum MHD_Result iret; | 1681 | enum MHD_Result iret; |
@@ -1702,7 +1704,7 @@ transmit_error_response (struct MHD_Connection *connection, | |||
1702 | MHD_destroy_response (connection->response); | 1704 | MHD_destroy_response (connection->response); |
1703 | connection->response = NULL; | 1705 | connection->response = NULL; |
1704 | } | 1706 | } |
1705 | response = MHD_create_response_from_buffer (strlen (message), | 1707 | response = MHD_create_response_from_buffer (message_len, |
1706 | (void *) message, | 1708 | (void *) message, |
1707 | MHD_RESPMEM_PERSISTENT); | 1709 | MHD_RESPMEM_PERSISTENT); |
1708 | if (NULL == response) | 1710 | if (NULL == response) |
@@ -1741,6 +1743,12 @@ transmit_error_response (struct MHD_Connection *connection, | |||
1741 | 1743 | ||
1742 | 1744 | ||
1743 | /** | 1745 | /** |
1746 | * Transmit static string as error response | ||
1747 | */ | ||
1748 | #define transmit_error_response_static(c, code, msg) \ | ||
1749 | transmit_error_response_len (c, code, msg, MHD_STATICSTR_LEN_(msg)) | ||
1750 | |||
1751 | /** | ||
1744 | * Update the 'event_loop_info' field of this connection based on the state | 1752 | * Update the 'event_loop_info' field of this connection based on the state |
1745 | * that the connection is now in. May also close the connection or | 1753 | * that the connection is now in. May also close the connection or |
1746 | * perform other updates to the connection if needed to prepare for | 1754 | * perform other updates to the connection if needed to prepare for |
@@ -1791,11 +1799,14 @@ MHD_connection_update_event_loop_info (struct MHD_Connection *connection) | |||
1791 | if ( (connection->read_buffer_offset == connection->read_buffer_size) && | 1799 | if ( (connection->read_buffer_offset == connection->read_buffer_size) && |
1792 | (! try_grow_read_buffer (connection, true)) ) | 1800 | (! try_grow_read_buffer (connection, true)) ) |
1793 | { | 1801 | { |
1794 | transmit_error_response (connection, | 1802 | if (connection->url != NULL) |
1795 | (connection->url != NULL) | 1803 | transmit_error_response_static (connection, |
1796 | ? MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE | 1804 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
1797 | : MHD_HTTP_URI_TOO_LONG, | 1805 | REQUEST_TOO_BIG); |
1798 | REQUEST_TOO_BIG); | 1806 | else |
1807 | transmit_error_response_static (connection, | ||
1808 | MHD_HTTP_URI_TOO_LONG, | ||
1809 | REQUEST_TOO_BIG); | ||
1799 | continue; | 1810 | continue; |
1800 | } | 1811 | } |
1801 | if (! connection->read_closed) | 1812 | if (! connection->read_closed) |
@@ -1831,9 +1842,9 @@ MHD_connection_update_event_loop_info (struct MHD_Connection *connection) | |||
1831 | on the connection (if a timeout is even | 1842 | on the connection (if a timeout is even |
1832 | set!). | 1843 | set!). |
1833 | Solution: we kill the connection with an error */ | 1844 | Solution: we kill the connection with an error */ |
1834 | transmit_error_response (connection, | 1845 | transmit_error_response_static (connection, |
1835 | MHD_HTTP_INTERNAL_SERVER_ERROR, | 1846 | MHD_HTTP_INTERNAL_SERVER_ERROR, |
1836 | INTERNAL_ERROR); | 1847 | INTERNAL_ERROR); |
1837 | continue; | 1848 | continue; |
1838 | } | 1849 | } |
1839 | } | 1850 | } |
@@ -1939,11 +1950,14 @@ get_next_header_line (struct MHD_Connection *connection, | |||
1939 | if ( (connection->read_buffer_offset == connection->read_buffer_size) && | 1950 | if ( (connection->read_buffer_offset == connection->read_buffer_size) && |
1940 | (! try_grow_read_buffer (connection, true)) ) | 1951 | (! try_grow_read_buffer (connection, true)) ) |
1941 | { | 1952 | { |
1942 | transmit_error_response (connection, | 1953 | if (NULL != connection->url) |
1943 | (NULL != connection->url) | 1954 | transmit_error_response_static (connection, |
1944 | ? MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE | 1955 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
1945 | : MHD_HTTP_URI_TOO_LONG, | 1956 | REQUEST_TOO_BIG); |
1946 | REQUEST_TOO_BIG); | 1957 | else |
1958 | transmit_error_response_static (connection, | ||
1959 | MHD_HTTP_URI_TOO_LONG, | ||
1960 | REQUEST_TOO_BIG); | ||
1947 | } | 1961 | } |
1948 | if (line_len) | 1962 | if (line_len) |
1949 | *line_len = 0; | 1963 | *line_len = 0; |
@@ -1997,9 +2011,9 @@ connection_add_header (struct MHD_Connection *connection, | |||
1997 | MHD_DLOG (connection->daemon, | 2011 | MHD_DLOG (connection->daemon, |
1998 | _ ("Not enough memory in pool to allocate header record!\n")); | 2012 | _ ("Not enough memory in pool to allocate header record!\n")); |
1999 | #endif | 2013 | #endif |
2000 | transmit_error_response (connection, | 2014 | transmit_error_response_static (connection, |
2001 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, | 2015 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
2002 | REQUEST_TOO_BIG); | 2016 | REQUEST_TOO_BIG); |
2003 | return MHD_NO; | 2017 | return MHD_NO; |
2004 | } | 2018 | } |
2005 | return MHD_YES; | 2019 | return MHD_YES; |
@@ -2044,9 +2058,9 @@ parse_cookie_header (struct MHD_Connection *connection) | |||
2044 | MHD_DLOG (connection->daemon, | 2058 | MHD_DLOG (connection->daemon, |
2045 | _ ("Not enough memory in pool to parse cookies!\n")); | 2059 | _ ("Not enough memory in pool to parse cookies!\n")); |
2046 | #endif | 2060 | #endif |
2047 | transmit_error_response (connection, | 2061 | transmit_error_response_static (connection, |
2048 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, | 2062 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
2049 | REQUEST_TOO_BIG); | 2063 | REQUEST_TOO_BIG); |
2050 | return MHD_NO; | 2064 | return MHD_NO; |
2051 | } | 2065 | } |
2052 | memcpy (cpy, | 2066 | memcpy (cpy, |
@@ -2157,9 +2171,9 @@ parse_http_version (struct MHD_Connection *connection, | |||
2157 | ((h[7] < '0') || (h[7] > '9'))) | 2171 | ((h[7] < '0') || (h[7] > '9'))) |
2158 | { | 2172 | { |
2159 | connection->http_ver = MHD_HTTP_VER_INVALID; | 2173 | connection->http_ver = MHD_HTTP_VER_INVALID; |
2160 | transmit_error_response (connection, | 2174 | transmit_error_response_static (connection, |
2161 | MHD_HTTP_BAD_REQUEST, | 2175 | MHD_HTTP_BAD_REQUEST, |
2162 | REQUEST_MALFORMED); | 2176 | REQUEST_MALFORMED); |
2163 | return MHD_NO; | 2177 | return MHD_NO; |
2164 | } | 2178 | } |
2165 | if (1 == h[5] - '0') | 2179 | if (1 == h[5] - '0') |
@@ -2179,16 +2193,16 @@ parse_http_version (struct MHD_Connection *connection, | |||
2179 | { | 2193 | { |
2180 | /* Too old major version */ | 2194 | /* Too old major version */ |
2181 | connection->http_ver = MHD_HTTP_VER_TOO_OLD; | 2195 | connection->http_ver = MHD_HTTP_VER_TOO_OLD; |
2182 | transmit_error_response (connection, | 2196 | transmit_error_response_static (connection, |
2183 | MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, | 2197 | MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, |
2184 | REQ_HTTP_VER_IS_TOO_OLD); | 2198 | REQ_HTTP_VER_IS_TOO_OLD); |
2185 | return MHD_NO; | 2199 | return MHD_NO; |
2186 | } | 2200 | } |
2187 | 2201 | ||
2188 | connection->http_ver = MHD_HTTP_VER_FUTURE; | 2202 | connection->http_ver = MHD_HTTP_VER_FUTURE; |
2189 | transmit_error_response (connection, | 2203 | transmit_error_response_static (connection, |
2190 | MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, | 2204 | MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, |
2191 | REQ_HTTP_VER_IS_NOT_SUPPORTED); | 2205 | REQ_HTTP_VER_IS_NOT_SUPPORTED); |
2192 | return MHD_NO; | 2206 | return MHD_NO; |
2193 | } | 2207 | } |
2194 | 2208 | ||
@@ -2731,9 +2745,9 @@ process_broken_line (struct MHD_Connection *connection, | |||
2731 | last_len + tmp_len + 1); | 2745 | last_len + tmp_len + 1); |
2732 | if (NULL == last) | 2746 | if (NULL == last) |
2733 | { | 2747 | { |
2734 | transmit_error_response (connection, | 2748 | transmit_error_response_static (connection, |
2735 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, | 2749 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
2736 | REQUEST_TOO_BIG); | 2750 | REQUEST_TOO_BIG); |
2737 | return MHD_NO; | 2751 | return MHD_NO; |
2738 | } | 2752 | } |
2739 | memcpy (&last[last_len], | 2753 | memcpy (&last[last_len], |
@@ -2752,9 +2766,9 @@ process_broken_line (struct MHD_Connection *connection, | |||
2752 | strlen (connection->colon), | 2766 | strlen (connection->colon), |
2753 | kind)) | 2767 | kind)) |
2754 | { | 2768 | { |
2755 | transmit_error_response (connection, | 2769 | transmit_error_response_static (connection, |
2756 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, | 2770 | MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, |
2757 | REQUEST_TOO_BIG); | 2771 | REQUEST_TOO_BIG); |
2758 | return MHD_NO; | 2772 | return MHD_NO; |
2759 | } | 2773 | } |
2760 | /* we still have the current line to deal with... */ | 2774 | /* we still have the current line to deal with... */ |
@@ -2763,9 +2777,9 @@ process_broken_line (struct MHD_Connection *connection, | |||
2763 | if (MHD_NO == process_header_line (connection, | 2777 | if (MHD_NO == process_header_line (connection, |
2764 | line)) | 2778 | line)) |
2765 | { | 2779 | { |
2766 | transmit_error_response (connection, | 2780 | transmit_error_response_static (connection, |
2767 | MHD_HTTP_BAD_REQUEST, | 2781 | MHD_HTTP_BAD_REQUEST, |
2768 | REQUEST_MALFORMED); | 2782 | REQUEST_MALFORMED); |
2769 | return MHD_NO; | 2783 | return MHD_NO; |
2770 | } | 2784 | } |
2771 | } | 2785 | } |
@@ -3542,9 +3556,9 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) | |||
3542 | if (MHD_NO == process_header_line (connection, | 3556 | if (MHD_NO == process_header_line (connection, |
3543 | line)) | 3557 | line)) |
3544 | { | 3558 | { |
3545 | transmit_error_response (connection, | 3559 | transmit_error_response_static (connection, |
3546 | MHD_HTTP_BAD_REQUEST, | 3560 | MHD_HTTP_BAD_REQUEST, |
3547 | REQUEST_MALFORMED); | 3561 | REQUEST_MALFORMED); |
3548 | break; | 3562 | break; |
3549 | } | 3563 | } |
3550 | connection->state = MHD_CONNECTION_HEADER_PART_RECEIVED; | 3564 | connection->state = MHD_CONNECTION_HEADER_PART_RECEIVED; |
@@ -3665,9 +3679,9 @@ MHD_connection_handle_idle (struct MHD_Connection *connection) | |||
3665 | if (MHD_NO == process_header_line (connection, | 3679 | if (MHD_NO == process_header_line (connection, |
3666 | line)) | 3680 | line)) |
3667 | { | 3681 | { |
3668 | transmit_error_response (connection, | 3682 | transmit_error_response_static (connection, |
3669 | MHD_HTTP_BAD_REQUEST, | 3683 | MHD_HTTP_BAD_REQUEST, |
3670 | REQUEST_MALFORMED); | 3684 | REQUEST_MALFORMED); |
3671 | break; | 3685 | break; |
3672 | } | 3686 | } |
3673 | connection->state = MHD_CONNECTION_FOOTER_PART_RECEIVED; | 3687 | connection->state = MHD_CONNECTION_FOOTER_PART_RECEIVED; |