2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
27 * This file is part of the lwIP TCP/IP stack.
29 * Author: Adam Dunkels <adam@sics.se>
31 * This version of the file has been modified by Texas Instruments to offer
32 * simple server-side-include (SSI) and Common Gateway Interface (CGI)
41 #include "lwip/pbuf.h"
44 /** Set this to 1 to support CGI */
45 #ifndef LWIP_HTTPD_CGI
46 #define LWIP_HTTPD_CGI 0
49 /** Set this to 1 to support SSI (Server-Side-Includes) */
50 #ifndef LWIP_HTTPD_SSI
51 #define LWIP_HTTPD_SSI 1
54 /** Set this to 1 to support HTTP POST */
55 #ifndef LWIP_HTTPD_SUPPORT_POST
56 #define LWIP_HTTPD_SUPPORT_POST 0
63 * Function pointer for a CGI script handler.
65 * This function is called each time the HTTPD server is asked for a file
66 * whose name was previously registered as a CGI function using a call to
67 * http_set_cgi_handler. The iIndex parameter provides the index of the
68 * CGI within the ppcURLs array passed to http_set_cgi_handler. Parameters
69 * pcParam and pcValue provide access to the parameters provided along with
70 * the URI. iNumParams provides a count of the entries in the pcParam and
71 * pcValue arrays. Each entry in the pcParam array contains the name of a
72 * parameter with the corresponding entry in the pcValue array containing the
73 * value for that parameter. Note that pcParam may contain multiple elements
74 * with the same name if, for example, a multi-selection list control is used
75 * in the form generating the data.
77 * The function should return a pointer to a character string which is the
78 * path and filename of the response that is to be sent to the connected
79 * browser, for example "/thanks.htm" or "/response/error.ssi".
81 * The maximum number of parameters that will be passed to this function via
82 * iNumParams is defined by LWIP_HTTPD_MAX_CGI_PARAMETERS. Any parameters in the incoming
83 * HTTP request above this number will be discarded.
85 * Requests intended for use by this CGI mechanism must be sent using the GET
86 * method (which encodes all parameters within the URI rather than in a block
87 * later in the request). Attempts to use the POST method will result in the
88 * request being ignored.
91 typedef const char *(*tCGIHandler)(int iIndex, int iNumParams, char *pcParam[],
95 * Structure defining the base filename (URL) of a CGI and the associated
96 * function which is to be called when that URL is requested.
100 const char *pcCGIName;
101 tCGIHandler pfnCGIHandler;
104 void http_set_cgi_handlers(const tCGI *pCGIs, int iNumHandlers);
107 /* The maximum number of parameters that the CGI handler can be sent. */
108 #ifndef LWIP_HTTPD_MAX_CGI_PARAMETERS
109 #define LWIP_HTTPD_MAX_CGI_PARAMETERS 16
112 #endif /* LWIP_HTTPD_CGI */
116 /** LWIP_HTTPD_SSI_MULTIPART==1: SSI handler function is called with 2 more
117 * arguments indicating a counter for insert string that are too long to be
118 * inserted at once: the SSI handler function must then set 'next_tag_part'
119 * which will be passed back to it in the next call. */
120 #ifndef LWIP_HTTPD_SSI_MULTIPART
121 #define LWIP_HTTPD_SSI_MULTIPART 0
125 * Function pointer for the SSI tag handler callback.
127 * This function will be called each time the HTTPD server detects a tag of the
128 * form <!--#name--> in a .shtml, .ssi or .shtm file where "name" appears as
129 * one of the tags supplied to http_set_ssi_handler in the ppcTags array. The
130 * returned insert string, which will be appended after the the string
131 * "<!--#name-->" in file sent back to the client,should be written to pointer
132 * pcInsert. iInsertLen contains the size of the buffer pointed to by
133 * pcInsert. The iIndex parameter provides the zero-based index of the tag as
134 * found in the ppcTags array and identifies the tag that is to be processed.
136 * The handler returns the number of characters written to pcInsert excluding
137 * any terminating NULL or a negative number to indicate a failure (tag not
138 * recognized, for example).
140 * Note that the behavior of this SSI mechanism is somewhat different from the
141 * "normal" SSI processing as found in, for example, the Apache web server. In
142 * this case, the inserted text is appended following the SSI tag rather than
143 * replacing the tag entirely. This allows for an implementation that does not
144 * require significant additional buffering of output data yet which will still
145 * offer usable SSI functionality. One downside to this approach is when
146 * attempting to use SSI within JavaScript. The SSI tag is structured to
147 * resemble an HTML comment but this syntax does not constitute a comment
148 * within JavaScript and, hence, leaving the tag in place will result in
149 * problems in these cases. To work around this, any SSI tag which needs to
150 * output JavaScript code must do so in an encapsulated way, sending the whole
151 * HTML <script>...</script> section as a single include.
153 typedef u16_t (*tSSIHandler)(int iIndex, char *pcInsert, int iInsertLen
154 #if LWIP_HTTPD_SSI_MULTIPART
155 , u16_t current_tag_part, u16_t *next_tag_part
156 #endif /* LWIP_HTTPD_SSI_MULTIPART */
157 #if LWIP_HTTPD_FILE_STATE
158 , void *connection_state
159 #endif /* LWIP_HTTPD_FILE_STATE */
162 void http_set_ssi_handler(tSSIHandler pfnSSIHandler,
163 const char **ppcTags, int iNumTags);
165 /* The maximum length of the string comprising the tag name */
166 #ifndef LWIP_HTTPD_MAX_TAG_NAME_LEN
167 #define LWIP_HTTPD_MAX_TAG_NAME_LEN 8
170 /* The maximum length of string that can be returned to replace any given tag */
171 #ifndef LWIP_HTTPD_MAX_TAG_INSERT_LEN
172 #define LWIP_HTTPD_MAX_TAG_INSERT_LEN 192
175 #endif /* LWIP_HTTPD_SSI */
177 #if LWIP_HTTPD_SUPPORT_POST
179 /* These functions must be implemented by the application */
181 /** Called when a POST request has been received. The application can decide
182 * whether to accept it or not.
184 * @param connection Unique connection identifier, valid until httpd_post_end
186 * @param uri The HTTP header URI receiving the POST request.
187 * @param http_request The raw HTTP request (the first packet, normally).
188 * @param http_request_len Size of 'http_request'.
189 * @param content_len Content-Length from HTTP header.
190 * @param response_uri Filename of response file, to be filled when denying the
192 * @param response_uri_len Size of the 'response_uri' buffer.
193 * @param post_auto_wnd Set this to 0 to let the callback code handle window
194 * updates by calling 'httpd_post_data_recved' (to throttle rx speed)
195 * default is 1 (httpd handles window updates automatically)
196 * @return ERR_OK: Accept the POST request, data may be passed in
197 * another err_t: Deny the POST request, send back 'bad request'.
199 err_t httpd_post_begin(void *connection, const char *uri, const char *http_request,
200 u16_t http_request_len, int content_len, char *response_uri,
201 u16_t response_uri_len, u8_t *post_auto_wnd);
203 /** Called for each pbuf of data that has been received for a POST.
204 * ATTENTION: The application is responsible for freeing the pbufs passed in!
206 * @param connection Unique connection identifier.
207 * @param p Received data.
208 * @return ERR_OK: Data accepted.
209 * another err_t: Data denied, http_post_get_response_uri will be called.
211 err_t httpd_post_receive_data(void *connection, struct pbuf *p);
213 /** Called when all data is received or when the connection is closed.
214 * The application must return the filename/URI of a file to send in response
215 * to this POST request. If the response_uri buffer is untouched, a 404
216 * response is returned.
218 * @param connection Unique connection identifier.
219 * @param response_uri Filename of response file, to be filled when denying the request
220 * @param response_uri_len Size of the 'response_uri' buffer.
222 void httpd_post_finished(void *connection, char *response_uri, u16_t response_uri_len);
224 #ifndef LWIP_HTTPD_POST_MANUAL_WND
225 #define LWIP_HTTPD_POST_MANUAL_WND 0
228 #if LWIP_HTTPD_POST_MANUAL_WND
229 void httpd_post_data_recved(void *connection, u16_t recved_len);
230 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
232 #endif /* LWIP_HTTPD_SUPPORT_POST */
234 void httpd_init(void);
236 #endif /* __HTTPD_H__ */