cgi.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*
  33. * $Log$
  34. * Revision 1.9 2009/02/13 14:52:05 haraldkipp
  35. * Include memdebug.h for heap management debugging support.
  36. *
  37. * Revision 1.8 2009/02/06 15:40:29 haraldkipp
  38. * Using newly available strdup() and calloc().
  39. * Replaced NutHeap routines by standard malloc/free.
  40. * Replaced pointer value 0 by NULL.
  41. *
  42. * Revision 1.7 2008/08/22 09:23:56 haraldkipp
  43. * GCC specific implementation removed.
  44. *
  45. * Revision 1.6 2008/07/26 14:09:29 haraldkipp
  46. * Fixed another problem with ICCAVR.
  47. *
  48. * Revision 1.5 2008/07/25 12:17:26 olereinhardt
  49. * Imagecraft compilers does not support alloca (to allocate memory from the
  50. * stack). So we use NutHeapAlloc / NutHeapClear instead for Imagecraft.
  51. *
  52. * Revision 1.4 2008/07/17 11:33:32 olereinhardt
  53. * - Check for unique cgi names
  54. * - Alloc local copy of cgi name. Allows dynamicaly generated cgi names
  55. * - Function to register a different cgi-bin path. Multiple path allowed.
  56. * See httpd demo for an example
  57. *
  58. * Revision 1.3 2008/07/09 14:23:49 haraldkipp
  59. * Info about NutRegisterCgi's first parameter updated.
  60. *
  61. * Revision 1.2 2006/10/08 16:48:22 haraldkipp
  62. * Documentation fixed
  63. *
  64. * Revision 1.1.1.1 2003/05/09 14:41:56 haraldkipp
  65. * Initial using 3.2.1
  66. *
  67. * Revision 1.7 2003/02/04 18:17:31 harald
  68. * Version 3 released
  69. *
  70. * Revision 1.6 2002/06/26 17:29:49 harald
  71. * First pre-release with 2.4 stack
  72. *
  73. */
  74. #include <sys/heap.h>
  75. #include <stdlib.h>
  76. #include <string.h>
  77. #include <memdebug.h>
  78. #include <pro/httpd.h>
  79. /*!
  80. * \addtogroup xgHTTPD
  81. */
  82. /*@{*/
  83. CGIFUNCTION *volatile cgiFunctionList = 0;
  84. char *cgiBinPath = NULL;
  85. /*!
  86. * \brief Register a new cgi-bin path.
  87. *
  88. * This function allows to redfine the cgi-bin path. Default is "cgi-bin/"
  89. * \param path New path.
  90. */
  91. void NutRegisterCgiBinPath(char *path)
  92. {
  93. if (cgiBinPath) {
  94. free(cgiBinPath);
  95. }
  96. cgiBinPath = strdup(path);
  97. }
  98. /*!
  99. * \brief Check if request is a cgi call.
  100. *
  101. * This functions checks the request if it's a cgi all and in case calls the cgi
  102. *
  103. * \param stream Stream of the socket connection, previously opened for
  104. * binary read and write.
  105. * \param req Contains the HTTP request.
  106. */
  107. int NutCgiCheckRequest(FILE * stream, REQUEST * req)
  108. {
  109. /*
  110. * My version sticks with basic C routines. I hope, that it is also
  111. * faster and uses less memory. But it hasn't been tested very well,
  112. * so let's keep Ole's variant for GCC.
  113. */
  114. size_t len;
  115. const char *cp;
  116. const char *cgi_bin = cgiBinPath ? cgiBinPath : "cgi-bin/";
  117. while (*cgi_bin) {
  118. /* Skip leading path separators. */
  119. while (*cgi_bin == ';')
  120. cgi_bin++;
  121. /* Determine the length of the next path component. */
  122. for (len = 0, cp = cgi_bin; *cp && *cp != ';'; len++, cp++);
  123. if (len) {
  124. /* Check if the URL starts with this component. If yes, run the CGI. */
  125. if (strncasecmp(req->req_url, cgi_bin, len) == 0) {
  126. NutCgiProcessRequest(stream, req, len);
  127. return 1;
  128. }
  129. /* Try the next path component. */
  130. cgi_bin += len;
  131. }
  132. }
  133. return 0;
  134. }
  135. /*!
  136. * \brief Register a CGI function.
  137. *
  138. * \param name Name of this CGI function. No dublicates allowed
  139. * \param func The function to be called, if the
  140. * client requests the specified name.
  141. *
  142. * \return 0 on success, -1 otherwise.
  143. */
  144. int NutRegisterCgi(char *name, int (*func) (FILE *, REQUEST *))
  145. {
  146. int unique_name = 1;
  147. CGIFUNCTION *cgi;
  148. cgi = cgiFunctionList;
  149. while (cgi != NULL) {
  150. if (strcmp(name, cgi->cgi_name) == 0) {
  151. unique_name = 0;
  152. break;
  153. }
  154. cgi = cgi->cgi_next;
  155. }
  156. if ((!unique_name) || ((cgi = malloc(sizeof(CGIFUNCTION))) == 0)) {
  157. return -1;
  158. }
  159. cgi->cgi_next = cgiFunctionList;
  160. cgi->cgi_name = strdup(name);
  161. cgi->cgi_func = func;
  162. cgiFunctionList = cgi;
  163. return 0;
  164. }
  165. /*!
  166. * \brief Process an incoming CGI request.
  167. *
  168. * Applications do not need to call this function. It
  169. * is automatically called by NutHttpProcessRequest().
  170. *
  171. * \param stream Stream of the socket connection, previously opened for
  172. * binary read and write.
  173. * \param req Contains the HTTP request.
  174. */
  175. void NutCgiProcessRequest(FILE * stream, REQUEST * req, int name_pos)
  176. {
  177. CGIFUNCTION *cgi;
  178. if (req->req_method != METHOD_GET && req->req_method != METHOD_POST) {
  179. NutHttpSendError(stream, req, 501);
  180. return;
  181. }
  182. for (cgi = cgiFunctionList; cgi; cgi = cgi->cgi_next) {
  183. if (strcasecmp(cgi->cgi_name, req->req_url + name_pos) == 0)
  184. break;
  185. }
  186. if (cgi == 0)
  187. NutHttpSendError(stream, req, 404);
  188. else if ((*cgi->cgi_func) (stream, req))
  189. NutHttpSendError(stream, req, 500);
  190. return;
  191. }
  192. /*@}*/