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 3</title>" );
00041 mihl_add( cnx, "<head>" );
00042 mihl_add( cnx, " <meta content='text/html; charset=ISO-8859-1'" );
00043 mihl_add( cnx, " http-equiv='content-type'>" );
00044 mihl_add( cnx, " <title>blah blah</title>" );
00045 mihl_add( cnx, "</head>" );
00046 mihl_add( cnx, "<body style='color: rgb(0, 0, 0); background-color: rgb(204, 204, 204);'" );
00047 mihl_add( cnx, " alink='#000099' link='#000099' vlink='#990099'>" );
00048 mihl_add( cnx, "<div style='text-align: center; font-weight: bold;'><big>Test AJAX" );
00049 mihl_add( cnx, "(prototype.js)<br>" );
00050 mihl_add( cnx, "</big></div>" );
00051 mihl_add( cnx, "<br>" );
00052 mihl_add( cnx, "<div style='text-align: center;'><img style='width: 70px; height: 72px;'" );
00053 mihl_add( cnx, " alt='' src='/image.jpg'><br>" );
00054 mihl_add( cnx, "</div>" );
00055 mihl_add( cnx, "<br>" );
00056 mihl_add( cnx, "<table" );
00057 mihl_add( cnx, " style='text-align: left; background-color: rgb(208, 208, 208); margin-left: auto; margin-right: auto;'" );
00058 mihl_add( cnx, " border='1' cellpadding='2' cellspacing='2'>" );
00059 mihl_add( cnx, " <tbody>" );
00060 mihl_add( cnx, " <tr>" );
00061 mihl_add( cnx, " <td style='vertical-align: top;'>Refreshed each second<br>" );
00062 mihl_add( cnx, " </td>" );
00063 mihl_add( cnx, " <td style='vertical-align: top;' id='field1'>XXXX<br>" );
00064 mihl_add( cnx, " </td>" );
00065 mihl_add( cnx, " </tr>" );
00066 mihl_add( cnx, " <tr>" );
00067 mihl_add( cnx, " <td style='vertical-align: top;'>Refreshed each 5 seconds<br>" );
00068 mihl_add( cnx, " </td>" );
00069 mihl_add( cnx, " <td style='vertical-align: top;' id='field2'>XXXX<br>" );
00070 mihl_add( cnx, " </td>" );
00071 mihl_add( cnx, " </tr>" );
00072 mihl_add( cnx, " <tr>" );
00073 mihl_add( cnx, " <td style='vertical-align: top;'>Refreshed each 30 seconds<br>" );
00074 mihl_add( cnx, " </td>" );
00075 mihl_add( cnx, " <td style='vertical-align: top;' id='field3'>XXXX<br>" );
00076 mihl_add( cnx, " </td>" );
00077 mihl_add( cnx, " </tr>" );
00078 mihl_add( cnx, " </tbody>" );
00079 mihl_add( cnx, "</table>" );
00080 mihl_add( cnx, "<br>" );
00081 mihl_add( cnx, "</body>" );
00082
00083 mihl_add( cnx, "<SCRIPT type='text/javascript' src='/prototype.js'> </SCRIPT>" );
00084
00085 mihl_add( cnx, "<SCRIPT type='text/javascript'>" );
00086 mihl_add( cnx, "<!--" );
00087
00088 mihl_add( cnx, "var cpt = 0;" );
00089 mihl_add( cnx, "function onLoadHandler( ) {" );
00090 mihl_add( cnx, " setInterval( 'timerFunction1()', 1000 );" );
00091 mihl_add( cnx, " setInterval( 'timerFunction5()', 5000 );" );
00092 mihl_add( cnx, " setInterval( 'timerFunction30()', 30000 );" );
00093 mihl_add( cnx, "};" );
00094
00095 mihl_add( cnx, "function timerFunction1( ) {" );
00096 mihl_add( cnx, " var url = 'http://%s/data1';", host );
00097 mihl_add( cnx, " var myAjax = new Ajax.Request( url, { method: 'get', onComplete: showData1 });" );
00098 mihl_add( cnx, "};" );
00099
00100 mihl_add( cnx, "function timerFunction5( ) {" );
00101 mihl_add( cnx, " var url = 'http://%s/data2';", host );
00102 mihl_add( cnx, " var myAjax = new Ajax.Request( url, { method: 'get', onComplete: showData2 });" );
00103 mihl_add( cnx, "};" );
00104
00105 mihl_add( cnx, "function timerFunction30( ) {" );
00106 mihl_add( cnx, " var url = 'http://%s/data3';", host );
00107 mihl_add( cnx, " var myAjax = new Ajax.Request( url, { method: 'get', onComplete: showData3 });" );
00108 mihl_add( cnx, "};" );
00109
00110 mihl_add( cnx, "function showData1( originalRequest ) {" );
00111 mihl_add( cnx, " document.getElementById('field1').childNodes[0].nodeValue = originalRequest.responseText;" );
00112
00113 mihl_add( cnx, "};" );
00114
00115 mihl_add( cnx, "function showData2( originalRequest ) {" );
00116 mihl_add( cnx, " document.getElementById('field2').childNodes[0].nodeValue = originalRequest.responseText;" );
00117
00118 mihl_add( cnx, "};" );
00119
00120 mihl_add( cnx, "function showData3( originalRequest ) {" );
00121 mihl_add( cnx, " document.getElementById('field3').childNodes[0].nodeValue = originalRequest.responseText;" );
00122
00123 mihl_add( cnx, "};" );
00124
00125 mihl_add( cnx, "-->" );
00126 mihl_add( cnx, "</SCRIPT>" );
00127
00128 mihl_add( cnx, "<BODY onLoad='onLoadHandler();'>" );
00129 mihl_add( cnx, "</BODY>" );
00130
00131 mihl_add( cnx, "</html>" );
00132 mihl_send( cnx, NULL,
00133 "Content-type: text/html\r\n" );
00134 return 0;
00135 }
00136
00146 int http_data( mihl_cnx_t *cnx, char const *tag, char const *host, void *param ) {
00147 int index = (int)param;
00148 static int cpts[3] = { 0, 0, 0 };
00149 mihl_add( cnx, "cpt=%d", cpts[index]++ );
00150 mihl_send( cnx, NULL,
00151 "Content-type: text/xml\r\n" );
00152 return 0;
00153 }
00154
00164 int main( int argc, char *argv[] ) {
00165
00166 help( 8080 );
00167
00168 mihl_ctx_t *ctx = mihl_init( NULL, 8080, 8,
00169 MIHL_LOG_ERROR | MIHL_LOG_WARNING | MIHL_LOG_INFO | MIHL_LOG_INFO_VERBOSE );
00170 if ( !ctx )
00171 return -1;
00172
00173 mihl_handle_get( ctx, "/", http_root, NULL );
00174 mihl_handle_get( ctx, "/data1", http_data, (void *)0 );
00175 mihl_handle_get( ctx, "/data2", http_data, (void *)1 );
00176 mihl_handle_get( ctx, "/data3", http_data, (void *)2 );
00177 if ( access( "../image.jpg", R_OK ) == 0 ) {
00178 mihl_handle_file( ctx, "/image.jpg", "../image.jpg", "image/jpeg", 0 );
00179 mihl_handle_file( ctx, "/prototype.js", "../prototype.js", "text/javascript", 0 );
00180 }
00181 else {
00182 mihl_handle_file( ctx, "/image.jpg", "/etc/mihl/examples/3/image.jpg", "image/jpeg", 0 );
00183 mihl_handle_file( ctx, "/prototype.js", "/etc/mihl/examples/3/prototype.js", "text/javascript", 0 );
00184 }
00185
00186 for (;;) {
00187 int status = mihl_server( ctx );
00188 if ( status == -2 )
00189 break;
00190 if ( peek_key( ctx ) )
00191 break;
00192 }
00193
00194 return 0;
00195 }