Definition in file mihl.c.
Go to the source code of this file.
Defines | |
#define | _GNU_SOURCE |
#define | BUILD_DLL_MIHL |
#define | __MAIN__ |
Functions | |
static int | add_new_connexion (mihl_ctx_t *ctx, SOCKET sockfd, struct sockaddr_in *client_addr) |
TBD. | |
static void | delete_connexion (mihl_cnx_t *cnx) |
TBD. | |
static int | bind_and_listen (mihl_ctx_t *ctx) |
TBD. | |
static int | page_not_found (mihl_cnx_t *cnx, char const *tag, char const *host, void *param) |
TBD. | |
mihl_ctx_t * | mihl_init (char const *bind_addr, int port, int maxnb_cnx, unsigned log_level) |
Library initializations: start waiting for HTTP connections. | |
mihl_ctx_t * | mihl_get_ctx (mihl_cnx_t *cnx) |
Utility function to return the Connexion Context, which is the value given by mihl_init(). | |
int | mihl_end (mihl_ctx_t *ctx) |
This function is the opposite of mish_init(): it close all current open HTTP connections, and release all resources that might have been allocated. | |
int | send_file (mihl_cnx_t *cnx, char const *tag, char const *filename, char *content_type, int close_connection) |
TBD. | |
static int | search_for_handle (mihl_cnx_t *cnx, char const *tag, char *host, int nb_variables, char **vars_names, char **vars_values) |
TBD. | |
static int | manage_new_connexions (mihl_ctx_t *ctx, time_t now) |
TBD. | |
static int | got_data_for_active_connexion (mihl_cnx_t *cnx) |
TBD. | |
static int | manage_existent_connexions (mihl_ctx_t *ctx, time_t now) |
TBD. | |
static int | manage_timedout_connexions (mihl_ctx_t *ctx, time_t now) |
TBD. | |
static int | mihl_handle_get_add (mihl_ctx_t *ctx, char const *tag, mihl_pf_handle_get_t *pf, void *param) |
Provide a C function handler for a GET operation. | |
int | mihl_handle_get (mihl_ctx_t *ctx, char const *tag, mihl_pf_handle_get_t *pf, void *param) |
Provide a C function handler for a GET operation. | |
int | mihl_handle_post (mihl_ctx_t *ctx, char const *tag, mihl_pf_handle_post_t *pf, void *param) |
Provide a C function handler for a POST operation. | |
int | mihl_handle_file (mihl_ctx_t *ctx, char const *tag, char const *filename, char const *content_type, int close_connection) |
This function is used to describe a file that will be served for a given HTTP request. | |
int | mihl_server (mihl_ctx_t *ctx) |
Manage new connections, existent connections, and connections timeout. | |
void | mihl_set_log_level (mihl_ctx_t *ctx, mihl_log_level_t level) |
TBD. | |
mihl_log_level_t | mihl_get_log_level (mihl_ctx_t *ctx) |
TBD. | |
int | mihl_log (mihl_ctx_t *ctx, unsigned level, const char *fmt,...) |
TBD. | |
int | mihl_dump_info (mihl_ctx_t *ctx) |
TBD. | |
int | mihl_info (mihl_ctx_t *ctx, int maxnb_cnxinfos, mihl_cnxinfo_t *infos) |
Provide information on current connections. | |
int | mihl_dump_info_handlers (mihl_ctx_t *ctx) |
TBD. | |
char * | mihl_authorization (mihl_cnx_t *cnx) |
TBD. |
static int add_new_connexion | ( | mihl_ctx_t * | ctx, | |
SOCKET | sockfd, | |||
struct sockaddr_in * | client_addr | |||
) | [static] |
TBD.
Called by: manage_new_connexions
ctx | opaque context structure as returned by mihl_init() | |
sockfd | TBD | |
client_addr | TBD |
Definition at line 41 of file mihl.c.
References mihl_cnx::active, mihl_cnx::authorization, mihl_cnxinfo_t::client_addr, mihl_ctx::connexions, mihl_cnxinfo_t::host, mihl_cnx::html_buffer, mihl_cnx::html_buffer_len, mihl_cnx::html_buffer_sz, mihl_cnx::info, mihl_cnx::keep_alive, mihl_cnxinfo_t::last_request, mihl_ctx::maxnb_cnx, mihl_log(), MIHL_LOG_INFO, MIHL_LOG_INFO_VERBOSE, mihl_ctx::nb_connexions, mihl_cnx::sockfd, mihl_cnxinfo_t::time_last_data, mihl_cnxinfo_t::time_started, and mihl_cnxinfo_t::user_agent.
Referenced by manage_new_connexions().
static void delete_connexion | ( | mihl_cnx_t * | cnx | ) | [static] |
TBD.
cnx | opaque context structure as returned by mihl_init() |
Definition at line 87 of file mihl.c.
References mihl_cnx::active, closesocket, mihl_cnx::ctx, FREE, mihl_cnxinfo_t::host, mihl_cnx::html_buffer, mihl_cnx::info, mihl_cnxinfo_t::last_request, mihl_log(), MIHL_LOG_INFO_VERBOSE, mihl_ctx::nb_connexions, mihl_cnx::sockfd, and mihl_cnxinfo_t::user_agent.
Referenced by manage_timedout_connexions().
static int bind_and_listen | ( | mihl_ctx_t * | ctx | ) | [static] |
TBD.
Called from: mihl_init
ctx | opaque context structure as returned by mihl_init() |
Definition at line 113 of file mihl.c.
References mihl_ctx::bind_addr, closesocket, ERRNO, mihl_log(), MIHL_LOG_ERROR, mihl_ctx::port, SOCKET_ERROR, and mihl_ctx::sockfd.
Referenced by mihl_init().
static int page_not_found | ( | mihl_cnx_t * | cnx, | |
char const * | tag, | |||
char const * | host, | |||
void * | param | |||
) | [static] |
TBD.
cnx | opaque context structure as returned by mihl_init() | |
tag | URL of the non existent page | |
host | TBD | |
param | TBD |
Definition at line 175 of file mihl.c.
References mihl_add(), and mihl_send().
Referenced by mihl_init().
mihl_ctx_t* mihl_init | ( | char const * | bind_addr, | |
int | port, | |||
int | maxnb_cnx, | |||
unsigned | log_level | |||
) |
Library initializations: start waiting for HTTP connections.
Initializes the library internally.
Typically, next library calls will be functions such as mihl_handle_get or mihl_handle_file.
bind_addr | Address on which the connections will be established. NULL means INADDR_ANY. | |
port | TCP port used for the HTTP connection. 80 is the standard port, but any other port not yet in use might be used, assuming you have sufficient privileges | |
maxnb_cnx | maximum number of allowed connections. If you did not installed your own handler, a standard page will be displayed if you have a number of connections exceeds the maximum number allowed. | |
log_level | initial log level (can be changed later with mihl_set_log_level. |
Definition at line 210 of file mihl.c.
References mihl_cnx::active, mihl_ctx::bind_addr, bind_and_listen(), mihl_ctx::connexions, mihl_cnx::ctx, mihl_ctx::handles, mihl_ctx::log_level, mihl_ctx::maxnb_cnx, mihl_handle_get(), mihl_ctx::nb_connexions, mihl_ctx::nb_handles, page_not_found(), mihl_ctx::port, mihl_ctx::read_buffer, and mihl_ctx::read_buffer_maxlen.
Referenced by main().
mihl_ctx_t* mihl_get_ctx | ( | mihl_cnx_t * | cnx | ) |
Utility function to return the Connexion Context, which is the value given by mihl_init().
cnx | opaque context structure as returned by mihl_init() |
Definition at line 255 of file mihl.c.
References mihl_cnx::ctx.
int mihl_end | ( | mihl_ctx_t * | ctx | ) |
This function is the opposite of mish_init(): it close all current open HTTP connections, and release all resources that might have been allocated.
All the sockets in use are closed.
ctx | context structure as returned by mihl_init() |
Definition at line 268 of file mihl.c.
References FREE, and mihl_ctx::read_buffer.
int send_file | ( | mihl_cnx_t * | cnx, | |
char const * | tag, | |||
char const * | filename, | |||
char * | content_type, | |||
int | close_connection | |||
) |
TBD.
cnx | opaque context structure as returned by mihl_init() | |
tag | TBD | |
filename | TBD | |
content_type | TBD | |
close_connection | TBD |
Definition at line 283 of file mihl.c.
References mihl_cnx::ctx, mihl_log(), MIHL_LOG_ERROR, MIN, read_file(), mihl_cnx::sockfd, and tcp_write().
Referenced by search_for_handle().
static int search_for_handle | ( | mihl_cnx_t * | cnx, | |
char const * | tag, | |||
char * | host, | |||
int | nb_variables, | |||
char ** | vars_names, | |||
char ** | vars_values | |||
) | [static] |
TBD.
Called from: got_data_for_active_connexion
cnx | opaque context structure as returned by mihl_init() | |
tag | TBD | |
host | TBD | |
nb_variables | TBD | |
vars_names | TBD | |
vars_values | TBD |
Definition at line 344 of file mihl.c.
References migl_handle::close_connection, migl_handle::content_type, mihl_cnx::ctx, migl_handle::filename, mihl_ctx::handles, mihl_ctx::nb_handles, migl_handle::param, migl_handle::partial, migl_handle::pf_get, migl_handle::pf_post, send_file(), and migl_handle::tag.
Referenced by got_data_for_active_connexion().
static int manage_new_connexions | ( | mihl_ctx_t * | ctx, | |
time_t | now | |||
) | [static] |
TBD.
Called by: mihl_server
ctx | opaque context structure as returned by mihl_init() | |
now | Current time |
Definition at line 375 of file mihl.c.
References add_new_connexion(), closesocket, ERRNO, mihl_log(), MIHL_LOG_ERROR, and mihl_ctx::sockfd.
Referenced by mihl_server().
static int got_data_for_active_connexion | ( | mihl_cnx_t * | cnx | ) | [static] |
TBD.
cnx | opaque context structure as returned by mihl_init() |
Definition at line 407 of file mihl.c.
References mihl_cnx::ctx, decode_keys_values(), mihl_cnxinfo_t::host, mihl_cnx::info, mihl_cnxinfo_t::last_request, mihl_ctx::log_level, mihl_log(), MIHL_LOG_DEBUG, MIHL_LOG_INFO_VERBOSE, mihl_ctx::read_buffer, mihl_ctx::read_buffer_maxlen, search_for_handle(), mihl_cnx::sockfd, tcp_read(), and mihl_cnxinfo_t::time_last_data.
Referenced by manage_existent_connexions().
static int manage_existent_connexions | ( | mihl_ctx_t * | ctx, | |
time_t | now | |||
) | [static] |
TBD.
ctx | The opaque context structure which was given by mihl_init() | |
now | TBD |
Definition at line 521 of file mihl.c.
References mihl_cnx::active, mihl_ctx::connexions, got_data_for_active_connexion(), mihl_ctx::maxnb_cnx, mihl_ctx::nb_connexions, and mihl_cnx::sockfd.
Referenced by mihl_server().
static int manage_timedout_connexions | ( | mihl_ctx_t * | ctx, | |
time_t | now | |||
) | [static] |
TBD.
ctx | TBD | |
now | TBD |
Definition at line 566 of file mihl.c.
References mihl_cnx::active, mihl_ctx::connexions, delete_connexion(), mihl_cnx::info, mihl_cnx::keep_alive, mihl_ctx::maxnb_cnx, and mihl_cnxinfo_t::time_last_data.
Referenced by mihl_server().
static int mihl_handle_get_add | ( | mihl_ctx_t * | ctx, | |
char const * | tag, | |||
mihl_pf_handle_get_t * | pf, | |||
void * | param | |||
) | [static] |
Provide a C function handler for a GET operation.
The mihl_handle_get() function installs a C handler function that will be used to construct an HTTP page for a given URL.
ctx | opaque context structure as returned by mihl_init() | |
tag | HTTP base URL (such as “/” or ‘/nextpage.html”). | |
pf | pointer to the C handler function that will be called for this particular HTTP URL. | |
param | user pointer that will be provided to the C handler function. |
Definition at line 592 of file mihl.c.
References migl_handle::close_connection, migl_handle::content_type, migl_handle::filename, mihl_ctx::handles, mihl_ctx::nb_handles, migl_handle::param, migl_handle::partial, migl_handle::pf_get, migl_handle::pf_post, and migl_handle::tag.
Referenced by mihl_handle_get().
int mihl_handle_get | ( | mihl_ctx_t * | ctx, | |
char const * | tag, | |||
mihl_pf_handle_get_t * | pf, | |||
void * | param | |||
) |
Provide a C function handler for a GET operation.
The mihl_handle_get() function installs a C handler function that will be used to construct an HTTP page for a given URL.
ctx | opaque context structure as returned by mihl_init() | |
tag | HTTP base URL (such as “/” or ‘/nextpage.html”). If this is NULL, the handler will be called for every non found page. | |
pf | pointer to the C handler function that will be called for this particular HTTP URL. | |
param | user pointer that will be provided to the C handler function. |
Definition at line 634 of file mihl.c.
References migl_handle::filename, FREE, mihl_ctx::handles, mihl_handle_get_add(), mihl_ctx::nb_handles, migl_handle::partial, migl_handle::pf_get, migl_handle::pf_post, and migl_handle::tag.
Referenced by main(), and mihl_init().
int mihl_handle_post | ( | mihl_ctx_t * | ctx, | |
char const * | tag, | |||
mihl_pf_handle_post_t * | pf, | |||
void * | param | |||
) |
Provide a C function handler for a POST operation.
The mihl_handle_get() function installs a C handler function that will be used to construct an HTTP page for a given URL
ctx | opaque context structure as returned by mihl_init() | |
tag | HTTP base URL (such as “/” or ‘/nextpage.html”). | |
pf | pointer to the C handler function that will be called for this particular HTTP URL. | |
param | user pointer that will be provided to the C handler function. |
Definition at line 683 of file mihl.c.
References migl_handle::close_connection, migl_handle::content_type, migl_handle::filename, mihl_ctx::handles, mihl_ctx::nb_handles, migl_handle::param, migl_handle::partial, migl_handle::pf_get, migl_handle::pf_post, and migl_handle::tag.
int mihl_handle_file | ( | mihl_ctx_t * | ctx, | |
char const * | tag, | |||
char const * | filename, | |||
char const * | content_type, | |||
int | close_connection | |||
) |
This function is used to describe a file that will be served for a given HTTP request.
ctx | opaque context structure as returned by mihl_init() | |
tag | HTTP base URL (such as “/image.jpg” for instance) | |
filename | ilename to send. The full pathname can be given. | |
content_type | HTTP content type, such as “image/jpeg”, “image/gif”, “text/javascript”, etc. | |
close_connection | indicate if the HTTP connection should be closed or not. |
Definition at line 719 of file mihl.c.
References migl_handle::close_connection, migl_handle::content_type, migl_handle::filename, mihl_ctx::handles, mihl_ctx::nb_handles, migl_handle::partial, migl_handle::pf_get, migl_handle::pf_post, and migl_handle::tag.
Referenced by main().
int mihl_server | ( | mihl_ctx_t * | ctx | ) |
Manage new connections, existent connections, and connections timeout.
MIHL is based on a non-blocking and single thread mode; therefore you’ll have to call this function on a frequent basis in order to:
In the case of an existent connection, typically a user-provided callback function is called. Its job is to build a new page which is then sent to the client. In a single-thread model, each of these functions should not take too much time: the next callback function will be ‘scheduled’ only when the current function is done (sort of a ‘cooperative multi-tasking’).
ctx | opaque context structure as returned by mihl_init() |
Definition at line 763 of file mihl.c.
References manage_existent_connexions(), manage_new_connexions(), manage_timedout_connexions(), and mihl_ctx::nb_connexions.
Referenced by main().
void mihl_set_log_level | ( | mihl_ctx_t * | ctx, | |
mihl_log_level_t | level | |||
) |
TBD.
ctx | opaque context structure as returned by mihl_init() | |
level | Specify which log levels we are interested in |
Definition at line 777 of file mihl.c.
References mihl_ctx::log_level.
Referenced by peek_key().
mihl_log_level_t mihl_get_log_level | ( | mihl_ctx_t * | ctx | ) |
TBD.
ctx | opaque context structure as returned by mihl_init() |
Definition at line 787 of file mihl.c.
References mihl_ctx::log_level.
Referenced by peek_key().
int mihl_log | ( | mihl_ctx_t * | ctx, | |
unsigned | level, | |||
const char * | fmt, | |||
... | ||||
) |
TBD.
ctx | opaque context structure as returned by mihl_init() | |
level | MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | | |
fmt | TBD | |
... | TBD |
Definition at line 800 of file mihl.c.
References mihl_ctx::log_level.
Referenced by add_new_connexion(), bind_and_listen(), delete_connexion(), got_data_for_active_connexion(), manage_new_connexions(), mihl_dump_info(), mihl_dump_info_handlers(), and send_file().
int mihl_dump_info | ( | mihl_ctx_t * | ctx | ) |
TBD.
ctx | opaque context structure as returned by mihl_init() |
Definition at line 817 of file mihl.c.
References mihl_cnx::active, mihl_cnxinfo_t::client_addr, mihl_ctx::connexions, mihl_cnx::info, mihl_cnxinfo_t::last_request, mihl_ctx::log_level, mihl_ctx::maxnb_cnx, mihl_log(), MIHL_LOG_DEBUG, MIHL_LOG_ERROR, MIHL_LOG_INFO, MIHL_LOG_INFO_VERBOSE, MIHL_LOG_WARNING, mihl_ctx::nb_connexions, mihl_cnx::sockfd, mihl_cnxinfo_t::time_last_data, and mihl_cnxinfo_t::time_started.
Referenced by peek_key().
int mihl_info | ( | mihl_ctx_t * | ctx, | |
int | maxnb_cnxinfos, | |||
mihl_cnxinfo_t * | infos | |||
) |
Provide information on current connections.
ctx | opaque context structure as returned by mihl_init() | |
maxnb_cnxinfos | TBD | |
infos | TBD |
Definition at line 855 of file mihl.c.
References mihl_cnx::active, mihl_ctx::connexions, mihl_cnx::info, and mihl_ctx::maxnb_cnx.
int mihl_dump_info_handlers | ( | mihl_ctx_t * | ctx | ) |
TBD.
ctx | opaque context structure as returned by mihl_init() |
Definition at line 876 of file mihl.c.
References migl_handle::filename, mihl_ctx::handles, mihl_ctx::log_level, mihl_log(), MIHL_LOG_DEBUG, MIHL_LOG_ERROR, MIHL_LOG_INFO, MIHL_LOG_INFO_VERBOSE, MIHL_LOG_WARNING, mihl_ctx::nb_connexions, mihl_ctx::nb_handles, migl_handle::pf_get, migl_handle::pf_post, and migl_handle::tag.
Referenced by peek_key().
char* mihl_authorization | ( | mihl_cnx_t * | cnx | ) |
TBD.
ctx | opaque context structure as returned by mihl_init() |
Definition at line 912 of file mihl.c.
References mihl_cnx::authorization.