34 #if HAVE_NETINET_TCP_H 36 #include <netinet/tcp.h> 39 #if defined(_WIN32) && defined(MHD_W32_MUTEX_) 40 #ifndef WIN32_LEAN_AND_MEAN 41 #define WIN32_LEAN_AND_MEAN 1 50 #define HTTP_100_CONTINUE "HTTP/1.1 100 Continue\r\n\r\n" 60 #define REQUEST_TOO_BIG "<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 "<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>" 75 #define REQUEST_LACKS_HOST "" 86 #define REQUEST_MALFORMED "<html><head><title>Request malformed</title></head><body>Your HTTP request was syntactically incorrect.</body></html>" 88 #define REQUEST_MALFORMED "" 98 #define INTERNAL_ERROR "<html><head><title>Internal server error</title></head><body>Some programmer needs to study the manual more carefully.</body></html>" 100 #define INTERNAL_ERROR "" 107 #define DEBUG_CLOSE MHD_NO 112 #define DEBUG_SEND_DATA MHD_NO 135 if (
NULL == connection)
139 if (0 != (pos->
kind & kind))
142 if ( (
NULL != iterator) &&
143 (
MHD_YES != iterator (iterator_cls,
181 const char *key,
const char *
value)
189 pos->
header = (
char *) key;
190 pos->
value = (
char *) value;
224 if (
NULL == connection)
227 if ((0 != (pos->
kind & kind)) &&
275 daemon = connection->
daemon;
304 MHD_DLOG (connection->
daemon, emsg);
316 #define CONNECTION_CLOSE_ERROR(c, emsg) connection_close_error (c, emsg) 318 #define CONNECTION_CLOSE_ERROR(c, emsg) connection_close_error (c, NULL) 372 (void) MHD_mutex_unlock_ (&response->
mutex);
373 if ( ((ssize_t)MHD_CONTENT_READER_END_OF_STREAM) == ret)
378 "Closing connection (stream error)\n");
387 (void) MHD_mutex_unlock_ (&response->
mutex);
424 "Closing connection (out of memory)\n");
445 ret = response->
data_size - data_write_offset;
449 &response->
data[data_write_offset], ret);
464 "Closing connection (error generating response)\n");
486 "%X\r\n", (
unsigned int) ret);
489 memcpy (&connection->
write_buffer[sizeof (cbuf) - cblen], cbuf, cblen);
490 memcpy (&connection->
write_buffer[sizeof (cbuf) + ret],
"\r\n", 2);
559 static const char *
const days[] =
560 {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
561 static const char *
const mons[] =
562 {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
567 #if defined(_WIN32) && !defined(HAVE_GMTIME_S) && !defined(__CYGWIN__) 574 if (
NULL != gmtime_r (&t, &now))
576 #elif defined(HAVE_GMTIME_S) 577 if (0 == gmtime_s (&now, &t))
579 #elif defined(__CYGWIN__) 580 if (
NULL != gmtime_r (&t, &now))
589 "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n",
590 days[now.tm_wday % 7],
591 (
unsigned int) now.tm_mday,
592 mons[now.tm_mon % 12],
593 (
unsigned int) (1900 + now.tm_year),
594 (
unsigned int) now.tm_hour,
595 (
unsigned int) now.tm_min,
596 (
unsigned int) now.tm_sec);
652 char content_length_buf[128];
653 size_t content_length_len;
656 const char *reason_phrase;
658 const char *client_requested_close;
659 const char *response_has_close;
660 const char *response_has_keepalive;
661 const char *have_encoding;
662 const char *have_content_length;
664 int must_add_chunked_encoding;
665 int must_add_keep_alive;
666 int must_add_content_length;
669 if (0 == connection->
version[0])
702 size += strlen (date);
715 must_add_chunked_encoding =
MHD_NO;
716 must_add_keep_alive =
MHD_NO;
717 must_add_content_length =
MHD_NO;
718 switch (connection->
state)
723 response_has_keepalive = response_has_close;
724 if ( (
NULL != response_has_close) &&
726 response_has_close =
NULL;
727 if ( (
NULL != response_has_keepalive) &&
729 response_has_keepalive =
NULL;
733 if ( (
NULL != client_requested_close) &&
735 client_requested_close =
NULL;
741 (
NULL == response_has_close) &&
742 (
NULL == client_requested_close) )
755 if (
NULL == have_encoding)
757 must_add_chunked_encoding =
MHD_YES;
774 if (
NULL == response_has_close)
780 if ( ( (
NULL != client_requested_close) ||
782 (
NULL == response_has_close) &&
791 (
NULL == have_content_length) &&
812 = sprintf (content_length_buf,
815 must_add_content_length =
MHD_YES;
819 if ( (
NULL == response_has_keepalive) &&
820 (
NULL == response_has_close) &&
821 (
MHD_NO == must_add_close) &&
833 size += strlen (
"Connection: close\r\n");
834 if (must_add_keep_alive)
835 size += strlen (
"Connection: Keep-Alive\r\n");
836 if (must_add_chunked_encoding)
837 size += strlen (
"Transfer-Encoding: chunked\r\n");
838 if (must_add_content_length)
839 size += content_length_len;
840 EXTRA_CHECK (! (must_add_close && must_add_keep_alive) );
841 EXTRA_CHECK (! (must_add_chunked_encoding && must_add_content_length) );
844 if ( (pos->
kind == kind) &&
845 (! ( (
MHD_YES == must_add_close) &&
846 (pos->
value == response_has_keepalive) &&
849 size += strlen (pos->
header) + strlen (pos->
value) + 4;
855 MHD_DLOG (connection->
daemon,
856 "Not enough memory for write!\n");
862 memcpy (data, code, off);
868 "Connection: close\r\n",
869 strlen (
"Connection: close\r\n"));
870 off += strlen (
"Connection: close\r\n");
872 if (must_add_keep_alive)
876 "Connection: Keep-Alive\r\n",
877 strlen (
"Connection: Keep-Alive\r\n"));
878 off += strlen (
"Connection: Keep-Alive\r\n");
880 if (must_add_chunked_encoding)
884 "Transfer-Encoding: chunked\r\n",
885 strlen (
"Transfer-Encoding: chunked\r\n"));
886 off += strlen (
"Transfer-Encoding: chunked\r\n");
888 if (must_add_content_length)
894 off += content_length_len;
897 if ( (pos->
kind == kind) &&
898 (! ( (pos->
value == response_has_keepalive) &&
902 off += sprintf (&data[off],
908 strcpy (&data[off], date);
909 off += strlen (date);
911 memcpy (&data[off],
"\r\n", 2);
935 unsigned int status_code,
949 MHD_DLOG (connection->
daemon,
950 "Error %u (`%s') processing request, closing connection.\n",
951 status_code, message);
964 "Closing connection (failed to create response header)\n");
987 MHD_DLOG (connection->
daemon,
990 MHD_state_to_string (connection->
state));
992 switch (connection->
state)
996 if (0 == gnutls_record_get_direction (connection->tls_session))
1139 while ((pos < connection->read_buffer_offset - 1) &&
1140 (
'\r' != rbuf[pos]) && (
'\n' != rbuf[pos]))
1143 (
'\n' != rbuf[pos]) )
1159 if ((
'\r' == rbuf[pos]) && (
'\n' == rbuf[pos + 1]))
1193 MHD_DLOG (connection->
daemon,
1194 "Not enough memory to allocate header record!\n");
1233 MHD_DLOG (connection->
daemon,
1234 "Not enough memory to parse cookies!\n");
1240 memcpy (cpy, hdr, strlen (hdr) + 1);
1248 while (((*sce) !=
'\0') &&
1249 ((*sce) !=
',') && ((*sce) !=
';') && ((*sce) !=
'='))
1253 while ((*ekill ==
' ') && (ekill >= pos))
1271 while ( (
'\0' != semicolon[0]) &&
1273 ( (
';' != semicolon[0]) &&
1274 (
',' != semicolon[0]) ) ) )
1276 if (
'"' == semicolon[0])
1277 quotes = (quotes + 1) & 1;
1280 if (
'\0' == semicolon[0])
1282 if (
NULL != semicolon)
1284 semicolon[0] =
'\0';
1288 if ( (
'"' == equals[0]) &&
1289 (
'"' == equals[strlen (equals) - 1]) )
1291 equals[strlen (equals) - 1] =
'\0';
1321 unsigned int unused_num_headers;
1323 if (
NULL == (uri = strchr (line,
' ')))
1326 connection->
method = line;
1328 while (
' ' == uri[0])
1330 http_version = strchr (uri,
' ');
1331 if (
NULL != http_version)
1333 http_version[0] =
'\0';
1341 args = strchr (uri,
'?');
1351 &unused_num_headers);
1356 connection->
url = uri;
1357 if (
NULL == http_version)
1360 connection->
version = http_version;
1392 "Internal application error, closing connection.\n");
1435 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1437 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1443 "Received malformed HTTP request (bad chunked encoding), closing connection.\n");
1460 if (processed > available)
1461 processed = available;
1462 if (available > processed)
1469 while (i < available)
1471 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1481 if ((i + 1 >= available) &&
1482 !((i == 1) && (available == 2) && (buffer_head[0] ==
'0')))
1484 malformed = (i >= 6);
1487 buffer_head[i] =
'\0';
1489 malformed = (
'\0' != *end);
1495 "Received malformed HTTP request (bad chunked encoding), closing connection.\n");
1499 if ((i < available) &&
1500 ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n')))
1532 processed = available;
1549 "Internal application error, closing connection.\n");
1552 if (processed > used)
1566 buffer_head += used;
1571 while (
MHD_YES == instant_retry);
1573 memmove (connection->
read_buffer, buffer_head, available);
1594 bytes_read = connection->
recv_cls (connection,
1602 if ((EINTR == err) || (EAGAIN == err) || (
EWOULDBLOCK == err))
1612 if (0 == bytes_read)
1641 ret = connection->
send_cls (connection,
1649 if ((EINTR == err) || (EAGAIN == err) || (
EWOULDBLOCK == err))
1656 "Sent response: `%.*s'\n",
1685 connection->
state = next_state;
1710 colon = strchr (line,
':');
1715 "Received malformed line (no colon), closing connection.\n");
1721 while ((colon[0] !=
'\0') && ((colon[0] ==
' ') || (colon[0] ==
'\t')))
1728 connection->
last = line;
1729 connection->
colon = colon;
1753 last = connection->
last;
1754 if ((line[0] ==
' ') || (line[0] ==
'\t'))
1758 last_len = strlen (last);
1761 while ((tmp[0] ==
' ') || (tmp[0] ==
'\t'))
1763 tmp_len = strlen (tmp);
1776 last_len + tmp_len + 1);
1784 memcpy (&last[last_len], tmp, tmp_len + 1);
1785 connection->
last = last;
1841 MHD_DLOG (connection->
daemon,
1842 "Received `%s' request without `%s' header.\n",
1872 cval = strtoul (clen, &end, 10);
1873 if ( (
'\0' != *end) ||
1874 ( (
LONG_MAX == cval) && (errno == ERANGE) ) )
1877 MHD_DLOG (connection->
daemon,
1878 "Failed to parse `%s' header `%s', closing connection.\n",
1910 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
1919 MHD_PANIC (
"Failed to release cleanup mutex\n");
1947 MHD_DLOG (connection->
daemon,
"%s: state: %s\n",
1949 MHD_state_to_string (connection->
state));
1951 switch (connection->
state)
2004 MHD_DLOG (connection->
daemon,
"%s: state: %s\n",
2006 MHD_state_to_string (connection->
state));
2008 switch (connection->
state)
2019 ret = connection->
send_cls (connection,
2027 if ((err == EINTR) || (err == EAGAIN) || (
EWOULDBLOCK == err))
2030 MHD_DLOG (connection->
daemon,
2031 "Failed to send data: %s\n",
2039 "Sent 100 continue response: `%.*s'\n",
2066 uint64_t data_write_offset;
2068 (void) MHD_mutex_lock_ (&response->
mutex);
2073 if (data_write_offset > (uint64_t)
SIZE_MAX)
2075 ret = connection->
send_cls (connection,
2077 [(
size_t)data_write_offset],
2079 (
size_t)data_write_offset);
2084 "Sent %d-byte DATA response: `%.*s'\n",
2091 (void) MHD_mutex_unlock_ (&response->
mutex);
2094 if ((err == EINTR) || (err == EAGAIN) || (
EWOULDBLOCK == err))
2097 MHD_DLOG (connection->
daemon,
2098 "Failed to send data: %s\n",
2123 case MHD_CONNECTION_CHUNKED_BODY_UNREADY:
2144 "Internal error\n");
2171 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
2196 MHD_PANIC (
"Failed to release cleanup mutex\n");
2212 unsigned int timeout;
2224 MHD_state_to_string (connection->
state));
2226 switch (connection->
state)
2233 if ( (
NULL == line) ||
2420 "Closing connection (failed to create response header)\n");
2425 #if HAVE_DECL_TCP_CORK 2429 setsockopt (connection->
socket_fd, IPPROTO_TCP, TCP_CORK, &val,
2495 "Closing connection (failed to create response header)\n");
2509 #if HAVE_DECL_TCP_CORK 2513 setsockopt (connection->
socket_fd, IPPROTO_TCP, TCP_CORK, &val,
2597 if ( (0 != timeout) &&
2615 daemon->eready_tail,
2626 daemon->eready_tail,
2638 daemon->eready_tail,
2647 return MHD_connection_epoll_update_ (connection);
2678 struct epoll_event event;
2680 event.events = EPOLLIN | EPOLLOUT | EPOLLET;
2681 event.data.ptr = connection;
2682 if (0 != epoll_ctl (daemon->epoll_fd,
2690 "Call to epoll_ctl failed: %s\n",
2737 if (connection->tls_session ==
NULL)
2739 connection->cipher = gnutls_cipher_get (connection->tls_session);
2742 if (connection->tls_session ==
NULL)
2744 connection->protocol = gnutls_protocol_get_version (connection->tls_session);
2747 if (connection->tls_session ==
NULL)
2782 daemon = connection->
daemon;
2788 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
2800 va_start (ap, option);
2816 MHD_PANIC (
"Failed to release cleanup mutex\n");
2837 unsigned int status_code,
2840 if ( (
NULL == connection) ||
2841 (
NULL == response) ||
static int process_header_line(struct MHD_Connection *connection, char *line)
#define MHD_HTTP_REQUEST_ENTITY_TOO_LARGE
int(* MHD_KeyValueIterator)(void *cls, enum MHD_ValueKind kind, const char *key, const char *value)
static int parse_cookie_header(struct MHD_Connection *connection)
void * unescape_callback_cls
#define XDLL_insert(head, tail, element)
static int try_ready_normal_body(struct MHD_Connection *connection)
_MHD_EXTERN const char * MHD_lookup_connection_value(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
#define MHD_HTTP_METHOD_CONNECT
static void MHD_connection_update_event_loop_info(struct MHD_Connection *connection)
enum MHD_CONNECTION_STATE state
static int try_ready_chunked_body(struct MHD_Connection *connection)
void * MHD_pool_reset(struct MemoryPool *pool, void *keep, size_t copy_bytes, size_t new_size)
uint64_t response_write_position
void MHD_pool_destroy(struct MemoryPool *pool)
#define HTTP_100_CONTINUE
enum MHD_ConnectionEventLoopInfo event_loop_info
size_t current_chunk_size
int MHD_connection_handle_write(struct MHD_Connection *connection)
static int connection_add_header(struct MHD_Connection *connection, const char *key, const char *value, enum MHD_ValueKind kind)
#define DLL_remove(head, tail, element)
Methods for managing connections.
void MHD_connection_close_(struct MHD_Connection *connection, enum MHD_RequestTerminationCode termination_code)
struct MHD_Response * response
#define REQUEST_LACKS_HOST
struct MHD_Connection * normal_timeout_tail
MHD_RequestTerminationCode
static int do_read(struct MHD_Connection *connection)
struct MHD_HTTP_Header * first_header
static void transmit_error_response(struct MHD_Connection *connection, unsigned int status_code, const char *message)
MHD_AccessHandlerCallback default_handler
void * MHD_pool_allocate(struct MemoryPool *pool, size_t size, int from_end)
size_t current_chunk_offset
static void call_connection_handler(struct MHD_Connection *connection)
uint64_t remaining_upload_size
unsigned int responseCode
Methods for managing response objects.
static void cleanup_connection(struct MHD_Connection *connection)
#define MHD_UNSIGNED_LONG_LONG
void * uri_log_callback_cls
struct MHD_Daemon * daemon
int(* idle_handler)(struct MHD_Connection *connection)
struct MHD_Connection * manual_timeout_head
struct MHD_Connection * cleanup_head
static void connection_close_error(struct MHD_Connection *connection, const char *emsg)
_MHD_EXTERN const union MHD_ConnectionInfo * MHD_get_connection_info(struct MHD_Connection *connection, enum MHD_ConnectionInfoType info_type,...)
static int keepalive_possible(struct MHD_Connection *connection)
struct MHD_Connection * cleanup_tail
int MHD_connection_handle_read(struct MHD_Connection *connection)
int MHD_parse_arguments_(struct MHD_Connection *connection, enum MHD_ValueKind kind, char *args, MHD_ArgumentIterator_ cb, unsigned int *num_headers)
size_t write_buffer_send_offset
void * MHD_pool_reallocate(struct MemoryPool *pool, void *old, size_t old_size, size_t new_size)
static void get_date_string(char *date)
struct MHD_Connection * manual_timeout_tail
size_t continue_message_write_offset
#define REQUEST_MALFORMED
#define MHD_INVALID_SOCKET
internal shared structures
LogCallback uri_log_callback
_MHD_EXTERN void MHD_destroy_response(struct MHD_Response *response)
_MHD_EXTERN int MHD_set_connection_option(struct MHD_Connection *connection, enum MHD_CONNECTION_OPTION option,...)
void MHD_increment_response_rc(struct MHD_Response *response)
#define MHD_CONTENT_READER_END_OF_STREAM
int(* read_handler)(struct MHD_Connection *connection)
struct MHD_Connection * normal_timeout_head
static void update_last_activity(struct MHD_Connection *connection)
static int need_100_continue(struct MHD_Connection *connection)
static char * get_next_header_line(struct MHD_Connection *connection)
#define MHD_HTTP_VERSION_1_1
UnescapeCallback unescape_callback
static int try_grow_read_buffer(struct MHD_Connection *connection)
_MHD_EXTERN int MHD_queue_response(struct MHD_Connection *connection, unsigned int status_code, struct MHD_Response *response)
internal monotonic clock functions implementations
int(* write_handler)(struct MHD_Connection *connection)
#define MHD_HTTP_BAD_REQUEST
static int parse_initial_message_line(struct MHD_Connection *connection, char *line)
_MHD_EXTERN struct MHD_Response * MHD_create_response_from_buffer(size_t size, void *buffer, enum MHD_ResponseMemoryMode mode)
struct MHD_Connection * connections_head
static int check_write_done(struct MHD_Connection *connection, enum MHD_CONNECTION_STATE next_state)
const char * MHD_get_reason_phrase_for(unsigned int code)
int MHD_connection_handle_idle(struct MHD_Connection *connection)
unsigned int connection_timeout
static int process_broken_line(struct MHD_Connection *connection, char *line, enum MHD_ValueKind kind)
enum MHD_ResponseFlags flags
#define MHD_HTTP_METHOD_HEAD
static void parse_connection_headers(struct MHD_Connection *connection)
time_t MHD_monotonic_sec_counter(void)
size_t write_buffer_append_offset
MHD_RequestCompletedCallback notify_completed
void MHD_set_http_callbacks_(struct MHD_Connection *connection)
#define MHD_HTTP_METHOD_PUT
void * notify_completed_cls
static void process_request_body(struct MHD_Connection *connection)
#define MHD_HTTP_VERSION_1_0
TransmitCallback send_cls
_MHD_EXTERN int MHD_get_connection_values(struct MHD_Connection *connection, enum MHD_ValueKind kind, MHD_KeyValueIterator iterator, void *iterator_cls)
#define XDLL_remove(head, tail, element)
#define DLL_insert(head, tail, element)
#define MHD_HTTP_INTERNAL_SERVER_ERROR
#define MHD_CONTENT_READER_END_WITH_ERROR
MHD_ContentReaderCallback crc
struct MHD_Connection * suspended_connections_tail
MHD_PanicCallback mhd_panic
#define MHD_UNSIGNED_LONG_LONG_PRINTF
static int do_write(struct MHD_Connection *connection)
size_t read_buffer_offset
void * default_handler_cls
unsigned int connection_timeout
_MHD_EXTERN const char * MHD_get_response_header(struct MHD_Response *response, const char *key)
#define MHD_HTTP_REQUEST_URI_TOO_LONG
MHD_mutex_ cleanup_connection_mutex
#define CONNECTION_CLOSE_ERROR(c, emsg)
struct MHD_HTTP_Header * headers_received
struct MHD_Connection * connections_tail
#define EDLL_insert(head, tail, element)
_MHD_EXTERN int MHD_set_connection_value(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key, const char *value)
struct MHD_HTTP_Header * headers_received_tail
static int build_header_response(struct MHD_Connection *connection)
#define MHD_HTTP_METHOD_POST
struct MHD_Connection * suspended_connections_head
memory pool; mostly used for efficient (de)allocation for each connection and bounding memory use for...