STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
httpd.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
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.
15  *
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
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Adam Dunkels <adam@sics.se>
30  * Simon Goldschmidt
31  *
32  */
33 
34 /* This httpd supports for a
35  * rudimentary server-side-include facility which will replace tags of the form
36  * <!--#tag--> in any file whose extension is .shtml, .shtm or .ssi with
37  * strings provided by an include handler whose pointer is provided to the
38  * module via function http_set_ssi_handler().
39  * Additionally, a simple common
40  * gateway interface (CGI) handling mechanism has been added to allow clients
41  * to hook functions to particular request URIs.
42  *
43  * To enable SSI support, define label LWIP_HTTPD_SSI in lwipopts.h.
44  * To enable CGI support, define label LWIP_HTTPD_CGI in lwipopts.h.
45  *
46  * By default, the server assumes that HTTP headers are already present in
47  * each file stored in the file system. By defining LWIP_HTTPD_DYNAMIC_HEADERS in
48  * lwipopts.h, this behavior can be changed such that the server inserts the
49  * headers automatically based on the extension of the file being served. If
50  * this mode is used, be careful to ensure that the file system image used
51  * does not already contain the header information.
52  *
53  * File system images without headers can be created using the makefsfile
54  * tool with the -h command line option.
55  *
56  *
57  * Notes about valid SSI tags
58  * --------------------------
59  *
60  * The following assumptions are made about tags used in SSI markers:
61  *
62  * 1. No tag may contain '-' or whitespace characters within the tag name.
63  * 2. Whitespace is allowed between the tag leadin "<!--#" and the start of
64  * the tag name and between the tag name and the leadout string "-->".
65  * 3. The maximum tag name length is LWIP_HTTPD_MAX_TAG_NAME_LEN, currently 8 characters.
66  *
67  * Notes on CGI usage
68  * ------------------
69  *
70  * The simple CGI support offered here works with GET method requests only
71  * and can handle up to 16 parameters encoded into the URI. The handler
72  * function may not write directly to the HTTP output but must return a
73  * filename that the HTTP server will send to the browser as a response to
74  * the incoming CGI request.
75  *
76  *
77  *
78  * The list of supported file types is quite short, so if makefsdata complains
79  * about an unknown extension, make sure to add it (and its doctype) to
80  * the 'g_psHTTPHeaders' list.
81  */
82 #include "lwip/apps/httpd.h"
83 #include "lwip/debug.h"
84 #include "lwip/stats.h"
85 #include "lwip/apps/fs.h"
86 #include "httpd_structs.h"
87 #include "lwip/tcp.h"
88 
89 #include <string.h>
90 #include <stdlib.h>
91 
92 #if LWIP_TCP
93 
95 #define MIN_REQ_LEN 7
96 
97 #define CRLF "\r\n"
98 #define HTTP11_CONNECTIONKEEPALIVE "Connection: keep-alive"
99 
100 #if LWIP_HTTPD_SSI
101 #define LWIP_HTTPD_IS_SSI(hs) ((hs)->ssi)
102 #else /* LWIP_HTTPD_SSI */
103 #define LWIP_HTTPD_IS_SSI(hs) 0
104 #endif /* LWIP_HTTPD_SSI */
105 
110 #ifndef HTTP_IS_DATA_VOLATILE
111 #if LWIP_HTTPD_SSI
112 /* Copy for SSI files, no copy for non-SSI files */
113 #define HTTP_IS_DATA_VOLATILE(hs) ((hs)->ssi ? TCP_WRITE_FLAG_COPY : 0)
114 #else /* LWIP_HTTPD_SSI */
115 
116 #define HTTP_IS_DATA_VOLATILE(hs) (((hs->file != NULL) && (hs->handle != NULL) && (hs->file == \
117  (const char*)hs->handle->data + hs->handle->len - hs->left)) \
118  ? 0 : TCP_WRITE_FLAG_COPY)
119 #endif /* LWIP_HTTPD_SSI */
120 #endif
121 
123 #ifndef HTTP_IS_HDR_VOLATILE
124 #define HTTP_IS_HDR_VOLATILE(hs, ptr) 0
125 #endif
126 
127 /* Return values for http_send_*() */
128 #define HTTP_DATA_TO_SEND_BREAK 2
129 #define HTTP_DATA_TO_SEND_CONTINUE 1
130 #define HTTP_NO_DATA_TO_SEND 0
131 
132 typedef struct
133 {
134  const char *name;
135  u8_t shtml;
136 } default_filename;
137 
138 const default_filename g_psDefaultFilenames[] = {
139  {"/index.shtml", 1 },
140  {"/index.ssi", 1 },
141  {"/index.shtm", 1 },
142  {"/index.html", 0 },
143  {"/index.htm", 0 }
144 };
145 
146 #define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / \
147  sizeof(default_filename))
148 
149 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
150 
151 static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH+1];
152 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
153 
154 #if LWIP_HTTPD_SUPPORT_POST
155 
156 static char http_post_response_filename[LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN+1];
157 #endif /* LWIP_HTTPD_SUPPORT_POST */
158 
159 #if LWIP_HTTPD_DYNAMIC_HEADERS
160 /* The number of individual strings that comprise the headers sent before each
161  * requested file.
162  */
163 #define NUM_FILE_HDR_STRINGS 3
164 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
165 
166 #if LWIP_HTTPD_SSI
167 
168 #define HTTPD_LAST_TAG_PART 0xFFFF
169 
170 enum tag_check_state {
171  TAG_NONE, /* Not processing an SSI tag */
172  TAG_LEADIN, /* Tag lead in "<!--#" being processed */
173  TAG_FOUND, /* Tag name being read, looking for lead-out start */
174  TAG_LEADOUT, /* Tag lead out "-->" being processed */
175  TAG_SENDING /* Sending tag replacement string */
176 };
177 
178 struct http_ssi_state {
179  const char *parsed; /* Pointer to the first unparsed byte in buf. */
180 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
181  const char *tag_started;/* Pointer to the first opening '<' of the tag. */
182 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
183  const char *tag_end; /* Pointer to char after the closing '>' of the tag. */
184  u32_t parse_left; /* Number of unparsed bytes in buf. */
185  u16_t tag_index; /* Counter used by tag parsing state machine */
186  u16_t tag_insert_len; /* Length of insert in string tag_insert */
187 #if LWIP_HTTPD_SSI_MULTIPART
188  u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */
189 #endif /* LWIP_HTTPD_SSI_MULTIPART */
190  u8_t tag_name_len; /* Length of the tag name in string tag_name */
191  char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */
192  char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */
193  enum tag_check_state tag_state; /* State of the tag processor */
194 };
195 #endif /* LWIP_HTTPD_SSI */
196 
197 struct http_state {
198 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
199  struct http_state *next;
200 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
201  struct fs_file file_handle;
202  struct fs_file *handle;
203  const char *file; /* Pointer to first unsent byte in buf. */
204 
205  struct tcp_pcb *pcb;
206 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
207  struct pbuf *req;
208 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
209 
210 #if LWIP_HTTPD_DYNAMIC_FILE_READ
211  char *buf; /* File read buffer. */
212  int buf_len; /* Size of file read buffer, buf. */
213 #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */
214  u32_t left; /* Number of unsent bytes in buf. */
215  u8_t retries;
216 #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
217  u8_t keepalive;
218 #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */
219 #if LWIP_HTTPD_SSI
220  struct http_ssi_state *ssi;
221 #endif /* LWIP_HTTPD_SSI */
222 #if LWIP_HTTPD_CGI
223  char *params[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Params extracted from the request URI */
224  char *param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Values for each extracted param */
225 #endif /* LWIP_HTTPD_CGI */
226 #if LWIP_HTTPD_DYNAMIC_HEADERS
227  const char *hdrs[NUM_FILE_HDR_STRINGS]; /* HTTP headers to be sent. */
228  u16_t hdr_pos; /* The position of the first unsent header byte in the
229  current string */
230  u16_t hdr_index; /* The index of the hdr string currently being sent. */
231 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
232 #if LWIP_HTTPD_TIMING
233  u32_t time_started;
234 #endif /* LWIP_HTTPD_TIMING */
235 #if LWIP_HTTPD_SUPPORT_POST
236  u32_t post_content_len_left;
237 #if LWIP_HTTPD_POST_MANUAL_WND
238  u32_t unrecved_bytes;
239  u8_t no_auto_wnd;
240  u8_t post_finished;
241 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
242 #endif /* LWIP_HTTPD_SUPPORT_POST*/
243 };
244 
245 #if HTTPD_USE_MEM_POOL
246 LWIP_MEMPOOL_DECLARE(HTTPD_STATE, 20, sizeof(struct http_state), "HTTPD_STATE")
247 #define HTTP_ALLOC_HTTP_STATE() (struct http_state *)LWIP_MEMPOOL_ALLOC(HTTPD_STATE)
248 #define HTTP_FREE_HTTP_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_STATE, (x))
249 
250 #if LWIP_HTTPD_SSI
251 LWIP_MEMPOOL_DECLARE(HTTPD_SSI_STATE, 20, sizeof(struct http_ssi_state), "HTTPD_SSI_STATE")
252 #define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)LWIP_MEMPOOL_ALLOC(HTTPD_SSI_STATE)
253 #define HTTP_FREE_SSI_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_SSI_STATE, (x))
254 #endif /* LWIP_HTTPD_SSI */
255 #else /* HTTPD_USE_MEM_POOL */
256 #define HTTP_ALLOC_HTTP_STATE() (struct http_state *)mem_malloc(sizeof(struct http_state))
257 #define HTTP_FREE_HTTP_STATE(x) mem_free(x)
258 #if LWIP_HTTPD_SSI
259 #define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)mem_malloc(sizeof(struct http_ssi_state))
260 #define HTTP_FREE_SSI_STATE(x) mem_free(x)
261 #endif /* LWIP_HTTPD_SSI */
262 #endif /* HTTPD_USE_MEM_POOL */
263 
264 static err_t http_close_conn(struct tcp_pcb *pcb, struct http_state *hs);
265 static err_t http_close_or_abort_conn(struct tcp_pcb *pcb, struct http_state *hs, u8_t abort_conn);
266 static err_t http_find_file(struct http_state *hs, const char *uri, int is_09);
267 static err_t http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri, u8_t tag_check);
268 static err_t http_poll(void *arg, struct tcp_pcb *pcb);
269 static u8_t http_check_eof(struct tcp_pcb *pcb, struct http_state *hs);
270 #if LWIP_HTTPD_FS_ASYNC_READ
271 static void http_continue(void *connection);
272 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
273 
274 #if LWIP_HTTPD_SSI
275 /* SSI insert handler function pointer. */
276 tSSIHandler g_pfnSSIHandler = NULL;
277 int g_iNumTags = 0;
278 const char **g_ppcTags = NULL;
279 
280 #define LEN_TAG_LEAD_IN 5
281 const char * const g_pcTagLeadIn = "<!--#";
282 
283 #define LEN_TAG_LEAD_OUT 3
284 const char * const g_pcTagLeadOut = "-->";
285 #endif /* LWIP_HTTPD_SSI */
286 
287 #if LWIP_HTTPD_CGI
288 /* CGI handler information */
289 const tCGI *g_pCGIs;
290 int g_iNumCGIs;
291 #endif /* LWIP_HTTPD_CGI */
292 
293 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
294 
296 static struct http_state *http_connections;
297 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
298 
299 #if LWIP_HTTPD_STRNSTR_PRIVATE
300 
301 static char*
302 strnstr(const char* buffer, const char* token, size_t n)
303 {
304  const char* p;
305  int tokenlen = (int)strlen(token);
306  if (tokenlen == 0) {
307  return (char *)(size_t)buffer; /* cast to size_t is a hack to cast away constness */
308  }
309  for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
310  if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
311  return (char *)(size_t)p; /* cast to size_t is a hack to cast away constness */
312  }
313  }
314  return NULL;
315 }
316 #endif /* LWIP_HTTPD_STRNSTR_PRIVATE */
317 
318 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
319 static void
320 http_kill_oldest_connection(u8_t ssi_required)
321 {
322  struct http_state *hs = http_connections;
323  struct http_state *hs_free_next = NULL;
324  while(hs && hs->next) {
325 #if LWIP_HTTPD_SSI
326  if (ssi_required) {
327  if (hs->next->ssi != NULL) {
328  hs_free_next = hs;
329  }
330  } else
331 #else /* LWIP_HTTPD_SSI */
332  LWIP_UNUSED_ARG(ssi_required);
333 #endif /* LWIP_HTTPD_SSI */
334  {
335  hs_free_next = hs;
336  }
337  hs = hs->next;
338  }
339  if (hs_free_next != NULL) {
340  LWIP_ASSERT("hs_free_next->next != NULL", hs_free_next->next != NULL);
341  LWIP_ASSERT("hs_free_next->next->pcb != NULL", hs_free_next->next->pcb != NULL);
342  /* send RST when killing a connection because of memory shortage */
343  http_close_or_abort_conn(hs_free_next->next->pcb, hs_free_next->next, 1); /* this also unlinks the http_state from the list */
344  }
345 }
346 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
347 
348 #if LWIP_HTTPD_SSI
349 
350 static struct http_ssi_state*
351 http_ssi_state_alloc(void)
352 {
353  struct http_ssi_state *ret = HTTP_ALLOC_SSI_STATE();
354 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
355  if (ret == NULL) {
356  http_kill_oldest_connection(1);
357  ret = HTTP_ALLOC_SSI_STATE();
358  }
359 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
360  if (ret != NULL) {
361  memset(ret, 0, sizeof(struct http_ssi_state));
362  }
363  return ret;
364 }
365 
367 static void
368 http_ssi_state_free(struct http_ssi_state *ssi)
369 {
370  if (ssi != NULL) {
371  HTTP_FREE_SSI_STATE(ssi);
372  }
373 }
374 #endif /* LWIP_HTTPD_SSI */
375 
378 static void
379 http_state_init(struct http_state* hs)
380 {
381  /* Initialize the structure. */
382  memset(hs, 0, sizeof(struct http_state));
383 #if LWIP_HTTPD_DYNAMIC_HEADERS
384  /* Indicate that the headers are not yet valid */
385  hs->hdr_index = NUM_FILE_HDR_STRINGS;
386 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
387 }
388 
390 static struct http_state*
391 http_state_alloc(void)
392 {
393  struct http_state *ret = HTTP_ALLOC_HTTP_STATE();
394 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
395  if (ret == NULL) {
396  http_kill_oldest_connection(0);
397  ret = HTTP_ALLOC_HTTP_STATE();
398  }
399 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
400  if (ret != NULL) {
401  http_state_init(ret);
402 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
403  /* add the connection to the list */
404  if (http_connections == NULL) {
405  http_connections = ret;
406  } else {
407  struct http_state *last;
408  for(last = http_connections; last->next != NULL; last = last->next);
409  LWIP_ASSERT("last != NULL", last != NULL);
410  last->next = ret;
411  }
412 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
413  }
414  return ret;
415 }
416 
420 static void
421 http_state_eof(struct http_state *hs)
422 {
423  if(hs->handle) {
424 #if LWIP_HTTPD_TIMING
425  u32_t ms_needed = sys_now() - hs->time_started;
426  u32_t needed = LWIP_MAX(1, (ms_needed/100));
427  LWIP_DEBUGF(HTTPD_DEBUG_TIMING, ("httpd: needed %"U32_F" ms to send file of %d bytes -> %"U32_F" bytes/sec\n",
428  ms_needed, hs->handle->len, ((((u32_t)hs->handle->len) * 10) / needed)));
429 #endif /* LWIP_HTTPD_TIMING */
430  fs_close(hs->handle);
431  hs->handle = NULL;
432  }
433 #if LWIP_HTTPD_DYNAMIC_FILE_READ
434  if (hs->buf != NULL) {
435  mem_free(hs->buf);
436  hs->buf = NULL;
437  }
438 #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */
439 #if LWIP_HTTPD_SSI
440  if (hs->ssi) {
441  http_ssi_state_free(hs->ssi);
442  hs->ssi = NULL;
443  }
444 #endif /* LWIP_HTTPD_SSI */
445 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
446  if (hs->req) {
447  pbuf_free(hs->req);
448  hs->req = NULL;
449  }
450 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
451 }
452 
456 static void
457 http_state_free(struct http_state *hs)
458 {
459  if (hs != NULL) {
460  http_state_eof(hs);
461 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
462  /* take the connection off the list */
463  if (http_connections) {
464  if (http_connections == hs) {
465  http_connections = hs->next;
466  } else {
467  struct http_state *last;
468  for(last = http_connections; last->next != NULL; last = last->next) {
469  if (last->next == hs) {
470  last->next = hs->next;
471  break;
472  }
473  }
474  }
475  }
476 #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */
477  HTTP_FREE_HTTP_STATE(hs);
478  }
479 }
480 
490 static err_t
491 http_write(struct tcp_pcb *pcb, const void* ptr, u16_t *length, u8_t apiflags)
492 {
493  u16_t len, max_len;
494  err_t err;
495  LWIP_ASSERT("length != NULL", length != NULL);
496  len = *length;
497  if (len == 0) {
498  return ERR_OK;
499  }
500  /* We cannot send more data than space available in the send buffer. */
501  max_len = tcp_sndbuf(pcb);
502  if (max_len < len) {
503  len = max_len;
504  }
505 #ifdef HTTPD_MAX_WRITE_LEN
506  /* Additional limitation: e.g. don't enqueue more than 2*mss at once */
507  max_len = HTTPD_MAX_WRITE_LEN(pcb);
508  if(len > max_len) {
509  len = max_len;
510  }
511 #endif /* HTTPD_MAX_WRITE_LEN */
512  do {
513  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Trying go send %d bytes\n", len));
514  err = tcp_write(pcb, ptr, len, apiflags);
515  if (err == ERR_MEM) {
516  if ((tcp_sndbuf(pcb) == 0) ||
517  (tcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)) {
518  /* no need to try smaller sizes */
519  len = 1;
520  } else {
521  len /= 2;
522  }
524  ("Send failed, trying less (%d bytes)\n", len));
525  }
526  } while ((err == ERR_MEM) && (len > 1));
527 
528  if (err == ERR_OK) {
529  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Sent %d bytes\n", len));
530  *length = len;
531  } else {
532  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Send failed with err %d (\"%s\")\n", err, lwip_strerr(err)));
533  *length = 0;
534  }
535 
536  return err;
537 }
538 
546 static err_t
547 http_close_or_abort_conn(struct tcp_pcb *pcb, struct http_state *hs, u8_t abort_conn)
548 {
549  err_t err;
550  LWIP_DEBUGF(HTTPD_DEBUG, ("Closing connection %p\n", (void*)pcb));
551 
552 #if LWIP_HTTPD_SUPPORT_POST
553  if (hs != NULL) {
554  if ((hs->post_content_len_left != 0)
556  || ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0))
557 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
558  ) {
559  /* make sure the post code knows that the connection is closed */
560  http_post_response_filename[0] = 0;
561  httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
562  }
563  }
564 #endif /* LWIP_HTTPD_SUPPORT_POST*/
565 
566 
567  tcp_arg(pcb, NULL);
568  tcp_recv(pcb, NULL);
569  tcp_err(pcb, NULL);
570  tcp_poll(pcb, NULL, 0);
571  tcp_sent(pcb, NULL);
572  if (hs != NULL) {
573  http_state_free(hs);
574  }
575 
576  if (abort_conn) {
577  tcp_abort(pcb);
578  return ERR_OK;
579  }
580  err = tcp_close(pcb);
581  if (err != ERR_OK) {
582  LWIP_DEBUGF(HTTPD_DEBUG, ("Error %d closing %p\n", err, (void*)pcb));
583  /* error closing, try again later in poll */
584  tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
585  }
586  return err;
587 }
588 
596 static err_t
597 http_close_conn(struct tcp_pcb *pcb, struct http_state *hs)
598 {
599  return http_close_or_abort_conn(pcb, hs, 0);
600 }
601 
605 static void
606 http_eof(struct tcp_pcb *pcb, struct http_state *hs)
607 {
608  /* HTTP/1.1 persistent connection? (Not supported for SSI) */
609 #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
610  if (hs->keepalive && !LWIP_HTTPD_IS_SSI(hs)) {
611 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
612  struct http_state* next = hs->next;
613 #endif
614  http_state_eof(hs);
615  http_state_init(hs);
616  /* restore state: */
617 #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
618  hs->next = next;
619 #endif
620  hs->pcb = pcb;
621  hs->keepalive = 1;
622  } else
623 #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */
624  {
625  http_close_conn(pcb, hs);
626  }
627 }
628 
629 #if LWIP_HTTPD_CGI
630 
639 static int
640 extract_uri_parameters(struct http_state *hs, char *params)
641 {
642  char *pair;
643  char *equals;
644  int loop;
645 
646  /* If we have no parameters at all, return immediately. */
647  if(!params || (params[0] == '\0')) {
648  return(0);
649  }
650 
651  /* Get a pointer to our first parameter */
652  pair = params;
653 
654  /* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
655  * remainder (if any) */
656  for(loop = 0; (loop < LWIP_HTTPD_MAX_CGI_PARAMETERS) && pair; loop++) {
657 
658  /* Save the name of the parameter */
659  hs->params[loop] = pair;
660 
661  /* Remember the start of this name=value pair */
662  equals = pair;
663 
664  /* Find the start of the next name=value pair and replace the delimiter
665  * with a 0 to terminate the previous pair string. */
666  pair = strchr(pair, '&');
667  if(pair) {
668  *pair = '\0';
669  pair++;
670  } else {
671  /* We didn't find a new parameter so find the end of the URI and
672  * replace the space with a '\0' */
673  pair = strchr(equals, ' ');
674  if(pair) {
675  *pair = '\0';
676  }
677 
678  /* Revert to NULL so that we exit the loop as expected. */
679  pair = NULL;
680  }
681 
682  /* Now find the '=' in the previous pair, replace it with '\0' and save
683  * the parameter value string. */
684  equals = strchr(equals, '=');
685  if(equals) {
686  *equals = '\0';
687  hs->param_vals[loop] = equals + 1;
688  } else {
689  hs->param_vals[loop] = NULL;
690  }
691  }
692 
693  return loop;
694 }
695 #endif /* LWIP_HTTPD_CGI */
696 
697 #if LWIP_HTTPD_SSI
698 
708 static void
709 get_tag_insert(struct http_state *hs)
710 {
711  int loop;
712  size_t len;
713  struct http_ssi_state *ssi;
714  LWIP_ASSERT("hs != NULL", hs != NULL);
715  ssi = hs->ssi;
716  LWIP_ASSERT("ssi != NULL", ssi != NULL);
717 #if LWIP_HTTPD_SSI_MULTIPART
718  u16_t current_tag_part = ssi->tag_part;
719  ssi->tag_part = HTTPD_LAST_TAG_PART;
720 #endif /* LWIP_HTTPD_SSI_MULTIPART */
721 
722  if(g_pfnSSIHandler && g_ppcTags && g_iNumTags) {
723 
724  /* Find this tag in the list we have been provided. */
725  for(loop = 0; loop < g_iNumTags; loop++) {
726  if(strcmp(ssi->tag_name, g_ppcTags[loop]) == 0) {
727  ssi->tag_insert_len = g_pfnSSIHandler(loop, ssi->tag_insert,
730  , current_tag_part, &ssi->tag_part
731 #endif /* LWIP_HTTPD_SSI_MULTIPART */
733  , hs->handle->state
734 #endif /* LWIP_HTTPD_FILE_STATE */
735  );
736  return;
737  }
738  }
739  }
740 
741  /* If we drop out, we were asked to serve a page which contains tags that
742  * we don't have a handler for. Merely echo back the tags with an error
743  * marker. */
744 #define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG "
745 #define UNKNOWN_TAG1_LEN 18
746 #define UNKNOWN_TAG2_TEXT "***</b>"
747 #define UNKNOWN_TAG2_LEN 7
748  len = LWIP_MIN(sizeof(ssi->tag_name), LWIP_MIN(strlen(ssi->tag_name),
749  LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN)));
750  MEMCPY(ssi->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN);
751  MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN], ssi->tag_name, len);
752  MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN);
753  ssi->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0;
754 
755  len = strlen(ssi->tag_insert);
756  LWIP_ASSERT("len <= 0xffff", len <= 0xffff);
757  ssi->tag_insert_len = (u16_t)len;
758 }
759 #endif /* LWIP_HTTPD_SSI */
760 
761 #if LWIP_HTTPD_DYNAMIC_HEADERS
762 
766 static void
767 get_http_headers(struct http_state *pState, char *pszURI)
768 {
769  unsigned int iLoop;
770  char *pszWork;
771  char *pszExt;
772  char *pszVars;
773 
774  /* Ensure that we initialize the loop counter. */
775  iLoop = 0;
776 
777  /* In all cases, the second header we send is the server identification
778  so set it here. */
779  pState->hdrs[1] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER];
780 
781  /* Is this a normal file or the special case we use to send back the
782  default "404: Page not found" response? */
783  if (pszURI == NULL) {
784  pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
785  pState->hdrs[2] = g_psHTTPHeaderStrings[DEFAULT_404_HTML];
786 
787  /* Set up to send the first header string. */
788  pState->hdr_index = 0;
789  pState->hdr_pos = 0;
790  return;
791  } else {
792  /* We are dealing with a particular filename. Look for one other
793  special case. We assume that any filename with "404" in it must be
794  indicative of a 404 server error whereas all other files require
795  the 200 OK header. */
796  if (strstr(pszURI, "404")) {
797  pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
798  } else if (strstr(pszURI, "400")) {
799  pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
800  } else if (strstr(pszURI, "501")) {
801  pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
802  } else {
803  pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_OK];
804  }
805 
806  /* Determine if the URI has any variables and, if so, temporarily remove
807  them. */
808  pszVars = strchr(pszURI, '?');
809  if(pszVars) {
810  *pszVars = '\0';
811  }
812 
813  /* Get a pointer to the file extension. We find this by looking for the
814  last occurrence of "." in the filename passed. */
815  pszExt = NULL;
816  pszWork = strchr(pszURI, '.');
817  while(pszWork) {
818  pszExt = pszWork + 1;
819  pszWork = strchr(pszExt, '.');
820  }
821 
822  /* Now determine the content type and add the relevant header for that. */
823  for(iLoop = 0; (iLoop < NUM_HTTP_HEADERS) && pszExt; iLoop++) {
824  /* Have we found a matching extension? */
825  if(!strcmp(g_psHTTPHeaders[iLoop].extension, pszExt)) {
826  pState->hdrs[2] = g_psHTTPHeaders[iLoop].content_type;
827  break;
828  }
829  }
830 
831  /* Reinstate the parameter marker if there was one in the original URI. */
832  if(pszVars) {
833  *pszVars = '?';
834  }
835  }
836 
837  /* Does the URL passed have any file extension? If not, we assume it
838  is a special-case URL used for control state notification and we do
839  not send any HTTP headers with the response. */
840  if(!pszExt) {
841  /* Force the header index to a value indicating that all headers
842  have already been sent. */
843  pState->hdr_index = NUM_FILE_HDR_STRINGS;
844  } else {
845  /* Did we find a matching extension? */
846  if(iLoop == NUM_HTTP_HEADERS) {
847  /* No - use the default, plain text file type. */
848  pState->hdrs[2] = HTTP_HDR_DEFAULT_TYPE;
849  }
850 
851  /* Set up to send the first header string. */
852  pState->hdr_index = 0;
853  pState->hdr_pos = 0;
854  }
855 }
856 
864 static u8_t
865 http_send_headers(struct tcp_pcb *pcb, struct http_state *hs)
866 {
867  err_t err;
868  u16_t len;
869  u8_t data_to_send = HTTP_NO_DATA_TO_SEND;
870  u16_t hdrlen, sendlen;
871 
872  /* How much data can we send? */
873  len = tcp_sndbuf(pcb);
874  sendlen = len;
875 
876  while(len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) {
877  const void *ptr;
878  u16_t old_sendlen;
879  u8_t apiflags;
880  /* How much do we have to send from the current header? */
881  hdrlen = (u16_t)strlen(hs->hdrs[hs->hdr_index]);
882 
883  /* How much of this can we send? */
884  sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos);
885 
886  /* Send this amount of data or as much as we can given memory
887  * constraints. */
888  ptr = (const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos);
889  old_sendlen = sendlen;
890  apiflags = HTTP_IS_HDR_VOLATILE(hs, ptr);
891  if (hs->hdr_index < NUM_FILE_HDR_STRINGS - 1) {
892  apiflags |= TCP_WRITE_FLAG_MORE;
893  }
894  err = http_write(pcb, ptr, &sendlen, apiflags);
895  if ((err == ERR_OK) && (old_sendlen != sendlen)) {
896  /* Remember that we added some more data to be transmitted. */
897  data_to_send = HTTP_DATA_TO_SEND_CONTINUE;
898  } else if (err != ERR_OK) {
899  /* special case: http_write does not try to send 1 byte */
900  sendlen = 0;
901  }
902 
903  /* Fix up the header position for the next time round. */
904  hs->hdr_pos += sendlen;
905  len -= sendlen;
906 
907  /* Have we finished sending this string? */
908  if(hs->hdr_pos == hdrlen) {
909  /* Yes - move on to the next one */
910  hs->hdr_index++;
911  hs->hdr_pos = 0;
912  }
913  }
914 
915  if ((hs->hdr_index >= NUM_FILE_HDR_STRINGS) && (hs->file == NULL)) {
916  /* When we are at the end of the headers, check for data to send
917  * instead of waiting for ACK from remote side to continue
918  * (which would happen when sending files from async read). */
919  if(http_check_eof(pcb, hs)) {
920  data_to_send = HTTP_DATA_TO_SEND_CONTINUE;
921  }
922  }
923  /* If we get here and there are still header bytes to send, we send
924  * the header information we just wrote immediately. If there are no
925  * more headers to send, but we do have file data to send, drop through
926  * to try to send some file data too. */
927  if((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) {
928  LWIP_DEBUGF(HTTPD_DEBUG, ("tcp_output\n"));
929  return HTTP_DATA_TO_SEND_BREAK;
930  }
931  return data_to_send;
932 }
933 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
934 
941 static u8_t
942 http_check_eof(struct tcp_pcb *pcb, struct http_state *hs)
943 {
944  int bytes_left;
945 #if LWIP_HTTPD_DYNAMIC_FILE_READ
946  int count;
947 #ifdef HTTPD_MAX_WRITE_LEN
948  int max_write_len;
949 #endif /* HTTPD_MAX_WRITE_LEN */
950 #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */
951 
952  /* Do we have a valid file handle? */
953  if (hs->handle == NULL) {
954  /* No - close the connection. */
955  http_eof(pcb, hs);
956  return 0;
957  }
958  bytes_left = fs_bytes_left(hs->handle);
959  if (bytes_left <= 0) {
960  /* We reached the end of the file so this request is done. */
961  LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
962  http_eof(pcb, hs);
963  return 0;
964  }
965 #if LWIP_HTTPD_DYNAMIC_FILE_READ
966  /* Do we already have a send buffer allocated? */
967  if(hs->buf) {
968  /* Yes - get the length of the buffer */
969  count = hs->buf_len;
970  } else {
971  /* We don't have a send buffer so allocate one now */
972  count = tcp_sndbuf(pcb);
973  if(bytes_left < count) {
974  count = bytes_left;
975  }
976 #ifdef HTTPD_MAX_WRITE_LEN
977  /* Additional limitation: e.g. don't enqueue more than 2*mss at once */
978  max_write_len = HTTPD_MAX_WRITE_LEN(pcb);
979  if (count > max_write_len) {
980  count = max_write_len;
981  }
982 #endif /* HTTPD_MAX_WRITE_LEN */
983  do {
984  hs->buf = (char*)mem_malloc((mem_size_t)count);
985  if (hs->buf != NULL) {
986  hs->buf_len = count;
987  break;
988  }
989  count = count / 2;
990  } while (count > 100);
991 
992  /* Did we get a send buffer? If not, return immediately. */
993  if (hs->buf == NULL) {
994  LWIP_DEBUGF(HTTPD_DEBUG, ("No buff\n"));
995  return 0;
996  }
997  }
998 
999  /* Read a block of data from the file. */
1000  LWIP_DEBUGF(HTTPD_DEBUG, ("Trying to read %d bytes.\n", count));
1001 
1002 #if LWIP_HTTPD_FS_ASYNC_READ
1003  count = fs_read_async(hs->handle, hs->buf, count, http_continue, hs);
1004 #else /* LWIP_HTTPD_FS_ASYNC_READ */
1005  count = fs_read(hs->handle, hs->buf, count);
1006 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
1007  if (count < 0) {
1008  if (count == FS_READ_DELAYED) {
1009  /* Delayed read, wait for FS to unblock us */
1010  return 0;
1011  }
1012  /* We reached the end of the file so this request is done.
1013  * @todo: don't close here for HTTP/1.1? */
1014  LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
1015  http_eof(pcb, hs);
1016  return 0;
1017  }
1018 
1019  /* Set up to send the block of data we just read */
1020  LWIP_DEBUGF(HTTPD_DEBUG, ("Read %d bytes.\n", count));
1021  hs->left = count;
1022  hs->file = hs->buf;
1023 #if LWIP_HTTPD_SSI
1024  if (hs->ssi) {
1025  hs->ssi->parse_left = count;
1026  hs->ssi->parsed = hs->buf;
1027  }
1028 #endif /* LWIP_HTTPD_SSI */
1029 #else /* LWIP_HTTPD_DYNAMIC_FILE_READ */
1030  LWIP_ASSERT("SSI and DYNAMIC_HEADERS turned off but eof not reached", 0);
1031 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
1032  return 1;
1033 }
1034 
1040 static u8_t
1041 http_send_data_nonssi(struct tcp_pcb *pcb, struct http_state *hs)
1042 {
1043  err_t err;
1044  u16_t len;
1045  u8_t data_to_send = 0;
1046 
1047  /* We are not processing an SHTML file so no tag checking is necessary.
1048  * Just send the data as we received it from the file. */
1049  len = (u16_t)LWIP_MIN(hs->left, 0xffff);
1050 
1051  err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1052  if (err == ERR_OK) {
1053  data_to_send = 1;
1054  hs->file += len;
1055  hs->left -= len;
1056  }
1057 
1058  return data_to_send;
1059 }
1060 
1061 #if LWIP_HTTPD_SSI
1062 
1067 static u8_t
1068 http_send_data_ssi(struct tcp_pcb *pcb, struct http_state *hs)
1069 {
1070  err_t err = ERR_OK;
1071  u16_t len;
1072  u8_t data_to_send = 0;
1073 
1074  struct http_ssi_state *ssi = hs->ssi;
1075  LWIP_ASSERT("ssi != NULL", ssi != NULL);
1076  /* We are processing an SHTML file so need to scan for tags and replace
1077  * them with insert strings. We need to be careful here since a tag may
1078  * straddle the boundary of two blocks read from the file and we may also
1079  * have to split the insert string between two tcp_write operations. */
1080 
1081  /* How much data could we send? */
1082  len = tcp_sndbuf(pcb);
1083 
1084  /* Do we have remaining data to send before parsing more? */
1085  if(ssi->parsed > hs->file) {
1086  len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff);
1087 
1088  err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1089  if (err == ERR_OK) {
1090  data_to_send = 1;
1091  hs->file += len;
1092  hs->left -= len;
1093  }
1094 
1095  /* If the send buffer is full, return now. */
1096  if(tcp_sndbuf(pcb) == 0) {
1097  return data_to_send;
1098  }
1099  }
1100 
1101  LWIP_DEBUGF(HTTPD_DEBUG, ("State %d, %d left\n", ssi->tag_state, (int)ssi->parse_left));
1102 
1103  /* We have sent all the data that was already parsed so continue parsing
1104  * the buffer contents looking for SSI tags. */
1105  while((ssi->parse_left) && (err == ERR_OK)) {
1106  if (len == 0) {
1107  return data_to_send;
1108  }
1109  switch(ssi->tag_state) {
1110  case TAG_NONE:
1111  /* We are not currently processing an SSI tag so scan for the
1112  * start of the lead-in marker. */
1113  if(*ssi->parsed == g_pcTagLeadIn[0]) {
1114  /* We found what could be the lead-in for a new tag so change
1115  * state appropriately. */
1116  ssi->tag_state = TAG_LEADIN;
1117  ssi->tag_index = 1;
1118 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1119  ssi->tag_started = ssi->parsed;
1120 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
1121  }
1122 
1123  /* Move on to the next character in the buffer */
1124  ssi->parse_left--;
1125  ssi->parsed++;
1126  break;
1127 
1128  case TAG_LEADIN:
1129  /* We are processing the lead-in marker, looking for the start of
1130  * the tag name. */
1131 
1132  /* Have we reached the end of the leadin? */
1133  if(ssi->tag_index == LEN_TAG_LEAD_IN) {
1134  ssi->tag_index = 0;
1135  ssi->tag_state = TAG_FOUND;
1136  } else {
1137  /* Have we found the next character we expect for the tag leadin? */
1138  if(*ssi->parsed == g_pcTagLeadIn[ssi->tag_index]) {
1139  /* Yes - move to the next one unless we have found the complete
1140  * leadin, in which case we start looking for the tag itself */
1141  ssi->tag_index++;
1142  } else {
1143  /* We found an unexpected character so this is not a tag. Move
1144  * back to idle state. */
1145  ssi->tag_state = TAG_NONE;
1146  }
1147 
1148  /* Move on to the next character in the buffer */
1149  ssi->parse_left--;
1150  ssi->parsed++;
1151  }
1152  break;
1153 
1154  case TAG_FOUND:
1155  /* We are reading the tag name, looking for the start of the
1156  * lead-out marker and removing any whitespace found. */
1157 
1158  /* Remove leading whitespace between the tag leading and the first
1159  * tag name character. */
1160  if((ssi->tag_index == 0) && ((*ssi->parsed == ' ') ||
1161  (*ssi->parsed == '\t') || (*ssi->parsed == '\n') ||
1162  (*ssi->parsed == '\r'))) {
1163  /* Move on to the next character in the buffer */
1164  ssi->parse_left--;
1165  ssi->parsed++;
1166  break;
1167  }
1168 
1169  /* Have we found the end of the tag name? This is signalled by
1170  * us finding the first leadout character or whitespace */
1171  if((*ssi->parsed == g_pcTagLeadOut[0]) ||
1172  (*ssi->parsed == ' ') || (*ssi->parsed == '\t') ||
1173  (*ssi->parsed == '\n') || (*ssi->parsed == '\r')) {
1174 
1175  if(ssi->tag_index == 0) {
1176  /* We read a zero length tag so ignore it. */
1177  ssi->tag_state = TAG_NONE;
1178  } else {
1179  /* We read a non-empty tag so go ahead and look for the
1180  * leadout string. */
1181  ssi->tag_state = TAG_LEADOUT;
1182  LWIP_ASSERT("ssi->tag_index <= 0xff", ssi->tag_index <= 0xff);
1183  ssi->tag_name_len = (u8_t)ssi->tag_index;
1184  ssi->tag_name[ssi->tag_index] = '\0';
1185  if(*ssi->parsed == g_pcTagLeadOut[0]) {
1186  ssi->tag_index = 1;
1187  } else {
1188  ssi->tag_index = 0;
1189  }
1190  }
1191  } else {
1192  /* This character is part of the tag name so save it */
1193  if(ssi->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) {
1194  ssi->tag_name[ssi->tag_index++] = *ssi->parsed;
1195  } else {
1196  /* The tag was too long so ignore it. */
1197  ssi->tag_state = TAG_NONE;
1198  }
1199  }
1200 
1201  /* Move on to the next character in the buffer */
1202  ssi->parse_left--;
1203  ssi->parsed++;
1204 
1205  break;
1206 
1207  /* We are looking for the end of the lead-out marker. */
1208  case TAG_LEADOUT:
1209  /* Remove leading whitespace between the tag leading and the first
1210  * tag leadout character. */
1211  if((ssi->tag_index == 0) && ((*ssi->parsed == ' ') ||
1212  (*ssi->parsed == '\t') || (*ssi->parsed == '\n') ||
1213  (*ssi->parsed == '\r'))) {
1214  /* Move on to the next character in the buffer */
1215  ssi->parse_left--;
1216  ssi->parsed++;
1217  break;
1218  }
1219 
1220  /* Have we found the next character we expect for the tag leadout? */
1221  if(*ssi->parsed == g_pcTagLeadOut[ssi->tag_index]) {
1222  /* Yes - move to the next one unless we have found the complete
1223  * leadout, in which case we need to call the client to process
1224  * the tag. */
1225 
1226  /* Move on to the next character in the buffer */
1227  ssi->parse_left--;
1228  ssi->parsed++;
1229 
1230  if(ssi->tag_index == (LEN_TAG_LEAD_OUT - 1)) {
1231  /* Call the client to ask for the insert string for the
1232  * tag we just found. */
1233 #if LWIP_HTTPD_SSI_MULTIPART
1234  ssi->tag_part = 0; /* start with tag part 0 */
1235 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1236  get_tag_insert(hs);
1237 
1238  /* Next time through, we are going to be sending data
1239  * immediately, either the end of the block we start
1240  * sending here or the insert string. */
1241  ssi->tag_index = 0;
1242  ssi->tag_state = TAG_SENDING;
1243  ssi->tag_end = ssi->parsed;
1244 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1245  ssi->parsed = ssi->tag_started;
1246 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1247 
1248  /* If there is any unsent data in the buffer prior to the
1249  * tag, we need to send it now. */
1250  if (ssi->tag_end > hs->file) {
1251  /* How much of the data can we send? */
1252 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1253  len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff);
1254 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1255  /* we would include the tag in sending */
1256  len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff);
1257 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1258 
1259  err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1260  if (err == ERR_OK) {
1261  data_to_send = 1;
1262 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1263  if(ssi->tag_started <= hs->file) {
1264  /* pretend to have sent the tag, too */
1265  len += ssi->tag_end - ssi->tag_started;
1266  }
1267 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1268  hs->file += len;
1269  hs->left -= len;
1270  }
1271  }
1272  } else {
1273  ssi->tag_index++;
1274  }
1275  } else {
1276  /* We found an unexpected character so this is not a tag. Move
1277  * back to idle state. */
1278  ssi->parse_left--;
1279  ssi->parsed++;
1280  ssi->tag_state = TAG_NONE;
1281  }
1282  break;
1283 
1284  /*
1285  * We have found a valid tag and are in the process of sending
1286  * data as a result of that discovery. We send either remaining data
1287  * from the file prior to the insert point or the insert string itself.
1288  */
1289  case TAG_SENDING:
1290  /* Do we have any remaining file data to send from the buffer prior
1291  * to the tag? */
1292  if(ssi->tag_end > hs->file) {
1293  /* How much of the data can we send? */
1294 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1295  len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff);
1296 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1297  LWIP_ASSERT("hs->started >= hs->file", ssi->tag_started >= hs->file);
1298  /* we would include the tag in sending */
1299  len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff);
1300 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1301  if (len != 0) {
1302  err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1303  } else {
1304  err = ERR_OK;
1305  }
1306  if (err == ERR_OK) {
1307  data_to_send = 1;
1308 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1309  if(ssi->tag_started <= hs->file) {
1310  /* pretend to have sent the tag, too */
1311  len += ssi->tag_end - ssi->tag_started;
1312  }
1313 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1314  hs->file += len;
1315  hs->left -= len;
1316  }
1317  } else {
1318 #if LWIP_HTTPD_SSI_MULTIPART
1319  if(ssi->tag_index >= ssi->tag_insert_len) {
1320  /* Did the last SSIHandler have more to send? */
1321  if (ssi->tag_part != HTTPD_LAST_TAG_PART) {
1322  /* If so, call it again */
1323  ssi->tag_index = 0;
1324  get_tag_insert(hs);
1325  }
1326  }
1327 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1328 
1329  /* Do we still have insert data left to send? */
1330  if(ssi->tag_index < ssi->tag_insert_len) {
1331  /* We are sending the insert string itself. How much of the
1332  * insert can we send? */
1333  len = (ssi->tag_insert_len - ssi->tag_index);
1334 
1335  /* Note that we set the copy flag here since we only have a
1336  * single tag insert buffer per connection. If we don't do
1337  * this, insert corruption can occur if more than one insert
1338  * is processed before we call tcp_output. */
1339  err = http_write(pcb, &(ssi->tag_insert[ssi->tag_index]), &len,
1340  HTTP_IS_TAG_VOLATILE(hs));
1341  if (err == ERR_OK) {
1342  data_to_send = 1;
1343  ssi->tag_index += len;
1344  /* Don't return here: keep on sending data */
1345  }
1346  } else {
1347 #if LWIP_HTTPD_SSI_MULTIPART
1348  if (ssi->tag_part == HTTPD_LAST_TAG_PART)
1349 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1350  {
1351  /* We have sent all the insert data so go back to looking for
1352  * a new tag. */
1353  LWIP_DEBUGF(HTTPD_DEBUG, ("Everything sent.\n"));
1354  ssi->tag_index = 0;
1355  ssi->tag_state = TAG_NONE;
1356 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1357  ssi->parsed = ssi->tag_end;
1358 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1359  }
1360  }
1361  break;
1362  }
1363  default:
1364  LWIP_ASSERT("Unknown state", 0);
1365  break;
1366  }
1367  }
1368 
1369  /* If we drop out of the end of the for loop, this implies we must have
1370  * file data to send so send it now. In TAG_SENDING state, we've already
1371  * handled this so skip the send if that's the case. */
1372  if((ssi->tag_state != TAG_SENDING) && (ssi->parsed > hs->file)) {
1373  len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff);
1374 
1375  err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1376  if (err == ERR_OK) {
1377  data_to_send = 1;
1378  hs->file += len;
1379  hs->left -= len;
1380  }
1381  }
1382  return data_to_send;
1383 }
1384 #endif /* LWIP_HTTPD_SSI */
1385 
1392 static u8_t
1393 http_send(struct tcp_pcb *pcb, struct http_state *hs)
1394 {
1395  u8_t data_to_send = HTTP_NO_DATA_TO_SEND;
1396 
1397  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_send: pcb=%p hs=%p left=%d\n", (void*)pcb,
1398  (void*)hs, hs != NULL ? (int)hs->left : 0));
1399 
1400 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
1401  if (hs->unrecved_bytes != 0) {
1402  return 0;
1403  }
1404 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
1405 
1406  /* If we were passed a NULL state structure pointer, ignore the call. */
1407  if (hs == NULL) {
1408  return 0;
1409  }
1410 
1411 #if LWIP_HTTPD_FS_ASYNC_READ
1412  /* Check if we are allowed to read from this file.
1413  (e.g. SSI might want to delay sending until data is available) */
1414  if (!fs_is_file_ready(hs->handle, http_continue, hs)) {
1415  return 0;
1416  }
1417 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
1418 
1419 #if LWIP_HTTPD_DYNAMIC_HEADERS
1420  /* Do we have any more header data to send for this file? */
1421  if(hs->hdr_index < NUM_FILE_HDR_STRINGS) {
1422  data_to_send = http_send_headers(pcb, hs);
1423  if (data_to_send != HTTP_DATA_TO_SEND_CONTINUE) {
1424  return data_to_send;
1425  }
1426  }
1427 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
1428 
1429  /* Have we run out of file data to send? If so, we need to read the next
1430  * block from the file. */
1431  if (hs->left == 0) {
1432  if (!http_check_eof(pcb, hs)) {
1433  return 0;
1434  }
1435  }
1436 
1437 #if LWIP_HTTPD_SSI
1438  if(hs->ssi) {
1439  data_to_send = http_send_data_ssi(pcb, hs);
1440  } else
1441 #endif /* LWIP_HTTPD_SSI */
1442  {
1443  data_to_send = http_send_data_nonssi(pcb, hs);
1444  }
1445 
1446  if((hs->left == 0) && (fs_bytes_left(hs->handle) <= 0)) {
1447  /* We reached the end of the file so this request is done.
1448  * This adds the FIN flag right into the last data segment. */
1449  LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
1450  http_eof(pcb, hs);
1451  return 0;
1452  }
1453  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("send_data end.\n"));
1454  return data_to_send;
1455 }
1456 
1457 #if LWIP_HTTPD_SUPPORT_EXTSTATUS
1458 
1465 static err_t
1466 http_find_error_file(struct http_state *hs, u16_t error_nr)
1467 {
1468  const char *uri1, *uri2, *uri3;
1469  err_t err;
1470 
1471  if (error_nr == 501) {
1472  uri1 = "/501.html";
1473  uri2 = "/501.htm";
1474  uri3 = "/501.shtml";
1475  } else {
1476  /* 400 (bad request is the default) */
1477  uri1 = "/400.html";
1478  uri2 = "/400.htm";
1479  uri3 = "/400.shtml";
1480  }
1481  err = fs_open(&hs->file_handle, uri1);
1482  if (err != ERR_OK) {
1483  err = fs_open(&hs->file_handle, uri2);
1484  if (err != ERR_OK) {
1485  err = fs_open(&hs->file_handle, uri3);
1486  if (err != ERR_OK) {
1487  LWIP_DEBUGF(HTTPD_DEBUG, ("Error page for error %"U16_F" not found\n",
1488  error_nr));
1489  return ERR_ARG;
1490  }
1491  }
1492  }
1493  return http_init_file(hs, &hs->file_handle, 0, NULL, 0);
1494 }
1495 #else /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1496 #define http_find_error_file(hs, error_nr) ERR_ARG
1497 #endif /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1498 
1506 static struct fs_file *
1507 http_get_404_file(struct http_state *hs, const char **uri)
1508 {
1509  err_t err;
1510 
1511  *uri = "/404.html";
1512  err = fs_open(&hs->file_handle, *uri);
1513  if (err != ERR_OK) {
1514  /* 404.html doesn't exist. Try 404.htm instead. */
1515  *uri = "/404.htm";
1516  err = fs_open(&hs->file_handle, *uri);
1517  if (err != ERR_OK) {
1518  /* 404.htm doesn't exist either. Try 404.shtml instead. */
1519  *uri = "/404.shtml";
1520  err = fs_open(&hs->file_handle, *uri);
1521  if (err != ERR_OK) {
1522  /* 404.htm doesn't exist either. Indicate to the caller that it should
1523  * send back a default 404 page.
1524  */
1525  *uri = NULL;
1526  return NULL;
1527  }
1528  }
1529  }
1530 
1531  return &hs->file_handle;
1532 }
1533 
1534 #if LWIP_HTTPD_SUPPORT_POST
1535 static err_t
1536 http_handle_post_finished(struct http_state *hs)
1537 {
1538 #if LWIP_HTTPD_POST_MANUAL_WND
1539  /* Prevent multiple calls to httpd_post_finished, since it might have already
1540  been called before from httpd_post_data_recved(). */
1541  if (hs->post_finished) {
1542  return ERR_OK;
1543  }
1544  hs->post_finished = 1;
1545 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1546  /* application error or POST finished */
1547  /* NULL-terminate the buffer */
1548  http_post_response_filename[0] = 0;
1549  httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
1550  return http_find_file(hs, http_post_response_filename, 0);
1551 }
1552 
1562 static err_t
1563 http_post_rxpbuf(struct http_state *hs, struct pbuf *p)
1564 {
1565  err_t err;
1566 
1567  /* adjust remaining Content-Length */
1568  if (hs->post_content_len_left < p->tot_len) {
1569  hs->post_content_len_left = 0;
1570  } else {
1571  hs->post_content_len_left -= p->tot_len;
1572  }
1573  err = httpd_post_receive_data(hs, p);
1574  if (err != ERR_OK) {
1575  /* Ignore remaining content in case of application error */
1576  hs->post_content_len_left = 0;
1577  }
1578  if (hs->post_content_len_left == 0) {
1579 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
1580  if (hs->unrecved_bytes != 0) {
1581  return ERR_OK;
1582  }
1583 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
1584  /* application error or POST finished */
1585  return http_handle_post_finished(hs);
1586  }
1587 
1588  return ERR_OK;
1589 }
1590 
1605 static err_t
1606 http_post_request(struct pbuf *inp, struct http_state *hs,
1607  char *data, u16_t data_len, char *uri, char *uri_end)
1608 {
1609  err_t err;
1610  /* search for end-of-header (first double-CRLF) */
1611  char* crlfcrlf = strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data));
1612 
1613  if (crlfcrlf != NULL) {
1614  /* search for "Content-Length: " */
1615 #define HTTP_HDR_CONTENT_LEN "Content-Length: "
1616 #define HTTP_HDR_CONTENT_LEN_LEN 16
1617 #define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10
1618  char *scontent_len = strnstr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1));
1619  if (scontent_len != NULL) {
1620  char *scontent_len_end = strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN);
1621  if (scontent_len_end != NULL) {
1622  int content_len;
1623  char *conten_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN;
1624  content_len = atoi(conten_len_num);
1625  if (content_len > 0) {
1626  /* adjust length of HTTP header passed to application */
1627  const char *hdr_start_after_uri = uri_end + 1;
1628  u16_t hdr_len = LWIP_MIN(data_len, crlfcrlf + 4 - data);
1629  u16_t hdr_data_len = LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri);
1630  u8_t post_auto_wnd = 1;
1631  http_post_response_filename[0] = 0;
1632  err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len,
1633  http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN, &post_auto_wnd);
1634  if (err == ERR_OK) {
1635  /* try to pass in data of the first pbuf(s) */
1636  struct pbuf *q = inp;
1637  u16_t start_offset = hdr_len;
1638 #if LWIP_HTTPD_POST_MANUAL_WND
1639  hs->no_auto_wnd = !post_auto_wnd;
1640 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1641  /* set the Content-Length to be received for this POST */
1642  hs->post_content_len_left = (u32_t)content_len;
1643 
1644  /* get to the pbuf where the body starts */
1645  while((q != NULL) && (q->len <= start_offset)) {
1646  start_offset -= q->len;
1647  q = q->next;
1648  }
1649  if (q != NULL) {
1650  /* hide the remaining HTTP header */
1651  pbuf_header(q, -(s16_t)start_offset);
1652 #if LWIP_HTTPD_POST_MANUAL_WND
1653  if (!post_auto_wnd) {
1654  /* already tcp_recved() this data... */
1655  hs->unrecved_bytes = q->tot_len;
1656  }
1657 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1658  pbuf_ref(q);
1659  return http_post_rxpbuf(hs, q);
1660  } else {
1661  return ERR_OK;
1662  }
1663  } else {
1664  /* return file passed from application */
1665  return http_find_file(hs, http_post_response_filename, 0);
1666  }
1667  } else {
1668  LWIP_DEBUGF(HTTPD_DEBUG, ("POST received invalid Content-Length: %s\n",
1669  conten_len_num));
1670  return ERR_ARG;
1671  }
1672  }
1673  }
1674  /* If we come here, headers are fully received (double-crlf), but Content-Length
1675  was not included. Since this is currently the only supported method, we have
1676  to fail in this case! */
1677  LWIP_DEBUGF(HTTPD_DEBUG, ("Error when parsing Content-Length\n"));
1678  return ERR_ARG;
1679  }
1680  /* if we come here, the POST is incomplete */
1681 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1682  return ERR_INPROGRESS;
1683 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1684  return ERR_ARG;
1685 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1686 }
1687 
1688 #if LWIP_HTTPD_POST_MANUAL_WND
1689 
1697 void httpd_post_data_recved(void *connection, u16_t recved_len)
1698 {
1699  struct http_state *hs = (struct http_state*)connection;
1700  if (hs != NULL) {
1701  if (hs->no_auto_wnd) {
1702  u16_t len = recved_len;
1703  if (hs->unrecved_bytes >= recved_len) {
1704  hs->unrecved_bytes -= recved_len;
1705  } else {
1706  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_LEVEL_WARNING, ("httpd_post_data_recved: recved_len too big\n"));
1707  len = (u16_t)hs->unrecved_bytes;
1708  hs->unrecved_bytes = 0;
1709  }
1710  if (hs->pcb != NULL) {
1711  if (len != 0) {
1712  tcp_recved(hs->pcb, len);
1713  }
1714  if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) {
1715  /* finished handling POST */
1716  http_handle_post_finished(hs);
1717  http_send(hs->pcb, hs);
1718  }
1719  }
1720  }
1721  }
1722 }
1723 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1724 
1725 #endif /* LWIP_HTTPD_SUPPORT_POST */
1726 
1727 #if LWIP_HTTPD_FS_ASYNC_READ
1728 
1731 static void
1732 http_continue(void *connection)
1733 {
1734  struct http_state *hs = (struct http_state*)connection;
1735  if (hs && (hs->pcb) && (hs->handle)) {
1736  LWIP_ASSERT("hs->pcb != NULL", hs->pcb != NULL);
1737  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("httpd_continue: try to send more data\n"));
1738  if (http_send(hs->pcb, hs)) {
1739  /* If we wrote anything to be sent, go ahead and send it now. */
1740  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
1741  tcp_output(hs->pcb);
1742  }
1743  }
1744 }
1745 #endif /* LWIP_HTTPD_FS_ASYNC_READ */
1746 
1758 static err_t
1759 http_parse_request(struct pbuf *inp, struct http_state *hs, struct tcp_pcb *pcb)
1760 {
1761  char *data;
1762  char *crlf;
1763  u16_t data_len;
1764  struct pbuf *p = inp;
1765 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1766  u16_t clen;
1767 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1768 #if LWIP_HTTPD_SUPPORT_POST
1769  err_t err;
1770 #endif /* LWIP_HTTPD_SUPPORT_POST */
1771 
1772  LWIP_UNUSED_ARG(pcb); /* only used for post */
1773  LWIP_ASSERT("p != NULL", p != NULL);
1774  LWIP_ASSERT("hs != NULL", hs != NULL);
1775 
1776  if ((hs->handle != NULL) || (hs->file != NULL)) {
1777  LWIP_DEBUGF(HTTPD_DEBUG, ("Received data while sending a file\n"));
1778  /* already sending a file */
1779  /* @todo: abort? */
1780  return ERR_USE;
1781  }
1782 
1783 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1784 
1785  LWIP_DEBUGF(HTTPD_DEBUG, ("Received %"U16_F" bytes\n", p->tot_len));
1786 
1787  /* first check allowed characters in this pbuf? */
1788 
1789  /* enqueue the pbuf */
1790  if (hs->req == NULL) {
1791  LWIP_DEBUGF(HTTPD_DEBUG, ("First pbuf\n"));
1792  hs->req = p;
1793  } else {
1794  LWIP_DEBUGF(HTTPD_DEBUG, ("pbuf enqueued\n"));
1795  pbuf_cat(hs->req, p);
1796  }
1797  /* increase pbuf ref counter as it is freed when we return but we want to
1798  keep it on the req list */
1799  pbuf_ref(p);
1800 
1801  if (hs->req->next != NULL) {
1802  data_len = LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH);
1803  pbuf_copy_partial(hs->req, httpd_req_buf, data_len, 0);
1804  data = httpd_req_buf;
1805  } else
1806 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1807  {
1808  data = (char *)p->payload;
1809  data_len = p->len;
1810  if (p->len != p->tot_len) {
1811  LWIP_DEBUGF(HTTPD_DEBUG, ("Warning: incomplete header due to chained pbufs\n"));
1812  }
1813  }
1814 
1815  /* received enough data for minimal request? */
1816  if (data_len >= MIN_REQ_LEN) {
1817  /* wait for CRLF before parsing anything */
1818  crlf = strnstr(data, CRLF, data_len);
1819  if (crlf != NULL) {
1820 #if LWIP_HTTPD_SUPPORT_POST
1821  int is_post = 0;
1822 #endif /* LWIP_HTTPD_SUPPORT_POST */
1823  int is_09 = 0;
1824  char *sp1, *sp2;
1825  u16_t left_len, uri_len;
1826  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("CRLF received, parsing request\n"));
1827  /* parse method */
1828  if (!strncmp(data, "GET ", 4)) {
1829  sp1 = data + 3;
1830  /* received GET request */
1831  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received GET request\"\n"));
1832 #if LWIP_HTTPD_SUPPORT_POST
1833  } else if (!strncmp(data, "POST ", 5)) {
1834  /* store request type */
1835  is_post = 1;
1836  sp1 = data + 4;
1837  /* received GET request */
1838  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received POST request\n"));
1839 #endif /* LWIP_HTTPD_SUPPORT_POST */
1840  } else {
1841  /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1842  data[4] = 0;
1843  /* unsupported method! */
1844  LWIP_DEBUGF(HTTPD_DEBUG, ("Unsupported request method (not implemented): \"%s\"\n",
1845  data));
1846  return http_find_error_file(hs, 501);
1847  }
1848  /* if we come here, method is OK, parse URI */
1849  left_len = (u16_t)(data_len - ((sp1 +1) - data));
1850  sp2 = strnstr(sp1 + 1, " ", left_len);
1851 #if LWIP_HTTPD_SUPPORT_V09
1852  if (sp2 == NULL) {
1853  /* HTTP 0.9: respond with correct protocol version */
1854  sp2 = strnstr(sp1 + 1, CRLF, left_len);
1855  is_09 = 1;
1856 #if LWIP_HTTPD_SUPPORT_POST
1857  if (is_post) {
1858  /* HTTP/0.9 does not support POST */
1859  goto badrequest;
1860  }
1861 #endif /* LWIP_HTTPD_SUPPORT_POST */
1862  }
1863 #endif /* LWIP_HTTPD_SUPPORT_V09 */
1864  uri_len = (u16_t)(sp2 - (sp1 + 1));
1865  if ((sp2 != 0) && (sp2 > sp1)) {
1866  /* wait for CRLFCRLF (indicating end of HTTP headers) before parsing anything */
1867  if (strnstr(data, CRLF CRLF, data_len) != NULL) {
1868  char *uri = sp1 + 1;
1869 #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
1870  if (!is_09 && strnstr(data, HTTP11_CONNECTIONKEEPALIVE, data_len)) {
1871  hs->keepalive = 1;
1872  } else {
1873  hs->keepalive = 0;
1874  }
1875 #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */
1876  /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1877  *sp1 = 0;
1878  uri[uri_len] = 0;
1879  LWIP_DEBUGF(HTTPD_DEBUG, ("Received \"%s\" request for URI: \"%s\"\n",
1880  data, uri));
1881 #if LWIP_HTTPD_SUPPORT_POST
1882  if (is_post) {
1883 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1884  struct pbuf *q = hs->req;
1885 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1886  struct pbuf *q = inp;
1887 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1888  err = http_post_request(q, hs, data, data_len, uri, sp2);
1889  if (err != ERR_OK) {
1890  /* restore header for next try */
1891  *sp1 = ' ';
1892  *sp2 = ' ';
1893  uri[uri_len] = ' ';
1894  }
1895  if (err == ERR_ARG) {
1896  goto badrequest;
1897  }
1898  return err;
1899  } else
1900 #endif /* LWIP_HTTPD_SUPPORT_POST */
1901  {
1902  return http_find_file(hs, uri, is_09);
1903  }
1904  }
1905  } else {
1906  LWIP_DEBUGF(HTTPD_DEBUG, ("invalid URI\n"));
1907  }
1908  }
1909  }
1910 
1911 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1912  clen = pbuf_clen(hs->req);
1913  if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) &&
1914  (clen <= LWIP_HTTPD_REQ_QUEUELEN)) {
1915  /* request not fully received (too short or CRLF is missing) */
1916  return ERR_INPROGRESS;
1917  } else
1918 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1919  {
1920 #if LWIP_HTTPD_SUPPORT_POST
1921 badrequest:
1922 #endif /* LWIP_HTTPD_SUPPORT_POST */
1923  LWIP_DEBUGF(HTTPD_DEBUG, ("bad request\n"));
1924  /* could not parse request */
1925  return http_find_error_file(hs, 400);
1926  }
1927 }
1928 
1938 static err_t
1939 http_find_file(struct http_state *hs, const char *uri, int is_09)
1940 {
1941  size_t loop;
1942  struct fs_file *file = NULL;
1943  char *params;
1944  err_t err;
1945 #if LWIP_HTTPD_CGI
1946  int i;
1947  int count;
1948 #endif /* LWIP_HTTPD_CGI */
1949 #if !LWIP_HTTPD_SSI
1950  const
1951 #endif /* !LWIP_HTTPD_SSI */
1952  /* By default, assume we will not be processing server-side-includes tags */
1953  u8_t tag_check = 0;
1954 
1955  /* Have we been asked for the default root file? */
1956  if((uri[0] == '/') && (uri[1] == 0)) {
1957  /* Try each of the configured default filenames until we find one
1958  that exists. */
1959  for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
1960  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Looking for %s...\n", g_psDefaultFilenames[loop].name));
1961  err = fs_open(&hs->file_handle, g_psDefaultFilenames[loop].name);
1962  uri = g_psDefaultFilenames[loop].name;
1963  if(err == ERR_OK) {
1964  file = &hs->file_handle;
1965  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opened.\n"));
1966 #if LWIP_HTTPD_SSI
1967  tag_check = g_psDefaultFilenames[loop].shtml;
1968 #endif /* LWIP_HTTPD_SSI */
1969  break;
1970  }
1971  }
1972  if (file == NULL) {
1973  /* None of the default filenames exist so send back a 404 page */
1974  file = http_get_404_file(hs, &uri);
1975 #if LWIP_HTTPD_SSI
1976  tag_check = 0;
1977 #endif /* LWIP_HTTPD_SSI */
1978  }
1979  } else {
1980  /* No - we've been asked for a specific file. */
1981  /* First, isolate the base URI (without any parameters) */
1982  params = (char *)strchr(uri, '?');
1983  if (params != NULL) {
1984  /* URI contains parameters. NULL-terminate the base URI */
1985  *params = '\0';
1986  params++;
1987  }
1988 
1989 #if LWIP_HTTPD_CGI
1990  /* Does the base URI we have isolated correspond to a CGI handler? */
1991  if (g_iNumCGIs && g_pCGIs) {
1992  for (i = 0; i < g_iNumCGIs; i++) {
1993  if (strcmp(uri, g_pCGIs[i].pcCGIName) == 0) {
1994  /*
1995  * We found a CGI that handles this URI so extract the
1996  * parameters and call the handler.
1997  */
1998  count = extract_uri_parameters(hs, params);
1999  uri = g_pCGIs[i].pfnCGIHandler(i, count, hs->params,
2000  hs->param_vals);
2001  break;
2002  }
2003  }
2004  }
2005 #endif /* LWIP_HTTPD_CGI */
2006 
2007  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opening %s\n", uri));
2008 
2009  err = fs_open(&hs->file_handle, uri);
2010  if (err == ERR_OK) {
2011  file = &hs->file_handle;
2012  } else {
2013  file = http_get_404_file(hs, &uri);
2014  }
2015 #if LWIP_HTTPD_SSI
2016  if (file != NULL) {
2017  /* See if we have been asked for an shtml file and, if so,
2018  enable tag checking. */
2019  tag_check = 0;
2020  for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
2021  if (strstr(uri, g_pcSSIExtensions[loop])) {
2022  tag_check = 1;
2023  break;
2024  }
2025  }
2026  }
2027 #endif /* LWIP_HTTPD_SSI */
2028  }
2029  return http_init_file(hs, file, is_09, uri, tag_check);
2030 }
2031 
2043 static err_t
2044 http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri, u8_t tag_check)
2045 {
2046  if (file != NULL) {
2047  /* file opened, initialise struct http_state */
2048 #if LWIP_HTTPD_SSI
2049  if (tag_check) {
2050  struct http_ssi_state *ssi = http_ssi_state_alloc();
2051  if (ssi != NULL) {
2052  ssi->tag_index = 0;
2053  ssi->tag_state = TAG_NONE;
2054  ssi->parsed = file->data;
2055  ssi->parse_left = file->len;
2056  ssi->tag_end = file->data;
2057  hs->ssi = ssi;
2058  }
2059  }
2060 #else /* LWIP_HTTPD_SSI */
2061  LWIP_UNUSED_ARG(tag_check);
2062 #endif /* LWIP_HTTPD_SSI */
2063  hs->handle = file;
2064  hs->file = file->data;
2065  LWIP_ASSERT("File length must be positive!", (file->len >= 0));
2066  hs->left = file->len;
2067  hs->retries = 0;
2068 #if LWIP_HTTPD_TIMING
2069  hs->time_started = sys_now();
2070 #endif /* LWIP_HTTPD_TIMING */
2071 #if !LWIP_HTTPD_DYNAMIC_HEADERS
2072  LWIP_ASSERT("HTTP headers not included in file system", hs->handle->http_header_included);
2073 #endif /* !LWIP_HTTPD_DYNAMIC_HEADERS */
2074 #if LWIP_HTTPD_SUPPORT_V09
2075  if (hs->handle->http_header_included && is_09) {
2076  /* HTTP/0.9 responses are sent without HTTP header,
2077  search for the end of the header. */
2078  const char *file_start = strnstr(hs->file, CRLF CRLF, hs->left);
2079  if (file_start != NULL) {
2080  size_t diff = file_start + 4 - hs->file;
2081  hs->file += diff;
2082  hs->left -= (u32_t)diff;
2083  }
2084  }
2085 #endif /* LWIP_HTTPD_SUPPORT_V09*/
2086  } else {
2087  hs->handle = NULL;
2088  hs->file = NULL;
2089  hs->left = 0;
2090  hs->retries = 0;
2091  }
2092 #if LWIP_HTTPD_DYNAMIC_HEADERS
2093  /* Determine the HTTP headers to send based on the file extension of
2094  * the requested URI. */
2095  if ((hs->handle == NULL) || !hs->handle->http_header_included) {
2096  get_http_headers(hs, (char*)uri);
2097  }
2098 #else /* LWIP_HTTPD_DYNAMIC_HEADERS */
2099  LWIP_UNUSED_ARG(uri);
2100 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
2101  return ERR_OK;
2102 }
2103 
2108 static void
2109 http_err(void *arg, err_t err)
2110 {
2111  struct http_state *hs = (struct http_state *)arg;
2112  LWIP_UNUSED_ARG(err);
2113 
2114  LWIP_DEBUGF(HTTPD_DEBUG, ("http_err: %s", lwip_strerr(err)));
2115 
2116  if (hs != NULL) {
2117  http_state_free(hs);
2118  }
2119 }
2120 
2125 static err_t
2126 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
2127 {
2128  struct http_state *hs = (struct http_state *)arg;
2129 
2130  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_sent %p\n", (void*)pcb));
2131 
2132  LWIP_UNUSED_ARG(len);
2133 
2134  if (hs == NULL) {
2135  return ERR_OK;
2136  }
2137 
2138  hs->retries = 0;
2139 
2140  http_send(pcb, hs);
2141 
2142  return ERR_OK;
2143 }
2144 
2152 static err_t
2153 http_poll(void *arg, struct tcp_pcb *pcb)
2154 {
2155  struct http_state *hs = (struct http_state *)arg;
2156  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n",
2157  (void*)pcb, (void*)hs, tcp_debug_state_str(pcb->state)));
2158 
2159  if (hs == NULL) {
2160  err_t closed;
2161  /* arg is null, close. */
2162  LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n"));
2163  closed = http_close_conn(pcb, NULL);
2164  LWIP_UNUSED_ARG(closed);
2165 #if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
2166  if (closed == ERR_MEM) {
2167  tcp_abort(pcb);
2168  return ERR_ABRT;
2169  }
2170 #endif /* LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR */
2171  return ERR_OK;
2172  } else {
2173  hs->retries++;
2174  if (hs->retries == HTTPD_MAX_RETRIES) {
2175  LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n"));
2176  http_close_conn(pcb, hs);
2177  return ERR_OK;
2178  }
2179 
2180  /* If this connection has a file open, try to send some more data. If
2181  * it has not yet received a GET request, don't do this since it will
2182  * cause the connection to close immediately. */
2183  if(hs && (hs->handle)) {
2184  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n"));
2185  if(http_send(pcb, hs)) {
2186  /* If we wrote anything to be sent, go ahead and send it now. */
2187  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
2188  tcp_output(pcb);
2189  }
2190  }
2191  }
2192 
2193  return ERR_OK;
2194 }
2195 
2200 static err_t
2201 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
2202 {
2203  struct http_state *hs = (struct http_state *)arg;
2204  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: pcb=%p pbuf=%p err=%s\n", (void*)pcb,
2205  (void*)p, lwip_strerr(err)));
2206 
2207  if ((err != ERR_OK) || (p == NULL) || (hs == NULL)) {
2208  /* error or closed by other side? */
2209  if (p != NULL) {
2210  /* Inform TCP that we have taken the data. */
2211  tcp_recved(pcb, p->tot_len);
2212  pbuf_free(p);
2213  }
2214  if (hs == NULL) {
2215  /* this should not happen, only to be robust */
2216  LWIP_DEBUGF(HTTPD_DEBUG, ("Error, http_recv: hs is NULL, close\n"));
2217  }
2218  http_close_conn(pcb, hs);
2219  return ERR_OK;
2220  }
2221 
2222 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
2223  if (hs->no_auto_wnd) {
2224  hs->unrecved_bytes += p->tot_len;
2225  } else
2226 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
2227  {
2228  /* Inform TCP that we have taken the data. */
2229  tcp_recved(pcb, p->tot_len);
2230  }
2231 
2232 #if LWIP_HTTPD_SUPPORT_POST
2233  if (hs->post_content_len_left > 0) {
2234  /* reset idle counter when POST data is received */
2235  hs->retries = 0;
2236  /* this is data for a POST, pass the complete pbuf to the application */
2237  http_post_rxpbuf(hs, p);
2238  /* pbuf is passed to the application, don't free it! */
2239  if (hs->post_content_len_left == 0) {
2240  /* all data received, send response or close connection */
2241  http_send(pcb, hs);
2242  }
2243  return ERR_OK;
2244  } else
2245 #endif /* LWIP_HTTPD_SUPPORT_POST */
2246  {
2247  if (hs->handle == NULL) {
2248  err_t parsed = http_parse_request(p, hs, pcb);
2249  LWIP_ASSERT("http_parse_request: unexpected return value", parsed == ERR_OK
2250  || parsed == ERR_INPROGRESS ||parsed == ERR_ARG || parsed == ERR_USE);
2251 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
2252  if (parsed != ERR_INPROGRESS) {
2253  /* request fully parsed or error */
2254  if (hs->req != NULL) {
2255  pbuf_free(hs->req);
2256  hs->req = NULL;
2257  }
2258  }
2259 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
2260  pbuf_free(p);
2261  if (parsed == ERR_OK) {
2262 #if LWIP_HTTPD_SUPPORT_POST
2263  if (hs->post_content_len_left == 0)
2264 #endif /* LWIP_HTTPD_SUPPORT_POST */
2265  {
2266  LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: data %p len %"S32_F"\n", hs->file, hs->left));
2267  http_send(pcb, hs);
2268  }
2269  } else if (parsed == ERR_ARG) {
2270  /* @todo: close on ERR_USE? */
2271  http_close_conn(pcb, hs);
2272  }
2273  } else {
2274  LWIP_DEBUGF(HTTPD_DEBUG, ("http_recv: already sending data\n"));
2275  /* already sending but still receiving data, we might want to RST here? */
2276  pbuf_free(p);
2277  }
2278  }
2279  return ERR_OK;
2280 }
2281 
2285 static err_t
2286 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
2287 {
2288  struct http_state *hs;
2289  struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)arg;
2290  LWIP_UNUSED_ARG(err);
2291  LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept %p / %p\n", (void*)pcb, arg));
2292 
2293  /* Decrease the listen backlog counter */
2294  tcp_accepted(lpcb);
2295  /* Set priority */
2296  tcp_setprio(pcb, HTTPD_TCP_PRIO);
2297 
2298  /* Allocate memory for the structure that holds the state of the
2299  connection - initialized by that function. */
2300  hs = http_state_alloc();
2301  if (hs == NULL) {
2302  LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n"));
2303  return ERR_MEM;
2304  }
2305  hs->pcb = pcb;
2306 
2307  /* Tell TCP that this is the structure we wish to be passed for our
2308  callbacks. */
2309  tcp_arg(pcb, hs);
2310 
2311  /* Set up the various callback functions */
2312  tcp_recv(pcb, http_recv);
2313  tcp_err(pcb, http_err);
2314  tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
2315  tcp_sent(pcb, http_sent);
2316 
2317  return ERR_OK;
2318 }
2319 
2323 static void
2324 httpd_init_addr(const ip_addr_t *local_addr)
2325 {
2326  struct tcp_pcb *pcb;
2327  err_t err;
2328 
2329  pcb = tcp_new();
2330  LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL);
2331  tcp_setprio(pcb, HTTPD_TCP_PRIO);
2332  /* set SOF_REUSEADDR here to explicitly bind httpd to multiple interfaces */
2333  err = tcp_bind(pcb, local_addr, HTTPD_SERVER_PORT);
2334  LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK);
2335  pcb = tcp_listen_dual(pcb);
2336  LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL);
2337  /* initialize callback arg and accept callback */
2338  tcp_arg(pcb, pcb);
2339  tcp_accept(pcb, http_accept);
2340 }
2341 
2345 void
2346 httpd_init(void)
2347 {
2348  LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n"));
2349 
2350 #if HTTPD_USE_MEM_POOL
2351  LWIP_MEMPOOL_INIT(HTTPD_STATE);
2352 #if LWIP_HTTPD_SSI
2353  LWIP_MEMPOOL_INIT(HTTPD_SSI_STATE);
2354 #endif /* LWIP_HTTPD_SSI */
2355 #endif /* HTTPD_USE_MEM_POOL */
2356 
2357  httpd_init_addr(IP_ADDR_ANY);
2358 }
2359 
2360 #if LWIP_HTTPD_SSI
2361 
2368 void
2369 http_set_ssi_handler(tSSIHandler ssi_handler, const char **tags, int num_tags)
2370 {
2371  LWIP_DEBUGF(HTTPD_DEBUG, ("http_set_ssi_handler\n"));
2372 
2373  LWIP_ASSERT("no ssi_handler given", ssi_handler != NULL);
2374  LWIP_ASSERT("no tags given", tags != NULL);
2375  LWIP_ASSERT("invalid number of tags", num_tags > 0);
2376 
2377  g_pfnSSIHandler = ssi_handler;
2378  g_ppcTags = tags;
2379  g_iNumTags = num_tags;
2380 }
2381 #endif /* LWIP_HTTPD_SSI */
2382 
2383 #if LWIP_HTTPD_CGI
2384 
2390 void
2391 http_set_cgi_handlers(const tCGI *cgis, int num_handlers)
2392 {
2393  LWIP_ASSERT("no cgis given", cgis != NULL);
2394  LWIP_ASSERT("invalid number of handlers", num_handlers > 0);
2395 
2396  g_pCGIs = cgis;
2397  g_iNumCGIs = num_handlers;
2398 }
2399 #endif /* LWIP_HTTPD_CGI */
2400 
2401 #endif /* LWIP_TCP */
#define LWIP_HTTPD_FILE_STATE
Definition: fs.h:52
#define LWIP_HTTPD_SSI_MULTIPART
Definition: httpd_opts.h:66
int fs_bytes_left(struct fs_file *file)
Definition: fs.c:174
#define HTTPD_TCP_PRIO
Definition: httpd_opts.h:130
#define ERR_USE
Definition: err.h:60
u16_t mem_size_t
Definition: mem.h:76
#define TCP_SND_QUEUELEN
Definition: lwipopts.h:125
void mem_free(void *rmem)
Definition: mem.c:334
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:46
#define U16_F
Definition: cc.h:48
signed short s16_t
Definition: cc.h:41
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define LWIP_HTTPD_MAX_TAG_NAME_LEN
Definition: httpd_opts.h:71
#define LWIP_MAX(x, y)
Definition: def.h:49
#define MEMCPY(dst, src, len)
Definition: opt.h:84
if(LCD_Lock==DISABLE)
Definition: lcd_log.c:249
u16_t len
Definition: pbuf.h:125
Definition: fs.h:70
#define LWIP_HTTPD_MAX_TAG_INSERT_LEN
Definition: httpd_opts.h:76
int len
Definition: fs.h:72
#define ERR_ABRT
Definition: err.h:67
void httpd_init(void)
#define S32_F
Definition: cc.h:52
#define lwip_strerr(x)
Definition: err.h:79
#define LWIP_MIN(x, y)
Definition: def.h:50
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define U32_F
Definition: cc.h:51
#define HTTPD_SERVER_PORT
Definition: httpd_opts.h:110
unsigned long u32_t
Definition: cc.h:42
#define LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN
Definition: httpd_opts.h:195
#define LWIP_MEMPOOL_INIT(name)
Definition: memp.h:101
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define HTTPD_DEBUG
Definition: httpd_opts.h:98
#define LWIP_DBG_TRACE
Definition: debug.h:57
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
struct pbuf * next
Definition: pbuf.h:110
#define LWIP_HTTPD_POST_MANUAL_WND
Definition: httpd_opts.h:80
#define LWIP_MEMPOOL_DECLARE(name, num, size, desc)
Definition: memp.h:88
#define LWIP_HTTPD_MAX_CGI_PARAMETERS
Definition: httpd_opts.h:58
void pbuf_ref(struct pbuf *p)
Definition: pbuf.c:757
#define LWIP_HTTPD_REQ_BUFSIZE
Definition: httpd_opts.h:180
const char * data
Definition: fs.h:71
#define HTTP_IS_TAG_VOLATILE(ptr)
Definition: httpd_opts.h:221
u32_t sys_now(void)
#define LWIP_HTTPD_REQ_QUEUELEN
Definition: httpd_opts.h:174
#define ERR_ARG
Definition: err.h:71
void fs_close(struct fs_file *file)
Definition: fs.c:141
u16_t pbuf_copy_partial(struct pbuf *buf, void *dataptr, u16_t len, u16_t offset)
Definition: pbuf.c:952
u8_t pbuf_clen(struct pbuf *p)
Definition: pbuf.c:738
#define LWIP_HTTPD_MAX_REQ_LENGTH
Definition: httpd_opts.h:187
void pbuf_cat(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:779
#define HTTPD_MAX_RETRIES
Definition: httpd_opts.h:118
unsigned char u8_t
Definition: cc.h:38
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
void * mem_malloc(mem_size_t size)
Definition: mem.c:517
#define HTTPD_DEBUG_TIMING
Definition: httpd_opts.h:138
#define HTTPD_POLL_INTERVAL
Definition: httpd_opts.h:123
#define ERR_INPROGRESS
Definition: err.h:57
struct fs_file * fs_open(const char *name)
Definition: fs.c:100
#define HTTPD_MAX_WRITE_LEN(pcb)
Definition: httpd_opts.h:234
#define FS_READ_DELAYED
Definition: fs.h:43
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
int fs_read(struct fs_file *file, char *buffer, int count)
Definition: fs.c:155