| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 *
31 */
32
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.
41 *
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.
44 *
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.
51 *
52 * File system images without headers can be created using the makefsfile
53 * tool with the -h command line option.
54 *
55 *
56 * Notes about valid SSI tags
57 * --------------------------
58 *
59 * The following assumptions are made about tags used in SSI markers:
60 *
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.
65 *
66 * Notes on CGI usage
67 * ------------------
68 *
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.
74 *
75 * @todo:
76 * - don't use mem_malloc() (for SSI/dynamic headers)
77 * - split too long functions into multiple smaller functions?
78 * - support more file types?
79 */
80 #include "httpd.h"
81 #include "lwip/debug.h"
82 #include "lwip/stats.h"
83 #include "httpd_structs.h"
84 #include "lwip/tcp.h"
85 #include "lwip_fs.h"
86
87 #include <string.h>
88 #include <stdlib.h>
89 #include <board.h>
90
91 #ifdef BOARD_HITEX_EVA_18504350
92 #define fs_open(nam) NULL
93 #define fs_read(fp,buff,sz) 0
94 #endif
95
96 #if LWIP_TCP
97
98 #ifndef HTTPD_DEBUG
99 #define HTTPD_DEBUG LWIP_DBG_OFF
100 #endif
101
107 #ifndef HTTPD_USE_MEM_POOL
108 #define HTTPD_USE_MEM_POOL 0
109 #endif
110
112 #ifndef HTTPD_SERVER_PORT
113 #define HTTPD_SERVER_PORT 80
114 #endif
115
120 #ifndef HTTPD_MAX_RETRIES
121 #define HTTPD_MAX_RETRIES 4
122 #endif
123
125 #ifndef HTTPD_POLL_INTERVAL
126 #define HTTPD_POLL_INTERVAL 4
127 #endif
128
132 #ifndef HTTPD_TCP_PRIO
133 #define HTTPD_TCP_PRIO TCP_PRIO_MIN
134 #endif
135
137 #ifndef LWIP_HTTPD_TIMING
138 #define LWIP_HTTPD_TIMING 0
139 #endif
140 #ifndef HTTPD_DEBUG_TIMING
141 #define HTTPD_DEBUG_TIMING LWIP_DBG_OFF
142 #endif
143
145 #ifndef LWIP_HTTPD_STRNSTR_PRIVATE
146 #define LWIP_HTTPD_STRNSTR_PRIVATE 1
147 #endif
148
151 #ifndef LWIP_HTTPD_SUPPORT_EXTSTATUS
152 #define LWIP_HTTPD_SUPPORT_EXTSTATUS 0
153 #endif
154
156 #ifndef LWIP_HTTPD_SUPPORT_V09
157 #define LWIP_HTTPD_SUPPORT_V09 1
158 #endif
159
161 #ifndef LWIP_HTTPD_SUPPORT_REQUESTLIST
162 #define LWIP_HTTPD_SUPPORT_REQUESTLIST 0
163 #endif
164
165 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
166
168 #ifndef LWIP_HTTPD_REQ_QUEUELEN
169 #define LWIP_HTTPD_REQ_QUEUELEN 10
170 #endif
171
174 #ifndef LWIP_HTTPD_REQ_BUFSIZE
175 #define LWIP_HTTPD_REQ_BUFSIZE LWIP_HTTPD_MAX_REQ_LENGTH
176 #endif
177
181 #ifndef LWIP_HTTPD_MAX_REQ_LENGTH
182 #define LWIP_HTTPD_MAX_REQ_LENGTH 1023
183 #endif
184 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
185
189 #ifndef LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN
190 #define LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN 63
191 #endif
192
195 #ifndef LWIP_HTTPD_SSI_INCLUDE_TAG
196 #define LWIP_HTTPD_SSI_INCLUDE_TAG 1
197 #endif
198
202 #ifndef LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
203 #define LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR 0
204 #endif
205
206 #ifndef true
207 #define true ((u8_t)1)
208 #endif
209
210 #ifndef false
211 #define false ((u8_t)0)
212 #endif
213
215 #define MIN_REQ_LEN 7
216
217 #define CRLF "\r\n"
218
223 #ifndef HTTP_IS_DATA_VOLATILE
224 #if LWIP_HTTPD_SSI
225 /* Copy for SSI files, no copy for non-SSI files */
226 #define HTTP_IS_DATA_VOLATILE(hs) ((hs)->tag_check ? TCP_WRITE_FLAG_COPY : 0)
227 #else /* LWIP_HTTPD_SSI */
228
229 #define HTTP_IS_DATA_VOLATILE(hs) (((hs->file != NULL) && (hs->handle != NULL) && (hs->file == \
230 (char*)hs->handle->data + hs->handle->len - hs->left)) \
231 ? 0 : TCP_WRITE_FLAG_COPY)
232 #endif /* LWIP_HTTPD_SSI */
233 #endif
234
236 #ifndef HTTP_IS_HDR_VOLATILE
237 #define HTTP_IS_HDR_VOLATILE(hs, ptr) 0
238 #endif
239
240 #if LWIP_HTTPD_SSI
241
242 #ifndef HTTP_IS_TAG_VOLATILE
243 #define HTTP_IS_TAG_VOLATILE(ptr) TCP_WRITE_FLAG_COPY
244 #endif
245 #endif /* LWIP_HTTPD_SSI */
246
247 typedef struct
248 {
249 const char *name;
250 u8_t shtml;
251 } default_filename;
252
253 const default_filename g_psDefaultFilenames[] = {
254 {"/index.shtml", true },
255 {"/index.ssi", true },
256 {"/index.shtm", true },
257 {"/index.html", false },
258 {"/index.htm", false }
259 };
260
261 #define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / \
262 sizeof(default_filename))
263
264 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
265
266 static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH+1];
267 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
268
269 #if LWIP_HTTPD_SUPPORT_POST
270
271 static char http_post_response_filename[LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN+1];
272 #endif /* LWIP_HTTPD_SUPPORT_POST */
273
274 #if LWIP_HTTPD_DYNAMIC_HEADERS
275 /* The number of individual strings that comprise the headers sent before each
276 * requested file.
277 */
278 #define NUM_FILE_HDR_STRINGS 3
279 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
280
281 #if LWIP_HTTPD_SSI
282
283 #define HTTPD_LAST_TAG_PART 0xFFFF
284
285 const char * const g_pcSSIExtensions[] = {
286 ".shtml", ".shtm", ".ssi", ".xml"
287 };
288
289 #define NUM_SHTML_EXTENSIONS (sizeof(g_pcSSIExtensions) / sizeof(const char *))
290
291 enum tag_check_state {
292 TAG_NONE, /* Not processing an SSI tag */
293 TAG_LEADIN, /* Tag lead in "<!--#" being processed */
294 TAG_FOUND, /* Tag name being read, looking for lead-out start */
295 TAG_LEADOUT, /* Tag lead out "-->" being processed */
296 TAG_SENDING /* Sending tag replacement string */
297 };
298 #endif /* LWIP_HTTPD_SSI */
299
300 struct http_state {
301 struct fs_file *handle;
302 char *file; /* Pointer to first unsent byte in buf. */
303
304 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
305 struct pbuf *req;
306 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
307
308 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
309 char *buf; /* File read buffer. */
310 int buf_len; /* Size of file read buffer, buf. */
311 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
312 u32_t left; /* Number of unsent bytes in buf. */
313 u8_t retries;
314 #if LWIP_HTTPD_SSI
315 const char *parsed; /* Pointer to the first unparsed byte in buf. */
316 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
317 const char *tag_started;/* Poitner to the first opening '<' of the tag. */
318 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
319 const char *tag_end; /* Pointer to char after the closing '>' of the tag. */
320 u32_t parse_left; /* Number of unparsed bytes in buf. */
321 u16_t tag_index; /* Counter used by tag parsing state machine */
322 u16_t tag_insert_len; /* Length of insert in string tag_insert */
323 #if LWIP_HTTPD_SSI_MULTIPART
324 u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */
325 #endif /* LWIP_HTTPD_SSI_MULTIPART */
326 u8_t tag_check; /* true if we are processing a .shtml file else false */
327 u8_t tag_name_len; /* Length of the tag name in string tag_name */
328 char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */
329 char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */
330 enum tag_check_state tag_state; /* State of the tag processor */
331 #endif /* LWIP_HTTPD_SSI */
332 #if LWIP_HTTPD_CGI
333 char *params[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Params extracted from the request URI */
334 char *param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Values for each extracted param */
335 #endif /* LWIP_HTTPD_CGI */
336 #if LWIP_HTTPD_DYNAMIC_HEADERS
337 const char *hdrs[NUM_FILE_HDR_STRINGS]; /* HTTP headers to be sent. */
338 u16_t hdr_pos; /* The position of the first unsent header byte in the
339 current string */
340 u16_t hdr_index; /* The index of the hdr string currently being sent. */
341 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
342 #if LWIP_HTTPD_TIMING
343 u32_t time_started;
344 #endif /* LWIP_HTTPD_TIMING */
345 #if LWIP_HTTPD_SUPPORT_POST
346 u32_t post_content_len_left;
347 #if LWIP_HTTPD_POST_MANUAL_WND
348 u32_t unrecved_bytes;
349 struct tcp_pcb *pcb;
350 u8_t no_auto_wnd;
351 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
352 #endif /* LWIP_HTTPD_SUPPORT_POST*/
353 };
354
355 static err_t http_find_file(struct http_state *hs, const char *uri, int is_09);
356 static err_t http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri);
357 static err_t http_poll(void *arg, struct tcp_pcb *pcb);
358
359 #if LWIP_HTTPD_SSI
360 /* SSI insert handler function pointer. */
361 tSSIHandler g_pfnSSIHandler = NULL;
362 int g_iNumTags = 0;
363 const char **g_ppcTags = NULL;
364
365 #define LEN_TAG_LEAD_IN 5
366 const char * const g_pcTagLeadIn = "<!--#";
367
368 #define LEN_TAG_LEAD_OUT 3
369 const char * const g_pcTagLeadOut = "-->";
370 #endif /* LWIP_HTTPD_SSI */
371
372 #if LWIP_HTTPD_CGI
373 /* CGI handler information */
374 const tCGI *g_pCGIs;
375 int g_iNumCGIs;
376 #endif /* LWIP_HTTPD_CGI */
377
378 #if LWIP_HTTPD_STRNSTR_PRIVATE
379
380 static char*
381 strnstr(const char* buffer, const char* token, size_t n)
382 {
383 const char* p;
384 int tokenlen = (int)strlen(token);
385 if (tokenlen == 0) {
386 return (char *)buffer;
387 }
388 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
389 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
390 return (char *)p;
391 }
392 }
393 return NULL;
394 }
395 #endif /* LWIP_HTTPD_STRNSTR_PRIVATE */
396
398 static struct http_state*
399 http_state_alloc(void)
400 {
401 struct http_state *ret;
402 #if HTTPD_USE_MEM_POOL
403 ret = (struct http_state *)memp_malloc(MEMP_HTTPD_STATE);
404 #else /* HTTPD_USE_MEM_POOL */
405 ret = (struct http_state *)mem_malloc(sizeof(struct http_state));
406 #endif /* HTTPD_USE_MEM_POOL */
407 if (ret != NULL) {
408 /* Initialize the structure. */
409 memset(ret, 0, sizeof(struct http_state));
410 #if LWIP_HTTPD_DYNAMIC_HEADERS
411 /* Indicate that the headers are not yet valid */
412 ret->hdr_index = NUM_FILE_HDR_STRINGS;
413 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
414 }
415 return ret;
416 }
417
421 static void
422 http_state_free(struct http_state *hs)
423 {
424 if (hs != NULL) {
425 if(hs->handle) {
426 #if LWIP_HTTPD_TIMING
427 u32_t ms_needed = sys_now() - hs->time_started;
428 u32_t needed = LWIP_MAX(1, (ms_needed/100));
429 LWIP_DEBUGF(HTTPD_DEBUG_TIMING, ("httpd: needed %"U32_F" ms to send file of %d bytes -> %"U32_F" bytes/sec\n",
430 ms_needed, hs->handle->len, ((((u32_t)hs->handle->len) * 10) / needed)));
431 #endif /* LWIP_HTTPD_TIMING */
432 fs_close(hs->handle);
433 hs->handle = NULL;
434 }
435 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
436 if (hs->buf != NULL) {
437 mem_free(hs->buf);
438 hs->buf = NULL;
439 }
440 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
441 #if HTTPD_USE_MEM_POOL
442 memp_free(MEMP_HTTPD_STATE, hs);
443 #else /* HTTPD_USE_MEM_POOL */
444 mem_free(hs);
445 #endif /* HTTPD_USE_MEM_POOL */
446 }
447 }
448
458 static err_t
459 http_write(struct tcp_pcb *pcb, const void* ptr, u16_t *length, u8_t apiflags)
460 {
461 u16_t len;
462 err_t err;
463 LWIP_ASSERT("length != NULL", length != NULL);
464 len = *length;
465 do {
466 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Trying go send %d bytes\n", len));
467 err = tcp_write(pcb, ptr, len, apiflags);
468 if (err == ERR_MEM) {
469 if ((tcp_sndbuf(pcb) == 0) ||
470 (tcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)) {
471 /* no need to try smaller sizes */
472 len = 1;
473 } else {
474 len /= 2;
475 }
476 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE,
477 ("Send failed, trying less (%d bytes)\n", len));
478 }
479 } while ((err == ERR_MEM) && (len > 1));
480
481 if (err == ERR_OK) {
482 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Sent %d bytes\n", len));
483 } else {
484 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Send failed with err %d (\"%s\")\n", err, lwip_strerr(err)));
485 }
486
487 *length = len;
488 return err;
489 }
490
498 static err_t
499 http_close_conn(struct tcp_pcb *pcb, struct http_state *hs)
500 {
501 err_t err;
502 LWIP_DEBUGF(HTTPD_DEBUG, ("Closing connection %p\n", (void*)pcb));
503
504 #if LWIP_HTTPD_SUPPORT_POST
505 if (hs != NULL) {
506 if ((hs->post_content_len_left != 0)
507 #if LWIP_HTTPD_POST_MANUAL_WND
508 || ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0))
509 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
510 ) {
511 /* make sure the post code knows that the connection is closed */
512 http_post_response_filename[0] = 0;
513 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
514 }
515 }
516 #endif /* LWIP_HTTPD_SUPPORT_POST*/
517
518
519 tcp_arg(pcb, NULL);
520 tcp_recv(pcb, NULL);
521 tcp_err(pcb, NULL);
522 tcp_poll(pcb, NULL, 0);
523 tcp_sent(pcb, NULL);
524 if(hs != NULL) {
525 http_state_free(hs);
526 }
527
528 err = tcp_close(pcb);
529 if (err != ERR_OK) {
530 LWIP_DEBUGF(HTTPD_DEBUG, ("Error %d closing %p\n", err, (void*)pcb));
531 /* error closing, try again later in poll */
532 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
533 }
534 return err;
535 }
536 #if LWIP_HTTPD_CGI
537
546 static int
547 extract_uri_parameters(struct http_state *hs, char *params)
548 {
549 char *pair;
550 char *equals;
551 int loop;
552
553 /* If we have no parameters at all, return immediately. */
554 if(!params || (params[0] == '\0')) {
555 return(0);
556 }
557
558 /* Get a pointer to our first parameter */
559 pair = params;
560
561 /* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the
562 * remainder (if any) */
563 for(loop = 0; (loop < LWIP_HTTPD_MAX_CGI_PARAMETERS) && pair; loop++) {
564
565 /* Save the name of the parameter */
566 hs->params[loop] = pair;
567
568 /* Remember the start of this name=value pair */
569 equals = pair;
570
571 /* Find the start of the next name=value pair and replace the delimiter
572 * with a 0 to terminate the previous pair string. */
573 pair = strchr(pair, '&');
574 if(pair) {
575 *pair = '\0';
576 pair++;
577 } else {
578 /* We didn't find a new parameter so find the end of the URI and
579 * replace the space with a '\0' */
580 pair = strchr(equals, ' ');
581 if(pair) {
582 *pair = '\0';
583 }
584
585 /* Revert to NULL so that we exit the loop as expected. */
586 pair = NULL;
587 }
588
589 /* Now find the '=' in the previous pair, replace it with '\0' and save
590 * the parameter value string. */
591 equals = strchr(equals, '=');
592 if(equals) {
593 *equals = '\0';
594 hs->param_vals[loop] = equals + 1;
595 } else {
596 hs->param_vals[loop] = NULL;
597 }
598 }
599
600 return loop;
601 }
602 #endif /* LWIP_HTTPD_CGI */
603
604 #if LWIP_HTTPD_SSI
605
615 static void
616 get_tag_insert(struct http_state *hs)
617 {
618 int loop;
619 size_t len;
620 #if LWIP_HTTPD_SSI_MULTIPART
621 u16_t current_tag_part = hs->tag_part;
622 hs->tag_part = HTTPD_LAST_TAG_PART;
623 #endif /* LWIP_HTTPD_SSI_MULTIPART */
624
625 if(g_pfnSSIHandler && g_ppcTags && g_iNumTags) {
626
627 /* Find this tag in the list we have been provided. */
628 for(loop = 0; loop < g_iNumTags; loop++) {
629 if(strcmp(hs->tag_name, g_ppcTags[loop]) == 0) {
630 hs->tag_insert_len = g_pfnSSIHandler(loop, hs->tag_insert,
631 LWIP_HTTPD_MAX_TAG_INSERT_LEN
632 #if LWIP_HTTPD_SSI_MULTIPART
633 , current_tag_part, &hs->tag_part
634 #endif /* LWIP_HTTPD_SSI_MULTIPART */
635 #if LWIP_HTTPD_FILE_STATE
636 , hs->handle->state
637 #endif /* LWIP_HTTPD_FILE_STATE */
638 );
639 return;
640 }
641 }
642 }
643
644 /* If we drop out, we were asked to serve a page which contains tags that
645 * we don't have a handler for. Merely echo back the tags with an error
646 * marker. */
647 #define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG "
648 #define UNKNOWN_TAG1_LEN 18
649 #define UNKNOWN_TAG2_TEXT "***</b>"
650 #define UNKNOWN_TAG2_LEN 7
651 len = LWIP_MIN(strlen(hs->tag_name),
652 LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN));
653 MEMCPY(hs->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN);
654 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN], hs->tag_name, len);
655 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN);
656 hs->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0;
657
658 len = strlen(hs->tag_insert);
659 LWIP_ASSERT("len <= 0xffff", len <= 0xffff);
660 hs->tag_insert_len = (u16_t)len;
661 }
662 #endif /* LWIP_HTTPD_SSI */
663
664 #if LWIP_HTTPD_DYNAMIC_HEADERS
665
669 static void
670 get_http_headers(struct http_state *pState, char *pszURI)
671 {
672 unsigned int iLoop;
673 char *pszWork;
674 char *pszExt;
675 char *pszVars;
676
677 /* Ensure that we initialize the loop counter. */
678 iLoop = 0;
679
680 /* In all cases, the second header we send is the server identification
681 so set it here. */
682 pState->hdrs[1] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER];
683
684 /* Is this a normal file or the special case we use to send back the
685 default "404: Page not found" response? */
686 if (pszURI == NULL) {
687 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
688 pState->hdrs[2] = g_psHTTPHeaderStrings[DEFAULT_404_HTML];
689
690 /* Set up to send the first header string. */
691 pState->hdr_index = 0;
692 pState->hdr_pos = 0;
693 return;
694 } else {
695 /* We are dealing with a particular filename. Look for one other
696 special case. We assume that any filename with "404" in it must be
697 indicative of a 404 server error whereas all other files require
698 the 200 OK header. */
699 if (strstr(pszURI, "404")) {
700 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
701 } else if (strstr(pszURI, "400")) {
702 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
703 } else if (strstr(pszURI, "501")) {
704 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
705 } else {
706 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_OK];
707 }
708
709 /* Determine if the URI has any variables and, if so, temporarily remove
710 them. */
711 pszVars = strchr(pszURI, '?');
712 if(pszVars) {
713 *pszVars = '\0';
714 }
715
716 /* Get a pointer to the file extension. We find this by looking for the
717 last occurrence of "." in the filename passed. */
718 pszExt = NULL;
719 pszWork = strchr(pszURI, '.');
720 while(pszWork) {
721 pszExt = pszWork + 1;
722 pszWork = strchr(pszExt, '.');
723 }
724
725 /* Now determine the content type and add the relevant header for that. */
726 for(iLoop = 0; (iLoop < NUM_HTTP_HEADERS) && pszExt; iLoop++) {
727 /* Have we found a matching extension? */
728 if(!strcmp(g_psHTTPHeaders[iLoop].extension, pszExt)) {
729 pState->hdrs[2] =
730 g_psHTTPHeaderStrings[g_psHTTPHeaders[iLoop].headerIndex];
731 break;
732 }
733 }
734
735 /* Reinstate the parameter marker if there was one in the original URI. */
736 if(pszVars) {
737 *pszVars = '?';
738 }
739 }
740
741 /* Does the URL passed have any file extension? If not, we assume it
742 is a special-case URL used for control state notification and we do
743 not send any HTTP headers with the response. */
744 if(!pszExt) {
745 /* Force the header index to a value indicating that all headers
746 have already been sent. */
747 pState->hdr_index = NUM_FILE_HDR_STRINGS;
748 } else {
749 /* Did we find a matching extension? */
750 if(iLoop == NUM_HTTP_HEADERS) {
751 /* No - use the default, plain text file type. */
752 pState->hdrs[2] = g_psHTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE];
753 }
754
755 /* Set up to send the first header string. */
756 pState->hdr_index = 0;
757 pState->hdr_pos = 0;
758 }
759 }
760 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
761
768 static u8_t
769 http_send_data(struct tcp_pcb *pcb, struct http_state *hs)
770 {
771 err_t err;
772 u16_t len;
773 u16_t mss;
774 u8_t data_to_send = false;
775 #if LWIP_HTTPD_DYNAMIC_HEADERS
776 u16_t hdrlen, sendlen;
777 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
778
779 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_send_data: pcb=%p hs=%p left=%d\n", (void*)pcb,
780 (void*)hs, hs != NULL ? hs->left : 0));
781
782 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
783 if (hs->unrecved_bytes != 0) {
784 return 0;
785 }
786 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
787
788 #if LWIP_HTTPD_DYNAMIC_HEADERS
789 /* If we were passed a NULL state structure pointer, ignore the call. */
790 if (hs == NULL) {
791 return 0;
792 }
793
794 /* Assume no error until we find otherwise */
795 err = ERR_OK;
796
797 /* Do we have any more header data to send for this file? */
798 if(hs->hdr_index < NUM_FILE_HDR_STRINGS) {
799 /* How much data can we send? */
800 len = tcp_sndbuf(pcb);
801 sendlen = len;
802
803 while(len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) {
804 const void *ptr;
805 u16_t old_sendlen;
806 /* How much do we have to send from the current header? */
807 hdrlen = (u16_t)strlen(hs->hdrs[hs->hdr_index]);
808
809 /* How much of this can we send? */
810 sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos);
811
812 /* Send this amount of data or as much as we can given memory
813 * constraints. */
814 ptr = (const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos);
815 old_sendlen = sendlen;
816 err = http_write(pcb, ptr, &sendlen, HTTP_IS_HDR_VOLATILE(hs, ptr));
817 if ((err == ERR_OK) && (old_sendlen != sendlen)) {
818 /* Remember that we added some more data to be transmitted. */
819 data_to_send = true;
820 } else if (err != ERR_OK) {
821 /* special case: http_write does not try to send 1 byte */
822 sendlen = 0;
823 }
824
825 /* Fix up the header position for the next time round. */
826 hs->hdr_pos += sendlen;
827 len -= sendlen;
828
829 /* Have we finished sending this string? */
830 if(hs->hdr_pos == hdrlen) {
831 /* Yes - move on to the next one */
832 hs->hdr_index++;
833 hs->hdr_pos = 0;
834 }
835 }
836
837 /* If we get here and there are still header bytes to send, we send
838 * the header information we just wrote immediately. If there are no
839 * more headers to send, but we do have file data to send, drop through
840 * to try to send some file data too. */
841 if((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) {
842 LWIP_DEBUGF(HTTPD_DEBUG, ("tcp_output1\n"));
843 return 1;
844 }
845 }
846 #else /* LWIP_HTTPD_DYNAMIC_HEADERS */
847 /* Assume no error until we find otherwise */
848 err = ERR_OK;
849 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
850
851 /* Have we run out of file data to send? If so, we need to read the next
852 * block from the file. */
853 if (hs->left == 0) {
854 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
855 int count;
856 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
857
858 /* Do we have a valid file handle? */
859 if (hs->handle == NULL) {
860 /* No - close the connection. */
861 http_close_conn(pcb, hs);
862 return 0;
863 }
864 if (fs_bytes_left(hs->handle) <= 0) {
865 /* We reached the end of the file so this request is done.
866 * @todo: don't close here for HTTP/1.1? */
867 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
868 http_close_conn(pcb, hs);
869 return 0;
870 }
871 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
872 /* Do we already have a send buffer allocated? */
873 if(hs->buf) {
874 /* Yes - get the length of the buffer */
875 count = hs->buf_len;
876 } else {
877 /* We don't have a send buffer so allocate one up to 2mss bytes long. */
878 count = 2 * tcp_mss(pcb);
879 do {
880 hs->buf = (char*)mem_malloc((mem_size_t)count);
881 if (hs->buf != NULL) {
882 hs->buf_len = count;
883 break;
884 }
885 count = count / 2;
886 } while (count > 100);
887
888 /* Did we get a send buffer? If not, return immediately. */
889 if (hs->buf == NULL) {
890 LWIP_DEBUGF(HTTPD_DEBUG, ("No buff\n"));
891 return 0;
892 }
893 }
894
895 /* Read a block of data from the file. */
896 LWIP_DEBUGF(HTTPD_DEBUG, ("Trying to read %d bytes.\n", count));
897
898 count = fs_read(hs->handle, hs->buf, count);
899 if(count < 0) {
900 /* We reached the end of the file so this request is done.
901 * @todo: don't close here for HTTP/1.1? */
902 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
903 http_close_conn(pcb, hs);
904 return 1;
905 }
906
907 /* Set up to send the block of data we just read */
908 LWIP_DEBUGF(HTTPD_DEBUG, ("Read %d bytes.\n", count));
909 hs->left = count;
910 hs->file = hs->buf;
911 #if LWIP_HTTPD_SSI
912 hs->parse_left = count;
913 hs->parsed = hs->buf;
914 #endif /* LWIP_HTTPD_SSI */
915 #else /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
916 LWIP_ASSERT("SSI and DYNAMIC_HEADERS turned off but eof not reached", 0);
917 #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */
918 }
919
920 #if LWIP_HTTPD_SSI
921 if(!hs->tag_check) {
922 #endif /* LWIP_HTTPD_SSI */
923 /* We are not processing an SHTML file so no tag checking is necessary.
924 * Just send the data as we received it from the file. */
925
926 /* We cannot send more data than space available in the send
927 buffer. */
928 if (tcp_sndbuf(pcb) < hs->left) {
929 len = tcp_sndbuf(pcb);
930 } else {
931 len = (u16_t)hs->left;
932 LWIP_ASSERT("hs->left did not fit into u16_t!", (len == hs->left));
933 }
934 mss = tcp_mss(pcb);
935 if(len > (2 * mss)) {
936 len = 2 * mss;
937 }
938
939 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
940 if (err == ERR_OK) {
941 data_to_send = true;
942 hs->file += len;
943 hs->left -= len;
944 }
945 #if LWIP_HTTPD_SSI
946 } else {
947 /* We are processing an SHTML file so need to scan for tags and replace
948 * them with insert strings. We need to be careful here since a tag may
949 * straddle the boundary of two blocks read from the file and we may also
950 * have to split the insert string between two tcp_write operations. */
951
952 /* How much data could we send? */
953 len = tcp_sndbuf(pcb);
954
955 /* Do we have remaining data to send before parsing more? */
956 if(hs->parsed > hs->file) {
957 /* We cannot send more data than space available in the send
958 buffer. */
959 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
960 len = tcp_sndbuf(pcb);
961 } else {
962 LWIP_ASSERT("Data size does not fit into u16_t!",
963 (hs->parsed - hs->file) <= 0xffff);
964 len = (u16_t)(hs->parsed - hs->file);
965 }
966 mss = tcp_mss(pcb);
967 if(len > (2 * mss)) {
968 len = 2 * mss;
969 }
970
971 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
972 if (err == ERR_OK) {
973 data_to_send = true;
974 hs->file += len;
975 hs->left -= len;
976 }
977
978 /* If the send buffer is full, return now. */
979 if(tcp_sndbuf(pcb) == 0) {
980 return data_to_send;
981 }
982 }
983
984 LWIP_DEBUGF(HTTPD_DEBUG, ("State %d, %d left\n", hs->tag_state, hs->parse_left));
985
986 /* We have sent all the data that was already parsed so continue parsing
987 * the buffer contents looking for SSI tags. */
988 while((hs->parse_left) && (err == ERR_OK)) {
989 /* @todo: somewhere in this loop, 'len' should grow again... */
990 if (len == 0) {
991 return data_to_send;
992 }
993 switch(hs->tag_state) {
994 case TAG_NONE:
995 /* We are not currently processing an SSI tag so scan for the
996 * start of the lead-in marker. */
997 if(*hs->parsed == g_pcTagLeadIn[0]) {
998 /* We found what could be the lead-in for a new tag so change
999 * state appropriately. */
1000 hs->tag_state = TAG_LEADIN;
1001 hs->tag_index = 1;
1002 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1003 hs->tag_started = hs->parsed;
1004 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */
1005 }
1006
1007 /* Move on to the next character in the buffer */
1008 hs->parse_left--;
1009 hs->parsed++;
1010 break;
1011
1012 case TAG_LEADIN:
1013 /* We are processing the lead-in marker, looking for the start of
1014 * the tag name. */
1015
1016 /* Have we reached the end of the leadin? */
1017 if(hs->tag_index == LEN_TAG_LEAD_IN) {
1018 hs->tag_index = 0;
1019 hs->tag_state = TAG_FOUND;
1020 } else {
1021 /* Have we found the next character we expect for the tag leadin? */
1022 if(*hs->parsed == g_pcTagLeadIn[hs->tag_index]) {
1023 /* Yes - move to the next one unless we have found the complete
1024 * leadin, in which case we start looking for the tag itself */
1025 hs->tag_index++;
1026 } else {
1027 /* We found an unexpected character so this is not a tag. Move
1028 * back to idle state. */
1029 hs->tag_state = TAG_NONE;
1030 }
1031
1032 /* Move on to the next character in the buffer */
1033 hs->parse_left--;
1034 hs->parsed++;
1035 }
1036 break;
1037
1038 case TAG_FOUND:
1039 /* We are reading the tag name, looking for the start of the
1040 * lead-out marker and removing any whitespace found. */
1041
1042 /* Remove leading whitespace between the tag leading and the first
1043 * tag name character. */
1044 if((hs->tag_index == 0) && ((*hs->parsed == ' ') ||
1045 (*hs->parsed == '\t') || (*hs->parsed == '\n') ||
1046 (*hs->parsed == '\r'))) {
1047 /* Move on to the next character in the buffer */
1048 hs->parse_left--;
1049 hs->parsed++;
1050 break;
1051 }
1052
1053 /* Have we found the end of the tag name? This is signalled by
1054 * us finding the first leadout character or whitespace */
1055 if((*hs->parsed == g_pcTagLeadOut[0]) ||
1056 (*hs->parsed == ' ') || (*hs->parsed == '\t') ||
1057 (*hs->parsed == '\n') || (*hs->parsed == '\r')) {
1058
1059 if(hs->tag_index == 0) {
1060 /* We read a zero length tag so ignore it. */
1061 hs->tag_state = TAG_NONE;
1062 } else {
1063 /* We read a non-empty tag so go ahead and look for the
1064 * leadout string. */
1065 hs->tag_state = TAG_LEADOUT;
1066 LWIP_ASSERT("hs->tag_index <= 0xff", hs->tag_index <= 0xff);
1067 hs->tag_name_len = (u8_t)hs->tag_index;
1068 hs->tag_name[hs->tag_index] = '\0';
1069 if(*hs->parsed == g_pcTagLeadOut[0]) {
1070 hs->tag_index = 1;
1071 } else {
1072 hs->tag_index = 0;
1073 }
1074 }
1075 } else {
1076 /* This character is part of the tag name so save it */
1077 if(hs->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) {
1078 hs->tag_name[hs->tag_index++] = *hs->parsed;
1079 } else {
1080 /* The tag was too long so ignore it. */
1081 hs->tag_state = TAG_NONE;
1082 }
1083 }
1084
1085 /* Move on to the next character in the buffer */
1086 hs->parse_left--;
1087 hs->parsed++;
1088
1089 break;
1090
1091 /* We are looking for the end of the lead-out marker. */
1092 case TAG_LEADOUT:
1093 /* Remove leading whitespace between the tag leading and the first
1094 * tag leadout character. */
1095 if((hs->tag_index == 0) && ((*hs->parsed == ' ') ||
1096 (*hs->parsed == '\t') || (*hs->parsed == '\n') ||
1097 (*hs->parsed == '\r'))) {
1098 /* Move on to the next character in the buffer */
1099 hs->parse_left--;
1100 hs->parsed++;
1101 break;
1102 }
1103
1104 /* Have we found the next character we expect for the tag leadout? */
1105 if(*hs->parsed == g_pcTagLeadOut[hs->tag_index]) {
1106 /* Yes - move to the next one unless we have found the complete
1107 * leadout, in which case we need to call the client to process
1108 * the tag. */
1109
1110 /* Move on to the next character in the buffer */
1111 hs->parse_left--;
1112 hs->parsed++;
1113
1114 if(hs->tag_index == (LEN_TAG_LEAD_OUT - 1)) {
1115 /* Call the client to ask for the insert string for the
1116 * tag we just found. */
1117 #if LWIP_HTTPD_SSI_MULTIPART
1118 hs->tag_part = 0; /* start with tag part 0 */
1119 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1120 get_tag_insert(hs);
1121
1122 /* Next time through, we are going to be sending data
1123 * immediately, either the end of the block we start
1124 * sending here or the insert string. */
1125 hs->tag_index = 0;
1126 hs->tag_state = TAG_SENDING;
1127 hs->tag_end = hs->parsed;
1128 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1129 hs->parsed = hs->tag_started;
1130 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1131
1132 /* If there is any unsent data in the buffer prior to the
1133 * tag, we need to send it now. */
1134 if (hs->tag_end > hs->file) {
1135 /* How much of the data can we send? */
1136 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1137 if(len > hs->tag_end - hs->file) {
1138 len = (u16_t)(hs->tag_end - hs->file);
1139 }
1140 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1141 if(len > hs->tag_started - hs->file) {
1142 /* we would include the tag in sending */
1143 len = (u16_t)(hs->tag_started - hs->file);
1144 }
1145 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1146
1147 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1148 if (err == ERR_OK) {
1149 data_to_send = true;
1150 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1151 if(hs->tag_started <= hs->file) {
1152 /* pretend to have sent the tag, too */
1153 len += hs->tag_end - hs->tag_started;
1154 }
1155 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1156 hs->file += len;
1157 hs->left -= len;
1158 }
1159 }
1160 } else {
1161 hs->tag_index++;
1162 }
1163 } else {
1164 /* We found an unexpected character so this is not a tag. Move
1165 * back to idle state. */
1166 hs->parse_left--;
1167 hs->parsed++;
1168 hs->tag_state = TAG_NONE;
1169 }
1170 break;
1171
1172 /*
1173 * We have found a valid tag and are in the process of sending
1174 * data as a result of that discovery. We send either remaining data
1175 * from the file prior to the insert point or the insert string itself.
1176 */
1177 case TAG_SENDING:
1178 /* Do we have any remaining file data to send from the buffer prior
1179 * to the tag? */
1180 if(hs->tag_end > hs->file) {
1181 /* How much of the data can we send? */
1182 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1183 if(len > hs->tag_end - hs->file) {
1184 len = (u16_t)(hs->tag_end - hs->file);
1185 }
1186 #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1187 LWIP_ASSERT("hs->started >= hs->file", hs->tag_started >= hs->file);
1188 if (len > hs->tag_started - hs->file) {
1189 /* we would include the tag in sending */
1190 len = (u16_t)(hs->tag_started - hs->file);
1191 }
1192 #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/
1193 if (len != 0) {
1194 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1195 } else {
1196 err = ERR_OK;
1197 }
1198 if (err == ERR_OK) {
1199 data_to_send = true;
1200 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1201 if(hs->tag_started <= hs->file) {
1202 /* pretend to have sent the tag, too */
1203 len += hs->tag_end - hs->tag_started;
1204 }
1205 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1206 hs->file += len;
1207 hs->left -= len;
1208 }
1209 } else {
1210 #if LWIP_HTTPD_SSI_MULTIPART
1211 if(hs->tag_index >= hs->tag_insert_len) {
1212 /* Did the last SSIHandler have more to send? */
1213 if (hs->tag_part != HTTPD_LAST_TAG_PART) {
1214 /* If so, call it again */
1215 hs->tag_index = 0;
1216 get_tag_insert(hs);
1217 }
1218 }
1219 #endif /* LWIP_HTTPD_SSI_MULTIPART */
1220
1221 /* Do we still have insert data left to send? */
1222 if(hs->tag_index < hs->tag_insert_len) {
1223 /* We are sending the insert string itself. How much of the
1224 * insert can we send? */
1225 if(len > (hs->tag_insert_len - hs->tag_index)) {
1226 len = (hs->tag_insert_len - hs->tag_index);
1227 }
1228
1229 /* Note that we set the copy flag here since we only have a
1230 * single tag insert buffer per connection. If we don't do
1231 * this, insert corruption can occur if more than one insert
1232 * is processed before we call tcp_output. */
1233 err = http_write(pcb, &(hs->tag_insert[hs->tag_index]), &len,
1234 HTTP_IS_TAG_VOLATILE(hs));
1235 if (err == ERR_OK) {
1236 data_to_send = true;
1237 hs->tag_index += len;
1238 /* Don't return here: keep on sending data */
1239 }
1240 } else {
1241 /* We have sent all the insert data so go back to looking for
1242 * a new tag. */
1243 LWIP_DEBUGF(HTTPD_DEBUG, ("Everything sent.\n"));
1244 hs->tag_index = 0;
1245 hs->tag_state = TAG_NONE;
1246 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1247 hs->parsed = hs->tag_end;
1248 #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/
1249 }
1250 break;
1251 }
1252 }
1253 }
1254
1255 /* If we drop out of the end of the for loop, this implies we must have
1256 * file data to send so send it now. In TAG_SENDING state, we've already
1257 * handled this so skip the send if that's the case. */
1258 if((hs->tag_state != TAG_SENDING) && (hs->parsed > hs->file)) {
1259 /* We cannot send more data than space available in the send
1260 buffer. */
1261 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
1262 len = tcp_sndbuf(pcb);
1263 } else {
1264 LWIP_ASSERT("Data size does not fit into u16_t!",
1265 (hs->parsed - hs->file) <= 0xffff);
1266 len = (u16_t)(hs->parsed - hs->file);
1267 }
1268 if(len > (2 * tcp_mss(pcb))) {
1269 len = 2 * tcp_mss(pcb);
1270 }
1271
1272 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1273 if (err == ERR_OK) {
1274 data_to_send = true;
1275 hs->file += len;
1276 hs->left -= len;
1277 }
1278 }
1279 }
1280 #endif /* LWIP_HTTPD_SSI */
1281
1282 if((hs->left == 0) && (fs_bytes_left(hs->handle) <= 0)) {
1283 /* We reached the end of the file so this request is done.
1284 * This adds the FIN flag right into the last data segment.
1285 * @todo: don't close here for HTTP/1.1? */
1286 LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
1287 http_close_conn(pcb, hs);
1288 return 0;
1289 }
1290 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("send_data end.\n"));
1291 return data_to_send;
1292 }
1293
1294 #if LWIP_HTTPD_SUPPORT_EXTSTATUS
1295
1302 static err_t
1303 http_find_error_file(struct http_state *hs, u16_t error_nr)
1304 {
1305 const char *uri1, *uri2, *uri3;
1306 struct fs_file *file;
1307
1308 if (error_nr == 501) {
1309 uri1 = "/501.html";
1310 uri2 = "/501.htm";
1311 uri3 = "/501.shtml";
1312 } else {
1313 /* 400 (bad request is the default) */
1314 uri1 = "/400.html";
1315 uri2 = "/400.htm";
1316 uri3 = "/400.shtml";
1317 }
1318 file = fs_open(uri1);
1319 if (file == NULL) {
1320 file = fs_open(uri2);
1321 if (file == NULL) {
1322 file = fs_open(uri3);
1323 if (file == NULL) {
1324 LWIP_DEBUGF(HTTPD_DEBUG, ("Error page for error %"U16_F" not found\n",
1325 error_nr));
1326 return ERR_ARG;
1327 }
1328 }
1329 }
1330 return http_init_file(hs, file, 0, NULL);
1331 }
1332 #else /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1333 #define http_find_error_file(hs, error_nr) ERR_ARG
1334 #endif /* LWIP_HTTPD_SUPPORT_EXTSTATUS */
1335
1343 static struct fs_file *
1344 http_get_404_file(const char **uri)
1345 {
1346 struct fs_file *file;
1347
1348 *uri = "/404.html";
1349 file = fs_open(*uri);
1350 if(file == NULL) {
1351 /* 404.html doesn't exist. Try 404.htm instead. */
1352 *uri = "/404.htm";
1353 file = fs_open(*uri);
1354 if(file == NULL) {
1355 /* 404.htm doesn't exist either. Try 404.shtml instead. */
1356 *uri = "/404.shtml";
1357 file = fs_open(*uri);
1358 if(file == NULL) {
1359 /* 404.htm doesn't exist either. Indicate to the caller that it should
1360 * send back a default 404 page.
1361 */
1362 *uri = NULL;
1363 }
1364 }
1365 }
1366
1367 return file;
1368 }
1369
1370 #if LWIP_HTTPD_SUPPORT_POST
1371 static err_t
1372 http_handle_post_finished(struct http_state *hs)
1373 {
1374 /* application error or POST finished */
1375 /* NULL-terminate the buffer */
1376 http_post_response_filename[0] = 0;
1377 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
1378 return http_find_file(hs, http_post_response_filename, 0);
1379 }
1380
1390 static err_t
1391 http_post_rxpbuf(struct http_state *hs, struct pbuf *p)
1392 {
1393 err_t err;
1394
1395 /* adjust remaining Content-Length */
1396 if (hs->post_content_len_left < p->tot_len) {
1397 hs->post_content_len_left = 0;
1398 } else {
1399 hs->post_content_len_left -= p->tot_len;
1400 }
1401 err = httpd_post_receive_data(hs, p);
1402 if ((err != ERR_OK) || (hs->post_content_len_left == 0)) {
1403 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
1404 if (hs->unrecved_bytes != 0) {
1405 return ERR_OK;
1406 }
1407 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
1408 /* application error or POST finished */
1409 return http_handle_post_finished(hs);
1410 }
1411
1412 return ERR_OK;
1413 }
1414
1430 static err_t
1431 http_post_request(struct tcp_pcb *pcb, struct pbuf **inp, struct http_state *hs,
1432 char *data, u16_t data_len, char *uri, char *uri_end)
1433 {
1434 err_t err;
1435 /* search for end-of-header (first double-CRLF) */
1436 char* crlfcrlf = strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data));
1437
1438 #if LWIP_HTTPD_POST_MANUAL_WND
1439 hs->pcb = pcb;
1440 #else /* LWIP_HTTPD_POST_MANUAL_WND */
1441 LWIP_UNUSED_ARG(pcb); /* only used for LWIP_HTTPD_POST_MANUAL_WND */
1442 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1443
1444 if (crlfcrlf != NULL) {
1445 /* search for "Content-Length: " */
1446 #define HTTP_HDR_CONTENT_LEN "Content-Length: "
1447 #define HTTP_HDR_CONTENT_LEN_LEN 16
1448 #define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10
1449 char *scontent_len = strnstr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1));
1450 if (scontent_len != NULL) {
1451 char *scontent_len_end = strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN);
1452 if (scontent_len_end != NULL) {
1453 int content_len;
1454 char *conten_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN;
1455 *scontent_len_end = 0;
1456 content_len = atoi(conten_len_num);
1457 if (content_len > 0) {
1458 /* adjust length of HTTP header passed to application */
1459 const char *hdr_start_after_uri = uri_end + 1;
1460 u16_t hdr_len = LWIP_MIN(data_len, crlfcrlf + 4 - data);
1461 u16_t hdr_data_len = LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri);
1462 u8_t post_auto_wnd = 1;
1463 http_post_response_filename[0] = 0;
1464 err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len,
1465 http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN, &post_auto_wnd);
1466 if (err == ERR_OK) {
1467 /* try to pass in data of the first pbuf(s) */
1468 struct pbuf *q = *inp;
1469 u16_t start_offset = hdr_len;
1470 #if LWIP_HTTPD_POST_MANUAL_WND
1471 hs->no_auto_wnd = !post_auto_wnd;
1472 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1473 /* set the Content-Length to be received for this POST */
1474 hs->post_content_len_left = (u32_t)content_len;
1475
1476 /* get to the pbuf where the body starts */
1477 while((q != NULL) && (q->len <= start_offset)) {
1478 struct pbuf *head = q;
1479 start_offset -= q->len;
1480 q = q->next;
1481 /* free the head pbuf */
1482 head->next = NULL;
1483 pbuf_free(head);
1484 }
1485 *inp = NULL;
1486 if (q != NULL) {
1487 /* hide the remaining HTTP header */
1488 pbuf_header(q, -(s16_t)start_offset);
1489 #if LWIP_HTTPD_POST_MANUAL_WND
1490 if (!post_auto_wnd) {
1491 /* already tcp_recved() this data... */
1492 hs->unrecved_bytes = q->tot_len;
1493 }
1494 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1495 return http_post_rxpbuf(hs, q);
1496 } else {
1497 return ERR_OK;
1498 }
1499 } else {
1500 /* return file passed from application */
1501 return http_find_file(hs, http_post_response_filename, 0);
1502 }
1503 } else {
1504 LWIP_DEBUGF(HTTPD_DEBUG, ("POST received invalid Content-Length: %s\n",
1505 conten_len_num));
1506 return ERR_ARG;
1507 }
1508 }
1509 }
1510 }
1511 /* if we come here, the POST is incomplete */
1512 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1513 return ERR_INPROGRESS;
1514 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1515 return ERR_ARG;
1516 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1517 }
1518
1519 #if LWIP_HTTPD_POST_MANUAL_WND
1520
1528 void httpd_post_data_recved(void *connection, u16_t recved_len)
1529 {
1530 struct http_state *hs = (struct http_state*)connection;
1531 if (hs != NULL) {
1532 if (hs->no_auto_wnd) {
1533 u16_t len = recved_len;
1534 if (hs->unrecved_bytes >= recved_len) {
1535 hs->unrecved_bytes -= recved_len;
1536 } else {
1537 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_LEVEL_WARNING, ("httpd_post_data_recved: recved_len too big\n"));
1538 len = (u16_t)hs->unrecved_bytes;
1539 hs->unrecved_bytes = 0;
1540 }
1541 if (hs->pcb != NULL) {
1542 if (len != 0) {
1543 tcp_recved(hs->pcb, len);
1544 }
1545 if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) {
1546 /* finished handling POST */
1547 http_handle_post_finished(hs);
1548 http_send_data(hs->pcb, hs);
1549 }
1550 }
1551 }
1552 }
1553 }
1554 #endif /* LWIP_HTTPD_POST_MANUAL_WND */
1555
1556 #endif /* LWIP_HTTPD_SUPPORT_POST */
1557
1569 static err_t
1570 http_parse_request(struct pbuf **inp, struct http_state *hs, struct tcp_pcb *pcb)
1571 {
1572 char *data;
1573 char *crlf;
1574 u16_t data_len;
1575 struct pbuf *p = *inp;
1576 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1577 u16_t clen;
1578 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1579 #if LWIP_HTTPD_SUPPORT_POST
1580 err_t err;
1581 #endif /* LWIP_HTTPD_SUPPORT_POST */
1582
1583 LWIP_UNUSED_ARG(pcb); /* only used for post */
1584 LWIP_ASSERT("p != NULL", p != NULL);
1585 LWIP_ASSERT("hs != NULL", hs != NULL);
1586
1587 if ((hs->handle != NULL) || (hs->file != NULL)) {
1588 LWIP_DEBUGF(HTTPD_DEBUG, ("Received data while sending a file\n"));
1589 /* already sending a file */
1590 /* @todo: abort? */
1591 return ERR_USE;
1592 }
1593
1594 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1595
1596 LWIP_DEBUGF(HTTPD_DEBUG, ("Received %"U16_F" bytes\n", p->tot_len));
1597
1598 /* first check allowed characters in this pbuf? */
1599
1600 /* enqueue the pbuf */
1601 if (hs->req == NULL) {
1602 LWIP_DEBUGF(HTTPD_DEBUG, ("First pbuf\n"));
1603 hs->req = p;
1604 } else {
1605 LWIP_DEBUGF(HTTPD_DEBUG, ("pbuf enqueued\n"));
1606 pbuf_cat(hs->req, p);
1607 }
1608
1609 if (hs->req->next != NULL) {
1610 data_len = LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH);
1611 pbuf_copy_partial(hs->req, httpd_req_buf, data_len, 0);
1612 data = httpd_req_buf;
1613 } else
1614 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1615 {
1616 data = (char *)p->payload;
1617 data_len = p->len;
1618 if (p->len != p->tot_len) {
1619 LWIP_DEBUGF(HTTPD_DEBUG, ("Warning: incomplete header due to chained pbufs\n"));
1620 }
1621 }
1622
1623 /* received enough data for minimal request? */
1624 if (data_len >= MIN_REQ_LEN) {
1625 /* wait for CRLF before parsing anything */
1626 crlf = strnstr(data, CRLF, data_len);
1627 if (crlf != NULL) {
1628 #if LWIP_HTTPD_SUPPORT_POST
1629 int is_post = 0;
1630 #endif /* LWIP_HTTPD_SUPPORT_POST */
1631 int is_09 = 0;
1632 char *sp1, *sp2;
1633 u16_t left_len, uri_len;
1634 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("CRLF received, parsing request\n"));
1635 /* parse method */
1636 if (!strncmp(data, "GET ", 4)) {
1637 sp1 = data + 3;
1638 /* received GET request */
1639 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received GET request\"\n"));
1640 #if LWIP_HTTPD_SUPPORT_POST
1641 } else if (!strncmp(data, "POST ", 5)) {
1642 /* store request type */
1643 is_post = 1;
1644 sp1 = data + 4;
1645 /* received GET request */
1646 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received POST request\n"));
1647 #endif /* LWIP_HTTPD_SUPPORT_POST */
1648 } else {
1649 /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1650 data[4] = 0;
1651 /* unsupported method! */
1652 LWIP_DEBUGF(HTTPD_DEBUG, ("Unsupported request method (not implemented): \"%s\"\n",
1653 data));
1654 return http_find_error_file(hs, 501);
1655 }
1656 /* if we come here, method is OK, parse URI */
1657 left_len = data_len - ((sp1 +1) - data);
1658 sp2 = strnstr(sp1 + 1, " ", left_len);
1659 #if LWIP_HTTPD_SUPPORT_V09
1660 if (sp2 == NULL) {
1661 /* HTTP 0.9: respond with correct protocol version */
1662 sp2 = strnstr(sp1 + 1, CRLF, left_len);
1663 is_09 = 1;
1664 #if LWIP_HTTPD_SUPPORT_POST
1665 if (is_post) {
1666 /* HTTP/0.9 does not support POST */
1667 goto badrequest;
1668 }
1669 #endif /* LWIP_HTTPD_SUPPORT_POST */
1670 }
1671 #endif /* LWIP_HTTPD_SUPPORT_V09 */
1672 uri_len = sp2 - (sp1 + 1);
1673 if ((sp2 != 0) && (sp2 > sp1)) {
1674 char *uri = sp1 + 1;
1675 /* null-terminate the METHOD (pbuf is freed anyway wen returning) */
1676 *sp1 = 0;
1677 uri[uri_len] = 0;
1678 LWIP_DEBUGF(HTTPD_DEBUG, ("Received \"%s\" request for URI: \"%s\"\n",
1679 data, uri));
1680 #if LWIP_HTTPD_SUPPORT_POST
1681 if (is_post) {
1682 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1683 struct pbuf **q = &hs->req;
1684 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1685 struct pbuf **q = inp;
1686 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1687 err = http_post_request(pcb, q, hs, data, data_len, uri, sp2);
1688 if (err != ERR_OK) {
1689 /* restore header for next try */
1690 *sp1 = ' ';
1691 *sp2 = ' ';
1692 uri[uri_len] = ' ';
1693 }
1694 if (err == ERR_ARG) {
1695 goto badrequest;
1696 }
1697 return err;
1698 } else
1699 #endif /* LWIP_HTTPD_SUPPORT_POST */
1700 {
1701 return http_find_file(hs, uri, is_09);
1702 }
1703 } else {
1704 LWIP_DEBUGF(HTTPD_DEBUG, ("invalid URI\n"));
1705 }
1706 }
1707 }
1708
1709 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1710 clen = pbuf_clen(hs->req);
1711 if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) &&
1712 (clen <= LWIP_HTTPD_REQ_QUEUELEN)) {
1713 /* request not fully received (too short or CRLF is missing) */
1714 return ERR_INPROGRESS;
1715 } else
1716 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
1717 {
1718 #if LWIP_HTTPD_SUPPORT_POST
1719 badrequest:
1720 #endif /* LWIP_HTTPD_SUPPORT_POST */
1721 LWIP_DEBUGF(HTTPD_DEBUG, ("bad request\n"));
1722 /* could not parse request */
1723 return http_find_error_file(hs, 400);
1724 }
1725 }
1726
1736 static err_t
1737 http_find_file(struct http_state *hs, const char *uri, int is_09)
1738 {
1739 size_t loop;
1740 struct fs_file *file = NULL;
1741 char *params;
1742 #if LWIP_HTTPD_CGI
1743 int i;
1744 int count;
1745 #endif /* LWIP_HTTPD_CGI */
1746
1747 #if LWIP_HTTPD_SSI
1748 /*
1749 * By default, assume we will not be processing server-side-includes
1750 * tags
1751 */
1752 hs->tag_check = false;
1753 #endif /* LWIP_HTTPD_SSI */
1754
1755 /* Have we been asked for the default root file? */
1756 if((uri[0] == '/') && (uri[1] == 0)) {
1757 /* Try each of the configured default filenames until we find one
1758 that exists. */
1759 for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
1760 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Looking for %s...\n", g_psDefaultFilenames[loop].name));
1761 file = fs_open((char *)g_psDefaultFilenames[loop].name);
1762 uri = (char *)g_psDefaultFilenames[loop].name;
1763 if(file != NULL) {
1764 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opened.\n"));
1765 #if LWIP_HTTPD_SSI
1766 hs->tag_check = g_psDefaultFilenames[loop].shtml;
1767 #endif /* LWIP_HTTPD_SSI */
1768 break;
1769 }
1770 }
1771 if (file == NULL) {
1772 extern struct fs_file *fs_open_default(void);
1773 file = fs_open_default();
1774 if (file == NULL)
1775 /* None of the default filenames exist so send back a 404 page */
1776 file = http_get_404_file(&uri);
1777 #if LWIP_HTTPD_SSI
1778 hs->tag_check = false;
1779 #endif /* LWIP_HTTPD_SSI */
1780 }
1781 } else {
1782 /* No - we've been asked for a specific file. */
1783 /* First, isolate the base URI (without any parameters) */
1784 params = (char *)strchr(uri, '?');
1785 if (params != NULL) {
1786 /* URI contains parameters. NULL-terminate the base URI */
1787 *params = '\0';
1788 params++;
1789 }
1790
1791 #if LWIP_HTTPD_CGI
1792 /* Does the base URI we have isolated correspond to a CGI handler? */
1793 if (g_iNumCGIs && g_pCGIs) {
1794 for (i = 0; i < g_iNumCGIs; i++) {
1795 if (strcmp(uri, g_pCGIs[i].pcCGIName) == 0) {
1796 /*
1797 * We found a CGI that handles this URI so extract the
1798 * parameters and call the handler.
1799 */
1800 count = extract_uri_parameters(hs, params);
1801 uri = g_pCGIs[i].pfnCGIHandler(i, count, hs->params,
1802 hs->param_vals);
1803 break;
1804 }
1805 }
1806 }
1807 #endif /* LWIP_HTTPD_CGI */
1808
1809 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opening %s\n", uri));
1810
1811 file = fs_open(uri);
1812 if (file == NULL) {
1813 file = http_get_404_file(&uri);
1814 }
1815 #if LWIP_HTTPD_SSI
1816 if (file != NULL) {
1817 /*
1818 * See if we have been asked for an shtml file and, if so,
1819 * enable tag checking.
1820 */
1821 hs->tag_check = false;
1822 for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
1823 if (strstr(uri, g_pcSSIExtensions[loop])) {
1824 hs->tag_check = true;
1825 break;
1826 }
1827 }
1828 }
1829 #endif /* LWIP_HTTPD_SSI */
1830 }
1831 return http_init_file(hs, file, is_09, uri);
1832 }
1833
1844 static err_t
1845 http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri)
1846 {
1847 if (file != NULL) {
1848 /* file opened, initialise struct http_state */
1849 #if LWIP_HTTPD_SSI
1850 hs->tag_index = 0;
1851 hs->tag_state = TAG_NONE;
1852 hs->parsed = file->data;
1853 hs->parse_left = file->len;
1854 hs->tag_end = file->data;
1855 #endif /* LWIP_HTTPD_SSI */
1856 hs->handle = file;
1857 hs->file = (char*)file->data;
1858 LWIP_ASSERT("File length must be positive!", (file->len >= 0));
1859 hs->left = file->index;
1860 hs->retries = 0;
1861 #if LWIP_HTTPD_TIMING
1862 hs->time_started = sys_now();
1863 #endif /* LWIP_HTTPD_TIMING */
1864 #if !LWIP_HTTPD_DYNAMIC_HEADERS
1865 LWIP_ASSERT("HTTP headers not included in file system", hs->handle->http_header_included);
1866 #endif /* !LWIP_HTTPD_DYNAMIC_HEADERS */
1867 #if LWIP_HTTPD_SUPPORT_V09
1868 if (hs->handle->http_header_included && is_09) {
1869 /* HTTP/0.9 responses are sent without HTTP header,
1870 search for the end of the header. */
1871 char *file_start = strnstr(hs->file, CRLF CRLF, hs->left);
1872 if (file_start != NULL) {
1873 size_t diff = file_start + 4 - hs->file;
1874 hs->file += diff;
1875 hs->left -= (u32_t)diff;
1876 }
1877 }
1878 #endif /* LWIP_HTTPD_SUPPORT_V09*/
1879 } else {
1880 hs->handle = NULL;
1881 hs->file = NULL;
1882 hs->left = 0;
1883 hs->retries = 0;
1884 }
1885 #if LWIP_HTTPD_DYNAMIC_HEADERS
1886 /* Determine the HTTP headers to send based on the file extension of
1887 * the requested URI. */
1888 if ((hs->handle == NULL) || !hs->handle->http_header_included) {
1889 get_http_headers(hs, (char*)uri);
1890 }
1891 #else /* LWIP_HTTPD_DYNAMIC_HEADERS */
1892 LWIP_UNUSED_ARG(uri);
1893 #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */
1894 return ERR_OK;
1895 }
1896
1901 static void
1902 http_err(void *arg, err_t err)
1903 {
1904 struct http_state *hs = (struct http_state *)arg;
1905 LWIP_UNUSED_ARG(err);
1906
1907 LWIP_DEBUGF(HTTPD_DEBUG, ("http_err: %s", lwip_strerr(err)));
1908
1909 if (hs != NULL) {
1910 http_state_free(hs);
1911 }
1912 }
1913
1918 static err_t
1919 http_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
1920 {
1921 struct http_state *hs = (struct http_state *)arg;
1922
1923 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_sent %p\n", (void*)pcb));
1924
1925 LWIP_UNUSED_ARG(len);
1926
1927 if (hs == NULL) {
1928 return ERR_OK;
1929 }
1930
1931 hs->retries = 0;
1932
1933 http_send_data(pcb, hs);
1934
1935 return ERR_OK;
1936 }
1937
1945 static err_t
1946 http_poll(void *arg, struct tcp_pcb *pcb)
1947 {
1948 struct http_state *hs = (struct http_state *)arg;
1949 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n",
1950 (void*)pcb, (void*)hs, tcp_debug_state_str(pcb->state)));
1951
1952 if (hs == NULL) {
1953 err_t closed;
1954 /* arg is null, close. */
1955 LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n"));
1956 closed = http_close_conn(pcb, hs);
1957 LWIP_UNUSED_ARG(closed);
1958 #if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
1959 if (closed == ERR_MEM) {
1960 tcp_abort(pcb);
1961 return ERR_ABRT;
1962 }
1963 #endif /* LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR */
1964 return ERR_OK;
1965 } else {
1966 hs->retries++;
1967 if (hs->retries == HTTPD_MAX_RETRIES) {
1968 LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n"));
1969 http_close_conn(pcb, hs);
1970 return ERR_OK;
1971 }
1972
1973 /* If this connection has a file open, try to send some more data. If
1974 * it has not yet received a GET request, don't do this since it will
1975 * cause the connection to close immediately. */
1976 if(hs && (hs->handle)) {
1977 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n"));
1978 if(http_send_data(pcb, hs)) {
1979 /* If we wrote anything to be sent, go ahead and send it now. */
1980 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output2\n"));
1981 tcp_output(pcb);
1982 }
1983 }
1984 }
1985
1986 return ERR_OK;
1987 }
1988
1993 static err_t
1994 http_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
1995 {
1996 err_t parsed = ERR_ABRT;
1997 struct http_state *hs = (struct http_state *)arg;
1998 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: pcb=%p pbuf=%p err=%s\n", (void*)pcb,
1999 (void*)p, lwip_strerr(err)));
2000
2001 if ((err != ERR_OK) || (p == NULL) || (hs == NULL)) {
2002 /* error or closed by other side? */
2003 if (p != NULL) {
2004 /* Inform TCP that we have taken the data. */
2005 tcp_recved(pcb, p->tot_len);
2006 pbuf_free(p);
2007 }
2008 if (hs == NULL) {
2009 /* this should not happen, only to be robust */
2010 LWIP_DEBUGF(HTTPD_DEBUG, ("Error, http_recv: hs is NULL, close\n"));
2011 }
2012 http_close_conn(pcb, hs);
2013 return ERR_OK;
2014 }
2015
2016 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
2017 if (hs->no_auto_wnd) {
2018 hs->unrecved_bytes += p->tot_len;
2019 } else
2020 #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */
2021 {
2022 /* Inform TCP that we have taken the data. */
2023 tcp_recved(pcb, p->tot_len);
2024 }
2025
2026 #if LWIP_HTTPD_SUPPORT_POST
2027 if (hs->post_content_len_left > 0) {
2028 /* reset idle counter when POST data is received */
2029 hs->retries = 0;
2030 /* this is data for a POST, pass the complete pbuf to the application */
2031 http_post_rxpbuf(hs, p);
2032 /* pbuf is passed to the application, don't free it! */
2033 if (hs->post_content_len_left == 0) {
2034 /* all data received, send response or close connection */
2035 http_send_data(pcb, hs);
2036 }
2037 return ERR_OK;
2038 } else
2039 #endif /* LWIP_HTTPD_SUPPORT_POST */
2040 {
2041 if (hs->handle == NULL) {
2042 parsed = http_parse_request(&p, hs, pcb);
2043 LWIP_ASSERT("http_parse_request: unexpected return value", parsed == ERR_OK
2044 || parsed == ERR_INPROGRESS ||parsed == ERR_ARG || parsed == ERR_USE);
2045 } else {
2046 LWIP_DEBUGF(HTTPD_DEBUG, ("http_recv: already sending data\n"));
2047 }
2048 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
2049 if (parsed != ERR_INPROGRESS) {
2050 /* request fully parsed or error */
2051 if (hs->req != NULL) {
2052 pbuf_free(hs->req);
2053 hs->req = NULL;
2054 }
2055 }
2056 #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
2057 if (p != NULL) {
2058 /* pbuf not passed to application, free it now */
2059 pbuf_free(p);
2060 }
2061 #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */
2062 if (parsed == ERR_OK) {
2063 #if LWIP_HTTPD_SUPPORT_POST
2064 if (hs->post_content_len_left == 0)
2065 #endif /* LWIP_HTTPD_SUPPORT_POST */
2066 {
2067 LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: data %p len %"S32_F"\n", hs->file, hs->left));
2068 http_send_data(pcb, hs);
2069 }
2070 } else if (parsed == ERR_ARG) {
2071 /* @todo: close on ERR_USE? */
2072 http_close_conn(pcb, hs);
2073 }
2074 }
2075 return ERR_OK;
2076 }
2077
2078
2082 static err_t
2083 http_accept(void *arg, struct tcp_pcb *pcb, err_t err)
2084 {
2085 struct http_state *hs;
2086 struct tcp_pcb_listen *lpcb = (struct tcp_pcb_listen*)arg;
2087
2088 LWIP_UNUSED_ARG(err);
2089 LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept %p / %p\n", (void*)pcb, arg));
2090
2091 /* Decrease the listen backlog counter */
2092 tcp_accepted(lpcb);
2093 /* Set priority */
2094 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2095
2096 /* Allocate memory for the structure that holds the state of the
2097 connection - initialized by that function. */
2098 hs = http_state_alloc();
2099 if (hs == NULL) {
2100 LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n"));
2101 return ERR_MEM;
2102 }
2103
2104 /* Tell TCP that this is the structure we wish to be passed for our
2105 callbacks. */
2106 tcp_arg(pcb, hs);
2107
2108 /* Set up the various callback functions */
2109 tcp_recv(pcb, http_recv);
2110 tcp_err(pcb, http_err);
2111 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
2112 tcp_sent(pcb, http_sent);
2113
2114 return ERR_OK;
2115 }
2116
2120 static void
2121 httpd_init_addr(ip_addr_t *local_addr)
2122 {
2123 struct tcp_pcb *pcb;
2124 err_t err;
2125
2126 pcb = tcp_new();
2127 LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL);
2128 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2129 /* set SOF_REUSEADDR here to explicitly bind httpd to multiple interfaces */
2130 err = tcp_bind(pcb, local_addr, HTTPD_SERVER_PORT);
2131 LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK);
2132 pcb = tcp_listen(pcb);
2133 LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL);
2134 /* initialize callback arg and accept callback */
2135 tcp_arg(pcb, pcb);
2136 tcp_accept(pcb, http_accept);
2137 }
2138
2142 void
2143 httpd_init(void)
2144 {
2145 #if HTTPD_USE_MEM_POOL
2146 LWIP_ASSERT("memp_sizes[MEMP_HTTPD_STATE] >= sizeof(http_state)",
2147 memp_sizes[MEMP_HTTPD_STATE] >= sizeof(http_state));
2148 #endif
2149 LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n"));
2150 httpd_init_addr(IP_ADDR_ANY);
2151 }
2152
2153 #if LWIP_HTTPD_SSI
2154
2161 void
2162 http_set_ssi_handler(tSSIHandler ssi_handler, const char **tags, int num_tags)
2163 {
2164 LWIP_DEBUGF(HTTPD_DEBUG, ("http_set_ssi_handler\n"));
2165
2166 LWIP_ASSERT("no ssi_handler given", ssi_handler != NULL);
2167 LWIP_ASSERT("no tags given", tags != NULL);
2168 LWIP_ASSERT("invalid number of tags", num_tags > 0);
2169
2170 g_pfnSSIHandler = ssi_handler;
2171 g_ppcTags = tags;
2172 g_iNumTags = num_tags;
2173 }
2174 #endif /* LWIP_HTTPD_SSI */
2175
2176 #if LWIP_HTTPD_CGI
2177
2183 void
2184 http_set_cgi_handlers(const tCGI *cgis, int num_handlers)
2185 {
2186 LWIP_ASSERT("no cgis given", cgis != NULL);
2187 LWIP_ASSERT("invalid number of handlers", num_handlers > 0);
2188
2189 g_pCGIs = cgis;
2190 g_iNumCGIs = num_handlers;
2191 }
2192 #endif /* LWIP_HTTPD_CGI */
2193
2194 #endif /* LWIP_TCP */