A minimal-HTTP server library. More...
#include "platform.h"
#include "internal.h"
#include "response.h"
#include "connection.h"
#include "memorypool.h"
Go to the source code of this file.
Defines | |
#define | MHD_MAX_CONNECTIONS_DEFAULT FD_SETSIZE -4 |
#define | MHD_POOL_SIZE_DEFAULT (1024 * 1024) |
#define | DEBUG_CLOSE MHD_NO |
#define | DEBUG_CONNECT MHD_NO |
Functions | |
static struct MHD_Daemon * | MHD_get_master (struct MHD_Daemon *daemon) |
static void | MHD_ip_count_lock (struct MHD_Daemon *daemon) |
static void | MHD_ip_count_unlock (struct MHD_Daemon *daemon) |
static int | MHD_ip_addr_compare (const void *a1, const void *a2) |
static int | MHD_ip_addr_to_key (struct sockaddr *addr, socklen_t addrlen, struct MHD_IPCount *key) |
static int | MHD_ip_limit_add (struct MHD_Daemon *daemon, struct sockaddr *addr, socklen_t addrlen) |
static void | MHD_ip_limit_del (struct MHD_Daemon *daemon, struct sockaddr *addr, socklen_t addrlen) |
int | MHD_get_fdset (struct MHD_Daemon *daemon, fd_set *read_fd_set, fd_set *write_fd_set, fd_set *except_fd_set, int *max_fd) |
static void * | MHD_handle_connection (void *data) |
static ssize_t | recv_param_adapter (struct MHD_Connection *connection, void *other, size_t i) |
static ssize_t | send_param_adapter (struct MHD_Connection *connection, const void *other, size_t i) |
static int | MHD_accept_connection (struct MHD_Daemon *daemon) |
static void | MHD_cleanup_connections (struct MHD_Daemon *daemon) |
int | MHD_get_timeout (struct MHD_Daemon *daemon, unsigned long long *timeout) |
static int | MHD_select (struct MHD_Daemon *daemon, int may_block) |
int | MHD_run (struct MHD_Daemon *daemon) |
static void * | MHD_select_thread (void *cls) |
struct MHD_Daemon * | MHD_start_daemon (unsigned int options, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls,...) |
struct MHD_Daemon * | MHD_start_daemon_va (unsigned int options, unsigned short port, MHD_AcceptPolicyCallback apc, void *apc_cls, MHD_AccessHandlerCallback dh, void *dh_cls, va_list ap) |
static void | MHD_close_connections (struct MHD_Daemon *daemon) |
void | MHD_stop_daemon (struct MHD_Daemon *daemon) |
union MHD_DaemonInfo * | MHD_get_daemon_info (struct MHD_Daemon *daemon, enum MHD_DaemonInfoType infoType,...) |
const char * | MHD_get_version (void) |
static void | sigalrmHandler (int sig) |
void | __attribute__ ((constructor)) |
void | __attribute__ ((destructor)) |
Variables | |
static struct sigaction | sig |
static struct sigaction | old |
A minimal-HTTP server library.
Definition in file daemon.c.
#define DEBUG_CLOSE MHD_NO |
#define DEBUG_CONNECT MHD_NO |
#define MHD_MAX_CONNECTIONS_DEFAULT FD_SETSIZE -4 |
Default connection limit.
Definition at line 42 of file daemon.c.
Referenced by MHD_start_daemon_va().
#define MHD_POOL_SIZE_DEFAULT (1024 * 1024) |
Default memory allowed per connection.
Definition at line 47 of file daemon.c.
Referenced by MHD_start_daemon_va().
void __attribute__ | ( | (destructor) | ) |
Definition at line 1547 of file daemon.c.
References MHD_gnutls_init_mutex, old, and sig.
void __attribute__ | ( | (constructor) | ) |
Initialize the signal handler for SIGALRM and do other setup work.
Definition at line 1529 of file daemon.c.
References MHD_gnutls_init_mutex, old, sig, and sigalrmHandler().
static int MHD_accept_connection | ( | struct MHD_Daemon * | daemon | ) | [static] |
Accept an incoming connection and create the MHD_Connection object for it. This function also enforces policy by way of checking with the accept policy callback.
Definition at line 557 of file daemon.c.
References ACCEPT, MHD_Connection::addr, MHD_Connection::addr_len, MHD_Daemon::apc, MHD_Daemon::apc_cls, CLOSE, MHD_Daemon::connections, MHD_Connection::daemon, EAGAIN, GNUTLS_SERVER, MHD_Connection::last_activity, MHD_Daemon::max_connections, MHD__gnutls_credentials_set(), MHD__gnutls_init(), MHD__gnutls_priority_set(), MHD__gnutls_transport_set_ptr(), MHD__gnutls_transport_set_pull_function(), MHD__gnutls_transport_set_push_function(), MHD_GNUTLS_CRD_CERTIFICATE, MHD_handle_connection(), MHD_ip_limit_add(), MHD_ip_limit_del(), MHD_NO, MHD_set_http_calbacks(), MHD_set_https_calbacks(), MHD_TLS_CONNECTION_INIT, MHD_USE_SSL, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Connection::next, MHD_Daemon::options, MHD_Connection::pid, MHD_Connection::pool, MHD_Connection::recv_cls, recv_param_adapter(), MHD_Connection::send_cls, send_param_adapter(), SHUTDOWN, MHD_Connection::socket_fd, MHD_Daemon::socket_fd, MHD_Connection::state, and STRERROR.
Referenced by MHD_select().
static void MHD_cleanup_connections | ( | struct MHD_Daemon * | daemon | ) | [static] |
Free resources associated with all closed connections. (destroy responses, free buffers, etc.). A connection is known to be closed if the socket_fd is -1.
Definition at line 738 of file daemon.c.
References MHD_Connection::addr, MHD_Connection::addr_len, MHD_Daemon::connections, MHD_Connection::daemon, MHD_Daemon::max_connections, MHD__gnutls_deinit(), MHD_destroy_response(), MHD_ip_limit_del(), MHD_pool_destroy(), MHD_USE_THREAD_PER_CONNECTION, MHD_Connection::next, MHD_Daemon::options, MHD_Connection::pid, MHD_Connection::pool, MHD_Connection::response, MHD_Daemon::shutdown, and MHD_Connection::socket_fd.
Referenced by MHD_close_connections(), MHD_run(), and MHD_select_thread().
static void MHD_close_connections | ( | struct MHD_Daemon * | daemon | ) | [static] |
Close all connections for the daemon
Definition at line 1385 of file daemon.c.
References MHD_Daemon::connections, MHD_cleanup_connections(), MHD_connection_close(), MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN, and MHD_Connection::socket_fd.
Referenced by MHD_stop_daemon().
union MHD_DaemonInfo* MHD_get_daemon_info | ( | struct MHD_Daemon * | daemon, | |
enum MHD_DaemonInfoType | infoType, | |||
... | ||||
) | [write] |
Obtain information about the given daemon (not fully implemented!).
daemon | what daemon to get information about | |
infoType | what information is desired? | |
... | depends on infoType |
int MHD_get_fdset | ( | struct MHD_Daemon * | daemon, | |
fd_set * | read_fd_set, | |||
fd_set * | write_fd_set, | |||
fd_set * | except_fd_set, | |||
int * | max_fd | |||
) |
Obtain the select sets for this daemon.
Definition at line 405 of file daemon.c.
References MHD_Daemon::connections, MHD_connection_get_fdset(), MHD_NO, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Connection::next, MHD_Daemon::options, MHD_Daemon::shutdown, and MHD_Daemon::socket_fd.
Referenced by MHD_select().
static struct MHD_Daemon* MHD_get_master | ( | struct MHD_Daemon * | daemon | ) | [static, read] |
Trace up to and return master daemon. If the supplied daemon is a master, then return the daemon itself.
Definition at line 72 of file daemon.c.
References MHD_Daemon::master.
Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().
int MHD_get_timeout | ( | struct MHD_Daemon * | daemon, | |
unsigned long long * | timeout | |||
) |
Obtain timeout value for select for this daemon (only needed if connection timeout is used). The returned value is how long select should at most block, not the timeout value set for connections.
timeout | set to the timeout (in milliseconds) |
Definition at line 794 of file daemon.c.
References MHD_Daemon::connection_timeout, MHD_Daemon::connections, MHD_Connection::last_activity, MHD_NO, MHD_YES, and MHD_Connection::next.
Referenced by MHD_select().
const char* MHD_get_version | ( | void | ) |
static void* MHD_handle_connection | ( | void * | data | ) | [static] |
Main function of the thread that handles an individual connection.
Definition at line 444 of file daemon.c.
References MHD_Daemon::connection_timeout, MHD_Connection::daemon, MHD_Connection::idle_handler, MHD_Connection::last_activity, MHD_CONNECTION_CHUNKED_BODY_UNREADY, MHD_connection_close(), MHD_connection_get_fdset(), MHD_CONNECTION_NORMAL_BODY_UNREADY, MHD_REQUEST_TERMINATED_DAEMON_SHUTDOWN, MHD_Connection::read_handler, SELECT, MHD_Daemon::shutdown, MHD_Connection::socket_fd, MHD_Connection::state, STRERROR, and MHD_Connection::write_handler.
Referenced by MHD_accept_connection().
static int MHD_ip_addr_compare | ( | const void * | a1, | |
const void * | a2 | |||
) | [static] |
Tree comparison function for IP addresses (supplied to tsearch() family). We compare everything in the struct up through the beginning of the 'count' field.
Definition at line 131 of file daemon.c.
Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().
static int MHD_ip_addr_to_key | ( | struct sockaddr * | addr, | |
socklen_t | addrlen, | |||
struct MHD_IPCount * | key | |||
) | [static] |
Parse address and initialize 'key' using the address. Returns MHD_YES on success and MHD_NO otherwise (e.g., invalid address type).
Definition at line 141 of file daemon.c.
References MHD_NO, and MHD_YES.
Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().
static void MHD_ip_count_lock | ( | struct MHD_Daemon * | daemon | ) | [static] |
Lock shared structure for IP connection counts
Definition at line 99 of file daemon.c.
References MHD_Daemon::per_ip_connection_mutex.
Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().
static void MHD_ip_count_unlock | ( | struct MHD_Daemon * | daemon | ) | [static] |
Unlock shared structure for IP connection counts
Definition at line 114 of file daemon.c.
References MHD_Daemon::per_ip_connection_mutex.
Referenced by MHD_ip_limit_add(), and MHD_ip_limit_del().
static int MHD_ip_limit_add | ( | struct MHD_Daemon * | daemon, | |
struct sockaddr * | addr, | |||
socklen_t | addrlen | |||
) | [static] |
Check if IP address is over its limit.
Definition at line 177 of file daemon.c.
References MHD_get_master(), MHD_ip_addr_compare(), MHD_ip_addr_to_key(), MHD_ip_count_lock(), MHD_ip_count_unlock(), MHD_NO, MHD_YES, MHD_Daemon::per_ip_connection_count, MHD_Daemon::per_ip_connection_limit, and TSEARCH.
Referenced by MHD_accept_connection().
static void MHD_ip_limit_del | ( | struct MHD_Daemon * | daemon, | |
struct sockaddr * | addr, | |||
socklen_t | addrlen | |||
) | [static] |
Decrement connection count for IP address, removing from table count reaches 0
Definition at line 237 of file daemon.c.
References MHD_get_master(), MHD_ip_addr_compare(), MHD_ip_addr_to_key(), MHD_ip_count_lock(), MHD_ip_count_unlock(), MHD_NO, MHD_Daemon::per_ip_connection_count, MHD_Daemon::per_ip_connection_limit, TDELETE, and TFIND.
Referenced by MHD_accept_connection(), and MHD_cleanup_connections().
int MHD_run | ( | struct MHD_Daemon * | daemon | ) |
Run webserver operations (without blocking unless in client callbacks). This method should be called by clients in combination with MHD_get_fdset if the client-controlled select method is used.
Definition at line 946 of file daemon.c.
References MHD_cleanup_connections(), MHD_NO, MHD_select(), MHD_USE_SELECT_INTERNALLY, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Daemon::options, and MHD_Daemon::shutdown.
static int MHD_select | ( | struct MHD_Daemon * | daemon, | |
int | may_block | |||
) | [static] |
Main select call.
may_block | YES if blocking, NO if non-blocking |
Definition at line 830 of file daemon.c.
References MHD_Daemon::connections, MHD_Connection::idle_handler, MHD_Daemon::max_connections, MHD_accept_connection(), MHD_get_fdset(), MHD_get_timeout(), MHD_NO, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Connection::next, MHD_Daemon::options, MHD_Connection::read_handler, MHD_Daemon::shutdown, MHD_Connection::socket_fd, MHD_Daemon::socket_fd, STRERROR, and MHD_Connection::write_handler.
Referenced by MHD_run(), and MHD_select_thread().
static void* MHD_select_thread | ( | void * | cls | ) | [static] |
Thread that runs the select loop until the daemon is explicitly shut down.
Definition at line 962 of file daemon.c.
References MHD_cleanup_connections(), MHD_NO, MHD_select(), MHD_YES, and MHD_Daemon::shutdown.
Referenced by MHD_start_daemon_va().
struct MHD_Daemon* MHD_start_daemon | ( | unsigned int | options, | |
unsigned short | port, | |||
MHD_AcceptPolicyCallback | apc, | |||
void * | apc_cls, | |||
MHD_AccessHandlerCallback | dh, | |||
void * | dh_cls, | |||
... | ||||
) | [read] |
Start a webserver on the given port.
port | port to bind to | |
apc | callback to call to check which clients will be allowed to connect | |
apc_cls | extra argument to apc | |
dh | default handler for all URIs | |
dh_cls | extra argument to dh |
Definition at line 985 of file daemon.c.
References MHD_start_daemon_va().
struct MHD_Daemon* MHD_start_daemon_va | ( | unsigned int | options, | |
unsigned short | port, | |||
MHD_AcceptPolicyCallback | apc, | |||
void * | apc_cls, | |||
MHD_AccessHandlerCallback | dh, | |||
void * | dh_cls, | |||
va_list | ap | |||
) | [read] |
Start a webserver on the given port.
port | port to bind to | |
apc | callback to call to check which clients will be allowed to connect | |
apc_cls | extra argument to apc | |
dh | default handler for all URIs | |
dh_cls | extra argument to dh |
Definition at line 1013 of file daemon.c.
References _set_priority(), MHD_Daemon::apc, MHD_Daemon::apc_cls, BIND, CLOSE, MHD_Daemon::connection_timeout, MHD_Daemon::custom_error_log, MHD_Daemon::custom_error_log_cls, MHD_Daemon::default_handler, MHD_Daemon::default_handler_cls, FPRINTF, LISTEN, MHD_Daemon::master, MHD_Daemon::max_connections, MHD__gnutls_global_init(), MHD_GNUTLS_CRD_CERTIFICATE, MHD_gnutls_init_mutex, MHD_MAX_CONNECTIONS_DEFAULT, MHD_OPTION_CIPHER_ALGORITHM, MHD_OPTION_CONNECTION_LIMIT, MHD_OPTION_CONNECTION_MEMORY_LIMIT, MHD_OPTION_CONNECTION_TIMEOUT, MHD_OPTION_END, MHD_OPTION_EXTERNAL_LOGGER, MHD_OPTION_HTTPS_MEM_CERT, MHD_OPTION_HTTPS_MEM_KEY, MHD_OPTION_NOTIFY_COMPLETED, MHD_OPTION_PER_IP_CONNECTION_LIMIT, MHD_OPTION_PROTOCOL_VERSION, MHD_OPTION_SOCK_ADDR, MHD_OPTION_THREAD_POOL_SIZE, MHD_OPTION_URI_LOG_CALLBACK, MHD_POOL_SIZE_DEFAULT, MHD_select_thread(), MHD_stop_daemon(), MHD_tls_set_default_priority(), MHD_USE_DEBUG, MHD_USE_IPv6, MHD_USE_SELECT_INTERNALLY, MHD_USE_SSL, MHD_USE_THREAD_PER_CONNECTION, MHD_Daemon::notify_completed, MHD_Daemon::notify_completed_cls, MHD_Daemon::options, MHD_Daemon::per_ip_connection_limit, MHD_Daemon::per_ip_connection_mutex, MHD_Daemon::pid, MHD_Daemon::pool_size, MHD_Daemon::port, SETSOCKOPT, SOCKET, MHD_Daemon::socket_fd, STRERROR, MHD_Daemon::uri_log_callback, MHD_Daemon::uri_log_callback_cls, MHD_Daemon::worker_pool, and MHD_Daemon::worker_pool_size.
Referenced by MHD_start_daemon().
void MHD_stop_daemon | ( | struct MHD_Daemon * | daemon | ) |
Shutdown an http daemon
Definition at line 1407 of file daemon.c.
References CLOSE, MHD__gnutls_certificate_free_credentials(), MHD__gnutls_global_deinit(), MHD__gnutls_priority_deinit(), MHD_close_connections(), MHD_gnutls_init_mutex, MHD_USE_SELECT_INTERNALLY, MHD_USE_SSL, MHD_USE_THREAD_PER_CONNECTION, MHD_YES, MHD_Daemon::options, MHD_Daemon::per_ip_connection_mutex, MHD_Daemon::pid, SHUTDOWN, MHD_Daemon::shutdown, MHD_Daemon::socket_fd, MHD_Daemon::worker_pool, and MHD_Daemon::worker_pool_size.
Referenced by MHD_start_daemon_va().
static ssize_t recv_param_adapter | ( | struct MHD_Connection * | connection, | |
void * | other, | |||
size_t | i | |||
) | [static] |
Callback for receiving data from the socket.
conn | the MHD connection structure | |
other | where to write received data to | |
i | maximum size of other (in bytes) |
Definition at line 521 of file daemon.c.
References MHD_Connection::daemon, MHD_USE_SSL, MHD_Daemon::options, RECV, and MHD_Connection::socket_fd.
Referenced by MHD_accept_connection().
static ssize_t send_param_adapter | ( | struct MHD_Connection * | connection, | |
const void * | other, | |||
size_t | i | |||
) | [static] |
Callback for writing data to the socket.
conn | the MHD connection structure | |
other | data to write | |
i | number of bytes to write |
Definition at line 540 of file daemon.c.
References MHD_Connection::daemon, MHD_USE_SSL, MHD_Daemon::options, SEND, and MHD_Connection::socket_fd.
Referenced by MHD_accept_connection().
static void sigalrmHandler | ( | int | sig | ) | [static] |
Definition at line 1520 of file daemon.c.
Referenced by __attribute__().
struct sigaction old [static] |
Definition at line 1517 of file daemon.c.
Referenced by __attribute__(), MHD_gtls_bin2hex(), and parse_cookie_header().
struct sigaction sig [static] |
Definition at line 1515 of file daemon.c.
Referenced by __attribute__(), and MHD_gtls_proc_cert_client_cert_vrfy().