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>
33 /* This httpd supports for a
34 * rudimentary server-side-include facility which will replace tags of the form
35 * <!--#tag--> in any file whose extension is .shtml, .shtm or .ssi with
36 * strings provided by an include handler whose pointer is provided to the
37 * module via function http_set_ssi_handler().
38 * Additionally, a simple common
39 * gateway interface (CGI) handling mechanism has been added to allow clients
40 * to hook functions to particular request URIs.
42 * To enable SSI support, define label LWIP_HTTPD_SSI in lwipopts.h.
43 * To enable CGI support, define label LWIP_HTTPD_CGI in lwipopts.h.
45 * By default, the server assumes that HTTP headers are already present in
46 * each file stored in the file system. By defining LWIP_HTTPD_DYNAMIC_HEADERS in
47 * lwipopts.h, this behavior can be changed such that the server inserts the
48 * headers automatically based on the extension of the file being served. If
49 * this mode is used, be careful to ensure that the file system image used
50 * does not already contain the header information.
52 * File system images without headers can be created using the makefsfile
53 * tool with the -h command line option.
56 * Notes about valid SSI tags
57 * --------------------------
59 * The following assumptions are made about tags used in SSI markers:
61 * 1. No tag may contain '-' or whitespace characters within the tag name.
62 * 2. Whitespace is allowed between the tag leadin "<!--#" and the start of
63 * the tag name and between the tag name and the leadout string "-->".
64 * 3. The maximum tag name length is LWIP_HTTPD_MAX_TAG_NAME_LEN, currently 8 characters.
69 * The simple CGI support offered here works with GET method requests only
70 * and can handle up to 16 parameters encoded into the URI. The handler
71 * function may not write directly to the HTTP output but must return a
72 * filename that the HTTP server will send to the browser as a response to
73 * the incoming CGI request.
76 * - don't use mem_malloc() (for SSI/dynamic headers)
77 * - split too long functions into multiple smaller functions?
78 * - support more file types?
80 #include "lwip/debug.h"
81 #include "lwip/stats.h"
83 #include "httpd_structs.h"
93 #define HTTPD_DEBUG LWIP_DBG_OFF
96 /** Set this to 1 and add the next line to lwippools.h to use a memp pool
97 * for allocating struct http_state instead of the heap:
99 * LWIP_MEMPOOL(HTTPD_STATE, 20, 100, "HTTPD_STATE")
101 #ifndef HTTPD_USE_MEM_POOL
102 #define HTTPD_USE_MEM_POOL 0
105 /** The server port for HTTPD to use */
106 #ifndef HTTPD_SERVER_PORT
107 #define HTTPD_SERVER_PORT 80
110 /** Maximum retries before the connection is aborted/closed.
111 * - number of times pcb->poll is called -> default is 4*500ms = 2s;
112 * - reset when pcb->sent is called
114 #ifndef HTTPD_MAX_RETRIES
115 #define HTTPD_MAX_RETRIES 4
118 /** The poll delay is X*500ms */
119 #ifndef HTTPD_POLL_INTERVAL
120 #define HTTPD_POLL_INTERVAL 4
123 /** Priority for tcp pcbs created by HTTPD (very low by default).
124 * Lower priorities get killed first when running out of memroy.
126 #ifndef HTTPD_TCP_PRIO
127 #define HTTPD_TCP_PRIO TCP_PRIO_MIN
130 /** Set this to 1 to enabled timing each file sent */
131 #ifndef LWIP_HTTPD_TIMING
132 #define LWIP_HTTPD_TIMING 0
134 #ifndef HTTPD_DEBUG_TIMING
135 #define HTTPD_DEBUG_TIMING LWIP_DBG_OFF
138 /** Set this to 1 on platforms where strnstr is not available */
139 #ifndef LWIP_HTTPD_STRNSTR_PRIVATE
140 #define LWIP_HTTPD_STRNSTR_PRIVATE 1
143 /** Set this to one to show error pages when parsing a request fails instead
144 of simply closing the connection. */
145 #ifndef LWIP_HTTPD_SUPPORT_EXTSTATUS
146 #define LWIP_HTTPD_SUPPORT_EXTSTATUS 0
149 /** Set this to 0 to drop support for HTTP/0.9 clients (to save some bytes) */
150 #ifndef LWIP_HTTPD_SUPPORT_V09
151 #define LWIP_HTTPD_SUPPORT_V09 1
154 /** Set this to 1 to support HTTP request coming in in multiple packets/pbufs */
155 #ifndef LWIP_HTTPD_SUPPORT_REQUESTLIST
156 #define LWIP_HTTPD_SUPPORT_REQUESTLIST 0
159 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
160 /** Number of rx pbufs to enqueue to parse an incoming request (up to the first
162 #ifndef LWIP_HTTPD_REQ_QUEUELEN
163 #define LWIP_HTTPD_REQ_QUEUELEN 10
166 /** Number of (TCP payload-) bytes (in pbufs) to enqueue to parse and incoming
167 request (up to the first double-newline) */
168 #ifndef LWIP_HTTPD_REQ_BUFSIZE
169 #define LWIP_HTTPD_REQ_BUFSIZE LWIP_HTTPD_MAX_REQ_LENGTH
172 /** Defines the maximum length of a HTTP request line (up to the first CRLF,
173 copied from pbuf into this a global buffer when pbuf- or packet-queues
174 are received - otherwise the input pbuf is used directly) */
175 #ifndef LWIP_HTTPD_MAX_REQ_LENGTH
176 #define LWIP_HTTPD_MAX_REQ_LENGTH 1023
178 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
180 /** Maximum length of the filename to send as response to a POST request,
181 * filled in by the application when a POST is finished.
183 #ifndef LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN
184 #define LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN 63
187 /** Set this to 0 to not send the SSI tag (default is on, so the tag will
188 * be sent in the HTML page */
189 #ifndef LWIP_HTTPD_SSI_INCLUDE_TAG
190 #define LWIP_HTTPD_SSI_INCLUDE_TAG 1
193 /** Set this to 1 to call tcp_abort when tcp_close fails with memory error.
194 * This can be used to prevent consuming all memory in situations where the
195 * HTTP server has low priority compared to other communication. */
196 #ifndef LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
197 #define LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR 0
201 #define true ((u8_t)1)
205 #define false ((u8_t)0)
208 /** Minimum length for a valid HTTP/0.9 request: "GET /\r\n" -> 7 bytes */
209 #define MIN_REQ_LEN 7
213 /** These defines check whether tcp_write has to copy data or not */
215 /** This was TI's check whether to let TCP copy data or not
216 #define HTTP_IS_DATA_VOLATILE(hs) ((hs->file < (char *)0x20000000) ? 0 : TCP_WRITE_FLAG_COPY)*/
217 #ifndef HTTP_IS_DATA_VOLATILE
219 /* Copy for SSI files, no copy for non-SSI files */
220 #define HTTP_IS_DATA_VOLATILE(hs) ((hs)->tag_check ? TCP_WRITE_FLAG_COPY : 0)
221 #else /* LWIP_HTTPD_SSI */
222 /** Default: don't copy if the data is sent from file-system directly */
223 #define HTTP_IS_DATA_VOLATILE(hs) (((hs->file != NULL) && (hs->handle != NULL) && (hs->file == \
224 (char*)hs->handle->data + hs->handle->len - hs->left)) \
225 ? 0 : TCP_WRITE_FLAG_COPY)
226 #endif /* LWIP_HTTPD_SSI */
229 /** Default: headers are sent from ROM */
230 #ifndef HTTP_IS_HDR_VOLATILE
231 #define HTTP_IS_HDR_VOLATILE(hs, ptr) 0
235 /** Default: Tags are sent from struct http_state and are therefore volatile */
236 #ifndef HTTP_IS_TAG_VOLATILE
237 #define HTTP_IS_TAG_VOLATILE(ptr) TCP_WRITE_FLAG_COPY
239 #endif /* LWIP_HTTPD_SSI */
247 const default_filename g_psDefaultFilenames[] = {
248 {"/index.shtml", true },
249 {"/index.ssi", true },
250 {"/index.shtm", true },
251 {"/index.html", false },
252 {"/index.htm", false }
255 #define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / \
256 sizeof(default_filename))
258 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
259 /** HTTP request is copied here from pbufs for simple parsing */
260 static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH+1];
261 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
263 #if LWIP_HTTPD_SUPPORT_POST
264 /** Filename for response file to send when POST is finished */
265 static char http_post_response_filename[LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN+1];
266 #endif /* LWIP_HTTPD_SUPPORT_POST */
268 #if LWIP_HTTPD_DYNAMIC_HEADERS
269 /* The number of individual strings that comprise the headers sent before each
272 #define NUM_FILE_HDR_STRINGS 3
273 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
277 #define HTTPD_LAST_TAG_PART 0xFFFF
279 const char * const g_pcSSIExtensions[] = {
280 ".shtml", ".shtm", ".ssi", ".xml"
283 #define NUM_SHTML_EXTENSIONS (sizeof(g_pcSSIExtensions) / sizeof(const char *))
285 enum tag_check_state {
286 TAG_NONE, /* Not processing an SSI tag */
287 TAG_LEADIN, /* Tag lead in "<!--#" being processed */
288 TAG_FOUND, /* Tag name being read, looking for lead-out start */
289 TAG_LEADOUT, /* Tag lead out "-->" being processed */
290 TAG_SENDING /* Sending tag replacement string */
292 #endif /* LWIP_HTTPD_SSI */
295 struct fs_file *handle;
296 char *file; /* Pointer to first unsent byte in buf. */
298 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
300 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
302 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
303 char *buf; /* File read buffer. */
304 int buf_len; /* Size of file read buffer, buf. */
305 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
306 u32_t left; /* Number of unsent bytes in buf. */
309 const char *parsed; /* Pointer to the first unparsed byte in buf. */
310 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
311 const char *tag_started;/* Poitner to the first opening '<' of the tag. */
312 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
313 const char *tag_end; /* Pointer to char after the closing '>' of the tag. */
314 u32_t parse_left; /* Number of unparsed bytes in buf. */
315 u16_t tag_index; /* Counter used by tag parsing state machine */
316 u16_t tag_insert_len; /* Length of insert in string tag_insert */
317 #if LWIP_HTTPD_SSI_MULTIPART
318 u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */
319 #endif /* LWIP_HTTPD_SSI_MULTIPART */
320 u8_t tag_check; /* true if we are processing a .shtml file else false */
321 u8_t tag_name_len; /* Length of the tag name in string tag_name */
322 char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */
323 char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */
324 enum tag_check_state tag_state; /* State of the tag processor */
325 #endif /* LWIP_HTTPD_SSI */
327 char *params[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Params extracted from the request URI */
328 char *param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Values for each extracted param */
329 #endif /* LWIP_HTTPD_CGI */
330 #if LWIP_HTTPD_DYNAMIC_HEADERS
331 const char *hdrs[NUM_FILE_HDR_STRINGS]; /* HTTP headers to be sent. */
332 u16_t hdr_pos; /* The position of the first unsent header byte in the
334 u16_t hdr_index; /* The index of the hdr string currently being sent. */
335 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
336 #if LWIP_HTTPD_TIMING
338 #endif /* LWIP_HTTPD_TIMING */
339 #if LWIP_HTTPD_SUPPORT_POST
340 u32_t post_content_len_left;
341 #if LWIP_HTTPD_POST_MANUAL_WND
342 u32_t unrecved_bytes;
345 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
346 #endif /* LWIP_HTTPD_SUPPORT_POST*/
349 static err_t http_find_file(struct http_state *hs, const char *uri, int is_09);
350 static err_t http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri);
351 static err_t http_poll(void *arg, struct tcp_pcb *pcb);
354 /* SSI insert handler function pointer. */
355 tSSIHandler g_pfnSSIHandler = NULL;
357 const char **g_ppcTags = NULL;
359 #define LEN_TAG_LEAD_IN 5
360 const char * const g_pcTagLeadIn = "<!--#";
362 #define LEN_TAG_LEAD_OUT 3
363 const char * const g_pcTagLeadOut = "-->";
364 #endif /* LWIP_HTTPD_SSI */
367 /* CGI handler information */
370 #endif /* LWIP_HTTPD_CGI */
372 #if LWIP_HTTPD_STRNSTR_PRIVATE
373 /** Like strstr but does not need 'buffer' to be NULL-terminated */
375 strnstr(const char* buffer, const char* token, size_t n)
378 int tokenlen = (int)strlen(token);
380 return (char *)buffer;
382 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
383 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
389 #endif /* LWIP_HTTPD_STRNSTR_PRIVATE */
391 /** Allocate a struct http_state. */
392 static struct http_state*
393 http_state_alloc(void)
395 struct http_state *ret;
396 #if HTTPD_USE_MEM_POOL
397 ret = (struct http_state *)memp_malloc(MEMP_HTTPD_STATE);
398 #else /* HTTPD_USE_MEM_POOL */
399 ret = (struct http_state *)mem_malloc(sizeof(struct http_state));
400 #endif /* HTTPD_USE_MEM_POOL */
402 /* Initialize the structure. */
403 memset(ret, 0, sizeof(struct http_state));
404 #if LWIP_HTTPD_DYNAMIC_HEADERS
405 /* Indicate that the headers are not yet valid */
406 ret->hdr_index = NUM_FILE_HDR_STRINGS;
407 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
412 /** Free a struct http_state.
413 * Also frees the file data if dynamic.
416 http_state_free(struct http_state *hs)
420 #if LWIP_HTTPD_TIMING
421 u32_t ms_needed = sys_now() - hs->time_started;
422 u32_t needed = LWIP_MAX(1, (ms_needed/100));
423 LWIP_DEBUGF(HTTPD_DEBUG_TIMING, ("httpd: needed %"U32_F" ms to send file of %d bytes -> %"U32_F" bytes/sec\n",
424 ms_needed, hs->handle->len, ((((u32_t)hs->handle->len) * 10) / needed)));
425 #endif /* LWIP_HTTPD_TIMING */
426 fs_close(hs->handle);
429 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
430 if (hs->buf != NULL) {
434 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
435 #if HTTPD_USE_MEM_POOL
436 memp_free(MEMP_HTTPD_STATE, hs);
437 #else /* HTTPD_USE_MEM_POOL */
439 #endif /* HTTPD_USE_MEM_POOL */
443 /** Call tcp_write() in a loop trying smaller and smaller length
445 * @param pcb tcp_pcb to send
446 * @param ptr Data to send
447 * @param length Length of data to send (in/out: on return, contains the
448 * amount of data sent)
449 * @param apiflags directly passed to tcp_write
450 * @return the return value of tcp_write
453 http_write(struct tcp_pcb *pcb, const void* ptr, u16_t *length, u8_t apiflags)
457 LWIP_ASSERT("length != NULL", length != NULL);
460 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Trying go send %d bytes\n", len));
461 err = tcp_write(pcb, ptr, len, apiflags);
462 if (err == ERR_MEM) {
463 if ((tcp_sndbuf(pcb) == 0) ||
464 (tcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)) {
465 /* no need to try smaller sizes */
470 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE,
471 ("Send failed, trying less (%d bytes)\n", len));
473 } while ((err == ERR_MEM) && (len > 1));
476 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Sent %d bytes\n", len));
478 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Send failed with err %d (\"%s\")\n", err, lwip_strerr(err)));
486 * The connection shall be actively closed.
487 * Reset the sent- and recv-callbacks.
489 * @param pcb the tcp pcb to reset callbacks
490 * @param hs connection state to free
493 http_close_conn(struct tcp_pcb *pcb, struct http_state *hs)
496 LWIP_DEBUGF(HTTPD_DEBUG, ("Closing connection %p\n", (void*)pcb));
498 #if LWIP_HTTPD_SUPPORT_POST
500 if ((hs->post_content_len_left != 0)
501 #if LWIP_HTTPD_POST_MANUAL_WND
502 || ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0))
503 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
505 /* make sure the post code knows that the connection is closed */
506 http_post_response_filename[0] = 0;
507 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
510 #endif /* LWIP_HTTPD_SUPPORT_POST*/
516 tcp_poll(pcb, NULL, 0);
522 err = tcp_close(pcb);
524 LWIP_DEBUGF(HTTPD_DEBUG, ("Error %d closing %p\n", err, (void*)pcb));
525 /* error closing, try again later in poll */
526 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
532 * Extract URI parameters from the parameter-part of an URI in the form
533 * "test.cgi?x=y" @todo: better explanation!
534 * Pointers to the parameters are stored in hs->param_vals.
536 * @param hs http connection state
537 * @param params pointer to the NULL-terminated parameter string from the URI
538 * @return number of parameters extracted
541 extract_uri_parameters(struct http_state *hs, char *params)
547 /* If we have no parameters at all, return immediately. */
548 if(!params || (params[0] == '\0')) {
552 /* Get a pointer to our first parameter */
555 /* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
556 * remainder (if any) */
557 for(loop = 0; (loop < LWIP_HTTPD_MAX_CGI_PARAMETERS) && pair; loop++) {
559 /* Save the name of the parameter */
560 hs->params[loop] = pair;
562 /* Remember the start of this name=value pair */
565 /* Find the start of the next name=value pair and replace the delimiter
566 * with a 0 to terminate the previous pair string. */
567 pair = strchr(pair, '&');
572 /* We didn't find a new parameter so find the end of the URI and
573 * replace the space with a '\0' */
574 pair = strchr(equals, ' ');
579 /* Revert to NULL so that we exit the loop as expected. */
583 /* Now find the '=' in the previous pair, replace it with '\0' and save
584 * the parameter value string. */
585 equals = strchr(equals, '=');
588 hs->param_vals[loop] = equals + 1;
590 hs->param_vals[loop] = NULL;
596 #endif /* LWIP_HTTPD_CGI */
600 * Insert a tag (found in an shtml in the form of "<!--#tagname-->" into the file.
601 * The tag's name is stored in hs->tag_name (NULL-terminated), the replacement
602 * should be written to hs->tag_insert (up to a length of LWIP_HTTPD_MAX_TAG_INSERT_LEN).
603 * The amount of data written is stored to hs->tag_insert_len.
605 * @todo: return tag_insert_len - maybe it can be removed from struct http_state?
607 * @param hs http connection state
610 get_tag_insert(struct http_state *hs)
614 #if LWIP_HTTPD_SSI_MULTIPART
615 u16_t current_tag_part = hs->tag_part;
616 hs->tag_part = HTTPD_LAST_TAG_PART;
617 #endif /* LWIP_HTTPD_SSI_MULTIPART */
619 if(g_pfnSSIHandler && g_ppcTags && g_iNumTags) {
621 /* Find this tag in the list we have been provided. */
622 for(loop = 0; loop < g_iNumTags; loop++) {
623 if(strcmp(hs->tag_name, g_ppcTags[loop]) == 0) {
624 hs->tag_insert_len = g_pfnSSIHandler(loop, hs->tag_insert,
625 LWIP_HTTPD_MAX_TAG_INSERT_LEN
626 #if LWIP_HTTPD_SSI_MULTIPART
627 , current_tag_part, &hs->tag_part
628 #endif /* LWIP_HTTPD_SSI_MULTIPART */
629 #if LWIP_HTTPD_FILE_STATE
631 #endif /* LWIP_HTTPD_FILE_STATE */
638 /* If we drop out, we were asked to serve a page which contains tags that
639 * we don't have a handler for. Merely echo back the tags with an error
641 #define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG "
642 #define UNKNOWN_TAG1_LEN 18
643 #define UNKNOWN_TAG2_TEXT "***</b>"
644 #define UNKNOWN_TAG2_LEN 7
645 len = LWIP_MIN(strlen(hs->tag_name),
646 LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN));
647 MEMCPY(hs->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN);
648 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN], hs->tag_name, len);
649 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN);
650 hs->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0;
652 len = strlen(hs->tag_insert);
653 LWIP_ASSERT("len <= 0xffff", len <= 0xffff);
654 hs->tag_insert_len = (u16_t)len;
656 #endif /* LWIP_HTTPD_SSI */
658 #if LWIP_HTTPD_DYNAMIC_HEADERS
660 * Generate the relevant HTTP headers for the given filename and write
661 * them into the supplied buffer.
664 get_http_headers(struct http_state *pState, char *pszURI)
671 /* Ensure that we initialize the loop counter. */
674 /* In all cases, the second header we send is the server identification
676 pState->hdrs[1] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER];
678 /* Is this a normal file or the special case we use to send back the
679 default "404: Page not found" response? */
680 if (pszURI == NULL) {
681 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
682 pState->hdrs[2] = g_psHTTPHeaderStrings[DEFAULT_404_HTML];
684 /* Set up to send the first header string. */
685 pState->hdr_index = 0;
689 /* We are dealing with a particular filename. Look for one other
690 special case. We assume that any filename with "404" in it must be
691 indicative of a 404 server error whereas all other files require
692 the 200 OK header. */
693 if (strstr(pszURI, "404")) {
694 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
695 } else if (strstr(pszURI, "400")) {
696 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
697 } else if (strstr(pszURI, "501")) {
698 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
700 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_OK];
703 /* Determine if the URI has any variables and, if so, temporarily remove
705 pszVars = strchr(pszURI, '?');
710 /* Get a pointer to the file extension. We find this by looking for the
711 last occurrence of "." in the filename passed. */
713 pszWork = strchr(pszURI, '.');
715 pszExt = pszWork + 1;
716 pszWork = strchr(pszExt, '.');
719 /* Now determine the content type and add the relevant header for that. */
720 for(iLoop = 0; (iLoop < NUM_HTTP_HEADERS) && pszExt; iLoop++) {
721 /* Have we found a matching extension? */
722 if(!strcmp(g_psHTTPHeaders[iLoop].extension, pszExt)) {
724 g_psHTTPHeaderStrings[g_psHTTPHeaders[iLoop].headerIndex];
729 /* Reinstate the parameter marker if there was one in the original URI. */
735 /* Does the URL passed have any file extension? If not, we assume it
736 is a special-case URL used for control state notification and we do
737 not send any HTTP headers with the response. */
739 /* Force the header index to a value indicating that all headers
740 have already been sent. */
741 pState->hdr_index = NUM_FILE_HDR_STRINGS;
743 /* Did we find a matching extension? */
744 if(iLoop == NUM_HTTP_HEADERS) {
745 /* No - use the default, plain text file type. */
746 pState->hdrs[2] = g_psHTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE];
749 /* Set up to send the first header string. */
750 pState->hdr_index = 0;
754 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
757 * Try to send more data on this pcb.
759 * @param pcb the pcb to send data
760 * @param hs connection state
763 http_send_data(struct tcp_pcb *pcb, struct http_state *hs)
768 u8_t data_to_send = false;
769 #if LWIP_HTTPD_DYNAMIC_HEADERS
770 u16_t hdrlen, sendlen;
771 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
773 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_send_data: pcb=%p hs=%p left=%d\n", (void*)pcb,
774 (void*)hs, hs != NULL ? hs->left : 0));
776 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
777 if (hs->unrecved_bytes != 0) {
780 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
782 #if LWIP_HTTPD_DYNAMIC_HEADERS
783 /* If we were passed a NULL state structure pointer, ignore the call. */
788 /* Assume no error until we find otherwise */
791 /* Do we have any more header data to send for this file? */
792 if(hs->hdr_index < NUM_FILE_HDR_STRINGS) {
793 /* How much data can we send? */
794 len = tcp_sndbuf(pcb);
797 while(len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) {
800 /* How much do we have to send from the current header? */
801 hdrlen = (u16_t)strlen(hs->hdrs[hs->hdr_index]);
803 /* How much of this can we send? */
804 sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos);
806 /* Send this amount of data or as much as we can given memory
808 ptr = (const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos);
809 old_sendlen = sendlen;
810 err = http_write(pcb, ptr, &sendlen, HTTP_IS_HDR_VOLATILE(hs, ptr));
811 if ((err == ERR_OK) && (old_sendlen != sendlen)) {
812 /* Remember that we added some more data to be transmitted. */
814 } else if (err != ERR_OK) {
815 /* special case: http_write does not try to send 1 byte */
819 /* Fix up the header position for the next time round. */
820 hs->hdr_pos += sendlen;
823 /* Have we finished sending this string? */
824 if(hs->hdr_pos == hdrlen) {
825 /* Yes - move on to the next one */
831 /* If we get here and there are still header bytes to send, we send
832 * the header information we just wrote immediately. If there are no
833 * more headers to send, but we do have file data to send, drop through
834 * to try to send some file data too. */
835 if((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) {
836 LWIP_DEBUGF(HTTPD_DEBUG, ("tcp_output\n"));
840 #else /* LWIP_HTTPD_DYNAMIC_HEADERS */
841 /* Assume no error until we find otherwise */
843 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
845 /* Have we run out of file data to send? If so, we need to read the next
846 * block from the file. */
848 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
850 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
852 /* Do we have a valid file handle? */
853 if (hs->handle == NULL) {
854 /* No - close the connection. */
855 http_close_conn(pcb, hs);
858 if (fs_bytes_left(hs->handle) <= 0) {
859 /* We reached the end of the file so this request is done.
860 * @todo: don't close here for HTTP/1.1? */
861 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
862 http_close_conn(pcb, hs);
865 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
866 /* Do we already have a send buffer allocated? */
868 /* Yes - get the length of the buffer */
871 /* We don't have a send buffer so allocate one up to 2mss bytes long. */
872 count = 2 * tcp_mss(pcb);
874 hs->buf = (char*)mem_malloc((mem_size_t)count);
875 if (hs->buf != NULL) {
880 } while (count > 100);
882 /* Did we get a send buffer? If not, return immediately. */
883 if (hs->buf == NULL) {
884 LWIP_DEBUGF(HTTPD_DEBUG, ("No buff\n"));
889 /* Read a block of data from the file. */
890 LWIP_DEBUGF(HTTPD_DEBUG, ("Trying to read %d bytes.\n", count));
892 count = fs_read(hs->handle, hs->buf, count);
894 /* We reached the end of the file so this request is done.
895 * @todo: don't close here for HTTP/1.1? */
896 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
897 http_close_conn(pcb, hs);
901 /* Set up to send the block of data we just read */
902 LWIP_DEBUGF(HTTPD_DEBUG, ("Read %d bytes.\n", count));
906 hs->parse_left = count;
907 hs->parsed = hs->buf;
908 #endif /* LWIP_HTTPD_SSI */
909 #else /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
910 LWIP_ASSERT("SSI and DYNAMIC_HEADERS turned off but eof not reached", 0);
911 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
916 #endif /* LWIP_HTTPD_SSI */
917 /* We are not processing an SHTML file so no tag checking is necessary.
918 * Just send the data as we received it from the file. */
920 /* We cannot send more data than space available in the send
922 if (tcp_sndbuf(pcb) < hs->left) {
923 len = tcp_sndbuf(pcb);
925 len = (u16_t)hs->left;
926 LWIP_ASSERT("hs->left did not fit into u16_t!", (len == hs->left));
929 if(len > (2 * mss)) {
933 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
941 /* We are processing an SHTML file so need to scan for tags and replace
942 * them with insert strings. We need to be careful here since a tag may
943 * straddle the boundary of two blocks read from the file and we may also
944 * have to split the insert string between two tcp_write operations. */
946 /* How much data could we send? */
947 len = tcp_sndbuf(pcb);
949 /* Do we have remaining data to send before parsing more? */
950 if(hs->parsed > hs->file) {
951 /* We cannot send more data than space available in the send
953 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
954 len = tcp_sndbuf(pcb);
956 LWIP_ASSERT("Data size does not fit into u16_t!",
957 (hs->parsed - hs->file) <= 0xffff);
958 len = (u16_t)(hs->parsed - hs->file);
961 if(len > (2 * mss)) {
965 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
972 /* If the send buffer is full, return now. */
973 if(tcp_sndbuf(pcb) == 0) {
978 LWIP_DEBUGF(HTTPD_DEBUG, ("State %d, %d left\n", hs->tag_state, hs->parse_left));
980 /* We have sent all the data that was already parsed so continue parsing
981 * the buffer contents looking for SSI tags. */
982 while((hs->parse_left) && (err == ERR_OK)) {
983 /* @todo: somewhere in this loop, 'len' should grow again... */
987 switch(hs->tag_state) {
989 /* We are not currently processing an SSI tag so scan for the
990 * start of the lead-in marker. */
991 if(*hs->parsed == g_pcTagLeadIn[0]) {
992 /* We found what could be the lead-in for a new tag so change
993 * state appropriately. */
994 hs->tag_state = TAG_LEADIN;
996 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
997 hs->tag_started = hs->parsed;
998 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
1001 /* Move on to the next character in the buffer */
1007 /* We are processing the lead-in marker, looking for the start of
1010 /* Have we reached the end of the leadin? */
1011 if(hs->tag_index == LEN_TAG_LEAD_IN) {
1013 hs->tag_state = TAG_FOUND;
1015 /* Have we found the next character we expect for the tag leadin? */
1016 if(*hs->parsed == g_pcTagLeadIn[hs->tag_index]) {
1017 /* Yes - move to the next one unless we have found the complete
1018 * leadin, in which case we start looking for the tag itself */
1021 /* We found an unexpected character so this is not a tag. Move
1022 * back to idle state. */
1023 hs->tag_state = TAG_NONE;
1026 /* Move on to the next character in the buffer */
1033 /* We are reading the tag name, looking for the start of the
1034 * lead-out marker and removing any whitespace found. */
1036 /* Remove leading whitespace between the tag leading and the first
1037 * tag name character. */
1038 if((hs->tag_index == 0) && ((*hs->parsed == ' ') ||
1039 (*hs->parsed == '\t') || (*hs->parsed == '\n') ||
1040 (*hs->parsed == '\r'))) {
1041 /* Move on to the next character in the buffer */
1047 /* Have we found the end of the tag name? This is signalled by
1048 * us finding the first leadout character or whitespace */
1049 if((*hs->parsed == g_pcTagLeadOut[0]) ||
1050 (*hs->parsed == ' ') || (*hs->parsed == '\t') ||
1051 (*hs->parsed == '\n') || (*hs->parsed == '\r')) {
1053 if(hs->tag_index == 0) {
1054 /* We read a zero length tag so ignore it. */
1055 hs->tag_state = TAG_NONE;
1057 /* We read a non-empty tag so go ahead and look for the
1058 * leadout string. */
1059 hs->tag_state = TAG_LEADOUT;
1060 LWIP_ASSERT("hs->tag_index <= 0xff", hs->tag_index <= 0xff);
1061 hs->tag_name_len = (u8_t)hs->tag_index;
1062 hs->tag_name[hs->tag_index] = '\0';
1063 if(*hs->parsed == g_pcTagLeadOut[0]) {
1070 /* This character is part of the tag name so save it */
1071 if(hs->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) {
1072 hs->tag_name[hs->tag_index++] = *hs->parsed;
1074 /* The tag was too long so ignore it. */
1075 hs->tag_state = TAG_NONE;
1079 /* Move on to the next character in the buffer */
1085 /* We are looking for the end of the lead-out marker. */
1087 /* Remove leading whitespace between the tag leading and the first
1088 * tag leadout character. */
1089 if((hs->tag_index == 0) && ((*hs->parsed == ' ') ||
1090 (*hs->parsed == '\t') || (*hs->parsed == '\n') ||
1091 (*hs->parsed == '\r'))) {
1092 /* Move on to the next character in the buffer */
1098 /* Have we found the next character we expect for the tag leadout? */
1099 if(*hs->parsed == g_pcTagLeadOut[hs->tag_index]) {
1100 /* Yes - move to the next one unless we have found the complete
1101 * leadout, in which case we need to call the client to process
1104 /* Move on to the next character in the buffer */
1108 if(hs->tag_index == (LEN_TAG_LEAD_OUT - 1)) {
1109 /* Call the client to ask for the insert string for the
1110 * tag we just found. */
1111 #if LWIP_HTTPD_SSI_MULTIPART
1112 hs->tag_part = 0; /* start with tag part 0 */
1113 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1116 /* Next time through, we are going to be sending data
1117 * immediately, either the end of the block we start
1118 * sending here or the insert string. */
1120 hs->tag_state = TAG_SENDING;
1121 hs->tag_end = hs->parsed;
1122 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1123 hs->parsed = hs->tag_started;
1124 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1126 /* If there is any unsent data in the buffer prior to the
1127 * tag, we need to send it now. */
1128 if (hs->tag_end > hs->file) {
1129 /* How much of the data can we send? */
1130 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1131 if(len > hs->tag_end - hs->file) {
1132 len = (u16_t)(hs->tag_end - hs->file);
1134 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1135 if(len > hs->tag_started - hs->file) {
1136 /* we would include the tag in sending */
1137 len = (u16_t)(hs->tag_started - hs->file);
1139 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1141 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1142 if (err == ERR_OK) {
1143 data_to_send = true;
1144 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1145 if(hs->tag_started <= hs->file) {
1146 /* pretend to have sent the tag, too */
1147 len += hs->tag_end - hs->tag_started;
1149 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1158 /* We found an unexpected character so this is not a tag. Move
1159 * back to idle state. */
1162 hs->tag_state = TAG_NONE;
1167 * We have found a valid tag and are in the process of sending
1168 * data as a result of that discovery. We send either remaining data
1169 * from the file prior to the insert point or the insert string itself.
1172 /* Do we have any remaining file data to send from the buffer prior
1174 if(hs->tag_end > hs->file) {
1175 /* How much of the data can we send? */
1176 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1177 if(len > hs->tag_end - hs->file) {
1178 len = (u16_t)(hs->tag_end - hs->file);
1180 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1181 LWIP_ASSERT("hs->started >= hs->file", hs->tag_started >= hs->file);
1182 if (len > hs->tag_started - hs->file) {
1183 /* we would include the tag in sending */
1184 len = (u16_t)(hs->tag_started - hs->file);
1186 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1188 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1192 if (err == ERR_OK) {
1193 data_to_send = true;
1194 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1195 if(hs->tag_started <= hs->file) {
1196 /* pretend to have sent the tag, too */
1197 len += hs->tag_end - hs->tag_started;
1199 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1204 #if LWIP_HTTPD_SSI_MULTIPART
1205 if(hs->tag_index >= hs->tag_insert_len) {
1206 /* Did the last SSIHandler have more to send? */
1207 if (hs->tag_part != HTTPD_LAST_TAG_PART) {
1208 /* If so, call it again */
1213 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1215 /* Do we still have insert data left to send? */
1216 if(hs->tag_index < hs->tag_insert_len) {
1217 /* We are sending the insert string itself. How much of the
1218 * insert can we send? */
1219 if(len > (hs->tag_insert_len - hs->tag_index)) {
1220 len = (hs->tag_insert_len - hs->tag_index);
1223 /* Note that we set the copy flag here since we only have a
1224 * single tag insert buffer per connection. If we don't do
1225 * this, insert corruption can occur if more than one insert
1226 * is processed before we call tcp_output. */
1227 err = http_write(pcb, &(hs->tag_insert[hs->tag_index]), &len,
1228 HTTP_IS_TAG_VOLATILE(hs));
1229 if (err == ERR_OK) {
1230 data_to_send = true;
1231 hs->tag_index += len;
1232 /* Don't return here: keep on sending data */
1235 /* We have sent all the insert data so go back to looking for
1237 LWIP_DEBUGF(HTTPD_DEBUG, ("Everything sent.\n"));
1239 hs->tag_state = TAG_NONE;
1240 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1241 hs->parsed = hs->tag_end;
1242 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1249 /* If we drop out of the end of the for loop, this implies we must have
1250 * file data to send so send it now. In TAG_SENDING state, we've already
1251 * handled this so skip the send if that's the case. */
1252 if((hs->tag_state != TAG_SENDING) && (hs->parsed > hs->file)) {
1253 /* We cannot send more data than space available in the send
1255 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
1256 len = tcp_sndbuf(pcb);
1258 LWIP_ASSERT("Data size does not fit into u16_t!",
1259 (hs->parsed - hs->file) <= 0xffff);
1260 len = (u16_t)(hs->parsed - hs->file);
1262 if(len > (2 * tcp_mss(pcb))) {
1263 len = 2 * tcp_mss(pcb);
1266 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1267 if (err == ERR_OK) {
1268 data_to_send = true;
1274 #endif /* LWIP_HTTPD_SSI */
1276 if((hs->left == 0) && (fs_bytes_left(hs->handle) <= 0)) {
1277 /* We reached the end of the file so this request is done.
1278 * This adds the FIN flag right into the last data segment.
1279 * @todo: don't close here for HTTP/1.1? */
1280 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
1281 http_close_conn(pcb, hs);
1284 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("send_data end.\n"));
1285 return data_to_send;
1288 #if LWIP_HTTPD_SUPPORT_EXTSTATUS
1289 /** Initialize a http connection with a file to send for an error message
1291 * @param hs http connection state
1292 * @param error_nr HTTP error number
1293 * @return ERR_OK if file was found and hs has been initialized correctly
1294 * another err_t otherwise
1297 http_find_error_file(struct http_state *hs, u16_t error_nr)
1299 const char *uri1, *uri2, *uri3;
1300 struct fs_file *file;
1302 if (error_nr == 501) {
1305 uri3 = "/501.shtml";
1307 /* 400 (bad request is the default) */
1310 uri3 = "/400.shtml";
1312 file = fs_open(uri1);
1314 file = fs_open(uri2);
1316 file = fs_open(uri3);
1318 LWIP_DEBUGF(HTTPD_DEBUG, ("Error page for error %"U16_F" not found\n",
1324 return http_init_file(hs, file, 0, NULL);
1326 #else /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1327 #define http_find_error_file(hs, error_nr) ERR_ARG
1328 #endif /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1331 * Get the file struct for a 404 error page.
1332 * Tries some file names and returns NULL if none found.
1334 * @param uri pointer that receives the actual file name URI
1335 * @return file struct for the error page or NULL no matching file was found
1337 static struct fs_file *
1338 http_get_404_file(const char **uri)
1340 struct fs_file *file;
1343 file = fs_open(*uri);
1345 /* 404.html doesn't exist. Try 404.htm instead. */
1347 file = fs_open(*uri);
1349 /* 404.htm doesn't exist either. Try 404.shtml instead. */
1350 *uri = "/404.shtml";
1351 file = fs_open(*uri);
1353 /* 404.htm doesn't exist either. Indicate to the caller that it should
1354 * send back a default 404 page.
1364 #if LWIP_HTTPD_SUPPORT_POST
1366 http_handle_post_finished(struct http_state *hs)
1368 /* application error or POST finished */
1369 /* NULL-terminate the buffer */
1370 http_post_response_filename[0] = 0;
1371 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
1372 return http_find_file(hs, http_post_response_filename, 0);
1375 /** Pass received POST body data to the application and correctly handle
1376 * returning a response document or closing the connection.
1377 * ATTENTION: The application is responsible for the pbuf now, so don't free it!
1379 * @param hs http connection state
1380 * @param p pbuf to pass to the application
1381 * @return ERR_OK if passed successfully, another err_t if the response file
1382 * hasn't been found (after POST finished)
1385 http_post_rxpbuf(struct http_state *hs, struct pbuf *p)
1389 /* adjust remaining Content-Length */
1390 if (hs->post_content_len_left < p->tot_len) {
1391 hs->post_content_len_left = 0;
1393 hs->post_content_len_left -= p->tot_len;
1395 err = httpd_post_receive_data(hs, p);
1396 if ((err != ERR_OK) || (hs->post_content_len_left == 0)) {
1397 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
1398 if (hs->unrecved_bytes != 0) {
1401 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
1402 /* application error or POST finished */
1403 return http_handle_post_finished(hs);
1409 /** Handle a post request. Called from http_parse_request when method 'POST'
1412 * @param pcb The tcp_pcb which received this packet.
1413 * @param p The input pbuf (containing the POST header and body).
1414 * @param hs The http connection state.
1415 * @param data HTTP request (header and part of body) from input pbuf(s).
1416 * @param data_len Size of 'data'.
1417 * @param uri The HTTP URI parsed from input pbuf(s).
1418 * @param uri_end Pointer to the end of 'uri' (here, the rest of the HTTP
1420 * @return ERR_OK: POST correctly parsed and accepted by the application.
1421 * ERR_INPROGRESS: POST not completely parsed (no error yet)
1422 * another err_t: Error parsing POST or denied by the application
1425 http_post_request(struct tcp_pcb *pcb, struct pbuf **inp, struct http_state *hs,
1426 char *data, u16_t data_len, char *uri, char *uri_end)
1429 /* search for end-of-header (first double-CRLF) */
1430 char* crlfcrlf = strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data));
1432 #if LWIP_HTTPD_POST_MANUAL_WND
1434 #else /* LWIP_HTTPD_POST_MANUAL_WND */
1435 LWIP_UNUSED_ARG(pcb); /* only used for LWIP_HTTPD_POST_MANUAL_WND */
1436 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1438 if (crlfcrlf != NULL) {
1439 /* search for "Content-Length: " */
1440 #define HTTP_HDR_CONTENT_LEN "Content-Length: "
1441 #define HTTP_HDR_CONTENT_LEN_LEN 16
1442 #define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10
1443 char *scontent_len = strnstr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1));
1444 if (scontent_len != NULL) {
1445 char *scontent_len_end = strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN);
1446 if (scontent_len_end != NULL) {
1448 char *conten_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN;
1449 *scontent_len_end = 0;
1450 content_len = atoi(conten_len_num);
1451 if (content_len > 0) {
1452 /* adjust length of HTTP header passed to application */
1453 const char *hdr_start_after_uri = uri_end + 1;
1454 u16_t hdr_len = LWIP_MIN(data_len, crlfcrlf + 4 - data);
1455 u16_t hdr_data_len = LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri);
1456 u8_t post_auto_wnd = 1;
1457 http_post_response_filename[0] = 0;
1458 err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len,
1459 http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN, &post_auto_wnd);
1460 if (err == ERR_OK) {
1461 /* try to pass in data of the first pbuf(s) */
1462 struct pbuf *q = *inp;
1463 u16_t start_offset = hdr_len;
1464 #if LWIP_HTTPD_POST_MANUAL_WND
1465 hs->no_auto_wnd = !post_auto_wnd;
1466 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1467 /* set the Content-Length to be received for this POST */
1468 hs->post_content_len_left = (u32_t)content_len;
1470 /* get to the pbuf where the body starts */
1471 while((q != NULL) && (q->len <= start_offset)) {
1472 struct pbuf *head = q;
1473 start_offset -= q->len;
1475 /* free the head pbuf */
1481 /* hide the remaining HTTP header */
1482 pbuf_header(q, -(s16_t)start_offset);
1483 #if LWIP_HTTPD_POST_MANUAL_WND
1484 if (!post_auto_wnd) {
1485 /* already tcp_recved() this data... */
1486 hs->unrecved_bytes = q->tot_len;
1488 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1489 return http_post_rxpbuf(hs, q);
1494 /* return file passed from application */
1495 return http_find_file(hs, http_post_response_filename, 0);
1498 LWIP_DEBUGF(HTTPD_DEBUG, ("POST received invalid Content-Length: %s\n",
1505 /* if we come here, the POST is incomplete */
1506 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1507 return ERR_INPROGRESS;
1508 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1510 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1513 #if LWIP_HTTPD_POST_MANUAL_WND
1514 /** A POST implementation can call this function to update the TCP window.
1515 * This can be used to throttle data reception (e.g. when received data is
1516 * programmed to flash and data is received faster than programmed).
1518 * @param connection A connection handle passed to httpd_post_begin for which
1519 * httpd_post_finished has *NOT* been called yet!
1520 * @param recved_len Length of data received (for window update)
1522 void httpd_post_data_recved(void *connection, u16_t recved_len)
1524 struct http_state *hs = (struct http_state*)connection;
1526 if (hs->no_auto_wnd) {
1527 u16_t len = recved_len;
1528 if (hs->unrecved_bytes >= recved_len) {
1529 hs->unrecved_bytes -= recved_len;
1531 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_LEVEL_WARNING, ("httpd_post_data_recved: recved_len too big\n"));
1532 len = (u16_t)hs->unrecved_bytes;
1533 hs->unrecved_bytes = 0;
1535 if (hs->pcb != NULL) {
1537 tcp_recved(hs->pcb, len);
1539 if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) {
1540 /* finished handling POST */
1541 http_handle_post_finished(hs);
1542 http_send_data(hs->pcb, hs);
1548 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1550 #endif /* LWIP_HTTPD_SUPPORT_POST */
1553 * When data has been received in the correct state, try to parse it
1554 * as a HTTP request.
1556 * @param p the received pbuf
1557 * @param hs the connection state
1558 * @param pcb the tcp_pcb which received this packet
1559 * @return ERR_OK if request was OK and hs has been initialized correctly
1560 * ERR_INPROGRESS if request was OK so far but not fully received
1561 * another err_t otherwise
1564 http_parse_request(struct pbuf **inp, struct http_state *hs, struct tcp_pcb *pcb)
1569 struct pbuf *p = *inp;
1570 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1572 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1573 #if LWIP_HTTPD_SUPPORT_POST
1575 #endif /* LWIP_HTTPD_SUPPORT_POST */
1577 LWIP_UNUSED_ARG(pcb); /* only used for post */
1578 LWIP_ASSERT("p != NULL", p != NULL);
1579 LWIP_ASSERT("hs != NULL", hs != NULL);
1581 if ((hs->handle != NULL) || (hs->file != NULL)) {
1582 LWIP_DEBUGF(HTTPD_DEBUG, ("Received data while sending a file\n"));
1583 /* already sending a file */
1588 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1590 LWIP_DEBUGF(HTTPD_DEBUG, ("Received %"U16_F" bytes\n", p->tot_len));
1592 /* first check allowed characters in this pbuf? */
1594 /* enqueue the pbuf */
1595 if (hs->req == NULL) {
1596 LWIP_DEBUGF(HTTPD_DEBUG, ("First pbuf\n"));
1599 LWIP_DEBUGF(HTTPD_DEBUG, ("pbuf enqueued\n"));
1600 pbuf_cat(hs->req, p);
1603 if (hs->req->next != NULL) {
1604 data_len = LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH);
1605 pbuf_copy_partial(hs->req, httpd_req_buf, data_len, 0);
1606 data = httpd_req_buf;
1608 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1610 data = (char *)p->payload;
1612 if (p->len != p->tot_len) {
1613 LWIP_DEBUGF(HTTPD_DEBUG, ("Warning: incomplete header due to chained pbufs\n"));
1617 /* received enough data for minimal request? */
1618 if (data_len >= MIN_REQ_LEN) {
1619 /* wait for CRLF before parsing anything */
1620 crlf = strnstr(data, CRLF, data_len);
1622 #if LWIP_HTTPD_SUPPORT_POST
1624 #endif /* LWIP_HTTPD_SUPPORT_POST */
1627 u16_t left_len, uri_len;
1628 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("CRLF received, parsing request\n"));
1630 if (!strncmp(data, "GET ", 4)) {
1632 /* received GET request */
1633 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received GET request\"\n"));
1634 #if LWIP_HTTPD_SUPPORT_POST
1635 } else if (!strncmp(data, "POST ", 5)) {
1636 /* store request type */
1639 /* received GET request */
1640 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received POST request\n"));
1641 #endif /* LWIP_HTTPD_SUPPORT_POST */
1643 /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1645 /* unsupported method! */
1646 LWIP_DEBUGF(HTTPD_DEBUG, ("Unsupported request method (not implemented): \"%s\"\n",
1648 return http_find_error_file(hs, 501);
1650 /* if we come here, method is OK, parse URI */
1651 left_len = data_len - ((sp1 +1) - data);
1652 sp2 = strnstr(sp1 + 1, " ", left_len);
1653 #if LWIP_HTTPD_SUPPORT_V09
1655 /* HTTP 0.9: respond with correct protocol version */
1656 sp2 = strnstr(sp1 + 1, CRLF, left_len);
1658 #if LWIP_HTTPD_SUPPORT_POST
1660 /* HTTP/0.9 does not support POST */
1663 #endif /* LWIP_HTTPD_SUPPORT_POST */
1665 #endif /* LWIP_HTTPD_SUPPORT_V09 */
1666 uri_len = sp2 - (sp1 + 1);
1667 if ((sp2 != 0) && (sp2 > sp1)) {
1668 char *uri = sp1 + 1;
1669 /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1672 LWIP_DEBUGF(HTTPD_DEBUG, ("Received \"%s\" request for URI: \"%s\"\n",
1674 #if LWIP_HTTPD_SUPPORT_POST
1676 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1677 struct pbuf **q = &hs->req;
1678 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1679 struct pbuf **q = inp;
1680 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1681 err = http_post_request(pcb, q, hs, data, data_len, uri, sp2);
1682 if (err != ERR_OK) {
1683 /* restore header for next try */
1688 if (err == ERR_ARG) {
1693 #endif /* LWIP_HTTPD_SUPPORT_POST */
1695 return http_find_file(hs, uri, is_09);
1698 LWIP_DEBUGF(HTTPD_DEBUG, ("invalid URI\n"));
1703 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1704 clen = pbuf_clen(hs->req);
1705 if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) &&
1706 (clen <= LWIP_HTTPD_REQ_QUEUELEN)) {
1707 /* request not fully received (too short or CRLF is missing) */
1708 return ERR_INPROGRESS;
1710 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1712 #if LWIP_HTTPD_SUPPORT_POST
1714 #endif /* LWIP_HTTPD_SUPPORT_POST */
1715 LWIP_DEBUGF(HTTPD_DEBUG, ("bad request\n"));
1716 /* could not parse request */
1717 return http_find_error_file(hs, 400);
1721 /** Try to find the file specified by uri and, if found, initialize hs
1724 * @param hs the connection state
1725 * @param uri the HTTP header URI
1726 * @param is_09 1 if the request is HTTP/0.9 (no HTTP headers in response)
1727 * @return ERR_OK if file was found and hs has been initialized correctly
1728 * another err_t otherwise
1731 http_find_file(struct http_state *hs, const char *uri, int is_09)
1734 struct fs_file *file = NULL;
1739 #endif /* LWIP_HTTPD_CGI */
1743 * By default, assume we will not be processing server-side-includes
1746 hs->tag_check = false;
1747 #endif /* LWIP_HTTPD_SSI */
1749 /* Have we been asked for the default root file? */
1750 if((uri[0] == '/') && (uri[1] == 0)) {
1751 /* Try each of the configured default filenames until we find one
1753 for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
1754 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Looking for %s...\n", g_psDefaultFilenames[loop].name));
1755 file = fs_open((char *)g_psDefaultFilenames[loop].name);
1756 uri = (char *)g_psDefaultFilenames[loop].name;
1758 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opened.\n"));
1760 hs->tag_check = g_psDefaultFilenames[loop].shtml;
1761 #endif /* LWIP_HTTPD_SSI */
1766 /* None of the default filenames exist so send back a 404 page */
1767 file = http_get_404_file(&uri);
1769 hs->tag_check = false;
1770 #endif /* LWIP_HTTPD_SSI */
1773 /* No - we've been asked for a specific file. */
1774 /* First, isolate the base URI (without any parameters) */
1775 params = (char *)strchr(uri, '?');
1776 if (params != NULL) {
1777 /* URI contains parameters. NULL-terminate the base URI */
1783 /* Does the base URI we have isolated correspond to a CGI handler? */
1784 if (g_iNumCGIs && g_pCGIs) {
1785 for (i = 0; i < g_iNumCGIs; i++) {
1786 if (strcmp(uri, g_pCGIs[i].pcCGIName) == 0) {
1788 * We found a CGI that handles this URI so extract the
1789 * parameters and call the handler.
1791 count = extract_uri_parameters(hs, params);
1792 uri = g_pCGIs[i].pfnCGIHandler(i, count, hs->params,
1798 #endif /* LWIP_HTTPD_CGI */
1800 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opening %s\n", uri));
1802 file = fs_open(uri);
1804 file = http_get_404_file(&uri);
1809 * See if we have been asked for an shtml file and, if so,
1810 * enable tag checking.
1812 hs->tag_check = false;
1813 for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
1814 if (strstr(uri, g_pcSSIExtensions[loop])) {
1815 hs->tag_check = true;
1820 #endif /* LWIP_HTTPD_SSI */
1822 return http_init_file(hs, file, is_09, uri);
1825 /** Initialize a http connection with a file to send (if found).
1826 * Called by http_find_file and http_find_error_file.
1828 * @param hs http connection state
1829 * @param file file structure to send (or NULL if not found)
1830 * @param is_09 1 if the request is HTTP/0.9 (no HTTP headers in response)
1831 * @param uri the HTTP header URI
1832 * @return ERR_OK if file was found and hs has been initialized correctly
1833 * another err_t otherwise
1836 http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri)
1839 /* file opened, initialise struct http_state */
1842 hs->tag_state = TAG_NONE;
1843 hs->parsed = file->data;
1844 hs->parse_left = file->len;
1845 hs->tag_end = file->data;
1846 #endif /* LWIP_HTTPD_SSI */
1848 hs->file = (char*)file->data;
1849 LWIP_ASSERT("File length must be positive!", (file->len >= 0));
1850 hs->left = file->len;
1852 #if LWIP_HTTPD_TIMING
1853 hs->time_started = sys_now();
1854 #endif /* LWIP_HTTPD_TIMING */
1855 #if !LWIP_HTTPD_DYNAMIC_HEADERS
1856 LWIP_ASSERT("HTTP headers not included in file system", hs->handle->http_header_included);
1857 #endif /* !LWIP_HTTPD_DYNAMIC_HEADERS */
1858 #if LWIP_HTTPD_SUPPORT_V09
1859 if (hs->handle->http_header_included && is_09) {
1860 /* HTTP/0.9 responses are sent without HTTP header,
1861 search for the end of the header. */
1862 char *file_start = strnstr(hs->file, CRLF CRLF, hs->left);
1863 if (file_start != NULL) {
1864 size_t diff = file_start + 4 - hs->file;
1866 hs->left -= (u32_t)diff;
1869 #endif /* LWIP_HTTPD_SUPPORT_V09*/
1876 #if LWIP_HTTPD_DYNAMIC_HEADERS
1877 /* Determine the HTTP headers to send based on the file extension of
1878 * the requested URI. */
1879 if ((hs->handle == NULL) || !hs->handle->http_header_included) {
1880 get_http_headers(hs, (char*)uri);
1882 #else /* LWIP_HTTPD_DYNAMIC_HEADERS */
1883 LWIP_UNUSED_ARG(uri);
1884 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
1889 * The pcb had an error and is already deallocated.
1890 * The argument might still be valid (if != NULL).
1893 http_err(void *arg, err_t err)
1895 struct http_state *hs = (struct http_state *)arg;
1896 LWIP_UNUSED_ARG(err);
1898 LWIP_DEBUGF(HTTPD_DEBUG, ("http_err: %s", lwip_strerr(err)));
1901 http_state_free(hs);
1906 * Data has been sent and acknowledged by the remote host.
1907 * This means that more data can be sent.
1910 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
1912 struct http_state *hs = (struct http_state *)arg;
1914 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_sent %p\n", (void*)pcb));
1916 LWIP_UNUSED_ARG(len);
1924 http_send_data(pcb, hs);
1930 * The poll function is called every 2nd second.
1931 * If there has been no data sent (which resets the retries) in 8 seconds, close.
1932 * If the last portion of a file has not been sent in 2 seconds, close.
1934 * This could be increased, but we don't want to waste resources for bad connections.
1937 http_poll(void *arg, struct tcp_pcb *pcb)
1939 struct http_state *hs = (struct http_state *)arg;
1940 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n",
1941 (void*)pcb, (void*)hs, tcp_debug_state_str(pcb->state)));
1945 /* arg is null, close. */
1946 LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n"));
1947 closed = http_close_conn(pcb, hs);
1948 LWIP_UNUSED_ARG(closed);
1949 #if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
1950 if (closed == ERR_MEM) {
1954 #endif /* LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR */
1958 if (hs->retries == HTTPD_MAX_RETRIES) {
1959 LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n"));
1960 http_close_conn(pcb, hs);
1964 /* If this connection has a file open, try to send some more data. If
1965 * it has not yet received a GET request, don't do this since it will
1966 * cause the connection to close immediately. */
1967 if(hs && (hs->handle)) {
1968 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n"));
1969 if(http_send_data(pcb, hs)) {
1970 /* If we wrote anything to be sent, go ahead and send it now. */
1971 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
1981 * Data has been received on this pcb.
1982 * For HTTP 1.0, this should normally only happen once (if the request fits in one packet).
1985 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1987 err_t parsed = ERR_ABRT;
1988 struct http_state *hs = (struct http_state *)arg;
1989 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: pcb=%p pbuf=%p err=%s\n", (void*)pcb,
1990 (void*)p, lwip_strerr(err)));
1992 if ((err != ERR_OK) || (p == NULL) || (hs == NULL)) {
1993 /* error or closed by other side? */
1995 /* Inform TCP that we have taken the data. */
1996 tcp_recved(pcb, p->tot_len);
2000 /* this should not happen, only to be robust */
2001 LWIP_DEBUGF(HTTPD_DEBUG, ("Error, http_recv: hs is NULL, close\n"));
2003 http_close_conn(pcb, hs);
2007 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
2008 if (hs->no_auto_wnd) {
2009 hs->unrecved_bytes += p->tot_len;
2011 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
2013 /* Inform TCP that we have taken the data. */
2014 tcp_recved(pcb, p->tot_len);
2017 #if LWIP_HTTPD_SUPPORT_POST
2018 if (hs->post_content_len_left > 0) {
2019 /* reset idle counter when POST data is received */
2021 /* this is data for a POST, pass the complete pbuf to the application */
2022 http_post_rxpbuf(hs, p);
2023 /* pbuf is passed to the application, don't free it! */
2024 if (hs->post_content_len_left == 0) {
2025 /* all data received, send response or close connection */
2026 http_send_data(pcb, hs);
2030 #endif /* LWIP_HTTPD_SUPPORT_POST */
2032 if (hs->handle == NULL) {
2033 parsed = http_parse_request(&p, hs, pcb);
2034 LWIP_ASSERT("http_parse_request: unexpected return value", parsed == ERR_OK
2035 || parsed == ERR_INPROGRESS ||parsed == ERR_ARG || parsed == ERR_USE);
2037 LWIP_DEBUGF(HTTPD_DEBUG, ("http_recv: already sending data\n"));
2039 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
2040 if (parsed != ERR_INPROGRESS) {
2041 /* request fully parsed or error */
2042 if (hs->req != NULL) {
2047 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
2049 /* pbuf not passed to application, free it now */
2052 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
2053 if (parsed == ERR_OK) {
2054 #if LWIP_HTTPD_SUPPORT_POST
2055 if (hs->post_content_len_left == 0)
2056 #endif /* LWIP_HTTPD_SUPPORT_POST */
2058 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: data %p len %"S32_F"\n", hs->file, hs->left));
2059 http_send_data(pcb, hs);
2061 } else if (parsed == ERR_ARG) {
2062 /* @todo: close on ERR_USE? */
2063 http_close_conn(pcb, hs);
2070 * A new incoming connection has been accepted.
2073 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
2075 struct http_state *hs;
2076 struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)arg;
2077 LWIP_UNUSED_ARG(err);
2078 LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept %p / %p\n", (void*)pcb, arg));
2080 /* Decrease the listen backlog counter */
2083 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2085 /* Allocate memory for the structure that holds the state of the
2086 connection - initialized by that function. */
2087 hs = http_state_alloc();
2089 LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n"));
2093 /* Tell TCP that this is the structure we wish to be passed for our
2097 /* Set up the various callback functions */
2098 tcp_recv(pcb, http_recv);
2099 tcp_err(pcb, http_err);
2100 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
2101 tcp_sent(pcb, http_sent);
2107 * Initialize the httpd with the specified local address.
2110 httpd_init_addr(ip_addr_t *local_addr)
2112 struct tcp_pcb *pcb;
2116 LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL);
2117 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2118 /* set SOF_REUSEADDR here to explicitly bind httpd to multiple interfaces */
2119 err = tcp_bind(pcb, local_addr, HTTPD_SERVER_PORT);
2120 LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK);
2121 pcb = tcp_listen(pcb);
2122 LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL);
2123 /* initialize callback arg and accept callback */
2125 tcp_accept(pcb, http_accept);
2129 * Initialize the httpd: set up a listening PCB and bind it to the defined port
2134 #if HTTPD_USE_MEM_POOL
2135 LWIP_ASSERT("memp_sizes[MEMP_HTTPD_STATE] >= sizeof(http_state)",
2136 memp_sizes[MEMP_HTTPD_STATE] >= sizeof(http_state));
2138 LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n"));
2140 httpd_init_addr(IP_ADDR_ANY);
2145 * Set the SSI handler function.
2147 * @param ssi_handler the SSI handler function
2148 * @param tags an array of SSI tag strings to search for in SSI-enabled files
2149 * @param num_tags number of tags in the 'tags' array
2152 http_set_ssi_handler(tSSIHandler ssi_handler, const char **tags, int num_tags)
2154 LWIP_DEBUGF(HTTPD_DEBUG, ("http_set_ssi_handler\n"));
2156 LWIP_ASSERT("no ssi_handler given", ssi_handler != NULL);
2157 LWIP_ASSERT("no tags given", tags != NULL);
2158 LWIP_ASSERT("invalid number of tags", num_tags > 0);
2160 g_pfnSSIHandler = ssi_handler;
2162 g_iNumTags = num_tags;
2164 #endif /* LWIP_HTTPD_SSI */
2168 * Set an array of CGI filenames/handler functions
2170 * @param cgis an array of CGI filenames/handler functions
2171 * @param num_handlers number of elements in the 'cgis' array
2174 http_set_cgi_handlers(const tCGI *cgis, int num_handlers)
2176 LWIP_ASSERT("no cgis given", cgis != NULL);
2177 LWIP_ASSERT("invalid number of handlers", num_handlers > 0);
2180 g_iNumCGIs = num_handlers;
2182 #endif /* LWIP_HTTPD_CGI */
2184 #endif /* LWIP_TCP */