cgi.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2001-2003 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 /*
00034  * $Log: cgi.c,v $
00035  * Revision 1.6  2008/07/26 14:09:29  haraldkipp
00036  * Fixed another problem with ICCAVR.
00037  *
00038  * Revision 1.5  2008/07/25 12:17:26  olereinhardt
00039  * Imagecraft compilers does not support alloca (to allocate memory from the
00040  * stack). So we use NutHeapAlloc / NutHeapClear instead for Imagecraft.
00041  *
00042  * Revision 1.4  2008/07/17 11:33:32  olereinhardt
00043  * - Check for unique cgi names
00044  * - Alloc local copy of cgi name. Allows dynamicaly generated cgi names
00045  * - Function to register a different cgi-bin path. Multiple path allowed.
00046  *   See httpd demo for an example
00047  *
00048  * Revision 1.3  2008/07/09 14:23:49  haraldkipp
00049  * Info about NutRegisterCgi's first parameter updated.
00050  *
00051  * Revision 1.2  2006/10/08 16:48:22  haraldkipp
00052  * Documentation fixed
00053  *
00054  * Revision 1.1.1.1  2003/05/09 14:41:56  haraldkipp
00055  * Initial using 3.2.1
00056  *
00057  * Revision 1.7  2003/02/04 18:17:31  harald
00058  * Version 3 released
00059  *
00060  * Revision 1.6  2002/06/26 17:29:49  harald
00061  * First pre-release with 2.4 stack
00062  *
00063  */
00064 
00065 #include <string.h>
00066 #if !defined(__IMAGECRAFT__)
00067 #include <alloca.h>
00068 #endif
00069 #include <sys/heap.h>
00070 
00071 #include <pro/httpd.h>
00072 
00077 
00078 CGIFUNCTION *volatile cgiFunctionList = 0;
00079 char *cgiBinPath = NULL;
00080 
00088 void NutRegisterCgiBinPath(char *path)
00089 {
00090     if (cgiBinPath) {
00091         NutHeapFree(cgiBinPath);
00092     }
00093     
00094     cgiBinPath = NutHeapAlloc(strlen(path) + 1);
00095     strcpy(cgiBinPath, path);    
00096 }
00097      
00108 int NutCgiCheckRequest(FILE * stream, REQUEST * req)
00109 {
00110 #ifdef __GNUC__
00111     /*
00112      * Ole's original version. Unfortunately the ImageCraft compiler neither
00113      * supports alloca() nor strtok_r().
00114      */
00115     char * cgi_bin = "cgi-bin/";
00116     char * tmp;
00117     char * save_ptr = NULL;
00118 
00119     if (cgiBinPath != NULL) {
00120         cgi_bin = cgiBinPath;
00121     }
00122     tmp = alloca(strlen(cgi_bin) + 1);
00123     strcpy(tmp, cgi_bin);
00124     
00125     tmp = strtok_r(tmp, ";", &save_ptr);
00126     
00127     do {
00128         if ((tmp != NULL) && (strncasecmp(req->req_url, tmp, strlen(tmp)) == 0)) {
00129             NutCgiProcessRequest(stream, req, strlen(tmp));
00130             return 1;
00131         }    
00132         tmp = strtok_r(NULL, ";", &save_ptr);
00133     } while (tmp != NULL);
00134 #else
00135     /*
00136      * My version sticks with basic C routines. I hope, that it is also
00137      * faster and uses less memory. But it hasn't been tested very well,
00138      * so let's keep Ole's variant for GCC.
00139      */
00140     size_t len;
00141     CONST char *cp;
00142     CONST char *cgi_bin = cgiBinPath ? cgiBinPath : "cgi-bin/";
00143 
00144     while (*cgi_bin) {
00145         /* Skip leading path separators. */
00146         while (*cgi_bin == ';')
00147             cgi_bin++;
00148         /* Determine the length of the next path component. */
00149         for (len = 0, cp = cgi_bin; *cp && *cp != ';'; len++, cp++);
00150         if (len) {
00151             /* Check if the URL starts with this component. If yes, run the CGI. */
00152             if (strncasecmp(req->req_url, cgi_bin, len) == 0) {
00153                 NutCgiProcessRequest(stream, req, len);
00154                 return 1;
00155             }
00156             /* Try the next path component. */
00157             cgi_bin += len;
00158         }
00159     }
00160 #endif
00161     return 0;
00162 }
00163 
00173 int NutRegisterCgi(char *name, int (*func) (FILE *, REQUEST *))
00174 {
00175     int unique_name = 1;
00176     CGIFUNCTION *cgi;
00177     
00178     cgi = cgiFunctionList;
00179     while (cgi != NULL) {
00180         if (strcmp(name, cgi->cgi_name) == 0) {
00181             unique_name = 0;
00182             break;
00183         }
00184         cgi = cgi->cgi_next;
00185     }
00186     
00187     if ((!unique_name) || ((cgi = NutHeapAlloc(sizeof(CGIFUNCTION))) == 0)) {
00188         return -1;
00189     }
00190     cgi->cgi_next = cgiFunctionList;
00191     cgi->cgi_name = NutHeapAlloc(strlen(name)+1);
00192     strcpy(cgi->cgi_name, name);
00193     cgi->cgi_func = func;
00194     cgiFunctionList = cgi;
00195 
00196     return 0;
00197 }
00198 
00209 void NutCgiProcessRequest(FILE * stream, REQUEST * req, int name_pos)
00210 {
00211     CGIFUNCTION *cgi;
00212     
00213     if (req->req_method != METHOD_GET && req->req_method != METHOD_POST) {
00214         NutHttpSendError(stream, req, 501);
00215         return;
00216     }
00217     
00218     for (cgi = cgiFunctionList; cgi; cgi = cgi->cgi_next) {
00219         if (strcasecmp(cgi->cgi_name, req->req_url + name_pos) == 0)
00220             break;
00221     }
00222     if (cgi == 0)
00223         NutHttpSendError(stream, req, 404);
00224     else if ((*cgi->cgi_func) (stream, req))
00225         NutHttpSendError(stream, req, 500);
00226     return;
00227 }
00228 

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