00001
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <time.h>
00025 #include <string.h>
00026
00027 #include "mihl.h"
00028
00029 #include "example_utils.h"
00030
00040 static int http_root( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00041 mihl_add( cnx, "<html>" );
00042 mihl_add( cnx, "<title>MIHL - Example 1</title>" );
00043 mihl_add( cnx, "<body>" );
00044 mihl_add( cnx, "This is a test HTML page for MIHL.<br>" );
00045 mihl_add( cnx, "<br>Here is a JPEG Image:<br>" );
00046 mihl_add( cnx, "<img style='width: 70px; height: 72px;' alt='' src='image.jpg'><br><br>" );
00047 mihl_add( cnx, "host= [%s]<br><br>", host );
00048 mihl_add( cnx, "<a href='nextpage.html'>Next Page<a><br><br>" );
00049 mihl_add( cnx, "<a href='unknown.html'>Non-Existent Page<a><br><br>" );
00050 if ( access( "../image.jpg", R_OK ) == 0 ) {
00051 mihl_add( cnx, "(Do <b>wget -r http://mihl.sourceforge.net</b> in the directory where the executable is)<br>" );
00052 mihl_add( cnx, "<a href='index.html'>Static Pages<a><br><br>" );
00053 }
00054 mihl_add( cnx, "<a href='protected.html'>Protected Page<a><br>" );
00055 mihl_add( cnx, " (username=John, password=Smith)<br><br>" );
00056 mihl_add( cnx, "</body>" );
00057 mihl_add( cnx, "</html>" );
00058 mihl_send( cnx, NULL,
00059 "Content-type: text/html\r\n" );
00060 return 0;
00061 }
00062
00072 static int http_page_not_found( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00073 mihl_add( cnx, "<html>" );
00074 mihl_add( cnx, "<head>" );
00075 mihl_add( cnx, "</head>" );
00076 mihl_add( cnx, "<br>" );
00077 mihl_add( cnx, " THIS PAGE IS NOT FOUND " );
00078 mihl_add( cnx, "<br>" );
00079 mihl_add( cnx, "The tag is [%s]", tag );
00080 mihl_add( cnx, "<br>" );
00081 mihl_add( cnx, "</body>" );
00082 mihl_add( cnx, "</html>" );
00083 return mihl_send( cnx, NULL,
00084 "Content-type: text/html\r\n" );
00085 }
00086
00096 int http_nextpage2( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00097
00098 mihl_add( cnx, "<html>" );
00099 mihl_add( cnx, "<body>" );
00100 mihl_add( cnx, "This is another page, again... TWO ...<br>" );
00101 mihl_add( cnx, "<a href='/'>Previous Page<a><br><br>" );
00102 mihl_add( cnx, "<a href='another_unknown.html'>Non-Existent Page<a><br><br>" );
00103 mihl_add( cnx, "</body>" );
00104 mihl_add( cnx, "</html>" );
00105 mihl_send( cnx, NULL,
00106 "Content-type: text/html\r\n" );
00107 return 0;
00108 }
00109
00119 int http_nextpage1( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00120
00121
00122 mihl_handle_get( mihl_get_ctx(cnx), NULL, http_page_not_found, NULL );
00123
00124
00125 mihl_handle_get( mihl_get_ctx(cnx), "/nextpage.html", http_nextpage2, NULL );
00126
00127 mihl_add( cnx, "<html>" );
00128 mihl_add( cnx, "<body>" );
00129 mihl_add( cnx, "This is another page... ONE ...<br>" );
00130 mihl_add( cnx, "<a href='/'>Previous Page<a><br><br>" );
00131 mihl_add( cnx, "<a href='another_unknown.html'>Non-Existent Page<a><br><br>" );
00132 mihl_add( cnx, "</body>" );
00133 mihl_add( cnx, "</html>" );
00134 mihl_send( cnx, NULL,
00135 "Content-type: text/html\r\n" );
00136 return 0;
00137 }
00138
00148 static int http_index_all_pages( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00149 int l = strlen(tag);
00150 if ( (l < 2) || (tag[0] != '/') ) {
00151 return -1;
00152 }
00153 if ( (l > 4) && !strcmp( &tag[l-4], ".gif" ) )
00154 return send_file( cnx, tag, &tag[1], "image/gif", 0 );
00155 else if ( (l > 4) && !strcmp( &tag[l-4], ".jpg" ) )
00156 return send_file( cnx, tag, &tag[1], "image/jpeg", 0 );
00157 printf( "-- %s\r\n", tag );
00158 fflush( stdout );
00159 return 0;
00160 }
00161
00171 int http_index( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00172 if ( chdir( "mihl.sourceforge.net" ) == -1 ) {
00173 mihl_add( cnx, "<html>" );
00174 mihl_add( cnx, "<title>MIHL - Example 1 - index.html</title>" );
00175 mihl_add( cnx, "<body>" );
00176 mihl_add( cnx, "This is a test HTML page for MIHL.<br>" );
00177 mihl_add( cnx, "<br>Unable to chdir into 'mihl.sourceforge.net': %m<br>" );
00178 mihl_add( cnx, "</body>" );
00179 mihl_add( cnx, "</html>" );
00180 mihl_send( cnx, NULL,
00181 "Content-type: text/html\r\n" );
00182 }
00183
00184
00185 mihl_handle_get( mihl_get_ctx(cnx), NULL, http_index_all_pages, NULL );
00186
00187 int status = send_file( cnx, tag, "index.html", "text/html", 0 );
00188 printf( "status=%d\n", status );
00189 return 0;
00190 }
00191
00201 static int http_protected( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00202
00203 char const *auth = mihl_authorization( cnx );
00204 if ( !auth ) {
00205 mihl_add( cnx, "<html>" );
00206 mihl_add( cnx, "<title>MIHL - Example 1</title>" );
00207 mihl_add( cnx, "<body>" );
00208 mihl_add( cnx, "Blah blah<br>" );
00209 mihl_add( cnx, "</body>" );
00210 mihl_add( cnx, "</html>" );
00211 mihl_send( cnx, "HTTP/1.0 401 Unauthorized\r\n",
00212 "WWW-Authenticate: Basic realm=\"This page is protected!\"\r\n"
00213 "Content-type: text/html\r\n" );
00214 }
00215 else {
00216 int len = strlen( auth );
00217 if ( (len < 7) || strncmp(auth, "Basic ", 6) ) {
00218
00219 }
00220 char realm[80];
00221 mihl_base64_decode( &auth[6], strlen(auth)-6, realm, sizeof(realm)-1 );
00222 if ( !strcmp( realm, "John:Smith" ) ) {
00223 mihl_add( cnx, "<html>" );
00224 mihl_add( cnx, "<body>" );
00225 mihl_add( cnx, "Authentication succedded! Welcome John Smith!<br>" );
00226 mihl_add( cnx, "<a href='/'>Previous Page<a><br><br>" );
00227 mihl_add( cnx, "</body>" );
00228 mihl_add( cnx, "</html>" );
00229 mihl_send( cnx, NULL,
00230 "Content-type: text/html\r\n" );
00231 }
00232 else {
00233 mihl_add( cnx, "<html>" );
00234 mihl_add( cnx, "<body>" );
00235 mihl_add( cnx, "Authentication did not succedded! You are not John Smith!<br>" );
00236 mihl_add( cnx, "<a href='/'>Previous Page<a><br><br>" );
00237 mihl_add( cnx, "</body>" );
00238 mihl_add( cnx, "</html>" );
00239 mihl_send( cnx, NULL,
00240 "Content-type: text/html\r\n" );
00241 }
00242 }
00243 return 0;
00244 }
00245
00255 int main( int argc, char *argv[] ) {
00256
00257 help( 8080 );
00258
00259 mihl_ctx_t *ctx = mihl_init( NULL, 8080, 8,
00260 MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | MIHL_LOG_INFO_VERBOSE );
00261 if ( !ctx )
00262 return -1;
00263
00264 mihl_handle_get( ctx, "/", http_root, NULL );
00265 if ( access( "../image.jpg", R_OK ) == 0 )
00266 mihl_handle_file( ctx, "/image.jpg", "../image.jpg", "image/jpeg", 0 );
00267 else
00268 mihl_handle_file( ctx, "/image.jpg", "/etc/mihl/examples/1/image.jpg", "image/jpeg", 0 );
00269 mihl_handle_get( ctx, "/nextpage.html", http_nextpage1, NULL );
00270 mihl_handle_get( ctx, "/index.html", http_index, NULL );
00271 mihl_handle_get( ctx, "/protected.html", http_protected, NULL );
00272
00273 for (;;) {
00274 int status = mihl_server( ctx );
00275 if ( status == -2 )
00276 break;
00277 if ( peek_key( ctx ) )
00278 break;
00279 }
00280
00281 return 0;
00282 }