35 #if HAVE_NETINET_TCP_H
37 #include <netinet/tcp.h>
44 #define HTTP_100_CONTINUE "HTTP/1.1 100 Continue\r\n\r\n"
54 #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>"
56 #define REQUEST_TOO_BIG ""
67 #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>"
69 #define REQUEST_LACKS_HOST ""
80 #define REQUEST_MALFORMED "<html><head><title>Request malformed</title></head><body>Your HTTP request was syntactically incorrect.</body></html>"
82 #define REQUEST_MALFORMED ""
92 #define INTERNAL_ERROR "<html><head><title>Internal server error</title></head><body>Some programmer needs to study the manual more carefully.</body></html>"
94 #define INTERNAL_ERROR ""
101 #define DEBUG_CLOSE MHD_NO
106 #define DEBUG_SEND_DATA MHD_NO
128 if (
NULL == connection)
132 if (0 != (pos->
kind & kind))
135 if ((
NULL != iterator) &&
136 (
MHD_YES != iterator (iterator_cls,
172 const char *key,
const char *
value)
180 pos->
header = (
char *) key;
181 pos->
value = (
char *) value;
215 if (
NULL == connection)
218 if ((0 != (pos->
kind & kind)) &&
222 (0 == strcasecmp (key, pos->
header))) ))
242 (0 == strcasecmp (connection->
version,
247 (0 == strcasecmp (expect,
"100-continue")) &&
266 daemon = connection->
daemon;
295 MHD_DLOG (connection->
daemon, emsg);
306 #define CONNECTION_CLOSE_ERROR(c, emsg) connection_close_error (c, emsg)
308 #define CONNECTION_CLOSE_ERROR(c, emsg) connection_close_error (c, NULL)
361 pthread_mutex_unlock (&response->
mutex);
362 if ( ((ssize_t)MHD_CONTENT_READER_END_OF_STREAM) == ret)
366 "Closing connection (stream error)\n");
375 pthread_mutex_unlock (&response->
mutex);
412 "Closing connection (out of memory)\n");
452 "Closing connection (error generating response)\n");
474 "%X\r\n", (
unsigned int) ret);
475 cblen = strlen (cbuf);
477 memcpy (&connection->
write_buffer[sizeof (cbuf) - cblen], cbuf, cblen);
478 memcpy (&connection->
write_buffer[sizeof (cbuf) + ret],
"\r\n", 2);
512 if (0 == strcasecmp (connection->
version,
517 if (0 == strcasecmp (end,
"close"))
521 if (0 == strcasecmp (connection->
version,
526 if (0 == strcasecmp (end,
"Keep-Alive"))
543 const char *have_close;
544 const char *have_keepalive;
545 const char *client_close;
546 const char *have_encoding;
554 if ( (
NULL != client_close) &&
555 (0 != strcasecmp (client_close,
"close")) )
559 have_keepalive = have_close;
560 if ( (
NULL != have_close) &&
561 (0 != strcasecmp (have_close,
"close")) )
563 if ( (
NULL != have_keepalive) &&
564 (0 != strcasecmp (have_keepalive,
"keep-alive")) )
565 have_keepalive =
NULL;
572 if (
NULL == have_close)
576 if ( (
NULL == client_close) &&
578 (0 == strcasecmp (connection->
version,
584 if (
NULL == have_encoding)
588 else if (0 != strcasecmp (have_encoding,
"chunked"))
601 if ( (
NULL != client_close) &&
602 (
NULL == have_close) )
609 (0 != strcasecmp (connection->
method,
639 if ( (
NULL == have_keepalive) &&
640 (
NULL == have_close) &&
658 static const char *
const days[] =
659 {
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat" };
660 static const char *
const mons[] =
661 {
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
666 #if defined(_WIN32) && !defined(HAVE_GMTIME_S) && !defined(__CYGWIN__)
673 if (
NULL != gmtime_r (&t, &now))
675 #elif defined(HAVE_GMTIME_S)
676 if (0 == gmtime_s (&now, &t))
678 #elif defined(__CYGWIN__)
679 if (
NULL != gmtime_r (&t, &now))
688 "Date: %3s, %02u %3s %04u %02u:%02u:%02u GMT\r\n",
689 days[now.tm_wday % 7],
690 (
unsigned int) now.tm_mday,
691 mons[now.tm_mon % 12],
692 (
unsigned int) (1900 + now.tm_year),
693 (
unsigned int) now.tm_hour,
694 (
unsigned int) now.tm_min,
695 (
unsigned int) now.tm_sec);
752 const char *reason_phrase;
758 if (0 == strlen (connection->
version))
792 size += strlen (date);
808 size += strlen (
"Connection: close\r\n");
810 if (pos->
kind == kind)
811 size += strlen (pos->
header) + strlen (pos->
value) + 4;
817 MHD_DLOG (connection->
daemon,
818 "Not enough memory for write!\n");
824 memcpy (data, code, off);
832 memcpy (&data[off],
"Connection: close\r\n",
833 strlen (
"Connection: close\r\n"));
834 off += strlen (
"Connection: close\r\n");
837 if (pos->
kind == kind)
838 off += sprintf (&data[off],
844 strcpy (&data[off], date);
845 off += strlen (date);
847 memcpy (&data[off],
"\r\n", 2);
871 unsigned int status_code,
885 MHD_DLOG (connection->
daemon,
886 "Error %u (`%s') processing request, closing connection.\n",
887 status_code, message);
900 "Closing connection (failed to create response header)\n");
923 MHD_DLOG (connection->
daemon,
926 MHD_state_to_string (connection->
state));
928 switch (connection->
state)
932 if (0 == gnutls_record_get_direction (connection->tls_session))
1075 while ((pos < connection->read_buffer_offset - 1) &&
1076 (
'\r' != rbuf[pos]) && (
'\n' != rbuf[pos]))
1079 (
'\n' != rbuf[pos]) )
1095 if ((
'\r' == rbuf[pos]) && (
'\n' == rbuf[pos + 1]))
1125 MHD_DLOG (connection->
daemon,
1126 "Not enough memory to allocate header record!\n");
1153 while (
NULL != args)
1155 equals = strchr (args,
'=');
1156 amper = strchr (args,
'&');
1185 if ( (
NULL == equals) ||
1248 MHD_DLOG (connection->
daemon,
"Not enough memory to parse cookies!\n");
1254 memcpy (cpy, hdr, strlen (hdr) + 1);
1262 while (((*sce) !=
'\0') &&
1263 ((*sce) !=
',') && ((*sce) !=
';') && ((*sce) !=
'='))
1267 while ((*ekill ==
' ') && (ekill >= pos))
1285 while ( (
'\0' != semicolon[0]) &&
1287 ( (
';' != semicolon[0]) &&
1288 (
',' != semicolon[0]) ) ) )
1290 if (
'"' == semicolon[0])
1291 quotes = (quotes + 1) & 1;
1294 if (
'\0' == semicolon[0])
1296 if (
NULL != semicolon)
1298 semicolon[0] =
'\0';
1302 if ( (
'"' == equals[0]) &&
1303 (
'"' == equals[strlen (equals) - 1]) )
1305 equals[strlen (equals) - 1] =
'\0';
1332 if (
NULL == (uri = strchr (line,
' ')))
1335 connection->
method = line;
1337 while (
' ' == uri[0])
1339 http_version = strchr (uri,
' ');
1340 if (
NULL != http_version)
1342 http_version[0] =
'\0';
1350 args = strchr (uri,
'?');
1360 connection->
url = uri;
1361 if (
NULL == http_version)
1364 connection->
version = http_version;
1396 "Internal application error, closing connection.\n");
1439 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1441 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1447 "Received malformed HTTP request (bad chunked encoding), closing connection.\n");
1464 if (processed > available)
1465 processed = available;
1466 if (available > processed)
1473 while (i < available)
1475 if ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n'))
1485 if ((i + 1 >= available) &&
1486 !((i == 1) && (available == 2) && (buffer_head[0] ==
'0')))
1488 malformed = (i >= 6);
1491 buffer_head[i] =
'\0';
1493 malformed = (
'\0' != *end);
1499 "Received malformed HTTP request (bad chunked encoding), closing connection.\n");
1503 if ((i < available) &&
1504 ((buffer_head[i] ==
'\r') || (buffer_head[i] ==
'\n')))
1536 processed = available;
1553 "Internal application error, closing connection.\n");
1556 if (processed > used)
1570 buffer_head += used;
1575 while (
MHD_YES == instant_retry);
1577 memmove (connection->
read_buffer, buffer_head, available);
1598 bytes_read = connection->
recv_cls (connection,
1606 if ((EINTR == err) || (EAGAIN == err) || (
ECONNRESET == err)
1612 MHD_DLOG (connection->
daemon,
1613 "Failed to receive data: %s\n",
1614 gnutls_strerror (bytes_read));
1617 MHD_DLOG (connection->
daemon,
1618 "Failed to receive data: %s\n",
1624 if (0 == bytes_read)
1653 ret = connection->
send_cls (connection,
1661 if ((EINTR == err) || (EAGAIN == err) || (
EWOULDBLOCK == err))
1666 MHD_DLOG (connection->
daemon,
1667 "Failed to send data: %s\n",
1668 gnutls_strerror ((
int) ret));
1671 MHD_DLOG (connection->
daemon,
1679 "Sent response: `%.*s'\n",
1708 connection->
state = next_state;
1733 colon = strchr (line,
':');
1738 "Received malformed line (no colon), closing connection.\n");
1744 while ((colon[0] !=
'\0') && ((colon[0] ==
' ') || (colon[0] ==
'\t')))
1751 connection->
last = line;
1752 connection->
colon = colon;
1776 last = connection->
last;
1777 if ((line[0] ==
' ') || (line[0] ==
'\t'))
1781 last_len = strlen (last);
1784 while ((tmp[0] ==
' ') || (tmp[0] ==
'\t'))
1786 tmp_len = strlen (tmp);
1799 last_len + tmp_len + 1);
1807 memcpy (&last[last_len], tmp, tmp_len + 1);
1808 connection->
last = last;
1813 last, connection->
colon, kind)))
1820 if (0 != strlen (line))
1862 MHD_DLOG (connection->
daemon,
1863 "Received `%s' request without `%s' header.\n",
1883 if (0 == strcasecmp (enc,
"chunked"))
1893 cval = strtoul (clen, &end, 10);
1894 if ( (
'\0' != *end) ||
1895 ( (LONG_MAX == cval) && (errno == ERANGE) ) )
1898 MHD_DLOG (connection->
daemon,
1899 "Failed to parse `%s' header `%s', closing connection.\n",
1931 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
1940 MHD_PANIC (
"Failed to release cleanup mutex\n");
1968 MHD_DLOG (connection->
daemon,
"%s: state: %s\n",
1970 MHD_state_to_string (connection->
state));
1972 switch (connection->
state)
2025 MHD_DLOG (connection->
daemon,
"%s: state: %s\n",
2027 MHD_state_to_string (connection->
state));
2029 switch (connection->
state)
2040 ret = connection->
send_cls (connection,
2048 if ((err == EINTR) || (err == EAGAIN) || (
EWOULDBLOCK == err))
2051 MHD_DLOG (connection->
daemon,
2052 "Failed to send data: %s\n",
2060 "Sent 100 continue response: `%.*s'\n",
2084 pthread_mutex_lock (&response->
mutex);
2087 ret = connection->
send_cls (connection,
2098 "Sent DATA response: `%.*s'\n",
2104 pthread_mutex_unlock (&response->
mutex);
2107 if ((err == EINTR) || (err == EAGAIN) || (
EWOULDBLOCK == err))
2110 MHD_DLOG (connection->
daemon,
2111 "Failed to send data: %s\n",
2135 case MHD_CONNECTION_CHUNKED_BODY_UNREADY:
2156 "Internal error\n");
2183 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
2208 MHD_PANIC (
"Failed to release cleanup mutex\n");
2224 unsigned int timeout;
2235 MHD_state_to_string (connection->
state));
2237 switch (connection->
state)
2272 if (strlen (line) == 0)
2303 if (0 == strlen (line))
2325 ( (0 == strcasecmp (connection->
method,
2327 (0 == strcasecmp (connection->
method,
2380 if (0 == strlen (line))
2411 if (0 == strlen (line))
2427 "Closing connection (failed to create response header)\n");
2432 #if HAVE_DECL_TCP_CORK
2436 setsockopt (connection->
socket_fd, IPPROTO_TCP, TCP_CORK, &val,
2507 #if HAVE_DECL_TCP_CORK
2511 setsockopt (connection->
socket_fd, IPPROTO_TCP, TCP_CORK, &val,
2529 ((
NULL != end) && (0 == strcasecmp (end,
"close"))) )
2582 if ( (0 != timeout) &&
2599 daemon->eready_tail,
2611 daemon->eready_tail,
2620 daemon->eready_tail,
2632 daemon->eready_tail,
2641 return MHD_connection_epoll_update_ (connection);
2672 struct epoll_event event;
2674 event.events = EPOLLIN | EPOLLOUT | EPOLLET;
2675 event.data.ptr = connection;
2676 if (0 != epoll_ctl (daemon->epoll_fd,
2684 "Call to epoll_ctl failed: %s\n",
2731 if (connection->tls_session ==
NULL)
2733 connection->cipher = gnutls_cipher_get (connection->tls_session);
2736 if (connection->tls_session ==
NULL)
2738 connection->protocol = gnutls_protocol_get_version (connection->tls_session);
2741 if (connection->tls_session ==
NULL)
2774 daemon = connection->
daemon;
2780 MHD_PANIC (
"Failed to acquire cleanup mutex\n");
2789 va_start (ap, option);
2802 MHD_PANIC (
"Failed to release cleanup mutex\n");
2823 unsigned int status_code,
struct MHD_Response *response)
2825 if ( (
NULL == connection) ||
2826 (
NULL == response) ||
2843 ( (0 == strcasecmp (connection->
method,
2845 (0 == strcasecmp (connection->
method,
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)
pthread_mutex_t cleanup_connection_mutex
_MHD_EXTERN const char * MHD_lookup_connection_value(struct MHD_Connection *connection, enum MHD_ValueKind kind, const char *key)
#define MHD_HTTP_METHOD_CONNECT
int(* write_handler)(struct MHD_Connection *connection)
static void MHD_connection_update_event_loop_info(struct MHD_Connection *connection)
enum MHD_CONNECTION_STATE state
int(* idle_handler)(struct MHD_Connection *connection)
static int try_ready_chunked_body(struct MHD_Connection *connection)
_MHD_EXTERN int MHD_add_response_header(struct MHD_Response *response, const char *header, const char *content)
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)
#define DLL_remove(head, tail, element)
Methods for managing connections.
static int connection_add_header(struct MHD_Connection *connection, char *key, char *value, enum MHD_ValueKind kind)
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
int(* read_handler)(struct MHD_Connection *connection)
struct MHD_Daemon * daemon
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)
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
static void add_extra_headers(struct MHD_Connection *connection)
time_t MHD_monotonic_time(void)
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
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)
const char * MHD_get_reason_phrase_for(unsigned int code)
#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)
void * MHD_pool_reset(struct MemoryPool *pool, void *keep, size_t size)
#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)
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)
#define MHD_HTTP_METHOD_HEAD
static void parse_connection_headers(struct MHD_Connection *connection)
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 parse_arguments(enum MHD_ValueKind kind, struct MHD_Connection *connection, char *args)
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
#define CONNECTION_CLOSE_ERROR(c, emsg)
struct MHD_HTTP_Header * headers_received
struct MHD_Connection * connections_tail
#define EDLL_insert(head, tail, element)
void MHD_connection_close(struct MHD_Connection *connection, enum MHD_RequestTerminationCode termination_code)
_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...