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