Currently, still
under development. |
MIHL is a C library that helps to implement an HTTP embedded server. It is designed to be small and to provide only a minimal set of functionalities. Typically, MIHL can be used in an embedded device where a Web-based interface is useful (configuration, status, etc.). MIHL C library can be compiled for both Linux (x86, PowerPC, ARM) and XP. MIHDL does not use threads, but rather use non blocking sockets I/O.
This very simple example should help to have a good high-level overview of a minimal HTTP server built using this library.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mihl.h"
int http_root( mhil_cnx_t *cnx, char const *tag, char const *host, void *param ) {
mihl_add( cnx, "<html>" );
mihl_add( cnx, "<body>" );
mihl_add( cnx, "This is a test HTML page for MIHL.<br>" );
mihl_add( cnx, "<br>Here is a JPEG Image:<br>" );
mihl_add( cnx, "<img style='width: 70px; height: 72px;' alt='' src='image.jpg'><br><br>" );
mihl_add( cnx, "<a href='nextpage.html'>Next Page<a>" );
mihl_add( cnx, "</body>" );
mihl_add( cnx, "</html>" );
mihl_send( cnx, NULL,
"Content-type: text/html\r\n" );
return 0;
}
int http_nextpage( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
mihl_add( cnx, "<html>" );
mihl_add( cnx, "<body>" );
mihl_add( cnx, "This is another page...<br>" );
mihl_add( cnx, "<a href=''>Previous Page<a>" );
mihl_add( cnx, "</body>" );
mihl_add( cnx, "</html>" );
mihl_send( cnx,
"Content-type: text/html\r\n" );
return 0;
}
int main( int argc, char *argv[] ) {
mihl_ctx_t *ctx = mihl_init( NULL, 8080, 8,
MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | MIHL_LOG_INFO_VERBOSE );
mihl_handle_get( ctx, "/", http_root, NULL );
mihl_handle_file( ctx, "/image.jpg", "image.jpg", "image/jpeg", 0 );
mihl_handle_get( ctx, "/nextpage.html", http_nextpage, NULL );
for (;;) {
int status = mihl_server( ctx );
if ( status == -2 )
break;
}
return 0;
}
MIHL uses non-blocking Socket and a single-thread model. You have to call a function (mihl_server) on a regular interval, in order to manage new connections, existent connections and timed out connections. You install C handler functions to build pages for specifics URL (such as “/”, “/mypage.html” and so forth).
Only a C API to the library is currently provided. The library can be build only for Linux, there is no plan to support Window$ platforms.
Doxygen generated documentation for the library is available on the left pane.
Please note that only about 25% is done so far, the documentation is still a work in progress.
Function |
Prototype
|
Description |
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. |
mihl_end |
int mihl_end( mihl_ctx_t *ctx ) |
End the server, close all connections and release all resources. |
mihl_server |
int mihl_server( mihl_ctx_t *ctx ) |
Manage new
connections, existent connections, |
mihl_info |
int mihl_info( mihl_ctx_t *ctx , int maxnb_cnxinfos,
mihl_cnxinfo_t *infos
) |
Return
information about existent connections. |
mihl_add |
int mihl_add( mihl_cnx_t *cnx,
char const *fmt, ... ) |
Build HTML
page for a given HTTP URL. |
mihl_send |
int mihl_send( mihl_cnx_t *cnx,
char const *fmt_header, ... ) |
Send an HTML
page build with calls to mihl_add. |
mihl_handle_file |
int mihl_handle_file(mihl_ctx_t *ctx, |
Install a file
handler function (GET). |
mihl_handle_get |
int mihl_handle_get(mihl_ctx_t *ctx , |
Install a GET
handler function. |
mihl_handle_post |
int mihl_handle_post(mihl_ctx_t *ctx, |
Install a POST
handler function. |
Several examples (C code) are provided to demonstrate and to test the use of the library:
Code |
Description |
Simple use of GET. |
|
Demonstrate how to use GET/POST. |
|
Demonstrate the use of |
All
the code for this project is developed using Eclipse/CDT (Eclipse 3.X +
CDT
4.X), and should be preferably compiled using this IDE.
This said, you can also download
the project with SVN and compile it
using the Makefiles (the Eclipses projects are all Makefile based).
The
easiest way to proceed is to use the SVN plugin
within Eclipse: subclipse.
You can also SVN the code, and then import the projects from Eclipse.
Or you can forget Eclipse, and just us the makefile.
Please note the following regarding the code:
· This is C code, not C++ code. Actually, the code is gnu99 (iso99 + gnu extensions).
· If you want to build the documentation (generated using Doxygen), you’ll need to have doxygen and graphviz installed. I suggest installing doxywizard as well, but you might try eclox as well.
This project is released under the BSD license: see here for details.
Developed
and tested using Fedora 7, then Fedor 8 and now Fedora 9. The code is
used on both X86 and ARM9 platforms.
Full source code |
svn co
https://mihl.svn.sourceforge.net/svnroot/mihl |
Anonymous SVN access |
|
Project Statistics |
|