httpd.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @file
  3. * HTTP server
  4. */
  5. /*
  6. * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Adam Dunkels <adam@sics.se>
  34. *
  35. * This version of the file has been modified by Texas Instruments to offer
  36. * simple server-side-include (SSI) and Common Gateway Interface (CGI)
  37. * capability.
  38. */
  39. #ifndef LWIP_HDR_APPS_HTTPD_H
  40. #define LWIP_HDR_APPS_HTTPD_H
  41. #include "httpd_opts.h"
  42. #include "lwip/err.h"
  43. #include "lwip/pbuf.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #if LWIP_HTTPD_CGI
  48. /**
  49. * @ingroup httpd
  50. * Function pointer for a CGI script handler.
  51. *
  52. * This function is called each time the HTTPD server is asked for a file
  53. * whose name was previously registered as a CGI function using a call to
  54. * http_set_cgi_handlers. The iIndex parameter provides the index of the
  55. * CGI within the cgis array passed to http_set_cgi_handlers. Parameters
  56. * pcParam and pcValue provide access to the parameters provided along with
  57. * the URI. iNumParams provides a count of the entries in the pcParam and
  58. * pcValue arrays. Each entry in the pcParam array contains the name of a
  59. * parameter with the corresponding entry in the pcValue array containing the
  60. * value for that parameter. Note that pcParam may contain multiple elements
  61. * with the same name if, for example, a multi-selection list control is used
  62. * in the form generating the data.
  63. *
  64. * The function should return a pointer to a character string which is the
  65. * path and filename of the response that is to be sent to the connected
  66. * browser, for example "/thanks.htm" or "/response/error.ssi".
  67. *
  68. * The maximum number of parameters that will be passed to this function via
  69. * iNumParams is defined by LWIP_HTTPD_MAX_CGI_PARAMETERS. Any parameters in
  70. * the incoming HTTP request above this number will be discarded.
  71. *
  72. * Requests intended for use by this CGI mechanism must be sent using the GET
  73. * method (which encodes all parameters within the URI rather than in a block
  74. * later in the request). Attempts to use the POST method will result in the
  75. * request being ignored.
  76. *
  77. */
  78. typedef const char *(*tCGIHandler)(int iIndex, int iNumParams, char *pcParam[],
  79. char *pcValue[]);
  80. /**
  81. * @ingroup httpd
  82. * Structure defining the base filename (URL) of a CGI and the associated
  83. * function which is to be called when that URL is requested.
  84. */
  85. typedef struct
  86. {
  87. const char *pcCGIName;
  88. tCGIHandler pfnCGIHandler;
  89. } tCGI;
  90. /** Set the array of cgi handlers. */
  91. void http_set_cgi_handlers(const tCGI *pCGIs, int iNumHandlers);
  92. #endif /* LWIP_HTTPD_CGI */
  93. #if LWIP_HTTPD_CGI || LWIP_HTTPD_CGI_SSI
  94. #if LWIP_HTTPD_CGI_SSI
  95. /* we have to prototype this struct here to make it available for the handler */
  96. struct fs_file;
  97. /** Define this generic CGI handler in your application.
  98. * It is called once for every URI with parameters.
  99. * The parameters can be stored to the object passed as connection_state, which
  100. * is allocated to file->state via fs_state_init() from fs_open() or fs_open_custom().
  101. * Content creation via SSI or complete dynamic files can retrieve the CGI params from there.
  102. */
  103. extern void httpd_cgi_handler(struct fs_file *file, const char* uri, int iNumParams,
  104. char **pcParam, char **pcValue
  105. #if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE
  106. , void *connection_state
  107. #endif /* LWIP_HTTPD_FILE_STATE */
  108. );
  109. #endif /* LWIP_HTTPD_CGI_SSI */
  110. #endif /* LWIP_HTTPD_CGI || LWIP_HTTPD_CGI_SSI */
  111. #if LWIP_HTTPD_SSI
  112. /**
  113. * @ingroup httpd
  114. * Function pointer for the SSI tag handler callback.
  115. *
  116. * This function will be called each time the HTTPD server detects a tag of the
  117. * form <!--#name--> in files with extensions mentioned in the g_pcSSIExtensions
  118. * array (currently .shtml, .shtm, .ssi, .xml, .json) where "name" appears as
  119. * one of the tags supplied to http_set_ssi_handler in the tags array. The
  120. * returned insert string, which will be appended after the the string
  121. * "<!--#name-->" in file sent back to the client, should be written to pointer
  122. * pcInsert. iInsertLen contains the size of the buffer pointed to by
  123. * pcInsert. The iIndex parameter provides the zero-based index of the tag as
  124. * found in the tags array and identifies the tag that is to be processed.
  125. *
  126. * The handler returns the number of characters written to pcInsert excluding
  127. * any terminating NULL or HTTPD_SSI_TAG_UNKNOWN when tag is not recognized.
  128. *
  129. * Note that the behavior of this SSI mechanism is somewhat different from the
  130. * "normal" SSI processing as found in, for example, the Apache web server. In
  131. * this case, the inserted text is appended following the SSI tag rather than
  132. * replacing the tag entirely. This allows for an implementation that does not
  133. * require significant additional buffering of output data yet which will still
  134. * offer usable SSI functionality. One downside to this approach is when
  135. * attempting to use SSI within JavaScript. The SSI tag is structured to
  136. * resemble an HTML comment but this syntax does not constitute a comment
  137. * within JavaScript and, hence, leaving the tag in place will result in
  138. * problems in these cases. In order to avoid these problems, define
  139. * LWIP_HTTPD_SSI_INCLUDE_TAG as zero in your lwip options file, or use JavaScript
  140. * style block comments in the form / * # name * / (without the spaces).
  141. */
  142. typedef u16_t (*tSSIHandler)(
  143. #if LWIP_HTTPD_SSI_RAW
  144. const char* ssi_tag_name,
  145. #else /* LWIP_HTTPD_SSI_RAW */
  146. int iIndex,
  147. #endif /* LWIP_HTTPD_SSI_RAW */
  148. char *pcInsert, int iInsertLen
  149. #if LWIP_HTTPD_SSI_MULTIPART
  150. , u16_t current_tag_part, u16_t *next_tag_part
  151. #endif /* LWIP_HTTPD_SSI_MULTIPART */
  152. #if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE
  153. , void *connection_state
  154. #endif /* LWIP_HTTPD_FILE_STATE */
  155. );
  156. /** Set the SSI handler function
  157. * (if LWIP_HTTPD_SSI_RAW==1, only the first argument is used)
  158. */
  159. void http_set_ssi_handler(tSSIHandler pfnSSIHandler,
  160. const char **ppcTags, int iNumTags);
  161. /** For LWIP_HTTPD_SSI_RAW==1, return this to indicate the tag is unknown.
  162. * In this case, the webserver writes a warning into the page.
  163. * You can also just return 0 to write nothing for unknown tags.
  164. */
  165. #define HTTPD_SSI_TAG_UNKNOWN 0xFFFF
  166. #endif /* LWIP_HTTPD_SSI */
  167. #if LWIP_HTTPD_SUPPORT_POST
  168. /* These functions must be implemented by the application */
  169. /**
  170. * @ingroup httpd
  171. * Called when a POST request has been received. The application can decide
  172. * whether to accept it or not.
  173. *
  174. * @param connection Unique connection identifier, valid until httpd_post_end
  175. * is called.
  176. * @param uri The HTTP header URI receiving the POST request.
  177. * @param http_request The raw HTTP request (the first packet, normally).
  178. * @param http_request_len Size of 'http_request'.
  179. * @param content_len Content-Length from HTTP header.
  180. * @param response_uri Filename of response file, to be filled when denying the
  181. * request
  182. * @param response_uri_len Size of the 'response_uri' buffer.
  183. * @param post_auto_wnd Set this to 0 to let the callback code handle window
  184. * updates by calling 'httpd_post_data_recved' (to throttle rx speed)
  185. * default is 1 (httpd handles window updates automatically)
  186. * @return ERR_OK: Accept the POST request, data may be passed in
  187. * another err_t: Deny the POST request, send back 'bad request'.
  188. */
  189. err_t httpd_post_begin(void *connection, const char *uri, const char *http_request,
  190. u16_t http_request_len, int content_len, char *response_uri,
  191. u16_t response_uri_len, u8_t *post_auto_wnd);
  192. /**
  193. * @ingroup httpd
  194. * Called for each pbuf of data that has been received for a POST.
  195. * ATTENTION: The application is responsible for freeing the pbufs passed in!
  196. *
  197. * @param connection Unique connection identifier.
  198. * @param p Received data.
  199. * @return ERR_OK: Data accepted.
  200. * another err_t: Data denied, http_post_get_response_uri will be called.
  201. */
  202. err_t httpd_post_receive_data(void *connection, struct pbuf *p);
  203. /**
  204. * @ingroup httpd
  205. * Called when all data is received or when the connection is closed.
  206. * The application must return the filename/URI of a file to send in response
  207. * to this POST request. If the response_uri buffer is untouched, a 404
  208. * response is returned.
  209. *
  210. * @param connection Unique connection identifier.
  211. * @param response_uri Filename of response file, to be filled when denying the request
  212. * @param response_uri_len Size of the 'response_uri' buffer.
  213. */
  214. void httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len);
  215. #if LWIP_HTTPD_POST_MANUAL_WND
  216. void httpd_post_data_recved(void *connection, u16_t recved_len);
  217. #endif /* LWIP_HTTPD_POST_MANUAL_WND */
  218. #endif /* LWIP_HTTPD_SUPPORT_POST */
  219. void httpd_init(void);
  220. #if HTTPD_ENABLE_HTTPS
  221. struct altcp_tls_config;
  222. void httpd_inits(struct altcp_tls_config *conf);
  223. #endif
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* LWIP_HDR_APPS_HTTPD_H */