00001
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <time.h>
00022 #include <string.h>
00023
00024 #include "mihl.h"
00025
00026 #include "example_utils.h"
00027
00037 int http_root( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00038 mihl_add( cnx, "<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>" );
00039 mihl_add( cnx, "<html>" );
00040 mihl_add( cnx, "<title>MIHL - Example 2</title>" );
00041 mihl_add( cnx, " <meta content='text/html; charset=ISO-8859-1'" );
00042 mihl_add( cnx, " http-equiv='content-type'>" );
00043 mihl_add( cnx, " <title>MIHL Test page</title>" );
00044 mihl_add( cnx, "</head>" );
00045 mihl_add( cnx, "<title>MIHL - Example 2</title>" );
00046 mihl_add( cnx, "<body>" );
00047 mihl_add( cnx, "This is a test HTML page for MIHL.<br>" );
00048 mihl_add( cnx, "<br>" );
00049 mihl_add( cnx, "Here is a JPEG Image:<br>" );
00050 mihl_add( cnx, "<img style='width: 70px; height: 72px;' alt=''" );
00051 mihl_add( cnx, " src='image.jpg'><br>" );
00052 mihl_add( cnx, "<br>" );
00053 mihl_add( cnx, "<form method='post' action='toto1'>" );
00054 mihl_add( cnx, "<table style='text-align: left; width: 100%;' border='1' cellpadding='2'" );
00055 mihl_add( cnx, " cellspacing='2'>" );
00056 mihl_add( cnx, " <tbody>" );
00057 mihl_add( cnx, " <tr>" );
00058 mihl_add( cnx, " <td style='vertical-align: top;'>Name<br>" );
00059 mihl_add( cnx, " </td>" );
00060 mihl_add( cnx, " <td style='vertical-align: top;'>" );
00061 mihl_add( cnx, " <input name='myname1' type='text'>" );
00062 mihl_add( cnx, " </td>" );
00063 mihl_add( cnx, " </tr>" );
00064 mihl_add( cnx, " <tr>" );
00065 mihl_add( cnx, " <td style='vertical-align: top;'>City<br>" );
00066 mihl_add( cnx, " </td>" );
00067 mihl_add( cnx, " <td style='vertical-align: top;'>" );
00068 mihl_add( cnx, " <input name='myname2' type='text'>" );
00069 mihl_add( cnx, " </td>" );
00070 mihl_add( cnx, " </tr>" );
00071 mihl_add( cnx, " <tr>" );
00072 mihl_add( cnx, " <td style='vertical-align: top;'>Zip<br>" );
00073 mihl_add( cnx, " </td>" );
00074 mihl_add( cnx, " <td style='vertical-align: top;'>" );
00075 mihl_add( cnx, " <input name='myname3' type='text'>" );
00076 mihl_add( cnx, " </td>" );
00077 mihl_add( cnx, " </tr>" );
00078 mihl_add( cnx, " </tbody>" );
00079 mihl_add( cnx, "</table>" );
00080 mihl_add( cnx, "Press <input type='submit' value='here'> to submit your query." );
00081 mihl_add( cnx, "</form>" );
00082 mihl_add( cnx, "<br>" );
00083 mihl_add( cnx, "</body>" );
00084 mihl_add( cnx, "</html>" );
00085 mihl_send( cnx, NULL,
00086 "Content-type: text/html\r\n" );
00087 return 0;
00088 }
00089
00105 int http_root_post( mihl_cnx_t *cnx, char const *tag, char const *host,
00106 int nb_variables, char **vars_names, char **vars_values,
00107 void *param ) {
00108 mihl_add( cnx, "<html>" );
00109 mihl_add( cnx, "<head>" );
00110 mihl_add( cnx, "nb_variables=%d<BR>", nb_variables );
00111 for ( int n = 0; n < nb_variables; n++ )
00112 mihl_add( cnx, " %2d: %s = [%s]<BR>", n, vars_names[n], vars_values[n] );
00113 mihl_add( cnx, "</body>" );
00114 mihl_add( cnx, "</html>" );
00115 mihl_send( cnx, NULL,
00116 "Content-type: text/html\r\n" );
00117
00118 return 0;
00119 }
00120
00130 int main( int argc, char *argv[] ) {
00131
00132 help( 8080 );
00133
00134 mihl_ctx_t *ctx = mihl_init( NULL, 8080, 8,
00135 MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | MIHL_LOG_INFO_VERBOSE );
00136 if ( !ctx )
00137 return -1;
00138
00139 mihl_handle_get( ctx, "/", http_root, NULL );
00140 if ( access( "../image.jpg", R_OK ) == 0 )
00141 mihl_handle_file( ctx, "/image.jpg", "../image.jpg", "image/jpeg", 0 );
00142 else
00143 mihl_handle_file( ctx, "/image.jpg", "/etc/mihl/examples/2/image.jpg", "image/jpeg", 0 );
00144 mihl_handle_post( ctx, "/toto1", http_root_post, NULL );
00145
00146 for (;;) {
00147 int status = mihl_server( ctx );
00148 if ( status == -2 )
00149 break;
00150 if ( peek_key( ctx ) )
00151 break;
00152 }
00153
00154 return 0;
00155 }