httpd/httpserv.c

Log
Revision 1.19 2008/07/25 10:20:12 olereinhardt Fixed compiler bug for AVR-ICC and added missing PSTR macro around prog_char strings

Revision 1.18 2008/07/17 11:56:20 olereinhardt Updated the webserver demo to show new webserver functions (different cgi pathes with seperate authentication, $QUERY_STRING parameter for ssi included CGIs)

Revision 1.17 2008/05/13 19:31:34 thiagocorrea NutHttpSendHeaderBot is marked as deprecated, use NutHttpSendHeaderBottom instead.

Revision 1.16 2007/07/17 18:29:30 haraldkipp Server thread names not unique on SAM7X. Fixed by Marti Raudsepp.

Revision 1.15 2006/09/07 09:01:36 haraldkipp Discovery registration added. Re-arranged network interface setup to exclude DHCP code from ICCAVR builds and make it work with the demo compiler. Unfinished. Added PHAT file system support. Untested.

Revision 1.14 2006/03/02 19:44:03 haraldkipp MMC and PHAT enabled.

Revision 1.13 2006/01/11 08:32:57 hwmaier Added explicit type casts to silence a few avr-gcc 3.4.3 warning messages

Revision 1.12 2005/11/22 09:14:13 haraldkipp Replaced specific device names by generalized macros.

Revision 1.11 2005/10/16 23:22:20 hwmaier Removed unreferenced nutconfig.h include statement

Revision 1.10 2005/08/05 11:32:50 olereinhardt Added SSI and ASP sample

Revision 1.9 2005/04/05 18:04:17 haraldkipp Support for ARM7 Wolf Board added.

Revision 1.8 2005/02/23 04:39:26 hwmaier no message

Revision 1.7 2005/02/22 02:44:34 hwmaier Changes to compile as well for AT90CAN128 device.

Revision 1.6 2004/12/16 10:17:18 haraldkipp Added Mikael Adolfsson's excellent parameter parsing routines.

Revision 1.5 2004/03/16 16:48:26 haraldkipp Added Jan Dubiec's H8/300 port.

Revision 1.4 2003/11/04 17:46:52 haraldkipp Adapted to Ethernut 2

Revision 1.3 2003/09/29 16:33:12 haraldkipp Using portable strtok and strtok_r

Revision 1.2 2003/08/07 08:27:58 haraldkipp Bugfix, remote not displayed in socket list

Revision 1.1 2003/07/20 15:56:14 haraldkipp *** empty log message ***

Simple multithreaded HTTP daemon.

