mihl.c File Reference


Detailed Description

Definition in file mihl.c.

Include dependency graph for 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_tmihl_init (char const *bind_addr, int port, int maxnb_cnx, unsigned log_level)
 Library initializations: start waiting for HTTP connections.
mihl_ctx_tmihl_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.


Define Documentation

#define _GNU_SOURCE

Definition at line 9 of file mihl.c.

#define BUILD_DLL_MIHL

Definition at line 20 of file mihl.c.

#define __MAIN__

Definition at line 23 of file mihl.c.


Function Documentation

static int add_new_connexion ( mihl_ctx_t ctx,
SOCKET  sockfd,
struct sockaddr_in *  client_addr 
) [static]

TBD.

Called by: manage_new_connexions

Parameters:
ctx opaque context structure as returned by mihl_init()
sockfd TBD
client_addr TBD
Returns:
:
  • -1 if an error occured
  • or Index into the table of the connexions

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().

Here is the call graph for this function:

Here is the caller graph for this function:

static void delete_connexion ( mihl_cnx_t cnx  )  [static]

TBD.

Parameters:
cnx opaque context structure as returned by mihl_init()
Returns:
Index into the table of the connexions

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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int bind_and_listen ( mihl_ctx_t ctx  )  [static]

TBD.

Called from: mihl_init

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
TBD

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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int page_not_found ( mihl_cnx_t cnx,
char const *  tag,
char const *  host,
void *  param 
) [static]

TBD.

Parameters:
cnx opaque context structure as returned by mihl_init()
tag URL of the non existent page
host TBD
param TBD
Returns:
TBD

Definition at line 175 of file mihl.c.

References mihl_add(), and mihl_send().

Referenced by mihl_init().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
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.
Returns:
  • an opaque context structure used by all further calls. This enables to run several instances of the embedded server.
  • or NULL, if the operation failed
Note:
Besides internal library initializations, this function performs bind() then listen().

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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().

Parameters:
cnx opaque context structure as returned by mihl_init()
Returns:
Connexion context as given 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.

Parameters:
ctx context structure as returned by mihl_init()
Returns:
  • number of connections closed (which might be 0)
  • or -1 if an error occurred (errno is then set).

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.

Parameters:
cnx opaque context structure as returned by mihl_init()
tag TBD
filename TBD
content_type TBD
close_connection TBD
Returns:
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().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Parameters:
cnx opaque context structure as returned by mihl_init()
tag TBD
host TBD
nb_variables TBD
vars_names TBD
vars_values TBD
Returns:
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int manage_new_connexions ( mihl_ctx_t ctx,
time_t  now 
) [static]

TBD.

Called by: mihl_server

Parameters:
ctx opaque context structure as returned by mihl_init()
now Current time
Returns:
TBD

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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int got_data_for_active_connexion ( mihl_cnx_t cnx  )  [static]

TBD.

Parameters:
cnx opaque context structure as returned by mihl_init()
Returns:
TBD

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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int manage_existent_connexions ( mihl_ctx_t ctx,
time_t  now 
) [static]

TBD.

Parameters:
ctx The opaque context structure which was given by mihl_init()
now TBD
Returns:
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().

Here is the call graph for this function:

Here is the caller graph for this function:

static int manage_timedout_connexions ( mihl_ctx_t ctx,
time_t  now 
) [static]

TBD.

Parameters:
ctx TBD
now TBD
Returns:
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().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
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.
Returns:
  • 0 if the operation succeeded
  • or -1 if an error occurred (errno is then set)

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().

Here is the caller graph for this function:

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.

Parameters:
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.
Returns:
  • 0 if the operation succeeded
  • or -1 if an error occurred (errno is then set)

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().

Here is the call graph for this function:

Here is the caller graph for this function:

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

Parameters:
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.
Returns:
  • 0 if the operation succeeded
  • or -1 if an error occurred (errno is then set)

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.

Parameters:
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.
Returns:
  • the number of defined handles if the operation succeeded,
  • or -1 if an error occurred (errno is then set).

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().

Here is the caller graph for this function:

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:

  • establish new connections;
  • serve pages for existent connections;
  • and finally close timed out connections.

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’).

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
The current number of active connexions.
Note:
Remember that this is a non blocking call. If you do not call this function, no new connection can be established.

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().

Here is the call graph for this function:

Here is the caller graph for this function:

void mihl_set_log_level ( mihl_ctx_t ctx,
mihl_log_level_t  level 
)

TBD.

Parameters:
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().

Here is the caller graph for this function:

mihl_log_level_t mihl_get_log_level ( mihl_ctx_t ctx  ) 

TBD.

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
Specify which log levels we are interested in

Definition at line 787 of file mihl.c.

References mihl_ctx::log_level.

Referenced by peek_key().

Here is the caller graph for this function:

int mihl_log ( mihl_ctx_t ctx,
unsigned  level,
const char *  fmt,
  ... 
)

TBD.

Parameters:
ctx opaque context structure as returned by mihl_init()
level MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO |
fmt TBD
... TBD
Returns:
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().

Here is the caller graph for this function:

int mihl_dump_info ( mihl_ctx_t ctx  ) 

TBD.

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
The number of active connexions

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().

Here is the call graph for this function:

Here is the caller graph for this function:

int mihl_info ( mihl_ctx_t ctx,
int  maxnb_cnxinfos,
mihl_cnxinfo_t infos 
)

Provide information on current connections.

Parameters:
ctx opaque context structure as returned by mihl_init()
maxnb_cnxinfos TBD
infos TBD
Returns:
  • number of connections which are documented (which might be 0)
  • or -1 if an error occurred (errno is then set).

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.

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
The number of active connexions

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().

Here is the call graph for this function:

Here is the caller graph for this function:

char* mihl_authorization ( mihl_cnx_t cnx  ) 

TBD.

Parameters:
ctx opaque context structure as returned by mihl_init()
Returns:
Value of the string 'Authorization:' if found, or NULL if not found

Definition at line 912 of file mihl.c.

References mihl_cnx::authorization.


Generated on Mon May 26 18:06:27 2008 for milh-example_3 by  doxygen 1.5.6