30 #ifdef MHD_LINUX_SOLARIS_SENDFILE 31 #include <sys/sendfile.h> 33 #if defined(HAVE_FREEBSD_SENDFILE) || defined(HAVE_DARWIN_SENDFILE) 34 #include <sys/types.h> 35 #include <sys/socket.h> 43 #define MHD_SENFILE_CHUNK_ (0x20000) 48 #define MHD_SENFILE_CHUNK_THR_P_C_ (0x200000) 59 #define REQUEST_TOO_BIG \ 60 "<html><head><title>Request too big</title></head><body>Your HTTP header was too big for the memory constraints of this webserver.</body></html>" 62 #define REQUEST_TOO_BIG "" 73 #define REQUEST_LACKS_HOST \ 74 "<html><head><title>"Host:" header required</title></head><body>In HTTP 1.1, requests must include a "Host:" header, and your HTTP 1.1 request lacked such a header.</body></html>" 76 #define REQUEST_LACKS_HOST "" 87 #define REQUEST_MALFORMED \ 88 "<html><head><title>Request malformed</title></head><body>Your HTTP request was syntactically incorrect.</body></html>" 90 #define REQUEST_MALFORMED "" 100 #define INTERNAL_ERROR \ 101 "<html><head><title>Internal server error</title></head><body>Please ask the developer of this Web server to carefully read the GNU libmicrohttpd documentation about connection management and blocking.</body></html>" 103 #define INTERNAL_ERROR "" 107 #ifdef HAVE_FREEBSD_SENDFILE 112 static int freebsd_sendfile_flags_;
117 static int freebsd_sendfile_flags_thd_p_c_;
127 MHD_conn_init_static_ (
void)
133 long sys_page_size = sysconf (_SC_PAGESIZE);
134 if (0 > sys_page_size)
136 freebsd_sendfile_flags_ = SF_NODISKIO;
137 freebsd_sendfile_flags_thd_p_c_ = SF_NODISKIO;
141 freebsd_sendfile_flags_ =
143 freebsd_sendfile_flags_thd_p_c_ =
156 #define HTTP_100_CONTINUE "HTTP/1.1 100 Continue\r\n\r\n" 169 enum MHD_StatusCode sc,
174 MHD_DLOG (connection->
daemon,
191 #define CONNECTION_CLOSE_ERROR(c, sc, emsg) connection_close_error (c, sc, emsg) 193 #define CONNECTION_CLOSE_ERROR(c, sc, emsg) connection_close_error (c, sc, NULL) 252 if ( (
NULL != (tls = daemon->tls_api)) &&
254 connection->tls_cs)) )
268 bytes_read = connection->
recv_cls (connection,
281 ? MHD_SC_CONNECTION_CLOSED
282 : MHD_SC_CONNECTION_RESET_CLOSED,
286 "Socket disconnected while reading request.\n"));
291 ? MHD_SC_CONNECTION_CLOSED
292 : MHD_SC_CONNECTION_READ_FAIL_CLOSED,
296 "Connection socket is closed due to error when reading request.\n"));
311 MHD_SC_STATE_MACHINE_STATUS_REPORT,
312 _ (
"In function %s handling connection at state: %s\n"),
314 MHD_state_to_string (request->
state));
316 switch (request->
state)
336 #ifdef UPGRADE_SUPPORT 337 case MHD_REQUEST_UPGRADE:
353 #if defined(_MHD_HAVE_SENDFILE) 367 const int file_fd = response->
fd;
370 #ifndef HAVE_SENDFILE64 371 const uint64_t max_off_t = (uint64_t)
OFF_T_MAX;
373 const uint64_t max_off_t = (uint64_t) OFF64_T_MAX;
375 #ifdef MHD_LINUX_SOLARIS_SENDFILE 376 #ifndef HAVE_SENDFILE64 382 #ifdef HAVE_FREEBSD_SENDFILE 386 #ifdef HAVE_DARWIN_SENDFILE 389 const bool used_thr_p_c = (MHD_TM_THREAD_PER_CONNECTION ==
393 size_t send_size = 0;
395 mhd_assert (MHD_resp_sender_sendfile == request->resp_sender);
400 send_size = (left > chunk_size) ? chunk_size : (
size_t) left;
401 if (max_off_t < offsetu64)
403 request->resp_sender = MHD_resp_sender_std;
406 #ifdef MHD_LINUX_SOLARIS_SENDFILE 407 #ifndef HAVE_SENDFILE64 408 offset = (off_t) offsetu64;
414 offset = (off64_t) offsetu64;
434 #ifdef HAVE_LINUX_SENDFILE 443 request->resp_sender = MHD_resp_sender_std;
446 if ( (EAFNOSUPPORT == err) ||
448 (EOPNOTSUPP == err) )
450 request->resp_sender = MHD_resp_sender_std;
453 if ( (ENOTCONN == err) ||
462 else if (send_size > (
size_t) ret)
465 #elif defined(HAVE_FREEBSD_SENDFILE) 467 flags = used_thr_p_c ?
468 freebsd_sendfile_flags_thd_p_c_ : freebsd_sendfile_flags_;
470 if (0 != sendfile (file_fd,
485 return (ssize_t) sent_bytes;
491 request->resp_sender = MHD_resp_sender_std;
496 ret = (ssize_t) sent_bytes;
497 #elif defined(HAVE_DARWIN_SENDFILE) 498 len = (off_t) send_size;
499 if (0 != sendfile (file_fd,
514 return (ssize_t) len;
518 if ((ENOTCONN == err) ||
521 if ((ENOTSUP == err) ||
522 (EOPNOTSUPP == err) )
525 request->resp_sender = MHD_resp_sender_std;
557 request->
state = next_state;
595 #if defined(_MHD_HAVE_SENDFILE) 596 if (MHD_resp_sender_sendfile ==
request->resp_sender)
620 MHD_SC_APPLICATION_DATA_GENERATION_FAILURE_CLOSED,
622 "Closing connection (application reported error generating data)\n"));
662 2 * (0xFFFFFF +
sizeof(cbuf) + 2));
671 MHD_SC_CONNECTION_POOL_MALLOC_FAILURE,
672 _ (
"Closing connection (out of memory)\n"));
693 const size_t data_write_offset
696 ret = response->
data_size - data_write_offset;
700 &response->
data[data_write_offset],
717 MHD_SC_APPLICATION_DATA_GENERATION_FAILURE_CLOSED,
719 "Closing connection (application error generating response)\n"));
742 cblen = MHD_snprintf_ (cbuf,
781 if ( (
NULL != (tls = daemon->tls_api)) &&
783 connection->tls_cs)) )
790 MHD_SC_STATE_MACHINE_STATUS_REPORT,
791 _ (
"In function %s handling connection at state: %s\n"),
793 MHD_state_to_string (request->
state));
795 switch (request->
state)
806 ret = connection->
send_cls (connection,
817 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
818 _ (
"Failed to send data in request for %s.\n"),
822 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
836 ret = connection->
send_cls (connection,
846 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
848 "Connection was closed while sending response headers.\n"));
865 uint64_t data_write_offset;
874 #if defined(_MHD_HAVE_SENDFILE) 875 if (MHD_resp_sender_sendfile == request->resp_sender)
877 ret = sendfile_adapter (connection);
886 if (data_write_offset > (uint64_t)
SIZE_MAX)
888 ret = connection->
send_cls (connection,
890 [(
size_t) data_write_offset],
892 - (
size_t) data_write_offset);
896 _ (
"Sent %d-byte DATA response: `%.*s'\n"),
911 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
912 _ (
"Failed to send data in request for `%s'.\n"),
916 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
931 ret = connection->
send_cls (connection,
941 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
943 "Connection was closed while sending response body.\n"));
961 ret = connection->
send_cls (connection,
971 MHD_SC_CONNECTION_WRITE_FAIL_CLOSED,
973 "Connection was closed while sending response body.\n"));
988 #ifdef UPGRADE_SUPPORT 989 case MHD_REQUEST_UPGRADE:
996 MHD_SC_STATEMACHINE_FAILURE_CONNECTION_CLOSED,
997 _ (
"Internal error\n"));
1024 if ( (
NULL == request) ||
1056 #define MHD_lookup_header_s_token_ci(r,h,tkn) \ 1057 MHD_lookup_header_token_ci ((r),(h),(tkn),MHD_STATICSTR_LEN_ (tkn)) 1124 static const char *
const days[] = {
1125 "Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" 1127 static const char *
const mons[] = {
1128 "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
1129 "Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" 1133 #if ! defined(HAVE_C11_GMTIME_S) && ! defined(HAVE_W32_GMTIME_S) && \ 1134 ! defined(HAVE_GMTIME_R) 1140 #if defined(HAVE_C11_GMTIME_S) 1141 if (
NULL == gmtime_s (&t,
1144 #elif defined(HAVE_W32_GMTIME_S) 1145 if (0 != gmtime_s (&now,
1148 #elif defined(HAVE_GMTIME_R) 1149 if (
NULL == gmtime_r (&t,
1158 MHD_snprintf_ (date,
1160 "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n",
1161 days[now.tm_wday % 7],
1162 (
unsigned int) now.tm_mday,
1163 mons[now.tm_mon % 12],
1164 (
unsigned int) (1900 + now.tm_year),
1165 (
unsigned int) now.tm_hour,
1166 (
unsigned int) now.tm_min,
1167 (
unsigned int) now.tm_sec);
1192 if ( (
NULL == key) ||
1195 (
'\0' == token[0]) )
1225 #define check_response_header_s_token_ci(r,k,tkn) \ 1226 check_response_header_token_ci ((r),(k),(tkn),MHD_STATICSTR_LEN_ (tkn)) 1250 char content_length_buf[128];
1251 size_t content_length_len;
1254 bool client_requested_close;
1255 bool response_has_close;
1256 bool response_has_keepalive;
1257 const char *have_encoding;
1258 const char *have_content_length;
1259 bool must_add_close;
1260 bool must_add_chunked_encoding;
1261 bool must_add_keep_alive;
1262 bool must_add_content_length;
1278 const char *reason_phrase;
1279 const char *version;
1290 MHD_snprintf_ (code,
1296 off = strlen (code);
1307 datelen = strlen (date);
1321 must_add_close =
false;
1322 must_add_chunked_encoding =
false;
1323 must_add_keep_alive =
false;
1324 must_add_content_length =
false;
1325 response_has_close =
false;
1326 switch (request->
state)
1333 response_has_keepalive
1337 client_requested_close
1344 #ifdef UPGRADE_SUPPORT 1345 else if (
NULL != response->upgrade_handler)
1354 #ifdef UPGRADE_SUPPORT
1355 (
NULL == response->upgrade_handler) &&
1357 (! response_has_close) &&
1358 (! client_requested_close) )
1372 if (
NULL == have_encoding)
1374 must_add_chunked_encoding =
true;
1381 must_add_close =
true;
1392 if (! response_has_close)
1393 must_add_close =
true;
1398 if ( ( (client_requested_close) ||
1401 (! response_has_close) &&
1402 #ifdef UPGRADE_SUPPORT
1403 (
NULL == response->upgrade_handler) &&
1406 must_add_close =
true;
1419 (MHD_HTTP_OK <= response->status_code) &&
1420 (
NULL == have_content_length) &&
1421 (request->
method != MHD_METHOD_CONNECT) )
1439 = MHD_snprintf_ (content_length_buf,
1440 sizeof (content_length_buf),
1444 must_add_content_length =
true;
1448 if ( (! response_has_keepalive) &&
1449 (! response_has_close) &&
1450 (! must_add_close) &&
1452 #ifdef UPGRADE_SUPPORT
1453 (
NULL == response->upgrade_handler) &&
1456 must_add_keep_alive =
true;
1459 response_has_keepalive =
false;
1468 if ( (must_add_close) ||
1469 (response_has_close) )
1471 else if ( (must_add_keep_alive) ||
1472 (response_has_keepalive) )
1478 if (must_add_keep_alive)
1480 if (must_add_chunked_encoding)
1482 if (must_add_content_length)
1483 size += content_length_len;
1484 mhd_assert (! (must_add_close && must_add_keep_alive) );
1485 mhd_assert (! (must_add_chunked_encoding && must_add_content_length) );
1491 (! ( (must_add_close) &&
1492 (response_has_keepalive) &&
1496 "Keep-Alive")) ) ) )
1497 size += strlen (pos->
header) + strlen (pos->
value) + 4;
1505 #ifdef HAVE_MESSAGES 1507 MHD_SC_CONNECTION_POOL_MALLOC_FAILURE,
1508 "Not enough memory for write!\n");
1522 "Connection: close\r\n",
1526 if (must_add_keep_alive)
1530 "Connection: Keep-Alive\r\n",
1534 if (must_add_chunked_encoding)
1538 "Transfer-Encoding: chunked\r\n",
1542 if (must_add_content_length)
1547 content_length_len);
1548 off += content_length_len;
1554 (! ( (must_add_close) &&
1555 (response_has_keepalive) &&
1559 "Keep-Alive")) ) ) )
1560 off += MHD_snprintf_ (&
data[off],
1603 enum MHD_StatusCode ec,
1604 enum MHD_HTTP_StatusCode status_code,
1605 const char *message)
1617 #ifdef HAVE_MESSAGES 1618 MHD_DLOG (request->
daemon,
1621 "Error processing request (HTTP response code is %u (`%s')). Closing connection.\n"),
1643 "Closing connection (failed to create response header)\n"));
1658 static enum MHD_Method
1664 enum MHD_Method value;
1666 {
"OPTIONS", MHD_METHOD_OPTIONS },
1667 {
"GET", MHD_METHOD_GET },
1668 {
"HEAD", MHD_METHOD_HEAD },
1669 {
"POST", MHD_METHOD_POST },
1670 {
"PUT", MHD_METHOD_PUT },
1671 {
"DELETE", MHD_METHOD_DELETE },
1672 {
"TRACE", MHD_METHOD_TRACE },
1673 {
"CONNECT", MHD_METHOD_CONNECT },
1674 {
"ACL", MHD_METHOD_ACL },
1675 {
"BASELINE_CONTROL", MHD_METHOD_BASELINE_CONTROL },
1676 {
"BIND", MHD_METHOD_BIND },
1677 {
"CHECKIN", MHD_METHOD_CHECKIN },
1678 {
"CHECKOUT", MHD_METHOD_CHECKOUT },
1679 {
"COPY", MHD_METHOD_COPY },
1680 {
"LABEL", MHD_METHOD_LABEL },
1681 {
"LINK", MHD_METHOD_LINK },
1682 {
"LOCK", MHD_METHOD_LOCK },
1683 {
"MERGE", MHD_METHOD_MERGE },
1684 {
"MKACTIVITY", MHD_METHOD_MKACTIVITY },
1685 {
"MKCOL", MHD_METHOD_MKCOL },
1686 {
"MKREDIRECTREF", MHD_METHOD_MKREDIRECTREF },
1687 {
"MKWORKSPACE", MHD_METHOD_MKWORKSPACE },
1688 {
"MOVE", MHD_METHOD_MOVE },
1689 {
"ORDERPATCH", MHD_METHOD_ORDERPATCH },
1690 {
"PRI", MHD_METHOD_PRI },
1691 {
"PROPFIND", MHD_METHOD_PROPFIND },
1692 {
"PROPPATCH", MHD_METHOD_PROPPATCH },
1693 {
"REBIND", MHD_METHOD_REBIND },
1694 {
"REPORT", MHD_METHOD_REPORT },
1695 {
"SEARCH", MHD_METHOD_SEARCH },
1696 {
"UNBIND", MHD_METHOD_UNBIND },
1697 {
"UNCHECKOUT", MHD_METHOD_UNCHECKOUT },
1698 {
"UNLINK", MHD_METHOD_UNLINK },
1699 {
"UNLOCK", MHD_METHOD_UNLOCK },
1700 {
"UPDATE", MHD_METHOD_UPDATE },
1701 {
"UPDATEDIRECTREF", MHD_METHOD_UPDATEDIRECTREF },
1702 {
"VERSION-CONTROL", MHD_METHOD_VERSION_CONTROL },
1703 {
NULL, MHD_METHOD_UNKNOWN }
1707 for (i = 0;
NULL != methods[i].key; i++)
1711 return methods[i].value;
1712 return MHD_METHOD_UNKNOWN;
1738 #ifdef HAVE_MESSAGES 1739 MHD_DLOG (request->
daemon,
1740 MHD_SC_CONNECTION_POOL_MALLOC_FAILURE,
1741 _ (
"Not enough memory in pool to allocate header record!\n"));
1744 MHD_SC_CLIENT_HEADER_TOO_BIG,
1771 unsigned int unused_num_headers;
1774 if (
NULL == (uri = memchr (line,
1784 while ( (
' ' == uri[0]) &&
1785 ( (
size_t) (uri - line) < line_len) )
1787 if ((
size_t) (uri - line) == line_len)
1793 url_end = line_len - (line - uri);
1799 http_version = line + line_len - 1;
1801 while ( (
' ' == http_version[0]) &&
1802 (http_version > uri) )
1805 while ( (
' ' != http_version[0]) &&
1806 (http_version > uri) )
1808 if (http_version > uri)
1810 http_version[0] =
'\0';
1814 http_version - uri);
1821 line_len - (uri - line));
1823 url_end = http_version - uri;
1826 (
NULL != memchr (curi,
1849 &unused_num_headers);
1855 request->
url = curi;
1877 colon = strchr (line,
1883 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
1885 "Received malformed line (no colon). Closing connection.\n"));
1895 white = strchr (line,
1896 (
unsigned char)
' ');
1897 if ( (
NULL != white) &&
1901 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
1903 "Whitespace before colon forbidden by RFC 7230. Closing connection.\n"));
1906 white = strchr (line,
1907 (
unsigned char)
'\t');
1908 if ( (
NULL != white) &&
1912 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
1914 "Tab before colon forbidden by RFC 7230. Closing connection.\n"));
1921 while ( (
'\0' !=
colon[0]) &&
1922 ( (
' ' ==
colon[0]) ||
1923 (
'\t' ==
colon[0]) ) )
1958 if ( (
' ' == line[0]) ||
1963 last_len = strlen (
last);
1966 while ( (
' ' == tmp[0]) ||
1969 tmp_len = strlen (tmp);
1982 last_len + tmp_len + 1);
1986 MHD_SC_CLIENT_HEADER_TOO_BIG,
1991 memcpy (&
last[last_len],
2005 MHD_SC_CLIENT_HEADER_TOO_BIG,
2011 if (
'\0' != line[0])
2017 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
2052 (
'\r' != rbuf[pos]) &&
2053 (
'\n' != rbuf[pos]) )
2056 (
'\n' != rbuf[pos]) )
2063 MHD_SC_CLIENT_HEADER_TOO_BIG,
2077 if ( (
'\r' == rbuf[pos]) &&
2078 (
'\n' == rbuf[pos + 1]) )
2111 #if defined(TCP_CORK) || defined(TCP_PUSH) 2131 #if defined(TCP_CORK) || defined(TCP_NOPUSH) 2133 #if defined(TCP_NODELAY) 2137 #if defined(TCP_NOPUSH) && ! defined(TCP_CORK) 2139 res = (0 == setsockopt (connection->
socket_fd,
2142 (
const void *) &on_val,
2145 #if defined(TCP_NODELAY) 2148 res &= (0 == setsockopt (connection->
socket_fd,
2151 (
const void *) &off_val,
2156 #if defined(TCP_NODELAY) 2160 (void) setsockopt (connection->
socket_fd,
2163 (
const void *) &off_val,
2167 res = (0 == setsockopt (connection->
socket_fd,
2170 (
const void *) &on_val,
2188 #if defined(TCP_NODELAY) 2191 #if defined(TCP_CORK) || defined(TCP_NOPUSH) 2197 #if defined(TCP_CORK) 2199 res &= (0 == setsockopt (connection->
socket_fd,
2202 (
const void *) &off_val,
2206 #if defined(TCP_NODELAY) 2208 res &= (0 == setsockopt (connection->
socket_fd,
2211 (
const void *) &on_val,
2215 #if defined(TCP_NOPUSH) && ! defined(TCP_CORK) 2217 res &= (0 == setsockopt (connection->
socket_fd,
2220 (
const void *) &off_val,
2242 #if defined(TCP_NOPUSH) && ! defined(TCP_CORK) 2243 const int dummy = 0;
2246 if (
NULL == connection)
2249 #if defined(TCP_NOPUSH) && ! defined(TCP_CORK) 2271 #if defined(TCP_NODELAY) 2274 #if defined(TCP_CORK) 2276 socklen_t param_size =
sizeof (cork_val);
2280 #if defined(TCP_CORK) 2284 if ( (0 != getsockopt (connection->
socket_fd,
2290 res &= (0 == setsockopt (connection->
socket_fd,
2293 (
const void *) &off_val,
2296 #elif defined(TCP_NOPUSH) 2300 res &= (0 == setsockopt (connection->
socket_fd,
2303 (
const void *) &off_val,
2308 res &= (0 == setsockopt (connection->
socket_fd,
2311 (
const void *) &off_val,
2377 #ifdef HAVE_MESSAGES 2379 MHD_SC_COOKIE_POOL_ALLOCATION_FAILURE,
2380 _ (
"Not enough memory in pool to parse cookies!\n"));
2383 MHD_SC_COOKIE_POOL_ALLOCATION_FAILURE,
2398 while ( ((*sce) !=
'\0') &&
2405 while ( (*ekill ==
' ') &&
2426 while ( (
'\0' != semicolon[0]) &&
2428 ( (
';' != semicolon[0]) &&
2429 (
',' != semicolon[0]) ) ) )
2431 if (
'"' == semicolon[0])
2432 quotes = (quotes + 1) & 1;
2435 if (
'\0' == semicolon[0])
2437 if (
NULL != semicolon)
2439 semicolon[0] =
'\0';
2443 if ( (
'"' == equals[0]) &&
2444 (
'"' == equals[strlen (equals) - 1]) )
2446 equals[strlen (equals) - 1] =
'\0';
2490 #ifdef HAVE_MESSAGES 2492 MHD_SC_HOST_HEADER_MISSING,
2493 _ (
"Received HTTP 1.1 request without `Host' header.\n"));
2525 if ( (clen == end) ||
2529 #ifdef HAVE_MESSAGES 2530 MHD_DLOG (request->
daemon,
2531 MHD_SC_CONTENT_LENGTH_MALFORMED,
2532 "Failed to parse `Content-Length' header. Closing connection.\n");
2535 MHD_SC_CONTENT_LENGTH_MALFORMED,
2564 MHD_SC_APPLICATION_CALLBACK_FAILURE_CLOSED,
2566 "Application reported internal error, closing connection.\n"));
2596 size_t to_be_processed;
2597 size_t left_unprocessed;
2598 size_t processed_size;
2600 instant_retry =
false;
2612 if ( (
'\r' == buffer_head[i]) ||
2613 (
'\n' == buffer_head[i]) )
2615 if ( (
'\r' == buffer_head[i]) ||
2616 (
'\n' == buffer_head[i]) )
2622 MHD_SC_CHUNKED_ENCODING_MALFORMED,
2624 "Received malformed HTTP request (bad chunked encoding). Closing connection.\n"));
2635 uint64_t cur_chunk_left;
2642 if (cur_chunk_left > available)
2644 to_be_processed = available;
2648 to_be_processed = (size_t) cur_chunk_left;
2649 if (available > to_be_processed)
2650 instant_retry =
true;
2661 while (i < available)
2663 if ( (
'\r' == buffer_head[i]) ||
2664 (
'\n' == buffer_head[i]) ||
2665 (
';' == buffer_head[i]) )
2673 if (
';' == buffer_head[i])
2675 while (i < available)
2677 if ( (
'\r' == buffer_head[i]) ||
2678 (
'\n' == buffer_head[i]) )
2686 if ( (i + 1 >= available) &&
2689 (
'0' == buffer_head[0]) ) )
2692 malformed = (end_size >= 16);
2698 malformed = (end_size != num_dig);
2704 MHD_SC_CHUNKED_ENCODING_MALFORMED,
2706 "Received malformed HTTP request (bad chunked encoding). Closing connection.\n"));
2710 if ( (i < available) &&
2711 ( (
'\r' == buffer_head[i]) ||
2712 (
'\n' == buffer_head[i]) ) )
2720 instant_retry =
true;
2744 to_be_processed = available;
2747 left_unprocessed = to_be_processed;
2761 MHD_SC_APPLICATION_CALLBACK_FAILURE_CLOSED,
2763 "Application reported internal error, closing connection.\n"));
2767 if (left_unprocessed > to_be_processed)
2771 #ifdef HAVE_MESSAGES
2772 ,
_ (
"libmicrohttpd API violation")
2777 if (0 != left_unprocessed)
2779 instant_retry =
false;
2780 #ifdef HAVE_MESSAGES 2787 MHD_SC_APPLICATION_HUNG_CONNECTION,
2789 "WARNING: incomplete upload processing and connection not suspended may result in hung connection.\n"));
2792 processed_size = to_be_processed - left_unprocessed;
2796 buffer_head += processed_size;
2797 available -= processed_size;
2801 while (instant_retry);
2868 if ( (MHD_ITC_IS_VALID_ (daemon->
itc)) &&
2869 (! MHD_itc_activate_ (daemon->
itc,
2872 #ifdef HAVE_MESSAGES 2874 MHD_SC_ITC_USE_FAILED,
2876 "Failed to signal end of connection via inter-thread communication channel"));
2883 #ifdef EPOLL_SUPPORT 2906 struct epoll_event event;
2908 event.events = EPOLLIN | EPOLLOUT | EPOLLPRI | EPOLLET;
2909 event.data.ptr = connection;
2910 if (0 != epoll_ctl (daemon->epoll_fd,
2915 #ifdef HAVE_MESSAGES 2917 MHD_SC_EPOLL_CTL_ADD_FAILED,
2918 _ (
"Call to epoll_ctl failed: %s\n"),
2949 #ifdef HTTPS_SUPPORT 2953 if ( (
NULL != (tls = daemon->tls_api)) &&
2964 MHD_SC_STATE_MACHINE_STATUS_REPORT,
2965 _ (
"In function %s handling connection at state: %s\n"),
2967 MHD_state_to_string (request->
state));
2969 switch (request->
state)
2980 MHD_SC_CLIENT_HEADER_TOO_BIG,
3019 MHD_SC_APPLICATION_HUNG_CONNECTION_CLOSED,
3038 MHD_SC_CONNECTION_READ_FAIL_CLOSED,
3080 #ifdef UPGRADE_SUPPORT 3081 case MHD_REQUEST_UPGRADE:
3115 #ifdef HTTPS_SUPPORT 3118 if ( (
NULL != (tls = daemon->tls_api)) &&
3120 connection->tls_cs)) )
3125 MHD_SC_STATE_MACHINE_STATUS_REPORT,
3126 _ (
"In function %s handling connection at state: %s\n"),
3128 MHD_state_to_string (request->
state));
3130 switch (request->
state)
3139 if ( (
NULL == line) ||
3147 MHD_SC_CONNECTION_READ_FAIL_CLOSED,
3158 MHD_SC_CONNECTION_CLOSED,
3173 MHD_SC_CONNECTION_READ_FAIL_CLOSED,
3189 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
3206 MHD_SC_CONNECTION_READ_FAIL_CLOSED,
3246 ( (MHD_METHOD_POST == request->
method) ||
3247 (MHD_METHOD_PUT == request->
method) ) )
3304 MHD_SC_CONNECTION_CLOSED,
3321 MHD_SC_CONNECTION_PARSE_FAIL_CLOSED,
3338 MHD_SC_CONNECTION_CLOSED,
3367 MHD_SC_FAILED_RESPONSE_HEADER_GENERATION,
3369 "Closing connection (failed to create response header)\n"));
3387 #ifdef UPGRADE_SUPPORT 3391 request->
state = MHD_REQUEST_UPGRADE;
3392 #if FIXME_LEGACY_STYLE 3399 MHD_SC_CONNECTION_CLOSED,
3482 MHD_SC_FAILED_RESPONSE_HEADER_GENERATION,
3484 "Closing connection (failed to create response header)\n"));
3562 request->
daemon = daemon;
3569 #ifdef UPGRADE_SUPPORT 3570 case MHD_REQUEST_UPGRADE:
3584 if ( (0 != timeout) &&
3596 #ifdef EPOLL_SUPPORT 3600 ret = connection_epoll_update_ (connection);
3630 bool states_info_processed =
false;
3634 #ifdef HTTPS_SUPPORT 3646 states_info_processed =
true;
3656 states_info_processed =
true;
3666 if (! states_info_processed)
3681 else if (on_fasttrack &&
3715 #ifdef HTTPS_SUPPORT #define check_response_header_s_token_ci(r, k, tkn)
static void parse_request_headers(struct MHD_Request *request)
#define MHD_send_(s, b, l)
size_t continue_message_write_offset
struct MHD_Request request
enum MHD_EventLoopSyscall event_loop_syscall
#define REQUEST_LACKS_HOST
#define HTTP_100_CONTINUE
bool data_already_pending
size_t connection_memory_limit_b
static void get_date_string(char *date, size_t date_len)
MHD_EarlyUriLogCallback early_uri_logger_cb
struct MHD_Connection * cleanup_head
size_t MHD_str_to_uint64_(const char *str, uint64_t *out_val)
size_t read_buffer_offset
#define MHD_mutex_unlock_chk_(pmutex)
MHD_PanicCallback mhd_panic
void * termination_cb_cls
#define MHD_socket_get_error_()
uint64_t current_chunk_size
enum MHD_ProtocolStrictLevel protocol_strict_level
static bool check_response_header_token_ci(const struct MHD_Response *response, const char *key, const char *token, size_t token_len)
enum MHD_Bool MHD_request_set_value(struct MHD_Request *request, enum MHD_ValueKind kind, const char *key, const char *value)
struct MHD_Response * response
MHD_RequestTerminationCallback termination_cb
#define REQUEST_MALFORMED
#define MHD_HTTP_NOT_MODIFIED
const char * MHD_request_lookup_value(struct MHD_Request *request, enum MHD_ValueKind kind, const char *key)
static bool build_header_response(struct MHD_Request *request)
bool MHD_str_has_token_caseless_(const char *str, const char *const token, size_t token_len)
bool MHD_parse_arguments_(struct MHD_Request *request, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, unsigned int *num_headers)
struct MHD_Connection * manual_timeout_head
int MHD_str_equal_caseless_(const char *str1, const char *str2)
time_t MHD_monotonic_sec_counter(void)
static bool check_write_done(struct MHD_Request *request, enum MHD_REQUEST_STATE next_state)
static void cleanup_connection(struct MHD_Connection *connection)
#define MHD_SCKT_ERR_IS_EAGAIN_(err)
static bool try_ready_chunked_body(struct MHD_Request *request)
#define MHD_lookup_header_s_token_ci(r, h, tkn)
uint64_t current_chunk_offset
internal shared structures
static void transmit_error_response(struct MHD_Request *request, enum MHD_StatusCode ec, enum MHD_HTTP_StatusCode status_code, const char *message)
static enum MHD_Method method_string_to_enum(const char *method)
#define MHD_ERR_CONNRESET_
static bool socket_start_extra_buffering(struct MHD_Connection *connection)
#define MHD_UNSIGNED_LONG_LONG
struct MHD_Daemon * daemon
int MHD_response_execute_upgrade_(struct MHD_Response *response, struct MHD_Connection *connection)
static bool socket_start_normal_buffering(struct MHD_Connection *connection)
function to update last activity of a connection
time_t connection_default_timeout
enum MHD_REQUEST_STATE state
enum MHD_ConnKeepAlive keepalive
static bool MHD_lookup_header_token_ci(const struct MHD_Request *request, const char *header, const char *token, size_t token_len)
struct MHD_Daemon * daemon
void * MHD_pool_reset(struct MemoryPool *pool, void *keep, size_t copy_bytes, size_t new_size)
#define DLL_insert(head, tail, element)
#define MHD_HTTP_URI_TOO_LONG
static bool process_header_line(struct MHD_Request *request, char *line)
static void MHD_request_handle_read_(struct MHD_Request *request)
struct MHD_HTTP_Header * first_header
#define MHD_socket_last_strerr_()
struct MHD_Connection * connections_tail
#define MHD_SENFILE_CHUNK_THR_P_C_
void * MHD_pool_allocate(struct MemoryPool *pool, size_t size, int from_end)
functions to close connection
struct MHD_Response * MHD_response_from_buffer(enum MHD_HTTP_StatusCode sc, size_t size, void *buffer, enum MHD_ResponseMemoryMode mode)
static bool socket_start_no_buffering(struct MHD_Connection *connection)
MHD_UnescapeCallback unescape_cb
static bool process_broken_line(struct MHD_Request *request, char *line, enum MHD_ValueKind kind)
size_t MHD_strx_to_uint64_n_(const char *str, size_t maxlen, uint64_t *out_val)
enum MHD_HTTP_StatusCode status_code
size_t connection_memory_increment_b
void * MHD_pool_reallocate(struct MemoryPool *pool, void *old, size_t old_size, size_t new_size)
struct MHD_Connection * connections_head
static bool socket_start_no_buffering_flush(struct MHD_Connection *connection)
#define MHD_HTTP_NO_CONTENT
void MHD_connection_close_(struct MHD_Connection *connection, enum MHD_RequestTerminationCode rtc)
uint64_t remaining_upload_size
time_t connection_timeout
#define MHD_SCKT_ERR_IS_(err, code)
enum MHD_RequestEventLoopInfo event_loop_info
#define MHD_CONTENT_READER_END_OF_STREAM
struct MHD_Connection * manual_timeout_tail
function to call event handlers based on event mask
static bool need_100_continue(struct MHD_Request *request)
static bool socket_flush_possible(struct MHD_Connection *connection)
static int parse_cookie_header(struct MHD_Request *request)
#define DLL_remove(head, tail, element)
static bool try_grow_read_buffer(struct MHD_Request *request)
#define MHD_STATICSTR_LEN_(macro)
#define MHD_HTTP_VERSION_1_1
struct MHD_Connection * normal_timeout_head
int MHD_connection_call_handlers_(struct MHD_Connection *con, bool read_ready, bool write_ready, bool force_close)
void MHD_pool_destroy(struct MemoryPool *pool)
#define MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE
static void call_request_handler(struct MHD_Request *request)
#define XDLL_remove(head, tail, element)
#define MHD_HTTP_BAD_REQUEST
static void connection_close_error(struct MHD_Connection *connection, enum MHD_StatusCode sc, const char *emsg)
struct MHD_Connection * suspended_connections_tail
struct MHD_Connection * cleanup_tail
struct MHD_HTTP_Header * headers_received
static void process_request_body(struct MHD_Request *request)
static bool try_ready_normal_body(struct MHD_Request *request)
#define CONNECTION_CLOSE_ERROR(c, sc, emsg)
enum MHD_ResponseFlags flags
size_t write_buffer_send_offset
struct MHD_Connection * normal_timeout_tail
struct MHD_Response * response
#define MHD_mutex_lock_chk_(pmutex)
void * early_uri_logger_cb_cls
static bool request_add_header(struct MHD_Request *request, const char *key, const char *value, enum MHD_ValueKind kind)
#define MHD_HTTP_VERSION_1_0
TransmitCallback send_cls
static bool keepalive_possible(struct MHD_Request *request)
#define MHD_HTTP_INTERNAL_SERVER_ERROR
uint64_t response_write_position
#define MHD_CONTENT_READER_END_WITH_ERROR
MHD_ContentReaderCallback crc
enum MHD_Bool(* handshake)(void *cls, struct MHD_TLS_ConnectionState *cs)
static char * get_next_header_line(struct MHD_Request *request, size_t *line_len)
size_t write_buffer_append_offset
static void MHD_request_handle_write_(struct MHD_Request *request)
#define MHD_UNSIGNED_LONG_LONG_PRINTF
struct MHD_Connection * connection
#define MHD_HTTP_PROCESSING
struct MHD_Connection * suspended_connections_head
size_t read_buffer_offset
static void connection_update_event_loop_info(struct MHD_Connection *connection)
enum MHD_ThreadingMode threading_mode
#define MHD_SCKT_ERR_IS_EINTR_(err)
#define MHD_SENFILE_CHUNK_
void MHD_connection_update_last_activity_(struct MHD_Connection *connection)
bool MHD_request_handle_idle_(struct MHD_Request *request)
enum MHD_Bool(* idle_ready)(void *cls, struct MHD_TLS_ConnectionState *cs)
MHD_mutex_ cleanup_connection_mutex
static bool parse_initial_message_line(struct MHD_Request *request, char *line, size_t line_len)
_MHD_EXTERN const char * MHD_get_reason_phrase_for(unsigned int code)
const char * MHD_response_get_header(struct MHD_Response *response, const char *key)
enum MHD_Bool(* update_event_loop_info)(void *cls, struct MHD_TLS_ConnectionState *cs, enum MHD_RequestEventLoopInfo *eli)
void MHD_response_queue_for_destroy(struct MHD_Response *response)