00001 /*
00002  * Copyright (C) 2001-2004 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00107 /* 
00108  * Unique MAC address of the Ethernut Board. 
00109  *
00110  * Ignored if EEPROM contains a valid configuration.
00111  */
00112 #define MY_MAC  "\x00\x06\x98\x30\x00\x35"
00113 
00114 /* 
00115  * Unique IP address of the Ethernut Board. 
00116  *
00117  * Ignored if DHCP is used. 
00118  */
00119 #define MY_IPADDR "192.168.192.35"
00120 
00121 /* 
00122  * IP network mask of the Ethernut Board.
00123  *
00124  * Ignored if DHCP is used. 
00125  */
00126 #define MY_IPMASK "255.255.255.0"
00127 
00128 /* 
00129  * Gateway IP address for the Ethernut Board.
00130  *
00131  * Ignored if DHCP is used. 
00132  */
00133 #define MY_IPGATE "192.168.192.1"
00134 
00135 /* ICCAVR Demo is limited. Try to use the bare minimum. */
00136 #if !defined(__IMAGECRAFT__)
00137 
00138 /* Wether we should use DHCP. */
00139 #define USE_DHCP
00140 /* Wether we should run a discovery responder. */
00141 #define USE_DISCOVERY
00142 /* Wether to use PHAT file system. */
00143 //#define USE_PHAT
00144 
00145 #endif /* __IMAGECRAFT__ */
00146 
00147 
00148 #ifdef USE_PHAT
00149 
00150 #if defined(ETHERNUT3)
00151 
00152 /* Ethernut 3 file system. */
00153 #define MY_FSDEV       devPhat0
00154 #define MY_FSDEV_NAME  "PHAT0" 
00155 
00156 /* Ethernut 3 block device interface. */
00157 #define MY_BLKDEV      devNplMmc0
00158 #define MY_BLKDEV_NAME "MMC0"
00159 
00160 #elif defined(AT91SAM7X_EK)
00161 
00162 /* SAM7X-EK file system. */
00163 #define MY_FSDEV       devPhat0
00164 #define MY_FSDEV_NAME  "PHAT0" 
00165 
00166 /* SAM7X-EK block device interface. */
00167 #define MY_BLKDEV      devAt91SpiMmc0
00168 #define MY_BLKDEV_NAME "MMC0"
00169 
00170 #elif defined(AT91SAM9260_EK)
00171 
00172 /* SAM9260-EK file system. */
00173 #define MY_FSDEV       devPhat0
00174 #define MY_FSDEV_NAME  "PHAT0" 
00175 
00176 /* SAM9260-EK block device interface. */
00177 #define MY_BLKDEV      devAt91Mci0
00178 #define MY_BLKDEV_NAME "MCI0"
00179 
00180 #endif
00181 #endif /* USE_PHAT */
00182 
00183 #ifndef MY_FSDEV
00184 #define MY_FSDEV        devUrom
00185 #endif
00186 
00187 #ifdef MY_FSDEV_NAME
00188 #define MY_HTTPROOT     MY_FSDEV_NAME ":/" 
00189 #endif
00190 
00191 
00192 #include <cfg/os.h>
00193 
00194 #include <string.h>
00195 #include <io.h>
00196 #include <fcntl.h>
00197 #include <time.h>
00198 
00199 #include <dev/board.h>
00200 #include <dev/urom.h>
00201 #include <dev/nplmmc.h>
00202 #include <dev/sbimmc.h>
00203 #include <fs/phatfs.h>
00204 
00205 #include <sys/version.h>
00206 #include <sys/thread.h>
00207 #include <sys/timer.h>
00208 #include <sys/heap.h>
00209 #include <sys/confnet.h>
00210 #include <sys/socket.h>
00211 
00212 #include <arpa/inet.h>
00213 #include <net/route.h>
00214 
00215 #include <pro/httpd.h>
00216 #include <pro/dhcp.h>
00217 #include <pro/ssi.h>
00218 #include <pro/asp.h>
00219 #include <pro/discover.h>
00220 
00221 #ifdef NUTDEBUG
00222 #include <sys/osdebug.h>
00223 #include <net/netdebug.h>
00224 #endif
00225 
00226 static char *html_mt = "text/html";
00227 
00228 
00229 /**************************************************************/
00230 /*  ASPCallback                                               */
00231 /*                                                            */
00232 /* This routine must have been registered by                  */
00233 /* NutRegisterAspCallback() and is automatically called by    */
00234 /* NutHttpProcessFileRequest() when the server process a page */
00235 /* with an asp function.                                      */
00236 /*                                                            */
00237 /* Return 0 on success, -1 otherwise.                         */
00238 /**************************************************************/
00239 
00240 
00241 static int ASPCallback (char *pASPFunction, FILE *stream)
00242 {
00243     if (strcmp(pASPFunction, "usr_date") == 0) {
00244         fprintf(stream, "Dummy example: 01.01.2005");
00245         return(0);
00246     }
00247 
00248     if (strcmp(pASPFunction, "usr_time") == 0) {
00249         fprintf(stream, "Dummy example: 12:15:02");
00250         return(0);
00251     }
00252 
00253     return (-1);
00254 }
00255 
00256 /*
00257  * CGI Sample: Show request parameters.
00258  *
00259  * See httpd.h for REQUEST structure.
00260  *
00261  * This routine must have been registered by NutRegisterCgi() and is
00262  * automatically called by NutHttpProcessRequest() when the client
00263  * request the URL 'cgi-bin/test.cgi'.
00264  */
00265 static int ShowQuery(FILE * stream, REQUEST * req)
00266 {
00267     char *cp;
00268     /*
00269      * This may look a little bit weird if you are not used to C programming
00270      * for flash microcontrollers. The special type 'prog_char' forces the
00271      * string literals to be placed in flash ROM. This saves us a lot of
00272      * precious RAM.
00273      */
00274     static prog_char head[] = "<HTML><HEAD><TITLE>Parameters</TITLE></HEAD><BODY><H1>Parameters</H1>";
00275     static prog_char foot[] = "</BODY></HTML>";
00276     static prog_char req_fmt[] = "Method: %s<BR>\r\nVersion: HTTP/%d.%d<BR>\r\nContent length: %ld<BR>\r\n";
00277     static prog_char url_fmt[] = "URL: %s<BR>\r\n";
00278     static prog_char query_fmt[] = "Argument: %s<BR>\r\n";
00279     static prog_char type_fmt[] = "Content type: %s<BR>\r\n";
00280     static prog_char cookie_fmt[] = "Cookie: %s<BR>\r\n";
00281     static prog_char auth_fmt[] = "Auth info: %s<BR>\r\n";
00282     static prog_char agent_fmt[] = "User agent: %s<BR>\r\n";
00283 
00284     /* These useful API calls create a HTTP response for us. */
00285     NutHttpSendHeaderTop(stream, req, 200, "Ok");
00286     NutHttpSendHeaderBottom(stream, req, html_mt, -1);
00287 
00288     /* Send HTML header. */
00289     fputs_P(head, stream);
00290 
00291     /*
00292      * Send request parameters.
00293      */
00294     switch (req->req_method) {
00295     case METHOD_GET:
00296         cp = "GET";
00297         break;
00298     case METHOD_POST:
00299         cp = "POST";
00300         break;
00301     case METHOD_HEAD:
00302         cp = "HEAD";
00303         break;
00304     default:
00305         cp = "UNKNOWN";
00306         break;
00307     }
00308     fprintf_P(stream, req_fmt, cp, req->req_version / 10, req->req_version % 10, req->req_length);
00309     if (req->req_url)
00310         fprintf_P(stream, url_fmt, req->req_url);
00311     if (req->req_query)
00312         fprintf_P(stream, query_fmt, req->req_query);
00313     if (req->req_type)
00314         fprintf_P(stream, type_fmt, req->req_type);
00315     if (req->req_cookie)
00316         fprintf_P(stream, cookie_fmt, req->req_cookie);
00317     if (req->req_auth)
00318         fprintf_P(stream, auth_fmt, req->req_auth);
00319     if (req->req_agent)
00320         fprintf_P(stream, agent_fmt, req->req_agent);
00321 
00322     /* Send HTML footer and flush output buffer. */
00323     fputs_P(foot, stream);
00324     fflush(stream);
00325 
00326     return 0;
00327 }
00328 
00329 /*
00330  * CGI Sample: Show list of threads.
00331  *
00332  * This routine must have been registered by NutRegisterCgi() and is
00333  * automatically called by NutHttpProcessRequest() when the client
00334  * request the URL 'cgi-bin/threads.cgi'.
00335  */
00336 static int ShowThreads(FILE * stream, REQUEST * req)
00337 {
00338     static prog_char head[] = "<HTML><HEAD><TITLE>Threads</TITLE></HEAD><BODY><H1>Threads</H1>\r\n"
00339         "<TABLE BORDER><TR><TH>Handle</TH><TH>Name</TH><TH>Priority</TH><TH>Status</TH><TH>Event<BR>Queue</TH><TH>Timer</TH><TH>Stack-<BR>pointer</TH><TH>Free<BR>Stack</TH></TR>\r\n";
00340 #if defined(__AVR__)
00341     static prog_char tfmt[] =
00342         "<TR><TD>%04X</TD><TD>%s</TD><TD>%u</TD><TD>%s</TD><TD>%04X</TD><TD>%04X</TD><TD>%04X</TD><TD>%u</TD><TD>%s</TD></TR>\r\n";
00343 #else
00344     static prog_char tfmt[] =
00345         "<TR><TD>%08lX</TD><TD>%s</TD><TD>%u</TD><TD>%s</TD><TD>%08lX</TD><TD>%08lX</TD><TD>%08lX</TD><TD>%lu</TD><TD>%s</TD></TR>\r\n";
00346 #endif
00347     static prog_char foot[] = "</TABLE></BODY></HTML>";
00348     static char *thread_states[] = { "TRM", "<FONT COLOR=#CC0000>RUN</FONT>", "<FONT COLOR=#339966>RDY</FONT>", "SLP" };
00349     NUTTHREADINFO *tdp = nutThreadList;
00350 
00351     /* Send HTTP response. */
00352     NutHttpSendHeaderTop(stream, req, 200, "Ok");
00353     NutHttpSendHeaderBottom(stream, req, html_mt, -1);
00354 
00355     /* Send HTML header. */
00356     fputs_P(head, stream);
00357     for (tdp = nutThreadList; tdp; tdp = tdp->td_next) {
00358         fprintf_P(stream, tfmt, (uptr_t) tdp, tdp->td_name, tdp->td_priority,
00359                   thread_states[tdp->td_state], (uptr_t) tdp->td_queue, (uptr_t) tdp->td_timer,
00360                   (uptr_t) tdp->td_sp, (uptr_t) tdp->td_sp - (uptr_t) tdp->td_memory,
00361                   *((u_long *) tdp->td_memory) != DEADBEEF ? "Corr" : "OK");
00362     }
00363     fputs_P(foot, stream);
00364     fflush(stream);
00365 
00366     return 0;
00367 }
00368 
00369 /*
00370  * CGI Sample: Show list of timers.
00371  *
00372  * This routine must have been registered by NutRegisterCgi() and is
00373  * automatically called by NutHttpProcessRequest() when the client
00374  * request the URL 'cgi-bin/timers.cgi'.
00375  */
00376 static int ShowTimers(FILE * stream, REQUEST * req)
00377 {
00378     static prog_char head[] = "<HTML><HEAD><TITLE>Timers</TITLE></HEAD><BODY><H1>Timers</H1>\r\n";
00379     static prog_char thead[] =
00380         "<TABLE BORDER><TR><TH>Handle</TH><TH>Countdown</TH><TH>Tick Reload</TH><TH>Callback<BR>Address</TH><TH>Callback<BR>Argument</TH></TR>\r\n";
00381 #if defined(__AVR__)
00382     static prog_char tfmt[] = "<TR><TD>%04X</TD><TD>%lu</TD><TD>%lu</TD><TD>%04X</TD><TD>%04X</TD></TR>\r\n";
00383 #else
00384     static prog_char tfmt[] = "<TR><TD>%08lX</TD><TD>%lu</TD><TD>%lu</TD><TD>%08lX</TD><TD>%08lX</TD></TR>\r\n";
00385 #endif
00386     static prog_char foot[] = "</TABLE></BODY></HTML>";
00387     NUTTIMERINFO *tnp;
00388     u_long ticks_left;
00389 
00390     NutHttpSendHeaderTop(stream, req, 200, "Ok");
00391     NutHttpSendHeaderBottom(stream, req, html_mt, -1);
00392 
00393     /* Send HTML header. */
00394     fputs_P(head, stream);
00395     if ((tnp = nutTimerList) != 0) {
00396         fputs_P(thead, stream);
00397         ticks_left = 0;
00398         while (tnp) {
00399             ticks_left += tnp->tn_ticks_left;
00400             fprintf_P(stream, tfmt, (uptr_t) tnp, ticks_left, tnp->tn_ticks, (uptr_t) tnp->tn_callback, (uptr_t) tnp->tn_arg);
00401             tnp = tnp->tn_next;
00402         }
00403     }
00404 
00405     fputs_P(foot, stream);
00406     fflush(stream);
00407 
00408     return 0;
00409 }
00410 
00411 /*
00412  * CGI Sample: Show list of sockets.
00413  *
00414  * This routine must have been registered by NutRegisterCgi() and is
00415  * automatically called by NutHttpProcessRequest() when the client
00416  * request the URL 'cgi-bin/sockets.cgi'.
00417  */
00418 static int ShowSockets(FILE * stream, REQUEST * req)
00419 {
00420     /* String literals are kept in flash ROM. */
00421     static prog_char head[] = "<HTML><HEAD><TITLE>Sockets</TITLE></HEAD>"
00422         "<BODY><H1>Sockets</H1>\r\n"
00423         "<TABLE BORDER><TR><TH>Handle</TH><TH>Type</TH><TH>Local</TH><TH>Remote</TH><TH>Status</TH></TR>\r\n";
00424 #if defined(__AVR__)
00425     static prog_char tfmt1[] = "<TR><TD>%04X</TD><TD>TCP</TD><TD>%s:%u</TD>";
00426 #else
00427     static prog_char tfmt1[] = "<TR><TD>%08lX</TD><TD>TCP</TD><TD>%s:%u</TD>";
00428 #endif
00429     static prog_char tfmt2[] = "<TD>%s:%u</TD><TD>";
00430     static prog_char foot[] = "</TABLE></BODY></HTML>";
00431     static prog_char st_listen[] = "LISTEN";
00432     static prog_char st_synsent[] = "SYNSENT";
00433     static prog_char st_synrcvd[] = "SYNRCVD";
00434     static prog_char st_estab[] = "<FONT COLOR=#CC0000>ESTABL</FONT>";
00435     static prog_char st_finwait1[] = "FINWAIT1";
00436     static prog_char st_finwait2[] = "FINWAIT2";
00437     static prog_char st_closewait[] = "CLOSEWAIT";
00438     static prog_char st_closing[] = "CLOSING";
00439     static prog_char st_lastack[] = "LASTACK";
00440     static prog_char st_timewait[] = "TIMEWAIT";
00441     static prog_char st_closed[] = "CLOSED";
00442     static prog_char st_unknown[] = "UNKNOWN";
00443     prog_char *st_P;
00444     extern TCPSOCKET *tcpSocketList;
00445     TCPSOCKET *ts;
00446 
00447     NutHttpSendHeaderTop(stream, req, 200, "Ok");
00448     NutHttpSendHeaderBottom(stream, req, html_mt, -1);
00449 
00450     /* Send HTML header. */
00451     fputs_P(head, stream);
00452     for (ts = tcpSocketList; ts; ts = ts->so_next) {
00453         switch (ts->so_state) {
00454         case TCPS_LISTEN:
00455             st_P = (prog_char *) st_listen;
00456             break;
00457         case TCPS_SYN_SENT:
00458             st_P = (prog_char *) st_synsent;
00459             break;
00460         case TCPS_SYN_RECEIVED:
00461             st_P = (prog_char *) st_synrcvd;
00462             break;
00463         case TCPS_ESTABLISHED:
00464             st_P = (prog_char *) st_estab;
00465             break;
00466         case TCPS_FIN_WAIT_1:
00467             st_P = (prog_char *) st_finwait1;
00468             break;
00469         case TCPS_FIN_WAIT_2:
00470             st_P = (prog_char *) st_finwait2;
00471             break;
00472         case TCPS_CLOSE_WAIT:
00473             st_P = (prog_char *) st_closewait;
00474             break;
00475         case TCPS_CLOSING:
00476             st_P = (prog_char *) st_closing;
00477             break;
00478         case TCPS_LAST_ACK:
00479             st_P = (prog_char *) st_lastack;
00480             break;
00481         case TCPS_TIME_WAIT:
00482             st_P = (prog_char *) st_timewait;
00483             break;
00484         case TCPS_CLOSED:
00485             st_P = (prog_char *) st_closed;
00486             break;
00487         default:
00488             st_P = (prog_char *) st_unknown;
00489             break;
00490         }
00491         /*
00492          * Fixed a bug reported by Zhao Weigang.
00493          */
00494         fprintf_P(stream, tfmt1, (uptr_t) ts, inet_ntoa(ts->so_local_addr), ntohs(ts->so_local_port));
00495         fprintf_P(stream, tfmt2, inet_ntoa(ts->so_remote_addr), ntohs(ts->so_remote_port));
00496         fputs_P(st_P, stream);
00497         fputs("</TD></TR>\r\n", stream);
00498         fflush(stream);
00499     }
00500 
00501     fputs_P(foot, stream);
00502     fflush(stream);
00503 
00504     return 0;
00505 }
00506 
00507 /*
00508  * CGI Sample: Proccessing a form.
00509  *
00510  * This routine must have been registered by NutRegisterCgi() and is
00511  * automatically called by NutHttpProcessRequest() when the client
00512  * request the URL 'cgi-bin/form.cgi'.
00513  *
00514  * Thanks to Tom Boettger, who provided this sample for ICCAVR.
00515  */
00516 int ShowForm(FILE * stream, REQUEST * req)
00517 {
00518     static prog_char html_head[] = "<HTML><BODY><BR><H1>Form Result</H1><BR><BR>";
00519     static prog_char html_body[] = "<BR><BR><p><a href=\"../index.html\">return to main</a></BODY></HTML></p>";
00520 
00521     NutHttpSendHeaderTop(stream, req, 200, "Ok");
00522     NutHttpSendHeaderBottom(stream, req, html_mt, -1);
00523 
00524     /* Send HTML header. */
00525     fputs_P(html_head, stream);
00526 
00527     if (req->req_query) {
00528         char *name;
00529         char *value;
00530         int i;
00531         int count;
00532 
00533         count = NutHttpGetParameterCount(req);
00534         /* Extract count parameters. */
00535         for (i = 0; i < count; i++) {
00536             name = NutHttpGetParameterName(req, i);
00537             value = NutHttpGetParameterValue(req, i);
00538 
00539             /* Send the parameters back to the client. */
00540 
00541 #ifdef __IMAGECRAFT__
00542             fprintf(stream, "%s: %s<BR>\r\n", name, value);
00543 #else
00544             fprintf_P(stream, PSTR("%s: %s<BR>\r\n"), name, value);
00545 #endif
00546         }
00547     }
00548 
00549     fputs_P(html_body, stream);
00550     fflush(stream);
00551 
00552     return 0;
00553 }
00554 
00555 /*
00556  * CGI Sample: Dynamic output cgi included by ssi.shtml file
00557  *
00558  * This routine must have been registered by NutRegisterCgi() and is
00559  * automatically called by NutHttpProcessRequest() when the client
00560  * request the URL 'cgi-bin/form.cgi'.
00561  *
00562  * Thanks to Tom Boettger, who provided this sample for ICCAVR.
00563  */
00564 int SSIDemoCGI(FILE * stream, REQUEST * req)
00565 {
00566     if (req->req_query) {
00567         char *name;
00568         char *value;
00569         int i;
00570         int count;
00571 
00572         count = NutHttpGetParameterCount(req);
00573         
00574         /* Extract count parameters. */
00575 #ifdef __IMAGECRAFT__        
00576         fprintf(stream, "CGI ssi-demo.cgi called with parameters: These are the parameters\r\n<p>");
00577 #else
00578         fprintf_P(stream, PSTR("CGI ssi-demo.cgi called with parameters: These are the parameters\r\n<p>"));
00579 #endif
00580         for (i = 0; i < count; i++) {
00581             name = NutHttpGetParameterName(req, i);
00582             value = NutHttpGetParameterValue(req, i);
00583 
00584             /* Send the parameters back to the client. */
00585 
00586 #ifdef __IMAGECRAFT__
00587             fprintf(stream, "%s: %s<BR>\r\n", name, value);
00588 #else
00589             fprintf_P(stream, PSTR("%s: %s<BR>\r\n"), name, value);
00590 #endif
00591         }
00592     } else {
00593         time_t now;
00594         tm     loc_time;
00595         
00596         /* Called without any parameter, show the current time */
00597         now = time(NULL);
00598         localtime_r(&now, &loc_time);
00599 #ifdef __IMAGECRAFT__        
00600         fprintf(stream, "CGI ssi-demo.cgi called without any parameter.<br><br>Current time is: %02d.%02d.%04d -- %02d:%02d:%02d<br>\r\n",
00601                   loc_time.tm_mday, loc_time.tm_mon+1, loc_time.tm_year+1900, loc_time.tm_hour, loc_time.tm_min, loc_time.tm_sec);
00602 #else 
00603         fprintf_P(stream, PSTR("CGI ssi-demo.cgi called without any parameter.<br><br>Current time is: %02d.%02d.%04d -- %02d:%02d:%02d<br>\r\n"),
00604                   loc_time.tm_mday, loc_time.tm_mon+1, loc_time.tm_year+1900, loc_time.tm_hour, loc_time.tm_min, loc_time.tm_sec);
00605 #endif
00606     }
00607 
00608     fflush(stream);
00609 
00610     return 0;
00611 }
00612 
00625 THREAD(Service, arg)
00626 {
00627     TCPSOCKET *sock;
00628     FILE *stream;
00629     u_char id = (u_char) ((uptr_t) arg);
00630 
00631     /*
00632      * Now loop endless for connections.
00633      */
00634     for (;;) {
00635 
00636         /*
00637          * Create a socket.
00638          */
00639         if ((sock = NutTcpCreateSocket()) == 0) {
00640             printf("[%u] Creating socket failed\n", id);
00641             NutSleep(5000);
00642             continue;
00643         }
00644 
00645         /*
00646          * Listen on port 80. This call will block until we get a connection
00647          * from a client.
00648          */
00649         NutTcpAccept(sock, 80);
00650 #if defined(__AVR__)
00651         printf("[%u] Connected, %u bytes free\n", id, NutHeapAvailable());
00652 #else
00653         printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable());
00654 #endif
00655 
00656         /*
00657          * Wait until at least 8 kByte of free RAM is available. This will
00658          * keep the client connected in low memory situations.
00659          */
00660 #if defined(__AVR__)
00661         while (NutHeapAvailable() < 8192) {
00662 #else
00663         while (NutHeapAvailable() < 4096) {
00664 #endif
00665             printf("[%u] Low mem\n", id);
00666             NutSleep(1000);
00667         }
00668 
00669         /*
00670          * Associate a stream with the socket so we can use standard I/O calls.
00671          */
00672         if ((stream = _fdopen((int) ((uptr_t) sock), "r+b")) == 0) {
00673             printf("[%u] Creating stream device failed\n", id);
00674         } else {
00675             /*
00676              * This API call saves us a lot of work. It will parse the
00677              * client's HTTP request, send any requested file from the
00678              * registered file system or handle CGI requests by calling
00679              * our registered CGI routine.
00680              */
00681             NutHttpProcessRequest(stream);
00682 
00683             /*
00684              * Destroy the virtual stream device.
00685              */
00686             fclose(stream);
00687         }
00688 
00689         /*
00690          * Close our socket.
00691          */
00692         NutTcpCloseSocket(sock);
00693         printf("[%u] Disconnected\n", id);
00694     }
00695 }
00696 
00702 int main(void)
00703 {
00704     u_long baud = 115200;
00705     u_char i;
00706 
00707     /*
00708      * Initialize the uart device.
00709      */
00710     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00711     freopen(DEV_DEBUG_NAME, "w", stdout);
00712     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00713     NutSleep(200);
00714     printf("\n\nNut/OS %s HTTP Daemon...", NutVersionString());
00715 
00716 #ifdef NUTDEBUG
00717     NutTraceTcp(stdout, 0);
00718     NutTraceOs(stdout, 0);
00719     NutTraceHeap(stdout, 0);
00720     NutTracePPP(stdout, 0);
00721 #endif
00722 
00723     /*
00724      * Register Ethernet controller.
00725      */
00726     if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
00727         puts("Registering device failed");
00728     }
00729 
00730     printf("Configure %s...", DEV_ETHER_NAME);
00731     if (NutNetLoadConfig(DEV_ETHER_NAME)) {
00732         u_char mac[] = MY_MAC;
00733 
00734         printf("initial boot...");
00735 #ifdef USE_DHCP
00736         if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000)) 
00737 #endif
00738         {
00739             u_long ip_addr = inet_addr(MY_IPADDR);
00740             u_long ip_mask = inet_addr(MY_IPMASK);
00741             u_long ip_gate = inet_addr(MY_IPGATE);
00742 
00743             printf("No DHCP...");
00744             if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask) == 0) {
00745                 /* Without DHCP we had to set the default gateway manually.*/
00746                 if(ip_gate) {
00747                     printf("hard coded gate...");
00748                     NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
00749                 }
00750                 puts("OK");
00751             }
00752             else {
00753                 puts("failed");
00754             }
00755         }
00756     }
00757     else {
00758 #ifdef USE_DHCP
00759         if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
00760             puts("failed");
00761         }
00762         else {
00763             puts("OK");
00764         }
00765 #else
00766         if (NutNetIfConfig(DEV_ETHER_NAME, 0, 0, confnet.cdn_ip_mask)) {
00767             puts("failed");
00768         }
00769         else {
00770             puts("OK");
00771         }
00772 #endif
00773     }
00774     printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr));
00775 
00776 #ifdef USE_DISCOVERY
00777     NutRegisterDiscovery((u_long)-1, 0, DISF_INITAL_ANN);
00778 #endif
00779 
00780     /*
00781      * Register our device for the file system.
00782      */
00783     NutRegisterDevice(&MY_FSDEV, 0, 0);
00784 
00785 #ifdef MY_BLKDEV
00786     /* Register block device. */
00787     printf("Registering block device '" MY_BLKDEV_NAME "'...");
00788     if (NutRegisterDevice(&MY_BLKDEV, 0, 0)) {
00789         puts("failed");
00790         for (;;);
00791     }
00792     puts("OK");
00793 
00794     /* Mount partition. */
00795     printf("Mounting block device '" MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME "'...");
00796     if (_open(MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME, _O_RDWR | _O_BINARY) == -1) {
00797         puts("failed");
00798         for (;;);
00799     }
00800     puts("OK");
00801 #endif
00802 
00803 #ifdef MY_HTTPROOT
00804     /* Register root path. */
00805     printf("Registering HTTP root '" MY_HTTPROOT "'...");
00806     if (NutRegisterHttpRoot(MY_HTTPROOT)) {
00807         puts("failed");
00808         for (;;);
00809     }
00810     puts("OK");
00811 #endif
00812 
00813     NutRegisterCgiBinPath("cgi-bin/;user/cgi-bin/;admin/cgi-bin/");
00814 
00815 
00816     /*
00817      * Register our CGI sample. This will be called
00818      * by http://host/cgi-bin/test.cgi?anyparams
00819      */
00820     NutRegisterCgi("test.cgi", ShowQuery);
00821 
00822     /* 
00823      * Register a cgi included by the ssi demo. This will show how dynamic 
00824      * content is included in a ssi page and how the request parameters for 
00825      * a site are passed down to the included cgi.
00826      */
00827     
00828     NutRegisterCgi("ssi-demo.cgi", SSIDemoCGI);
00829     
00830     /*
00831      * Register some CGI samples, which display interesting
00832      * system informations.
00833      */
00834     NutRegisterCgi("threads.cgi", ShowThreads);
00835     NutRegisterCgi("timers.cgi", ShowTimers);
00836     NutRegisterCgi("sockets.cgi", ShowSockets);
00837 
00838     /*
00839      * Finally a CGI example to process a form.
00840      */
00841     NutRegisterCgi("form.cgi", ShowForm);
00842 
00843     /*
00844      * Protect the cgi-bin directory with
00845      * user and password.
00846      */
00847     NutRegisterAuth("admin", "root:root");
00848     NutRegisterAuth("user", "user:user");
00849 
00850     /*
00851      * Register SSI and ASP handler
00852      */
00853     NutRegisterSsi();
00854     NutRegisterAsp();
00855     NutRegisterAspCallback(ASPCallback);
00856     /*
00857      * Start four server threads.
00858      */
00859     for (i = 1; i <= 4; i++) {
00860         char thname[] = "httpd0";
00861 
00862         thname[5] = '0' + i;
00863         NutThreadCreate(thname, Service, (void *) (uptr_t) i, NUT_THREAD_MAINSTACK);
00864     }
00865 
00866     /*
00867      * We could do something useful here, like serving a watchdog.
00868      */
00869     NutThreadSetPriority(254);
00870     for (;;) {
00871         NutSleep(60000);
00872     }
00873 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/