BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file |
||
3 | * LWIP HTTP server implementation |
||
4 | */ |
||
5 | |||
6 | /* |
||
7 | * Copyright (c) 2001-2003 Swedish Institute of Computer Science. |
||
8 | * All rights reserved. |
||
9 | * |
||
10 | * Redistribution and use in source and binary forms, with or without modification, |
||
11 | * are permitted provided that the following conditions are met: |
||
12 | * |
||
13 | * 1. Redistributions of source code must retain the above copyright notice, |
||
14 | * this list of conditions and the following disclaimer. |
||
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
||
16 | * this list of conditions and the following disclaimer in the documentation |
||
17 | * and/or other materials provided with the distribution. |
||
18 | * 3. The name of the author may not be used to endorse or promote products |
||
19 | * derived from this software without specific prior written permission. |
||
20 | * |
||
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
||
22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
||
23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
||
24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
||
26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
||
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
||
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
||
29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
||
30 | * OF SUCH DAMAGE. |
||
31 | * |
||
32 | * This file is part of the lwIP TCP/IP stack. |
||
33 | * |
||
34 | * Author: Adam Dunkels <adam@sics.se> |
||
35 | * Simon Goldschmidt |
||
36 | * |
||
37 | */ |
||
38 | |||
39 | /** |
||
40 | * @defgroup httpd HTTP server |
||
41 | * @ingroup apps |
||
42 | * |
||
43 | * This httpd supports for a |
||
44 | * rudimentary server-side-include facility which will replace tags of the form |
||
45 | * <!--#tag--> in any file whose extension is .shtml, .shtm or .ssi with |
||
46 | * strings provided by an include handler whose pointer is provided to the |
||
47 | * module via function http_set_ssi_handler(). |
||
48 | * Additionally, a simple common |
||
49 | * gateway interface (CGI) handling mechanism has been added to allow clients |
||
50 | * to hook functions to particular request URIs. |
||
51 | * |
||
52 | * To enable SSI support, define label LWIP_HTTPD_SSI in lwipopts.h. |
||
53 | * To enable CGI support, define label LWIP_HTTPD_CGI in lwipopts.h. |
||
54 | * |
||
55 | * By default, the server assumes that HTTP headers are already present in |
||
56 | * each file stored in the file system. By defining LWIP_HTTPD_DYNAMIC_HEADERS in |
||
57 | * lwipopts.h, this behavior can be changed such that the server inserts the |
||
58 | * headers automatically based on the extension of the file being served. If |
||
59 | * this mode is used, be careful to ensure that the file system image used |
||
60 | * does not already contain the header information. |
||
61 | * |
||
62 | * File system images without headers can be created using the makefsfile |
||
63 | * tool with the -h command line option. |
||
64 | * |
||
65 | * |
||
66 | * Notes about valid SSI tags |
||
67 | * -------------------------- |
||
68 | * |
||
69 | * The following assumptions are made about tags used in SSI markers: |
||
70 | * |
||
71 | * 1. No tag may contain '-' or whitespace characters within the tag name. |
||
72 | * 2. Whitespace is allowed between the tag leadin "<!--#" and the start of |
||
73 | * the tag name and between the tag name and the leadout string "-->". |
||
74 | * 3. The maximum tag name length is LWIP_HTTPD_MAX_TAG_NAME_LEN, currently 8 characters. |
||
75 | * |
||
76 | * Notes on CGI usage |
||
77 | * ------------------ |
||
78 | * |
||
79 | * The simple CGI support offered here works with GET method requests only |
||
80 | * and can handle up to 16 parameters encoded into the URI. The handler |
||
81 | * function may not write directly to the HTTP output but must return a |
||
82 | * filename that the HTTP server will send to the browser as a response to |
||
83 | * the incoming CGI request. |
||
84 | * |
||
85 | * |
||
86 | * |
||
87 | * The list of supported file types is quite short, so if makefsdata complains |
||
88 | * about an unknown extension, make sure to add it (and its doctype) to |
||
89 | * the 'g_psHTTPHeaders' list. |
||
90 | */ |
||
91 | #include "lwip/init.h" |
||
92 | #include "lwip/apps/httpd.h" |
||
93 | #include "lwip/debug.h" |
||
94 | #include "lwip/stats.h" |
||
95 | #include "lwip/apps/fs.h" |
||
96 | #include "httpd_structs.h" |
||
97 | #include "lwip/def.h" |
||
98 | |||
99 | #include "lwip/altcp.h" |
||
100 | #include "lwip/altcp_tcp.h" |
||
101 | #if HTTPD_ENABLE_HTTPS |
||
102 | #include "lwip/altcp_tls.h" |
||
103 | #endif |
||
104 | #ifdef LWIP_HOOK_FILENAME |
||
105 | #include LWIP_HOOK_FILENAME |
||
106 | #endif |
||
107 | |||
108 | #include <string.h> /* memset */ |
||
109 | #include <stdlib.h> /* atoi */ |
||
110 | #include <stdio.h> |
||
111 | |||
112 | #if LWIP_TCP && LWIP_CALLBACK_API |
||
113 | |||
114 | /** Minimum length for a valid HTTP/0.9 request: "GET /\r\n" -> 7 bytes */ |
||
115 | #define MIN_REQ_LEN 7 |
||
116 | |||
117 | #define CRLF "\r\n" |
||
118 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
119 | #define HTTP11_CONNECTIONKEEPALIVE "Connection: keep-alive" |
||
120 | #define HTTP11_CONNECTIONKEEPALIVE2 "Connection: Keep-Alive" |
||
121 | #endif |
||
122 | |||
123 | #if LWIP_HTTPD_DYNAMIC_FILE_READ |
||
124 | #define HTTP_IS_DYNAMIC_FILE(hs) ((hs)->buf != NULL) |
||
125 | #else |
||
126 | #define HTTP_IS_DYNAMIC_FILE(hs) 0 |
||
127 | #endif |
||
128 | |||
129 | /* This defines checks whether tcp_write has to copy data or not */ |
||
130 | |||
131 | #ifndef HTTP_IS_DATA_VOLATILE |
||
132 | /** tcp_write does not have to copy data when sent from rom-file-system directly */ |
||
133 | #define HTTP_IS_DATA_VOLATILE(hs) (HTTP_IS_DYNAMIC_FILE(hs) ? TCP_WRITE_FLAG_COPY : 0) |
||
134 | #endif |
||
135 | /** Default: dynamic headers are sent from ROM (non-dynamic headers are handled like file data) */ |
||
136 | #ifndef HTTP_IS_HDR_VOLATILE |
||
137 | #define HTTP_IS_HDR_VOLATILE(hs, ptr) 0 |
||
138 | #endif |
||
139 | |||
140 | /* Return values for http_send_*() */ |
||
141 | #define HTTP_DATA_TO_SEND_BREAK 2 |
||
142 | #define HTTP_DATA_TO_SEND_CONTINUE 1 |
||
143 | #define HTTP_NO_DATA_TO_SEND 0 |
||
144 | |||
145 | typedef struct { |
||
146 | const char *name; |
||
147 | u8_t shtml; |
||
148 | } default_filename; |
||
149 | |||
150 | const default_filename g_psDefaultFilenames[] = { |
||
151 | {"/index.shtml", 1 }, |
||
152 | {"/index.ssi", 1 }, |
||
153 | {"/index.shtm", 1 }, |
||
154 | {"/index.html", 0 }, |
||
155 | {"/index.htm", 0 } |
||
156 | }; |
||
157 | |||
158 | #define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / \ |
||
159 | sizeof(default_filename)) |
||
160 | |||
161 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
162 | /** HTTP request is copied here from pbufs for simple parsing */ |
||
163 | static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH + 1]; |
||
164 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
165 | |||
166 | #if LWIP_HTTPD_SUPPORT_POST |
||
167 | #if LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN > LWIP_HTTPD_MAX_REQUEST_URI_LEN |
||
168 | #define LWIP_HTTPD_URI_BUF_LEN LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN |
||
169 | #endif |
||
170 | #endif |
||
171 | #ifndef LWIP_HTTPD_URI_BUF_LEN |
||
172 | #define LWIP_HTTPD_URI_BUF_LEN LWIP_HTTPD_MAX_REQUEST_URI_LEN |
||
173 | #endif |
||
174 | #if LWIP_HTTPD_URI_BUF_LEN |
||
175 | /* Filename for response file to send when POST is finished or |
||
176 | * search for default files when a directory is requested. */ |
||
177 | static char http_uri_buf[LWIP_HTTPD_URI_BUF_LEN + 1]; |
||
178 | #endif |
||
179 | |||
180 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
181 | /* The number of individual strings that comprise the headers sent before each |
||
182 | * requested file. |
||
183 | */ |
||
184 | #define NUM_FILE_HDR_STRINGS 5 |
||
185 | #define HDR_STRINGS_IDX_HTTP_STATUS 0 /* e.g. "HTTP/1.0 200 OK\r\n" */ |
||
186 | #define HDR_STRINGS_IDX_SERVER_NAME 1 /* e.g. "Server: "HTTPD_SERVER_AGENT"\r\n" */ |
||
187 | #define HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE 2 /* e.g. "Content-Length: xy\r\n" and/or "Connection: keep-alive\r\n" */ |
||
188 | #define HDR_STRINGS_IDX_CONTENT_LEN_NR 3 /* the byte count, when content-length is used */ |
||
189 | #define HDR_STRINGS_IDX_CONTENT_TYPE 4 /* the content type (or default answer content type including default document) */ |
||
190 | |||
191 | /* The dynamically generated Content-Length buffer needs space for CRLF + NULL */ |
||
192 | #define LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET 3 |
||
193 | #ifndef LWIP_HTTPD_MAX_CONTENT_LEN_SIZE |
||
194 | /* The dynamically generated Content-Length buffer shall be able to work with |
||
195 | ~953 MB (9 digits) */ |
||
196 | #define LWIP_HTTPD_MAX_CONTENT_LEN_SIZE (9 + LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET) |
||
197 | #endif |
||
198 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
199 | |||
200 | #if LWIP_HTTPD_SSI |
||
201 | |||
202 | #define HTTPD_LAST_TAG_PART 0xFFFF |
||
203 | |||
204 | enum tag_check_state { |
||
205 | TAG_NONE, /* Not processing an SSI tag */ |
||
206 | TAG_LEADIN, /* Tag lead in "<!--#" being processed */ |
||
207 | TAG_FOUND, /* Tag name being read, looking for lead-out start */ |
||
208 | TAG_LEADOUT, /* Tag lead out "-->" being processed */ |
||
209 | TAG_SENDING /* Sending tag replacement string */ |
||
210 | }; |
||
211 | |||
212 | struct http_ssi_state { |
||
213 | const char *parsed; /* Pointer to the first unparsed byte in buf. */ |
||
214 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
215 | const char *tag_started;/* Pointer to the first opening '<' of the tag. */ |
||
216 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */ |
||
217 | const char *tag_end; /* Pointer to char after the closing '>' of the tag. */ |
||
218 | u32_t parse_left; /* Number of unparsed bytes in buf. */ |
||
219 | u16_t tag_index; /* Counter used by tag parsing state machine */ |
||
220 | u16_t tag_insert_len; /* Length of insert in string tag_insert */ |
||
221 | #if LWIP_HTTPD_SSI_MULTIPART |
||
222 | u16_t tag_part; /* Counter passed to and changed by tag insertion function to insert multiple times */ |
||
223 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
224 | u8_t tag_name_len; /* Length of the tag name in string tag_name */ |
||
225 | char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1]; /* Last tag name extracted */ |
||
226 | char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1]; /* Insert string for tag_name */ |
||
227 | enum tag_check_state tag_state; /* State of the tag processor */ |
||
228 | }; |
||
229 | #endif /* LWIP_HTTPD_SSI */ |
||
230 | |||
231 | struct http_state { |
||
232 | #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED |
||
233 | struct http_state *next; |
||
234 | #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */ |
||
235 | struct fs_file file_handle; |
||
236 | struct fs_file *handle; |
||
237 | const char *file; /* Pointer to first unsent byte in buf. */ |
||
238 | |||
239 | struct altcp_pcb *pcb; |
||
240 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
241 | struct pbuf *req; |
||
242 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
243 | |||
244 | #if LWIP_HTTPD_DYNAMIC_FILE_READ |
||
245 | char *buf; /* File read buffer. */ |
||
246 | int buf_len; /* Size of file read buffer, buf. */ |
||
247 | #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */ |
||
248 | u32_t left; /* Number of unsent bytes in buf. */ |
||
249 | u8_t retries; |
||
250 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
251 | u8_t keepalive; |
||
252 | #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
253 | #if LWIP_HTTPD_SSI |
||
254 | struct http_ssi_state *ssi; |
||
255 | #endif /* LWIP_HTTPD_SSI */ |
||
256 | #if LWIP_HTTPD_CGI |
||
257 | char *params[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Params extracted from the request URI */ |
||
258 | char *param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Values for each extracted param */ |
||
259 | #endif /* LWIP_HTTPD_CGI */ |
||
260 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
261 | const char *hdrs[NUM_FILE_HDR_STRINGS]; /* HTTP headers to be sent. */ |
||
262 | char hdr_content_len[LWIP_HTTPD_MAX_CONTENT_LEN_SIZE]; |
||
263 | u16_t hdr_pos; /* The position of the first unsent header byte in the |
||
264 | current string */ |
||
265 | u16_t hdr_index; /* The index of the hdr string currently being sent. */ |
||
266 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
267 | #if LWIP_HTTPD_TIMING |
||
268 | u32_t time_started; |
||
269 | #endif /* LWIP_HTTPD_TIMING */ |
||
270 | #if LWIP_HTTPD_SUPPORT_POST |
||
271 | u32_t post_content_len_left; |
||
272 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
273 | u32_t unrecved_bytes; |
||
274 | u8_t no_auto_wnd; |
||
275 | u8_t post_finished; |
||
276 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
277 | #endif /* LWIP_HTTPD_SUPPORT_POST*/ |
||
278 | }; |
||
279 | |||
280 | #if HTTPD_USE_MEM_POOL |
||
281 | LWIP_MEMPOOL_DECLARE(HTTPD_STATE, MEMP_NUM_PARALLEL_HTTPD_CONNS, sizeof(struct http_state), "HTTPD_STATE") |
||
282 | #if LWIP_HTTPD_SSI |
||
283 | LWIP_MEMPOOL_DECLARE(HTTPD_SSI_STATE, MEMP_NUM_PARALLEL_HTTPD_SSI_CONNS, sizeof(struct http_ssi_state), "HTTPD_SSI_STATE") |
||
284 | #define HTTP_FREE_SSI_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_SSI_STATE, (x)) |
||
285 | #define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)LWIP_MEMPOOL_ALLOC(HTTPD_SSI_STATE) |
||
286 | #endif /* LWIP_HTTPD_SSI */ |
||
287 | #define HTTP_ALLOC_HTTP_STATE() (struct http_state *)LWIP_MEMPOOL_ALLOC(HTTPD_STATE) |
||
288 | #define HTTP_FREE_HTTP_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_STATE, (x)) |
||
289 | #else /* HTTPD_USE_MEM_POOL */ |
||
290 | #define HTTP_ALLOC_HTTP_STATE() (struct http_state *)mem_malloc(sizeof(struct http_state)) |
||
291 | #define HTTP_FREE_HTTP_STATE(x) mem_free(x) |
||
292 | #if LWIP_HTTPD_SSI |
||
293 | #define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)mem_malloc(sizeof(struct http_ssi_state)) |
||
294 | #define HTTP_FREE_SSI_STATE(x) mem_free(x) |
||
295 | #endif /* LWIP_HTTPD_SSI */ |
||
296 | #endif /* HTTPD_USE_MEM_POOL */ |
||
297 | |||
298 | static err_t http_close_conn(struct altcp_pcb *pcb, struct http_state *hs); |
||
299 | static err_t http_close_or_abort_conn(struct altcp_pcb *pcb, struct http_state *hs, u8_t abort_conn); |
||
300 | static err_t http_find_file(struct http_state *hs, const char *uri, int is_09); |
||
301 | static err_t http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri, u8_t tag_check, char *params); |
||
302 | static err_t http_poll(void *arg, struct altcp_pcb *pcb); |
||
303 | static u8_t http_check_eof(struct altcp_pcb *pcb, struct http_state *hs); |
||
304 | #if LWIP_HTTPD_FS_ASYNC_READ |
||
305 | static void http_continue(void *connection); |
||
306 | #endif /* LWIP_HTTPD_FS_ASYNC_READ */ |
||
307 | |||
308 | #if LWIP_HTTPD_SSI |
||
309 | /* SSI insert handler function pointer. */ |
||
310 | tSSIHandler g_pfnSSIHandler; |
||
311 | #if !LWIP_HTTPD_SSI_RAW |
||
312 | int g_iNumTags; |
||
313 | const char **g_ppcTags; |
||
314 | #endif /* !LWIP_HTTPD_SSI_RAW */ |
||
315 | |||
316 | #define LEN_TAG_LEAD_IN 5 |
||
317 | const char *const g_pcTagLeadIn = "<!--#"; |
||
318 | |||
319 | #define LEN_TAG_LEAD_OUT 3 |
||
320 | const char *const g_pcTagLeadOut = "-->"; |
||
321 | #endif /* LWIP_HTTPD_SSI */ |
||
322 | |||
323 | #if LWIP_HTTPD_CGI |
||
324 | /* CGI handler information */ |
||
325 | const tCGI *g_pCGIs; |
||
326 | int g_iNumCGIs; |
||
327 | int http_cgi_paramcount; |
||
328 | #define http_cgi_params hs->params |
||
329 | #define http_cgi_param_vals hs->param_vals |
||
330 | #elif LWIP_HTTPD_CGI_SSI |
||
331 | char *http_cgi_params[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Params extracted from the request URI */ |
||
332 | char *http_cgi_param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS]; /* Values for each extracted param */ |
||
333 | #endif /* LWIP_HTTPD_CGI */ |
||
334 | |||
335 | #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED |
||
336 | /** global list of active HTTP connections, use to kill the oldest when |
||
337 | running out of memory */ |
||
338 | static struct http_state *http_connections; |
||
339 | |||
340 | static void |
||
341 | http_add_connection(struct http_state *hs) |
||
342 | { |
||
343 | /* add the connection to the list */ |
||
344 | hs->next = http_connections; |
||
345 | http_connections = hs; |
||
346 | } |
||
347 | |||
348 | static void |
||
349 | http_remove_connection(struct http_state *hs) |
||
350 | { |
||
351 | /* take the connection off the list */ |
||
352 | if (http_connections) { |
||
353 | if (http_connections == hs) { |
||
354 | http_connections = hs->next; |
||
355 | } else { |
||
356 | struct http_state *last; |
||
357 | for (last = http_connections; last->next != NULL; last = last->next) { |
||
358 | if (last->next == hs) { |
||
359 | last->next = hs->next; |
||
360 | break; |
||
361 | } |
||
362 | } |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 | |||
367 | static void |
||
368 | http_kill_oldest_connection(u8_t ssi_required) |
||
369 | { |
||
370 | struct http_state *hs = http_connections; |
||
371 | struct http_state *hs_free_next = NULL; |
||
372 | while (hs && hs->next) { |
||
373 | #if LWIP_HTTPD_SSI |
||
374 | if (ssi_required) { |
||
375 | if (hs->next->ssi != NULL) { |
||
376 | hs_free_next = hs; |
||
377 | } |
||
378 | } else |
||
379 | #else /* LWIP_HTTPD_SSI */ |
||
380 | LWIP_UNUSED_ARG(ssi_required); |
||
381 | #endif /* LWIP_HTTPD_SSI */ |
||
382 | { |
||
383 | hs_free_next = hs; |
||
384 | } |
||
385 | LWIP_ASSERT("broken list", hs != hs->next); |
||
386 | hs = hs->next; |
||
387 | } |
||
388 | if (hs_free_next != NULL) { |
||
389 | LWIP_ASSERT("hs_free_next->next != NULL", hs_free_next->next != NULL); |
||
390 | LWIP_ASSERT("hs_free_next->next->pcb != NULL", hs_free_next->next->pcb != NULL); |
||
391 | /* send RST when killing a connection because of memory shortage */ |
||
392 | http_close_or_abort_conn(hs_free_next->next->pcb, hs_free_next->next, 1); /* this also unlinks the http_state from the list */ |
||
393 | } |
||
394 | } |
||
395 | #else /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */ |
||
396 | |||
397 | #define http_add_connection(hs) |
||
398 | #define http_remove_connection(hs) |
||
399 | |||
400 | #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */ |
||
401 | |||
402 | #if LWIP_HTTPD_SSI |
||
403 | /** Allocate as struct http_ssi_state. */ |
||
404 | static struct http_ssi_state * |
||
405 | http_ssi_state_alloc(void) |
||
406 | { |
||
407 | struct http_ssi_state *ret = HTTP_ALLOC_SSI_STATE(); |
||
408 | #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED |
||
409 | if (ret == NULL) { |
||
410 | http_kill_oldest_connection(1); |
||
411 | ret = HTTP_ALLOC_SSI_STATE(); |
||
412 | } |
||
413 | #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */ |
||
414 | if (ret != NULL) { |
||
415 | memset(ret, 0, sizeof(struct http_ssi_state)); |
||
416 | } |
||
417 | return ret; |
||
418 | } |
||
419 | |||
420 | /** Free a struct http_ssi_state. */ |
||
421 | static void |
||
422 | http_ssi_state_free(struct http_ssi_state *ssi) |
||
423 | { |
||
424 | if (ssi != NULL) { |
||
425 | HTTP_FREE_SSI_STATE(ssi); |
||
426 | } |
||
427 | } |
||
428 | #endif /* LWIP_HTTPD_SSI */ |
||
429 | |||
430 | /** Initialize a struct http_state. |
||
431 | */ |
||
432 | static void |
||
433 | http_state_init(struct http_state *hs) |
||
434 | { |
||
435 | /* Initialize the structure. */ |
||
436 | memset(hs, 0, sizeof(struct http_state)); |
||
437 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
438 | /* Indicate that the headers are not yet valid */ |
||
439 | hs->hdr_index = NUM_FILE_HDR_STRINGS; |
||
440 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
441 | } |
||
442 | |||
443 | /** Allocate a struct http_state. */ |
||
444 | static struct http_state * |
||
445 | http_state_alloc(void) |
||
446 | { |
||
447 | struct http_state *ret = HTTP_ALLOC_HTTP_STATE(); |
||
448 | #if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED |
||
449 | if (ret == NULL) { |
||
450 | http_kill_oldest_connection(0); |
||
451 | ret = HTTP_ALLOC_HTTP_STATE(); |
||
452 | } |
||
453 | #endif /* LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED */ |
||
454 | if (ret != NULL) { |
||
455 | http_state_init(ret); |
||
456 | http_add_connection(ret); |
||
457 | } |
||
458 | return ret; |
||
459 | } |
||
460 | |||
461 | /** Free a struct http_state. |
||
462 | * Also frees the file data if dynamic. |
||
463 | */ |
||
464 | static void |
||
465 | http_state_eof(struct http_state *hs) |
||
466 | { |
||
467 | if (hs->handle) { |
||
468 | #if LWIP_HTTPD_TIMING |
||
469 | u32_t ms_needed = sys_now() - hs->time_started; |
||
470 | u32_t needed = LWIP_MAX(1, (ms_needed / 100)); |
||
471 | LWIP_DEBUGF(HTTPD_DEBUG_TIMING, ("httpd: needed %"U32_F" ms to send file of %d bytes -> %"U32_F" bytes/sec\n", |
||
472 | ms_needed, hs->handle->len, ((((u32_t)hs->handle->len) * 10) / needed))); |
||
473 | #endif /* LWIP_HTTPD_TIMING */ |
||
474 | fs_close(hs->handle); |
||
475 | hs->handle = NULL; |
||
476 | } |
||
477 | #if LWIP_HTTPD_DYNAMIC_FILE_READ |
||
478 | if (hs->buf != NULL) { |
||
479 | mem_free(hs->buf); |
||
480 | hs->buf = NULL; |
||
481 | } |
||
482 | #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */ |
||
483 | #if LWIP_HTTPD_SSI |
||
484 | if (hs->ssi) { |
||
485 | http_ssi_state_free(hs->ssi); |
||
486 | hs->ssi = NULL; |
||
487 | } |
||
488 | #endif /* LWIP_HTTPD_SSI */ |
||
489 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
490 | if (hs->req) { |
||
491 | pbuf_free(hs->req); |
||
492 | hs->req = NULL; |
||
493 | } |
||
494 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
495 | } |
||
496 | |||
497 | /** Free a struct http_state. |
||
498 | * Also frees the file data if dynamic. |
||
499 | */ |
||
500 | static void |
||
501 | http_state_free(struct http_state *hs) |
||
502 | { |
||
503 | if (hs != NULL) { |
||
504 | http_state_eof(hs); |
||
505 | http_remove_connection(hs); |
||
506 | HTTP_FREE_HTTP_STATE(hs); |
||
507 | } |
||
508 | } |
||
509 | |||
510 | /** Call tcp_write() in a loop trying smaller and smaller length |
||
511 | * |
||
512 | * @param pcb altcp_pcb to send |
||
513 | * @param ptr Data to send |
||
514 | * @param length Length of data to send (in/out: on return, contains the |
||
515 | * amount of data sent) |
||
516 | * @param apiflags directly passed to tcp_write |
||
517 | * @return the return value of tcp_write |
||
518 | */ |
||
519 | static err_t |
||
520 | http_write(struct altcp_pcb *pcb, const void *ptr, u16_t *length, u8_t apiflags) |
||
521 | { |
||
522 | u16_t len, max_len; |
||
523 | err_t err; |
||
524 | LWIP_ASSERT("length != NULL", length != NULL); |
||
525 | len = *length; |
||
526 | if (len == 0) { |
||
527 | return ERR_OK; |
||
528 | } |
||
529 | /* We cannot send more data than space available in the send buffer. */ |
||
530 | max_len = altcp_sndbuf(pcb); |
||
531 | if (max_len < len) { |
||
532 | len = max_len; |
||
533 | } |
||
534 | #ifdef HTTPD_MAX_WRITE_LEN |
||
535 | /* Additional limitation: e.g. don't enqueue more than 2*mss at once */ |
||
536 | max_len = HTTPD_MAX_WRITE_LEN(pcb); |
||
537 | if (len > max_len) { |
||
538 | len = max_len; |
||
539 | } |
||
540 | #endif /* HTTPD_MAX_WRITE_LEN */ |
||
541 | do { |
||
542 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Trying to send %d bytes\n", len)); |
||
543 | err = altcp_write(pcb, ptr, len, apiflags); |
||
544 | if (err == ERR_MEM) { |
||
545 | if ((altcp_sndbuf(pcb) == 0) || |
||
546 | (altcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)) { |
||
547 | /* no need to try smaller sizes */ |
||
548 | len = 1; |
||
549 | } else { |
||
550 | len /= 2; |
||
551 | } |
||
552 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, |
||
553 | ("Send failed, trying less (%d bytes)\n", len)); |
||
554 | } |
||
555 | } while ((err == ERR_MEM) && (len > 1)); |
||
556 | |||
557 | if (err == ERR_OK) { |
||
558 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Sent %d bytes\n", len)); |
||
559 | *length = len; |
||
560 | } else { |
||
561 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Send failed with err %d (\"%s\")\n", err, lwip_strerr(err))); |
||
562 | *length = 0; |
||
563 | } |
||
564 | |||
565 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
566 | /* ensure nagle is normally enabled (only disabled for persistent connections |
||
567 | when all data has been enqueued but the connection stays open for the next |
||
568 | request */ |
||
569 | altcp_nagle_enable(pcb); |
||
570 | #endif |
||
571 | |||
572 | return err; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * The connection shall be actively closed (using RST to close from fault states). |
||
577 | * Reset the sent- and recv-callbacks. |
||
578 | * |
||
579 | * @param pcb the tcp pcb to reset callbacks |
||
580 | * @param hs connection state to free |
||
581 | */ |
||
582 | static err_t |
||
583 | http_close_or_abort_conn(struct altcp_pcb *pcb, struct http_state *hs, u8_t abort_conn) |
||
584 | { |
||
585 | err_t err; |
||
586 | LWIP_DEBUGF(HTTPD_DEBUG, ("Closing connection %p\n", (void *)pcb)); |
||
587 | |||
588 | #if LWIP_HTTPD_SUPPORT_POST |
||
589 | if (hs != NULL) { |
||
590 | if ((hs->post_content_len_left != 0) |
||
591 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
592 | || ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0)) |
||
593 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
594 | ) { |
||
595 | /* make sure the post code knows that the connection is closed */ |
||
596 | http_uri_buf[0] = 0; |
||
597 | httpd_post_finished(hs, http_uri_buf, LWIP_HTTPD_URI_BUF_LEN); |
||
598 | } |
||
599 | } |
||
600 | #endif /* LWIP_HTTPD_SUPPORT_POST*/ |
||
601 | |||
602 | |||
603 | altcp_arg(pcb, NULL); |
||
604 | altcp_recv(pcb, NULL); |
||
605 | altcp_err(pcb, NULL); |
||
606 | altcp_poll(pcb, NULL, 0); |
||
607 | altcp_sent(pcb, NULL); |
||
608 | if (hs != NULL) { |
||
609 | http_state_free(hs); |
||
610 | } |
||
611 | |||
612 | if (abort_conn) { |
||
613 | altcp_abort(pcb); |
||
614 | return ERR_OK; |
||
615 | } |
||
616 | err = altcp_close(pcb); |
||
617 | if (err != ERR_OK) { |
||
618 | LWIP_DEBUGF(HTTPD_DEBUG, ("Error %d closing %p\n", err, (void *)pcb)); |
||
619 | /* error closing, try again later in poll */ |
||
620 | altcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL); |
||
621 | } |
||
622 | return err; |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * The connection shall be actively closed. |
||
627 | * Reset the sent- and recv-callbacks. |
||
628 | * |
||
629 | * @param pcb the tcp pcb to reset callbacks |
||
630 | * @param hs connection state to free |
||
631 | */ |
||
632 | static err_t |
||
633 | http_close_conn(struct altcp_pcb *pcb, struct http_state *hs) |
||
634 | { |
||
635 | return http_close_or_abort_conn(pcb, hs, 0); |
||
636 | } |
||
637 | |||
638 | /** End of file: either close the connection (Connection: close) or |
||
639 | * close the file (Connection: keep-alive) |
||
640 | */ |
||
641 | static void |
||
642 | http_eof(struct altcp_pcb *pcb, struct http_state *hs) |
||
643 | { |
||
644 | /* HTTP/1.1 persistent connection? (Not supported for SSI) */ |
||
645 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
646 | if (hs->keepalive) { |
||
647 | http_remove_connection(hs); |
||
648 | |||
649 | http_state_eof(hs); |
||
650 | http_state_init(hs); |
||
651 | /* restore state: */ |
||
652 | hs->pcb = pcb; |
||
653 | hs->keepalive = 1; |
||
654 | http_add_connection(hs); |
||
655 | /* ensure nagle doesn't interfere with sending all data as fast as possible: */ |
||
656 | altcp_nagle_disable(pcb); |
||
657 | } else |
||
658 | #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
659 | { |
||
660 | http_close_conn(pcb, hs); |
||
661 | } |
||
662 | } |
||
663 | |||
664 | #if LWIP_HTTPD_CGI || LWIP_HTTPD_CGI_SSI |
||
665 | /** |
||
666 | * Extract URI parameters from the parameter-part of an URI in the form |
||
667 | * "test.cgi?x=y" @todo: better explanation! |
||
668 | * Pointers to the parameters are stored in hs->param_vals. |
||
669 | * |
||
670 | * @param hs http connection state |
||
671 | * @param params pointer to the NULL-terminated parameter string from the URI |
||
672 | * @return number of parameters extracted |
||
673 | */ |
||
674 | static int |
||
675 | extract_uri_parameters(struct http_state *hs, char *params) |
||
676 | { |
||
677 | char *pair; |
||
678 | char *equals; |
||
679 | int loop; |
||
680 | |||
681 | LWIP_UNUSED_ARG(hs); |
||
682 | |||
683 | /* If we have no parameters at all, return immediately. */ |
||
684 | if (!params || (params[0] == '\0')) { |
||
685 | return (0); |
||
686 | } |
||
687 | |||
688 | /* Get a pointer to our first parameter */ |
||
689 | pair = params; |
||
690 | |||
691 | /* Parse up to LWIP_HTTPD_MAX_CGI_PARAMETERS from the passed string and ignore the |
||
692 | * remainder (if any) */ |
||
693 | for (loop = 0; (loop < LWIP_HTTPD_MAX_CGI_PARAMETERS) && pair; loop++) { |
||
694 | |||
695 | /* Save the name of the parameter */ |
||
696 | http_cgi_params[loop] = pair; |
||
697 | |||
698 | /* Remember the start of this name=value pair */ |
||
699 | equals = pair; |
||
700 | |||
701 | /* Find the start of the next name=value pair and replace the delimiter |
||
702 | * with a 0 to terminate the previous pair string. */ |
||
703 | pair = strchr(pair, '&'); |
||
704 | if (pair) { |
||
705 | *pair = '\0'; |
||
706 | pair++; |
||
707 | } else { |
||
708 | /* We didn't find a new parameter so find the end of the URI and |
||
709 | * replace the space with a '\0' */ |
||
710 | pair = strchr(equals, ' '); |
||
711 | if (pair) { |
||
712 | *pair = '\0'; |
||
713 | } |
||
714 | |||
715 | /* Revert to NULL so that we exit the loop as expected. */ |
||
716 | pair = NULL; |
||
717 | } |
||
718 | |||
719 | /* Now find the '=' in the previous pair, replace it with '\0' and save |
||
720 | * the parameter value string. */ |
||
721 | equals = strchr(equals, '='); |
||
722 | if (equals) { |
||
723 | *equals = '\0'; |
||
724 | http_cgi_param_vals[loop] = equals + 1; |
||
725 | } else { |
||
726 | http_cgi_param_vals[loop] = NULL; |
||
727 | } |
||
728 | } |
||
729 | |||
730 | return loop; |
||
731 | } |
||
732 | #endif /* LWIP_HTTPD_CGI || LWIP_HTTPD_CGI_SSI */ |
||
733 | |||
734 | #if LWIP_HTTPD_SSI |
||
735 | /** |
||
736 | * Insert a tag (found in an shtml in the form of "<!--#tagname-->" into the file. |
||
737 | * The tag's name is stored in ssi->tag_name (NULL-terminated), the replacement |
||
738 | * should be written to hs->tag_insert (up to a length of LWIP_HTTPD_MAX_TAG_INSERT_LEN). |
||
739 | * The amount of data written is stored to ssi->tag_insert_len. |
||
740 | * |
||
741 | * @todo: return tag_insert_len - maybe it can be removed from struct http_state? |
||
742 | * |
||
743 | * @param hs http connection state |
||
744 | */ |
||
745 | static void |
||
746 | get_tag_insert(struct http_state *hs) |
||
747 | { |
||
748 | #if LWIP_HTTPD_SSI_RAW |
||
749 | const char *tag; |
||
750 | #else /* LWIP_HTTPD_SSI_RAW */ |
||
751 | int tag; |
||
752 | #endif /* LWIP_HTTPD_SSI_RAW */ |
||
753 | size_t len; |
||
754 | struct http_ssi_state *ssi; |
||
755 | #if LWIP_HTTPD_SSI_MULTIPART |
||
756 | u16_t current_tag_part; |
||
757 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
758 | |||
759 | LWIP_ASSERT("hs != NULL", hs != NULL); |
||
760 | ssi = hs->ssi; |
||
761 | LWIP_ASSERT("ssi != NULL", ssi != NULL); |
||
762 | #if LWIP_HTTPD_SSI_MULTIPART |
||
763 | current_tag_part = ssi->tag_part; |
||
764 | ssi->tag_part = HTTPD_LAST_TAG_PART; |
||
765 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
766 | #if LWIP_HTTPD_SSI_RAW |
||
767 | tag = ssi->tag_name; |
||
768 | #endif |
||
769 | |||
770 | if (g_pfnSSIHandler |
||
771 | #if !LWIP_HTTPD_SSI_RAW |
||
772 | && g_ppcTags && g_iNumTags |
||
773 | #endif /* !LWIP_HTTPD_SSI_RAW */ |
||
774 | ) { |
||
775 | |||
776 | /* Find this tag in the list we have been provided. */ |
||
777 | #if LWIP_HTTPD_SSI_RAW |
||
778 | { |
||
779 | #else /* LWIP_HTTPD_SSI_RAW */ |
||
780 | for (tag = 0; tag < g_iNumTags; tag++) { |
||
781 | if (strcmp(ssi->tag_name, g_ppcTags[tag]) == 0) |
||
782 | #endif /* LWIP_HTTPD_SSI_RAW */ |
||
783 | { |
||
784 | ssi->tag_insert_len = g_pfnSSIHandler(tag, ssi->tag_insert, |
||
785 | LWIP_HTTPD_MAX_TAG_INSERT_LEN |
||
786 | #if LWIP_HTTPD_SSI_MULTIPART |
||
787 | , current_tag_part, &ssi->tag_part |
||
788 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
789 | #if LWIP_HTTPD_FILE_STATE |
||
790 | , (hs->handle ? hs->handle->state : NULL) |
||
791 | #endif /* LWIP_HTTPD_FILE_STATE */ |
||
792 | ); |
||
793 | #if LWIP_HTTPD_SSI_RAW |
||
794 | if (ssi->tag_insert_len != HTTPD_SSI_TAG_UNKNOWN) |
||
795 | #endif /* LWIP_HTTPD_SSI_RAW */ |
||
796 | { |
||
797 | return; |
||
798 | } |
||
799 | } |
||
800 | } |
||
801 | } |
||
802 | |||
803 | /* If we drop out, we were asked to serve a page which contains tags that |
||
804 | * we don't have a handler for. Merely echo back the tags with an error |
||
805 | * marker. */ |
||
806 | #define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG " |
||
807 | #define UNKNOWN_TAG1_LEN 18 |
||
808 | #define UNKNOWN_TAG2_TEXT "***</b>" |
||
809 | #define UNKNOWN_TAG2_LEN 7 |
||
810 | len = LWIP_MIN(sizeof(ssi->tag_name), LWIP_MIN(strlen(ssi->tag_name), |
||
811 | LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN))); |
||
812 | MEMCPY(ssi->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN); |
||
813 | MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN], ssi->tag_name, len); |
||
814 | MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN); |
||
815 | ssi->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0; |
||
816 | |||
817 | len = strlen(ssi->tag_insert); |
||
818 | LWIP_ASSERT("len <= 0xffff", len <= 0xffff); |
||
819 | ssi->tag_insert_len = (u16_t)len; |
||
820 | } |
||
821 | #endif /* LWIP_HTTPD_SSI */ |
||
822 | |||
823 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
824 | /** |
||
825 | * Generate the relevant HTTP headers for the given filename and write |
||
826 | * them into the supplied buffer. |
||
827 | */ |
||
828 | static void |
||
829 | get_http_headers(struct http_state *hs, const char *uri) |
||
830 | { |
||
831 | size_t content_type; |
||
832 | char *tmp; |
||
833 | char *ext; |
||
834 | char *vars; |
||
835 | u8_t add_content_len; |
||
836 | |||
837 | /* In all cases, the second header we send is the server identification |
||
838 | so set it here. */ |
||
839 | hs->hdrs[HDR_STRINGS_IDX_SERVER_NAME] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER]; |
||
840 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE] = NULL; |
||
841 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_NR] = NULL; |
||
842 | |||
843 | /* Is this a normal file or the special case we use to send back the |
||
844 | default "404: Page not found" response? */ |
||
845 | if (uri == NULL) { |
||
846 | hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND]; |
||
847 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
848 | if (hs->keepalive) { |
||
849 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaderStrings[DEFAULT_404_HTML_PERSISTENT]; |
||
850 | } else |
||
851 | #endif |
||
852 | { |
||
853 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaderStrings[DEFAULT_404_HTML]; |
||
854 | } |
||
855 | |||
856 | /* Set up to send the first header string. */ |
||
857 | hs->hdr_index = 0; |
||
858 | hs->hdr_pos = 0; |
||
859 | return; |
||
860 | } |
||
861 | /* We are dealing with a particular filename. Look for one other |
||
862 | special case. We assume that any filename with "404" in it must be |
||
863 | indicative of a 404 server error whereas all other files require |
||
864 | the 200 OK header. */ |
||
865 | if (strstr(uri, "404")) { |
||
866 | hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND]; |
||
867 | } else if (strstr(uri, "400")) { |
||
868 | hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST]; |
||
869 | } else if (strstr(uri, "501")) { |
||
870 | hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL]; |
||
871 | } else { |
||
872 | hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_OK]; |
||
873 | } |
||
874 | |||
875 | /* Determine if the URI has any variables and, if so, temporarily remove |
||
876 | them. */ |
||
877 | vars = strchr(uri, '?'); |
||
878 | if (vars) { |
||
879 | *vars = '\0'; |
||
880 | } |
||
881 | |||
882 | /* Get a pointer to the file extension. We find this by looking for the |
||
883 | last occurrence of "." in the filename passed. */ |
||
884 | ext = NULL; |
||
885 | tmp = strchr(uri, '.'); |
||
886 | while (tmp) { |
||
887 | ext = tmp + 1; |
||
888 | tmp = strchr(ext, '.'); |
||
889 | } |
||
890 | if (ext != NULL) { |
||
891 | /* Now determine the content type and add the relevant header for that. */ |
||
892 | for (content_type = 0; content_type < NUM_HTTP_HEADERS; content_type++) { |
||
893 | /* Have we found a matching extension? */ |
||
894 | if (!lwip_stricmp(g_psHTTPHeaders[content_type].extension, ext)) { |
||
895 | break; |
||
896 | } |
||
897 | } |
||
898 | } else { |
||
899 | content_type = NUM_HTTP_HEADERS; |
||
900 | } |
||
901 | |||
902 | /* Reinstate the parameter marker if there was one in the original URI. */ |
||
903 | if (vars) { |
||
904 | *vars = '?'; |
||
905 | } |
||
906 | |||
907 | #if LWIP_HTTPD_OMIT_HEADER_FOR_EXTENSIONLESS_URI |
||
908 | /* Does the URL passed have any file extension? If not, we assume it |
||
909 | is a special-case URL used for control state notification and we do |
||
910 | not send any HTTP headers with the response. */ |
||
911 | if (!ext) { |
||
912 | /* Force the header index to a value indicating that all headers |
||
913 | have already been sent. */ |
||
914 | hs->hdr_index = NUM_FILE_HDR_STRINGS; |
||
915 | return; |
||
916 | } |
||
917 | #endif /* LWIP_HTTPD_OMIT_HEADER_FOR_EXTENSIONLESS_URI */ |
||
918 | add_content_len = 1; |
||
919 | /* Did we find a matching extension? */ |
||
920 | if (content_type < NUM_HTTP_HEADERS) { |
||
921 | /* yes, store it */ |
||
922 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaders[content_type].content_type; |
||
923 | } else if (!ext) { |
||
924 | /* no, no extension found -> use binary transfer to prevent the browser adding '.txt' on save */ |
||
925 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = HTTP_HDR_APP; |
||
926 | } else { |
||
927 | /* No - use the default, plain text file type. */ |
||
928 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = HTTP_HDR_DEFAULT_TYPE; |
||
929 | } |
||
930 | /* Add content-length header? */ |
||
931 | #if LWIP_HTTPD_SSI |
||
932 | if (hs->ssi != NULL) { |
||
933 | add_content_len = 0; /* @todo: get maximum file length from SSI */ |
||
934 | } else |
||
935 | #endif /* LWIP_HTTPD_SSI */ |
||
936 | if ((hs->handle == NULL) || |
||
937 | ((hs->handle->flags & (FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT)) == FS_FILE_FLAGS_HEADER_INCLUDED)) { |
||
938 | add_content_len = 0; |
||
939 | } |
||
940 | if (add_content_len) { |
||
941 | size_t len; |
||
942 | lwip_itoa(hs->hdr_content_len, (size_t)LWIP_HTTPD_MAX_CONTENT_LEN_SIZE, |
||
943 | hs->handle->len); |
||
944 | len = strlen(hs->hdr_content_len); |
||
945 | if (len <= LWIP_HTTPD_MAX_CONTENT_LEN_SIZE - LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET) { |
||
946 | SMEMCPY(&hs->hdr_content_len[len], CRLF, 3); |
||
947 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_NR] = hs->hdr_content_len; |
||
948 | } else { |
||
949 | add_content_len = 0; |
||
950 | } |
||
951 | } |
||
952 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
953 | if (add_content_len) { |
||
954 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_KEEPALIVE_LEN]; |
||
955 | } else { |
||
956 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_CONN_CLOSE]; |
||
957 | } |
||
958 | #else /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
959 | if (add_content_len) { |
||
960 | hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH]; |
||
961 | } |
||
962 | #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
963 | |||
964 | /* Set up to send the first header string. */ |
||
965 | hs->hdr_index = 0; |
||
966 | hs->hdr_pos = 0; |
||
967 | } |
||
968 | |||
969 | /** Sub-function of http_send(): send dynamic headers |
||
970 | * |
||
971 | * @returns: - HTTP_NO_DATA_TO_SEND: no new data has been enqueued |
||
972 | * - HTTP_DATA_TO_SEND_CONTINUE: continue with sending HTTP body |
||
973 | * - HTTP_DATA_TO_SEND_BREAK: data has been enqueued, headers pending, |
||
974 | * so don't send HTTP body yet |
||
975 | */ |
||
976 | static u8_t |
||
977 | http_send_headers(struct altcp_pcb *pcb, struct http_state *hs) |
||
978 | { |
||
979 | err_t err; |
||
980 | u16_t len; |
||
981 | u8_t data_to_send = HTTP_NO_DATA_TO_SEND; |
||
982 | u16_t hdrlen, sendlen; |
||
983 | |||
984 | /* How much data can we send? */ |
||
985 | len = altcp_sndbuf(pcb); |
||
986 | sendlen = len; |
||
987 | |||
988 | while (len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) { |
||
989 | const void *ptr; |
||
990 | u16_t old_sendlen; |
||
991 | u8_t apiflags; |
||
992 | /* How much do we have to send from the current header? */ |
||
993 | hdrlen = (u16_t)strlen(hs->hdrs[hs->hdr_index]); |
||
994 | |||
995 | /* How much of this can we send? */ |
||
996 | sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos); |
||
997 | |||
998 | /* Send this amount of data or as much as we can given memory |
||
999 | * constraints. */ |
||
1000 | ptr = (const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos); |
||
1001 | old_sendlen = sendlen; |
||
1002 | apiflags = HTTP_IS_HDR_VOLATILE(hs, ptr); |
||
1003 | if (hs->hdr_index == HDR_STRINGS_IDX_CONTENT_LEN_NR) { |
||
1004 | /* content-length is always volatile */ |
||
1005 | apiflags |= TCP_WRITE_FLAG_COPY; |
||
1006 | } |
||
1007 | if (hs->hdr_index < NUM_FILE_HDR_STRINGS - 1) { |
||
1008 | apiflags |= TCP_WRITE_FLAG_MORE; |
||
1009 | } |
||
1010 | err = http_write(pcb, ptr, &sendlen, apiflags); |
||
1011 | if ((err == ERR_OK) && (old_sendlen != sendlen)) { |
||
1012 | /* Remember that we added some more data to be transmitted. */ |
||
1013 | data_to_send = HTTP_DATA_TO_SEND_CONTINUE; |
||
1014 | } else if (err != ERR_OK) { |
||
1015 | /* special case: http_write does not try to send 1 byte */ |
||
1016 | sendlen = 0; |
||
1017 | } |
||
1018 | |||
1019 | /* Fix up the header position for the next time round. */ |
||
1020 | hs->hdr_pos += sendlen; |
||
1021 | len -= sendlen; |
||
1022 | |||
1023 | /* Have we finished sending this string? */ |
||
1024 | if (hs->hdr_pos == hdrlen) { |
||
1025 | /* Yes - move on to the next one */ |
||
1026 | hs->hdr_index++; |
||
1027 | /* skip headers that are NULL (not all headers are required) */ |
||
1028 | while ((hs->hdr_index < NUM_FILE_HDR_STRINGS) && |
||
1029 | (hs->hdrs[hs->hdr_index] == NULL)) { |
||
1030 | hs->hdr_index++; |
||
1031 | } |
||
1032 | hs->hdr_pos = 0; |
||
1033 | } |
||
1034 | } |
||
1035 | |||
1036 | if ((hs->hdr_index >= NUM_FILE_HDR_STRINGS) && (hs->file == NULL)) { |
||
1037 | /* When we are at the end of the headers, check for data to send |
||
1038 | * instead of waiting for ACK from remote side to continue |
||
1039 | * (which would happen when sending files from async read). */ |
||
1040 | if (http_check_eof(pcb, hs)) { |
||
1041 | data_to_send = HTTP_DATA_TO_SEND_CONTINUE; |
||
1042 | } |
||
1043 | } |
||
1044 | /* If we get here and there are still header bytes to send, we send |
||
1045 | * the header information we just wrote immediately. If there are no |
||
1046 | * more headers to send, but we do have file data to send, drop through |
||
1047 | * to try to send some file data too. */ |
||
1048 | if ((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) { |
||
1049 | LWIP_DEBUGF(HTTPD_DEBUG, ("tcp_output\n")); |
||
1050 | return HTTP_DATA_TO_SEND_BREAK; |
||
1051 | } |
||
1052 | return data_to_send; |
||
1053 | } |
||
1054 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
1055 | |||
1056 | /** Sub-function of http_send(): end-of-file (or block) is reached, |
||
1057 | * either close the file or read the next block (if supported). |
||
1058 | * |
||
1059 | * @returns: 0 if the file is finished or no data has been read |
||
1060 | * 1 if the file is not finished and data has been read |
||
1061 | */ |
||
1062 | static u8_t |
||
1063 | http_check_eof(struct altcp_pcb *pcb, struct http_state *hs) |
||
1064 | { |
||
1065 | int bytes_left; |
||
1066 | #if LWIP_HTTPD_DYNAMIC_FILE_READ |
||
1067 | int count; |
||
1068 | #ifdef HTTPD_MAX_WRITE_LEN |
||
1069 | int max_write_len; |
||
1070 | #endif /* HTTPD_MAX_WRITE_LEN */ |
||
1071 | #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ */ |
||
1072 | |||
1073 | /* Do we have a valid file handle? */ |
||
1074 | if (hs->handle == NULL) { |
||
1075 | /* No - close the connection. */ |
||
1076 | http_eof(pcb, hs); |
||
1077 | return 0; |
||
1078 | } |
||
1079 | bytes_left = fs_bytes_left(hs->handle); |
||
1080 | if (bytes_left <= 0) { |
||
1081 | /* We reached the end of the file so this request is done. */ |
||
1082 | LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n")); |
||
1083 | http_eof(pcb, hs); |
||
1084 | return 0; |
||
1085 | } |
||
1086 | #if LWIP_HTTPD_DYNAMIC_FILE_READ |
||
1087 | /* Do we already have a send buffer allocated? */ |
||
1088 | if (hs->buf) { |
||
1089 | /* Yes - get the length of the buffer */ |
||
1090 | count = LWIP_MIN(hs->buf_len, bytes_left); |
||
1091 | } else { |
||
1092 | /* We don't have a send buffer so allocate one now */ |
||
1093 | count = altcp_sndbuf(pcb); |
||
1094 | if (bytes_left < count) { |
||
1095 | count = bytes_left; |
||
1096 | } |
||
1097 | #ifdef HTTPD_MAX_WRITE_LEN |
||
1098 | /* Additional limitation: e.g. don't enqueue more than 2*mss at once */ |
||
1099 | max_write_len = HTTPD_MAX_WRITE_LEN(pcb); |
||
1100 | if (count > max_write_len) { |
||
1101 | count = max_write_len; |
||
1102 | } |
||
1103 | #endif /* HTTPD_MAX_WRITE_LEN */ |
||
1104 | do { |
||
1105 | hs->buf = (char *)mem_malloc((mem_size_t)count); |
||
1106 | if (hs->buf != NULL) { |
||
1107 | hs->buf_len = count; |
||
1108 | break; |
||
1109 | } |
||
1110 | count = count / 2; |
||
1111 | } while (count > 100); |
||
1112 | |||
1113 | /* Did we get a send buffer? If not, return immediately. */ |
||
1114 | if (hs->buf == NULL) { |
||
1115 | LWIP_DEBUGF(HTTPD_DEBUG, ("No buff\n")); |
||
1116 | return 0; |
||
1117 | } |
||
1118 | } |
||
1119 | |||
1120 | /* Read a block of data from the file. */ |
||
1121 | LWIP_DEBUGF(HTTPD_DEBUG, ("Trying to read %d bytes.\n", count)); |
||
1122 | |||
1123 | #if LWIP_HTTPD_FS_ASYNC_READ |
||
1124 | count = fs_read_async(hs->handle, hs->buf, count, http_continue, hs); |
||
1125 | #else /* LWIP_HTTPD_FS_ASYNC_READ */ |
||
1126 | count = fs_read(hs->handle, hs->buf, count); |
||
1127 | #endif /* LWIP_HTTPD_FS_ASYNC_READ */ |
||
1128 | if (count < 0) { |
||
1129 | if (count == FS_READ_DELAYED) { |
||
1130 | /* Delayed read, wait for FS to unblock us */ |
||
1131 | return 0; |
||
1132 | } |
||
1133 | /* We reached the end of the file so this request is done. |
||
1134 | * @todo: close here for HTTP/1.1 when reading file fails */ |
||
1135 | LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n")); |
||
1136 | http_eof(pcb, hs); |
||
1137 | return 0; |
||
1138 | } |
||
1139 | |||
1140 | /* Set up to send the block of data we just read */ |
||
1141 | LWIP_DEBUGF(HTTPD_DEBUG, ("Read %d bytes.\n", count)); |
||
1142 | hs->left = count; |
||
1143 | hs->file = hs->buf; |
||
1144 | #if LWIP_HTTPD_SSI |
||
1145 | if (hs->ssi) { |
||
1146 | hs->ssi->parse_left = count; |
||
1147 | hs->ssi->parsed = hs->buf; |
||
1148 | } |
||
1149 | #endif /* LWIP_HTTPD_SSI */ |
||
1150 | #else /* LWIP_HTTPD_DYNAMIC_FILE_READ */ |
||
1151 | LWIP_ASSERT("SSI and DYNAMIC_HEADERS turned off but eof not reached", 0); |
||
1152 | #endif /* LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
1153 | return 1; |
||
1154 | } |
||
1155 | |||
1156 | /** Sub-function of http_send(): This is the normal send-routine for non-ssi files |
||
1157 | * |
||
1158 | * @returns: - 1: data has been written (so call tcp_ouput) |
||
1159 | * - 0: no data has been written (no need to call tcp_output) |
||
1160 | */ |
||
1161 | static u8_t |
||
1162 | http_send_data_nonssi(struct altcp_pcb *pcb, struct http_state *hs) |
||
1163 | { |
||
1164 | err_t err; |
||
1165 | u16_t len; |
||
1166 | u8_t data_to_send = 0; |
||
1167 | |||
1168 | /* We are not processing an SHTML file so no tag checking is necessary. |
||
1169 | * Just send the data as we received it from the file. */ |
||
1170 | len = (u16_t)LWIP_MIN(hs->left, 0xffff); |
||
1171 | |||
1172 | err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs)); |
||
1173 | if (err == ERR_OK) { |
||
1174 | data_to_send = 1; |
||
1175 | hs->file += len; |
||
1176 | hs->left -= len; |
||
1177 | } |
||
1178 | |||
1179 | return data_to_send; |
||
1180 | } |
||
1181 | |||
1182 | #if LWIP_HTTPD_SSI |
||
1183 | /** Sub-function of http_send(): This is the send-routine for ssi files |
||
1184 | * |
||
1185 | * @returns: - 1: data has been written (so call tcp_ouput) |
||
1186 | * - 0: no data has been written (no need to call tcp_output) |
||
1187 | */ |
||
1188 | static u8_t |
||
1189 | http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs) |
||
1190 | { |
||
1191 | err_t err = ERR_OK; |
||
1192 | u16_t len; |
||
1193 | u8_t data_to_send = 0; |
||
1194 | |||
1195 | struct http_ssi_state *ssi = hs->ssi; |
||
1196 | LWIP_ASSERT("ssi != NULL", ssi != NULL); |
||
1197 | /* We are processing an SHTML file so need to scan for tags and replace |
||
1198 | * them with insert strings. We need to be careful here since a tag may |
||
1199 | * straddle the boundary of two blocks read from the file and we may also |
||
1200 | * have to split the insert string between two tcp_write operations. */ |
||
1201 | |||
1202 | /* How much data could we send? */ |
||
1203 | len = altcp_sndbuf(pcb); |
||
1204 | |||
1205 | /* Do we have remaining data to send before parsing more? */ |
||
1206 | if (ssi->parsed > hs->file) { |
||
1207 | len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff); |
||
1208 | |||
1209 | err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs)); |
||
1210 | if (err == ERR_OK) { |
||
1211 | data_to_send = 1; |
||
1212 | hs->file += len; |
||
1213 | hs->left -= len; |
||
1214 | } |
||
1215 | |||
1216 | /* If the send buffer is full, return now. */ |
||
1217 | if (altcp_sndbuf(pcb) == 0) { |
||
1218 | return data_to_send; |
||
1219 | } |
||
1220 | } |
||
1221 | |||
1222 | LWIP_DEBUGF(HTTPD_DEBUG, ("State %d, %d left\n", ssi->tag_state, (int)ssi->parse_left)); |
||
1223 | |||
1224 | /* We have sent all the data that was already parsed so continue parsing |
||
1225 | * the buffer contents looking for SSI tags. */ |
||
1226 | while (((ssi->tag_state == TAG_SENDING) || ssi->parse_left) && (err == ERR_OK)) { |
||
1227 | if (len == 0) { |
||
1228 | return data_to_send; |
||
1229 | } |
||
1230 | switch (ssi->tag_state) { |
||
1231 | case TAG_NONE: |
||
1232 | /* We are not currently processing an SSI tag so scan for the |
||
1233 | * start of the lead-in marker. */ |
||
1234 | if (*ssi->parsed == g_pcTagLeadIn[0]) { |
||
1235 | /* We found what could be the lead-in for a new tag so change |
||
1236 | * state appropriately. */ |
||
1237 | ssi->tag_state = TAG_LEADIN; |
||
1238 | ssi->tag_index = 1; |
||
1239 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1240 | ssi->tag_started = ssi->parsed; |
||
1241 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG */ |
||
1242 | } |
||
1243 | |||
1244 | /* Move on to the next character in the buffer */ |
||
1245 | ssi->parse_left--; |
||
1246 | ssi->parsed++; |
||
1247 | break; |
||
1248 | |||
1249 | case TAG_LEADIN: |
||
1250 | /* We are processing the lead-in marker, looking for the start of |
||
1251 | * the tag name. */ |
||
1252 | |||
1253 | /* Have we reached the end of the leadin? */ |
||
1254 | if (ssi->tag_index == LEN_TAG_LEAD_IN) { |
||
1255 | ssi->tag_index = 0; |
||
1256 | ssi->tag_state = TAG_FOUND; |
||
1257 | } else { |
||
1258 | /* Have we found the next character we expect for the tag leadin? */ |
||
1259 | if (*ssi->parsed == g_pcTagLeadIn[ssi->tag_index]) { |
||
1260 | /* Yes - move to the next one unless we have found the complete |
||
1261 | * leadin, in which case we start looking for the tag itself */ |
||
1262 | ssi->tag_index++; |
||
1263 | } else { |
||
1264 | /* We found an unexpected character so this is not a tag. Move |
||
1265 | * back to idle state. */ |
||
1266 | ssi->tag_state = TAG_NONE; |
||
1267 | } |
||
1268 | |||
1269 | /* Move on to the next character in the buffer */ |
||
1270 | ssi->parse_left--; |
||
1271 | ssi->parsed++; |
||
1272 | } |
||
1273 | break; |
||
1274 | |||
1275 | case TAG_FOUND: |
||
1276 | /* We are reading the tag name, looking for the start of the |
||
1277 | * lead-out marker and removing any whitespace found. */ |
||
1278 | |||
1279 | /* Remove leading whitespace between the tag leading and the first |
||
1280 | * tag name character. */ |
||
1281 | if ((ssi->tag_index == 0) && ((*ssi->parsed == ' ') || |
||
1282 | (*ssi->parsed == '\t') || (*ssi->parsed == '\n') || |
||
1283 | (*ssi->parsed == '\r'))) { |
||
1284 | /* Move on to the next character in the buffer */ |
||
1285 | ssi->parse_left--; |
||
1286 | ssi->parsed++; |
||
1287 | break; |
||
1288 | } |
||
1289 | |||
1290 | /* Have we found the end of the tag name? This is signalled by |
||
1291 | * us finding the first leadout character or whitespace */ |
||
1292 | if ((*ssi->parsed == g_pcTagLeadOut[0]) || |
||
1293 | (*ssi->parsed == ' ') || (*ssi->parsed == '\t') || |
||
1294 | (*ssi->parsed == '\n') || (*ssi->parsed == '\r')) { |
||
1295 | |||
1296 | if (ssi->tag_index == 0) { |
||
1297 | /* We read a zero length tag so ignore it. */ |
||
1298 | ssi->tag_state = TAG_NONE; |
||
1299 | } else { |
||
1300 | /* We read a non-empty tag so go ahead and look for the |
||
1301 | * leadout string. */ |
||
1302 | ssi->tag_state = TAG_LEADOUT; |
||
1303 | LWIP_ASSERT("ssi->tag_index <= 0xff", ssi->tag_index <= 0xff); |
||
1304 | ssi->tag_name_len = (u8_t)ssi->tag_index; |
||
1305 | ssi->tag_name[ssi->tag_index] = '\0'; |
||
1306 | if (*ssi->parsed == g_pcTagLeadOut[0]) { |
||
1307 | ssi->tag_index = 1; |
||
1308 | } else { |
||
1309 | ssi->tag_index = 0; |
||
1310 | } |
||
1311 | } |
||
1312 | } else { |
||
1313 | /* This character is part of the tag name so save it */ |
||
1314 | if (ssi->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) { |
||
1315 | ssi->tag_name[ssi->tag_index++] = *ssi->parsed; |
||
1316 | } else { |
||
1317 | /* The tag was too long so ignore it. */ |
||
1318 | ssi->tag_state = TAG_NONE; |
||
1319 | } |
||
1320 | } |
||
1321 | |||
1322 | /* Move on to the next character in the buffer */ |
||
1323 | ssi->parse_left--; |
||
1324 | ssi->parsed++; |
||
1325 | |||
1326 | break; |
||
1327 | |||
1328 | /* We are looking for the end of the lead-out marker. */ |
||
1329 | case TAG_LEADOUT: |
||
1330 | /* Remove leading whitespace between the tag leading and the first |
||
1331 | * tag leadout character. */ |
||
1332 | if ((ssi->tag_index == 0) && ((*ssi->parsed == ' ') || |
||
1333 | (*ssi->parsed == '\t') || (*ssi->parsed == '\n') || |
||
1334 | (*ssi->parsed == '\r'))) { |
||
1335 | /* Move on to the next character in the buffer */ |
||
1336 | ssi->parse_left--; |
||
1337 | ssi->parsed++; |
||
1338 | break; |
||
1339 | } |
||
1340 | |||
1341 | /* Have we found the next character we expect for the tag leadout? */ |
||
1342 | if (*ssi->parsed == g_pcTagLeadOut[ssi->tag_index]) { |
||
1343 | /* Yes - move to the next one unless we have found the complete |
||
1344 | * leadout, in which case we need to call the client to process |
||
1345 | * the tag. */ |
||
1346 | |||
1347 | /* Move on to the next character in the buffer */ |
||
1348 | ssi->parse_left--; |
||
1349 | ssi->parsed++; |
||
1350 | |||
1351 | if (ssi->tag_index == (LEN_TAG_LEAD_OUT - 1)) { |
||
1352 | /* Call the client to ask for the insert string for the |
||
1353 | * tag we just found. */ |
||
1354 | #if LWIP_HTTPD_SSI_MULTIPART |
||
1355 | ssi->tag_part = 0; /* start with tag part 0 */ |
||
1356 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
1357 | get_tag_insert(hs); |
||
1358 | |||
1359 | /* Next time through, we are going to be sending data |
||
1360 | * immediately, either the end of the block we start |
||
1361 | * sending here or the insert string. */ |
||
1362 | ssi->tag_index = 0; |
||
1363 | ssi->tag_state = TAG_SENDING; |
||
1364 | ssi->tag_end = ssi->parsed; |
||
1365 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1366 | ssi->parsed = ssi->tag_started; |
||
1367 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1368 | |||
1369 | /* If there is any unsent data in the buffer prior to the |
||
1370 | * tag, we need to send it now. */ |
||
1371 | if (ssi->tag_end > hs->file) { |
||
1372 | /* How much of the data can we send? */ |
||
1373 | #if LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1374 | len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff); |
||
1375 | #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1376 | /* we would include the tag in sending */ |
||
1377 | len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff); |
||
1378 | #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1379 | |||
1380 | err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs)); |
||
1381 | if (err == ERR_OK) { |
||
1382 | data_to_send = 1; |
||
1383 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1384 | if (ssi->tag_started <= hs->file) { |
||
1385 | /* pretend to have sent the tag, too */ |
||
1386 | len += (u16_t)(ssi->tag_end - ssi->tag_started); |
||
1387 | } |
||
1388 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1389 | hs->file += len; |
||
1390 | hs->left -= len; |
||
1391 | } |
||
1392 | } |
||
1393 | } else { |
||
1394 | ssi->tag_index++; |
||
1395 | } |
||
1396 | } else { |
||
1397 | /* We found an unexpected character so this is not a tag. Move |
||
1398 | * back to idle state. */ |
||
1399 | ssi->parse_left--; |
||
1400 | ssi->parsed++; |
||
1401 | ssi->tag_state = TAG_NONE; |
||
1402 | } |
||
1403 | break; |
||
1404 | |||
1405 | /* |
||
1406 | * We have found a valid tag and are in the process of sending |
||
1407 | * data as a result of that discovery. We send either remaining data |
||
1408 | * from the file prior to the insert point or the insert string itself. |
||
1409 | */ |
||
1410 | case TAG_SENDING: |
||
1411 | /* Do we have any remaining file data to send from the buffer prior |
||
1412 | * to the tag? */ |
||
1413 | if (ssi->tag_end > hs->file) { |
||
1414 | /* How much of the data can we send? */ |
||
1415 | #if LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1416 | len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff); |
||
1417 | #else /* LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1418 | LWIP_ASSERT("hs->started >= hs->file", ssi->tag_started >= hs->file); |
||
1419 | /* we would include the tag in sending */ |
||
1420 | len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff); |
||
1421 | #endif /* LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1422 | if (len != 0) { |
||
1423 | err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs)); |
||
1424 | } else { |
||
1425 | err = ERR_OK; |
||
1426 | } |
||
1427 | if (err == ERR_OK) { |
||
1428 | data_to_send = 1; |
||
1429 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1430 | if (ssi->tag_started <= hs->file) { |
||
1431 | /* pretend to have sent the tag, too */ |
||
1432 | len += (u16_t)(ssi->tag_end - ssi->tag_started); |
||
1433 | } |
||
1434 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1435 | hs->file += len; |
||
1436 | hs->left -= len; |
||
1437 | } |
||
1438 | } else { |
||
1439 | #if LWIP_HTTPD_SSI_MULTIPART |
||
1440 | if (ssi->tag_index >= ssi->tag_insert_len) { |
||
1441 | /* Did the last SSIHandler have more to send? */ |
||
1442 | if (ssi->tag_part != HTTPD_LAST_TAG_PART) { |
||
1443 | /* If so, call it again */ |
||
1444 | ssi->tag_index = 0; |
||
1445 | get_tag_insert(hs); |
||
1446 | } |
||
1447 | } |
||
1448 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
1449 | |||
1450 | /* Do we still have insert data left to send? */ |
||
1451 | if (ssi->tag_index < ssi->tag_insert_len) { |
||
1452 | /* We are sending the insert string itself. How much of the |
||
1453 | * insert can we send? */ |
||
1454 | len = (ssi->tag_insert_len - ssi->tag_index); |
||
1455 | |||
1456 | /* Note that we set the copy flag here since we only have a |
||
1457 | * single tag insert buffer per connection. If we don't do |
||
1458 | * this, insert corruption can occur if more than one insert |
||
1459 | * is processed before we call tcp_output. */ |
||
1460 | err = http_write(pcb, &(ssi->tag_insert[ssi->tag_index]), &len, |
||
1461 | HTTP_IS_TAG_VOLATILE(hs)); |
||
1462 | if (err == ERR_OK) { |
||
1463 | data_to_send = 1; |
||
1464 | ssi->tag_index += len; |
||
1465 | /* Don't return here: keep on sending data */ |
||
1466 | } |
||
1467 | } else { |
||
1468 | #if LWIP_HTTPD_SSI_MULTIPART |
||
1469 | if (ssi->tag_part == HTTPD_LAST_TAG_PART) |
||
1470 | #endif /* LWIP_HTTPD_SSI_MULTIPART */ |
||
1471 | { |
||
1472 | /* We have sent all the insert data so go back to looking for |
||
1473 | * a new tag. */ |
||
1474 | LWIP_DEBUGF(HTTPD_DEBUG, ("Everything sent.\n")); |
||
1475 | ssi->tag_index = 0; |
||
1476 | ssi->tag_state = TAG_NONE; |
||
1477 | #if !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1478 | ssi->parsed = ssi->tag_end; |
||
1479 | #endif /* !LWIP_HTTPD_SSI_INCLUDE_TAG*/ |
||
1480 | } |
||
1481 | } |
||
1482 | break; |
||
1483 | default: |
||
1484 | break; |
||
1485 | } |
||
1486 | } |
||
1487 | } |
||
1488 | |||
1489 | /* If we drop out of the end of the for loop, this implies we must have |
||
1490 | * file data to send so send it now. In TAG_SENDING state, we've already |
||
1491 | * handled this so skip the send if that's the case. */ |
||
1492 | if ((ssi->tag_state != TAG_SENDING) && (ssi->parsed > hs->file)) { |
||
1493 | #if LWIP_HTTPD_DYNAMIC_FILE_READ && !LWIP_HTTPD_SSI_INCLUDE_TAG |
||
1494 | if ((ssi->tag_state != TAG_NONE) && (ssi->tag_started > ssi->tag_end)) { |
||
1495 | /* If we found tag on the edge of the read buffer: just throw away the first part |
||
1496 | (we have copied/saved everything required for parsing on later). */ |
||
1497 | len = (u16_t)(ssi->tag_started - hs->file); |
||
1498 | hs->left -= (ssi->parsed - ssi->tag_started); |
||
1499 | ssi->parsed = ssi->tag_started; |
||
1500 | ssi->tag_started = hs->buf; |
||
1501 | } else |
||
1502 | #endif /* LWIP_HTTPD_DYNAMIC_FILE_READ && !LWIP_HTTPD_SSI_INCLUDE_TAG */ |
||
1503 | { |
||
1504 | len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff); |
||
1505 | } |
||
1506 | |||
1507 | err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs)); |
||
1508 | if (err == ERR_OK) { |
||
1509 | data_to_send = 1; |
||
1510 | hs->file += len; |
||
1511 | hs->left -= len; |
||
1512 | } |
||
1513 | } |
||
1514 | return data_to_send; |
||
1515 | } |
||
1516 | #endif /* LWIP_HTTPD_SSI */ |
||
1517 | |||
1518 | /** |
||
1519 | * Try to send more data on this pcb. |
||
1520 | * |
||
1521 | * @param pcb the pcb to send data |
||
1522 | * @param hs connection state |
||
1523 | */ |
||
1524 | static u8_t |
||
1525 | http_send(struct altcp_pcb *pcb, struct http_state *hs) |
||
1526 | { |
||
1527 | u8_t data_to_send = HTTP_NO_DATA_TO_SEND; |
||
1528 | |||
1529 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_send: pcb=%p hs=%p left=%d\n", (void *)pcb, |
||
1530 | (void *)hs, hs != NULL ? (int)hs->left : 0)); |
||
1531 | |||
1532 | #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND |
||
1533 | if (hs->unrecved_bytes != 0) { |
||
1534 | return 0; |
||
1535 | } |
||
1536 | #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */ |
||
1537 | |||
1538 | /* If we were passed a NULL state structure pointer, ignore the call. */ |
||
1539 | if (hs == NULL) { |
||
1540 | return 0; |
||
1541 | } |
||
1542 | |||
1543 | #if LWIP_HTTPD_FS_ASYNC_READ |
||
1544 | /* Check if we are allowed to read from this file. |
||
1545 | (e.g. SSI might want to delay sending until data is available) */ |
||
1546 | if (!fs_is_file_ready(hs->handle, http_continue, hs)) { |
||
1547 | return 0; |
||
1548 | } |
||
1549 | #endif /* LWIP_HTTPD_FS_ASYNC_READ */ |
||
1550 | |||
1551 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
1552 | /* Do we have any more header data to send for this file? */ |
||
1553 | if (hs->hdr_index < NUM_FILE_HDR_STRINGS) { |
||
1554 | data_to_send = http_send_headers(pcb, hs); |
||
1555 | if ((data_to_send != HTTP_DATA_TO_SEND_CONTINUE) && |
||
1556 | (hs->hdr_index < NUM_FILE_HDR_STRINGS)) { |
||
1557 | return data_to_send; |
||
1558 | } |
||
1559 | } |
||
1560 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
1561 | |||
1562 | /* Have we run out of file data to send? If so, we need to read the next |
||
1563 | * block from the file. */ |
||
1564 | if (hs->left == 0) { |
||
1565 | if (!http_check_eof(pcb, hs)) { |
||
1566 | return 0; |
||
1567 | } |
||
1568 | } |
||
1569 | |||
1570 | #if LWIP_HTTPD_SSI |
||
1571 | if (hs->ssi) { |
||
1572 | data_to_send = http_send_data_ssi(pcb, hs); |
||
1573 | } else |
||
1574 | #endif /* LWIP_HTTPD_SSI */ |
||
1575 | { |
||
1576 | data_to_send = http_send_data_nonssi(pcb, hs); |
||
1577 | } |
||
1578 | |||
1579 | if ((hs->left == 0) && (fs_bytes_left(hs->handle) <= 0)) { |
||
1580 | /* We reached the end of the file so this request is done. |
||
1581 | * This adds the FIN flag right into the last data segment. */ |
||
1582 | LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n")); |
||
1583 | http_eof(pcb, hs); |
||
1584 | return 0; |
||
1585 | } |
||
1586 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("send_data end.\n")); |
||
1587 | return data_to_send; |
||
1588 | } |
||
1589 | |||
1590 | #if LWIP_HTTPD_SUPPORT_EXTSTATUS |
||
1591 | /** Initialize a http connection with a file to send for an error message |
||
1592 | * |
||
1593 | * @param hs http connection state |
||
1594 | * @param error_nr HTTP error number |
||
1595 | * @return ERR_OK if file was found and hs has been initialized correctly |
||
1596 | * another err_t otherwise |
||
1597 | */ |
||
1598 | static err_t |
||
1599 | http_find_error_file(struct http_state *hs, u16_t error_nr) |
||
1600 | { |
||
1601 | const char *uri1, *uri2, *uri3; |
||
1602 | err_t err; |
||
1603 | |||
1604 | if (error_nr == 501) { |
||
1605 | uri1 = "/501.html"; |
||
1606 | uri2 = "/501.htm"; |
||
1607 | uri3 = "/501.shtml"; |
||
1608 | } else { |
||
1609 | /* 400 (bad request is the default) */ |
||
1610 | uri1 = "/400.html"; |
||
1611 | uri2 = "/400.htm"; |
||
1612 | uri3 = "/400.shtml"; |
||
1613 | } |
||
1614 | err = fs_open(&hs->file_handle, uri1); |
||
1615 | if (err != ERR_OK) { |
||
1616 | err = fs_open(&hs->file_handle, uri2); |
||
1617 | if (err != ERR_OK) { |
||
1618 | err = fs_open(&hs->file_handle, uri3); |
||
1619 | if (err != ERR_OK) { |
||
1620 | LWIP_DEBUGF(HTTPD_DEBUG, ("Error page for error %"U16_F" not found\n", |
||
1621 | error_nr)); |
||
1622 | return ERR_ARG; |
||
1623 | } |
||
1624 | } |
||
1625 | } |
||
1626 | return http_init_file(hs, &hs->file_handle, 0, NULL, 0, NULL); |
||
1627 | } |
||
1628 | #else /* LWIP_HTTPD_SUPPORT_EXTSTATUS */ |
||
1629 | #define http_find_error_file(hs, error_nr) ERR_ARG |
||
1630 | #endif /* LWIP_HTTPD_SUPPORT_EXTSTATUS */ |
||
1631 | |||
1632 | /** |
||
1633 | * Get the file struct for a 404 error page. |
||
1634 | * Tries some file names and returns NULL if none found. |
||
1635 | * |
||
1636 | * @param uri pointer that receives the actual file name URI |
||
1637 | * @return file struct for the error page or NULL no matching file was found |
||
1638 | */ |
||
1639 | static struct fs_file * |
||
1640 | http_get_404_file(struct http_state *hs, const char **uri) |
||
1641 | { |
||
1642 | err_t err; |
||
1643 | |||
1644 | *uri = "/404.html"; |
||
1645 | err = fs_open(&hs->file_handle, *uri); |
||
1646 | if (err != ERR_OK) { |
||
1647 | /* 404.html doesn't exist. Try 404.htm instead. */ |
||
1648 | *uri = "/404.htm"; |
||
1649 | err = fs_open(&hs->file_handle, *uri); |
||
1650 | if (err != ERR_OK) { |
||
1651 | /* 404.htm doesn't exist either. Try 404.shtml instead. */ |
||
1652 | *uri = "/404.shtml"; |
||
1653 | err = fs_open(&hs->file_handle, *uri); |
||
1654 | if (err != ERR_OK) { |
||
1655 | /* 404.htm doesn't exist either. Indicate to the caller that it should |
||
1656 | * send back a default 404 page. |
||
1657 | */ |
||
1658 | *uri = NULL; |
||
1659 | return NULL; |
||
1660 | } |
||
1661 | } |
||
1662 | } |
||
1663 | |||
1664 | return &hs->file_handle; |
||
1665 | } |
||
1666 | |||
1667 | #if LWIP_HTTPD_SUPPORT_POST |
||
1668 | static err_t |
||
1669 | http_handle_post_finished(struct http_state *hs) |
||
1670 | { |
||
1671 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
1672 | /* Prevent multiple calls to httpd_post_finished, since it might have already |
||
1673 | been called before from httpd_post_data_recved(). */ |
||
1674 | if (hs->post_finished) { |
||
1675 | return ERR_OK; |
||
1676 | } |
||
1677 | hs->post_finished = 1; |
||
1678 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
1679 | /* application error or POST finished */ |
||
1680 | /* NULL-terminate the buffer */ |
||
1681 | http_uri_buf[0] = 0; |
||
1682 | httpd_post_finished(hs, http_uri_buf, LWIP_HTTPD_URI_BUF_LEN); |
||
1683 | return http_find_file(hs, http_uri_buf, 0); |
||
1684 | } |
||
1685 | |||
1686 | /** Pass received POST body data to the application and correctly handle |
||
1687 | * returning a response document or closing the connection. |
||
1688 | * ATTENTION: The application is responsible for the pbuf now, so don't free it! |
||
1689 | * |
||
1690 | * @param hs http connection state |
||
1691 | * @param p pbuf to pass to the application |
||
1692 | * @return ERR_OK if passed successfully, another err_t if the response file |
||
1693 | * hasn't been found (after POST finished) |
||
1694 | */ |
||
1695 | static err_t |
||
1696 | http_post_rxpbuf(struct http_state *hs, struct pbuf *p) |
||
1697 | { |
||
1698 | err_t err; |
||
1699 | |||
1700 | if (p != NULL) { |
||
1701 | /* adjust remaining Content-Length */ |
||
1702 | if (hs->post_content_len_left < p->tot_len) { |
||
1703 | hs->post_content_len_left = 0; |
||
1704 | } else { |
||
1705 | hs->post_content_len_left -= p->tot_len; |
||
1706 | } |
||
1707 | } |
||
1708 | #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND |
||
1709 | /* prevent connection being closed if httpd_post_data_recved() is called nested */ |
||
1710 | hs->unrecved_bytes++; |
||
1711 | #endif |
||
1712 | err = httpd_post_receive_data(hs, p); |
||
1713 | #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND |
||
1714 | hs->unrecved_bytes--; |
||
1715 | #endif |
||
1716 | if (err != ERR_OK) { |
||
1717 | /* Ignore remaining content in case of application error */ |
||
1718 | hs->post_content_len_left = 0; |
||
1719 | } |
||
1720 | if (hs->post_content_len_left == 0) { |
||
1721 | #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND |
||
1722 | if (hs->unrecved_bytes != 0) { |
||
1723 | return ERR_OK; |
||
1724 | } |
||
1725 | #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */ |
||
1726 | /* application error or POST finished */ |
||
1727 | return http_handle_post_finished(hs); |
||
1728 | } |
||
1729 | |||
1730 | return ERR_OK; |
||
1731 | } |
||
1732 | |||
1733 | /** Handle a post request. Called from http_parse_request when method 'POST' |
||
1734 | * is found. |
||
1735 | * |
||
1736 | * @param p The input pbuf (containing the POST header and body). |
||
1737 | * @param hs The http connection state. |
||
1738 | * @param data HTTP request (header and part of body) from input pbuf(s). |
||
1739 | * @param data_len Size of 'data'. |
||
1740 | * @param uri The HTTP URI parsed from input pbuf(s). |
||
1741 | * @param uri_end Pointer to the end of 'uri' (here, the rest of the HTTP |
||
1742 | * header starts). |
||
1743 | * @return ERR_OK: POST correctly parsed and accepted by the application. |
||
1744 | * ERR_INPROGRESS: POST not completely parsed (no error yet) |
||
1745 | * another err_t: Error parsing POST or denied by the application |
||
1746 | */ |
||
1747 | static err_t |
||
1748 | http_post_request(struct pbuf *inp, struct http_state *hs, |
||
1749 | char *data, u16_t data_len, char *uri, char *uri_end) |
||
1750 | { |
||
1751 | err_t err; |
||
1752 | /* search for end-of-header (first double-CRLF) */ |
||
1753 | char *crlfcrlf = lwip_strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data)); |
||
1754 | |||
1755 | if (crlfcrlf != NULL) { |
||
1756 | /* search for "Content-Length: " */ |
||
1757 | #define HTTP_HDR_CONTENT_LEN "Content-Length: " |
||
1758 | #define HTTP_HDR_CONTENT_LEN_LEN 16 |
||
1759 | #define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10 |
||
1760 | char *scontent_len = lwip_strnstr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1)); |
||
1761 | if (scontent_len != NULL) { |
||
1762 | char *scontent_len_end = lwip_strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN); |
||
1763 | if (scontent_len_end != NULL) { |
||
1764 | int content_len; |
||
1765 | char *content_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN; |
||
1766 | content_len = atoi(content_len_num); |
||
1767 | if (content_len == 0) { |
||
1768 | /* if atoi returns 0 on error, fix this */ |
||
1769 | if ((content_len_num[0] != '0') || (content_len_num[1] != '\r')) { |
||
1770 | content_len = -1; |
||
1771 | } |
||
1772 | } |
||
1773 | if (content_len >= 0) { |
||
1774 | /* adjust length of HTTP header passed to application */ |
||
1775 | const char *hdr_start_after_uri = uri_end + 1; |
||
1776 | u16_t hdr_len = (u16_t)LWIP_MIN(data_len, crlfcrlf + 4 - data); |
||
1777 | u16_t hdr_data_len = (u16_t)LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri); |
||
1778 | u8_t post_auto_wnd = 1; |
||
1779 | http_uri_buf[0] = 0; |
||
1780 | /* trim http header */ |
||
1781 | *crlfcrlf = 0; |
||
1782 | err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len, |
||
1783 | http_uri_buf, LWIP_HTTPD_URI_BUF_LEN, &post_auto_wnd); |
||
1784 | if (err == ERR_OK) { |
||
1785 | /* try to pass in data of the first pbuf(s) */ |
||
1786 | struct pbuf *q = inp; |
||
1787 | u16_t start_offset = hdr_len; |
||
1788 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
1789 | hs->no_auto_wnd = !post_auto_wnd; |
||
1790 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
1791 | /* set the Content-Length to be received for this POST */ |
||
1792 | hs->post_content_len_left = (u32_t)content_len; |
||
1793 | |||
1794 | /* get to the pbuf where the body starts */ |
||
1795 | while ((q != NULL) && (q->len <= start_offset)) { |
||
1796 | start_offset -= q->len; |
||
1797 | q = q->next; |
||
1798 | } |
||
1799 | if (q != NULL) { |
||
1800 | /* hide the remaining HTTP header */ |
||
1801 | pbuf_remove_header(q, start_offset); |
||
1802 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
1803 | if (!post_auto_wnd) { |
||
1804 | /* already tcp_recved() this data... */ |
||
1805 | hs->unrecved_bytes = q->tot_len; |
||
1806 | } |
||
1807 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
1808 | pbuf_ref(q); |
||
1809 | return http_post_rxpbuf(hs, q); |
||
1810 | } else if (hs->post_content_len_left == 0) { |
||
1811 | q = pbuf_alloc(PBUF_RAW, 0, PBUF_REF); |
||
1812 | return http_post_rxpbuf(hs, q); |
||
1813 | } else { |
||
1814 | return ERR_OK; |
||
1815 | } |
||
1816 | } else { |
||
1817 | /* return file passed from application */ |
||
1818 | return http_find_file(hs, http_uri_buf, 0); |
||
1819 | } |
||
1820 | } else { |
||
1821 | LWIP_DEBUGF(HTTPD_DEBUG, ("POST received invalid Content-Length: %s\n", |
||
1822 | content_len_num)); |
||
1823 | return ERR_ARG; |
||
1824 | } |
||
1825 | } |
||
1826 | } |
||
1827 | /* If we come here, headers are fully received (double-crlf), but Content-Length |
||
1828 | was not included. Since this is currently the only supported method, we have |
||
1829 | to fail in this case! */ |
||
1830 | LWIP_DEBUGF(HTTPD_DEBUG, ("Error when parsing Content-Length\n")); |
||
1831 | return ERR_ARG; |
||
1832 | } |
||
1833 | /* if we come here, the POST is incomplete */ |
||
1834 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
1835 | return ERR_INPROGRESS; |
||
1836 | #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
1837 | return ERR_ARG; |
||
1838 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
1839 | } |
||
1840 | |||
1841 | #if LWIP_HTTPD_POST_MANUAL_WND |
||
1842 | /** A POST implementation can call this function to update the TCP window. |
||
1843 | * This can be used to throttle data reception (e.g. when received data is |
||
1844 | * programmed to flash and data is received faster than programmed). |
||
1845 | * |
||
1846 | * @param connection A connection handle passed to httpd_post_begin for which |
||
1847 | * httpd_post_finished has *NOT* been called yet! |
||
1848 | * @param recved_len Length of data received (for window update) |
||
1849 | */ |
||
1850 | void httpd_post_data_recved(void *connection, u16_t recved_len) |
||
1851 | { |
||
1852 | struct http_state *hs = (struct http_state *)connection; |
||
1853 | if (hs != NULL) { |
||
1854 | if (hs->no_auto_wnd) { |
||
1855 | u16_t len = recved_len; |
||
1856 | if (hs->unrecved_bytes >= recved_len) { |
||
1857 | hs->unrecved_bytes -= recved_len; |
||
1858 | } else { |
||
1859 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_LEVEL_WARNING, ("httpd_post_data_recved: recved_len too big\n")); |
||
1860 | len = (u16_t)hs->unrecved_bytes; |
||
1861 | hs->unrecved_bytes = 0; |
||
1862 | } |
||
1863 | if (hs->pcb != NULL) { |
||
1864 | if (len != 0) { |
||
1865 | altcp_recved(hs->pcb, len); |
||
1866 | } |
||
1867 | if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) { |
||
1868 | /* finished handling POST */ |
||
1869 | http_handle_post_finished(hs); |
||
1870 | http_send(hs->pcb, hs); |
||
1871 | } |
||
1872 | } |
||
1873 | } |
||
1874 | } |
||
1875 | } |
||
1876 | #endif /* LWIP_HTTPD_POST_MANUAL_WND */ |
||
1877 | |||
1878 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
1879 | |||
1880 | #if LWIP_HTTPD_FS_ASYNC_READ |
||
1881 | /** Try to send more data if file has been blocked before |
||
1882 | * This is a callback function passed to fs_read_async(). |
||
1883 | */ |
||
1884 | static void |
||
1885 | http_continue(void *connection) |
||
1886 | { |
||
1887 | struct http_state *hs = (struct http_state *)connection; |
||
1888 | if (hs && (hs->pcb) && (hs->handle)) { |
||
1889 | LWIP_ASSERT("hs->pcb != NULL", hs->pcb != NULL); |
||
1890 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("httpd_continue: try to send more data\n")); |
||
1891 | if (http_send(hs->pcb, hs)) { |
||
1892 | /* If we wrote anything to be sent, go ahead and send it now. */ |
||
1893 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n")); |
||
1894 | altcp_output(hs->pcb); |
||
1895 | } |
||
1896 | } |
||
1897 | } |
||
1898 | #endif /* LWIP_HTTPD_FS_ASYNC_READ */ |
||
1899 | |||
1900 | /** |
||
1901 | * When data has been received in the correct state, try to parse it |
||
1902 | * as a HTTP request. |
||
1903 | * |
||
1904 | * @param inp the received pbuf |
||
1905 | * @param hs the connection state |
||
1906 | * @param pcb the altcp_pcb which received this packet |
||
1907 | * @return ERR_OK if request was OK and hs has been initialized correctly |
||
1908 | * ERR_INPROGRESS if request was OK so far but not fully received |
||
1909 | * another err_t otherwise |
||
1910 | */ |
||
1911 | static err_t |
||
1912 | http_parse_request(struct pbuf *inp, struct http_state *hs, struct altcp_pcb *pcb) |
||
1913 | { |
||
1914 | char *data; |
||
1915 | char *crlf; |
||
1916 | u16_t data_len; |
||
1917 | struct pbuf *p = inp; |
||
1918 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
1919 | u16_t clen; |
||
1920 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
1921 | #if LWIP_HTTPD_SUPPORT_POST |
||
1922 | err_t err; |
||
1923 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
1924 | |||
1925 | LWIP_UNUSED_ARG(pcb); /* only used for post */ |
||
1926 | LWIP_ASSERT("p != NULL", p != NULL); |
||
1927 | LWIP_ASSERT("hs != NULL", hs != NULL); |
||
1928 | |||
1929 | if ((hs->handle != NULL) || (hs->file != NULL)) { |
||
1930 | LWIP_DEBUGF(HTTPD_DEBUG, ("Received data while sending a file\n")); |
||
1931 | /* already sending a file */ |
||
1932 | /* @todo: abort? */ |
||
1933 | return ERR_USE; |
||
1934 | } |
||
1935 | |||
1936 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
1937 | |||
1938 | LWIP_DEBUGF(HTTPD_DEBUG, ("Received %"U16_F" bytes\n", p->tot_len)); |
||
1939 | |||
1940 | /* first check allowed characters in this pbuf? */ |
||
1941 | |||
1942 | /* enqueue the pbuf */ |
||
1943 | if (hs->req == NULL) { |
||
1944 | LWIP_DEBUGF(HTTPD_DEBUG, ("First pbuf\n")); |
||
1945 | hs->req = p; |
||
1946 | } else { |
||
1947 | LWIP_DEBUGF(HTTPD_DEBUG, ("pbuf enqueued\n")); |
||
1948 | pbuf_cat(hs->req, p); |
||
1949 | } |
||
1950 | /* increase pbuf ref counter as it is freed when we return but we want to |
||
1951 | keep it on the req list */ |
||
1952 | pbuf_ref(p); |
||
1953 | |||
1954 | if (hs->req->next != NULL) { |
||
1955 | data_len = LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH); |
||
1956 | pbuf_copy_partial(hs->req, httpd_req_buf, data_len, 0); |
||
1957 | data = httpd_req_buf; |
||
1958 | } else |
||
1959 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
1960 | { |
||
1961 | data = (char *)p->payload; |
||
1962 | data_len = p->len; |
||
1963 | if (p->len != p->tot_len) { |
||
1964 | LWIP_DEBUGF(HTTPD_DEBUG, ("Warning: incomplete header due to chained pbufs\n")); |
||
1965 | } |
||
1966 | } |
||
1967 | |||
1968 | /* received enough data for minimal request? */ |
||
1969 | if (data_len >= MIN_REQ_LEN) { |
||
1970 | /* wait for CRLF before parsing anything */ |
||
1971 | crlf = lwip_strnstr(data, CRLF, data_len); |
||
1972 | if (crlf != NULL) { |
||
1973 | #if LWIP_HTTPD_SUPPORT_POST |
||
1974 | int is_post = 0; |
||
1975 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
1976 | int is_09 = 0; |
||
1977 | char *sp1, *sp2; |
||
1978 | u16_t left_len, uri_len; |
||
1979 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("CRLF received, parsing request\n")); |
||
1980 | /* parse method */ |
||
1981 | if (!strncmp(data, "GET ", 4)) { |
||
1982 | sp1 = data + 3; |
||
1983 | /* received GET request */ |
||
1984 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received GET request\"\n")); |
||
1985 | #if LWIP_HTTPD_SUPPORT_POST |
||
1986 | } else if (!strncmp(data, "POST ", 5)) { |
||
1987 | /* store request type */ |
||
1988 | is_post = 1; |
||
1989 | sp1 = data + 4; |
||
1990 | /* received GET request */ |
||
1991 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received POST request\n")); |
||
1992 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
1993 | } else { |
||
1994 | /* null-terminate the METHOD (pbuf is freed anyway wen returning) */ |
||
1995 | data[4] = 0; |
||
1996 | /* unsupported method! */ |
||
1997 | LWIP_DEBUGF(HTTPD_DEBUG, ("Unsupported request method (not implemented): \"%s\"\n", |
||
1998 | data)); |
||
1999 | return http_find_error_file(hs, 501); |
||
2000 | } |
||
2001 | /* if we come here, method is OK, parse URI */ |
||
2002 | left_len = (u16_t)(data_len - ((sp1 + 1) - data)); |
||
2003 | sp2 = lwip_strnstr(sp1 + 1, " ", left_len); |
||
2004 | #if LWIP_HTTPD_SUPPORT_V09 |
||
2005 | if (sp2 == NULL) { |
||
2006 | /* HTTP 0.9: respond with correct protocol version */ |
||
2007 | sp2 = lwip_strnstr(sp1 + 1, CRLF, left_len); |
||
2008 | is_09 = 1; |
||
2009 | #if LWIP_HTTPD_SUPPORT_POST |
||
2010 | if (is_post) { |
||
2011 | /* HTTP/0.9 does not support POST */ |
||
2012 | goto badrequest; |
||
2013 | } |
||
2014 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
2015 | } |
||
2016 | #endif /* LWIP_HTTPD_SUPPORT_V09 */ |
||
2017 | uri_len = (u16_t)(sp2 - (sp1 + 1)); |
||
2018 | if ((sp2 != 0) && (sp2 > sp1)) { |
||
2019 | /* wait for CRLFCRLF (indicating end of HTTP headers) before parsing anything */ |
||
2020 | if (lwip_strnstr(data, CRLF CRLF, data_len) != NULL) { |
||
2021 | char *uri = sp1 + 1; |
||
2022 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
2023 | /* This is HTTP/1.0 compatible: for strict 1.1, a connection |
||
2024 | would always be persistent unless "close" was specified. */ |
||
2025 | if (!is_09 && (lwip_strnstr(data, HTTP11_CONNECTIONKEEPALIVE, data_len) || |
||
2026 | lwip_strnstr(data, HTTP11_CONNECTIONKEEPALIVE2, data_len))) { |
||
2027 | hs->keepalive = 1; |
||
2028 | } else { |
||
2029 | hs->keepalive = 0; |
||
2030 | } |
||
2031 | #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
2032 | /* null-terminate the METHOD (pbuf is freed anyway wen returning) */ |
||
2033 | *sp1 = 0; |
||
2034 | uri[uri_len] = 0; |
||
2035 | LWIP_DEBUGF(HTTPD_DEBUG, ("Received \"%s\" request for URI: \"%s\"\n", |
||
2036 | data, uri)); |
||
2037 | #if LWIP_HTTPD_SUPPORT_POST |
||
2038 | if (is_post) { |
||
2039 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
2040 | struct pbuf *q = hs->req; |
||
2041 | #else /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
2042 | struct pbuf *q = inp; |
||
2043 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
2044 | err = http_post_request(q, hs, data, data_len, uri, sp2); |
||
2045 | if (err != ERR_OK) { |
||
2046 | /* restore header for next try */ |
||
2047 | *sp1 = ' '; |
||
2048 | *sp2 = ' '; |
||
2049 | uri[uri_len] = ' '; |
||
2050 | } |
||
2051 | if (err == ERR_ARG) { |
||
2052 | goto badrequest; |
||
2053 | } |
||
2054 | return err; |
||
2055 | } else |
||
2056 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
2057 | { |
||
2058 | return http_find_file(hs, uri, is_09); |
||
2059 | } |
||
2060 | } |
||
2061 | } else { |
||
2062 | LWIP_DEBUGF(HTTPD_DEBUG, ("invalid URI\n")); |
||
2063 | } |
||
2064 | } |
||
2065 | } |
||
2066 | |||
2067 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
2068 | clen = pbuf_clen(hs->req); |
||
2069 | if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) && |
||
2070 | (clen <= LWIP_HTTPD_REQ_QUEUELEN)) { |
||
2071 | /* request not fully received (too short or CRLF is missing) */ |
||
2072 | return ERR_INPROGRESS; |
||
2073 | } else |
||
2074 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
2075 | { |
||
2076 | #if LWIP_HTTPD_SUPPORT_POST |
||
2077 | badrequest: |
||
2078 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
2079 | LWIP_DEBUGF(HTTPD_DEBUG, ("bad request\n")); |
||
2080 | /* could not parse request */ |
||
2081 | return http_find_error_file(hs, 400); |
||
2082 | } |
||
2083 | } |
||
2084 | |||
2085 | /** Try to find the file specified by uri and, if found, initialize hs |
||
2086 | * accordingly. |
||
2087 | * |
||
2088 | * @param hs the connection state |
||
2089 | * @param uri the HTTP header URI |
||
2090 | * @param is_09 1 if the request is HTTP/0.9 (no HTTP headers in response) |
||
2091 | * @return ERR_OK if file was found and hs has been initialized correctly |
||
2092 | * another err_t otherwise |
||
2093 | */ |
||
2094 | static err_t |
||
2095 | http_find_file(struct http_state *hs, const char *uri, int is_09) |
||
2096 | { |
||
2097 | size_t loop; |
||
2098 | struct fs_file *file = NULL; |
||
2099 | char *params = NULL; |
||
2100 | err_t err; |
||
2101 | #if LWIP_HTTPD_CGI |
||
2102 | int i; |
||
2103 | #endif /* LWIP_HTTPD_CGI */ |
||
2104 | #if !LWIP_HTTPD_SSI |
||
2105 | const |
||
2106 | #endif /* !LWIP_HTTPD_SSI */ |
||
2107 | /* By default, assume we will not be processing server-side-includes tags */ |
||
2108 | u8_t tag_check = 0; |
||
2109 | |||
2110 | /* Have we been asked for the default file (in root or a directory) ? */ |
||
2111 | #if LWIP_HTTPD_MAX_REQUEST_URI_LEN |
||
2112 | size_t uri_len = strlen(uri); |
||
2113 | if ((uri_len > 0) && (uri[uri_len - 1] == '/') && |
||
2114 | ((uri != http_uri_buf) || (uri_len == 1))) { |
||
2115 | size_t copy_len = LWIP_MIN(sizeof(http_uri_buf) - 1, uri_len - 1); |
||
2116 | if (copy_len > 0) { |
||
2117 | MEMCPY(http_uri_buf, uri, copy_len); |
||
2118 | http_uri_buf[copy_len] = 0; |
||
2119 | } |
||
2120 | #else /* LWIP_HTTPD_MAX_REQUEST_URI_LEN */ |
||
2121 | if ((uri[0] == '/') && (uri[1] == 0)) { |
||
2122 | #endif /* LWIP_HTTPD_MAX_REQUEST_URI_LEN */ |
||
2123 | /* Try each of the configured default filenames until we find one |
||
2124 | that exists. */ |
||
2125 | for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) { |
||
2126 | const char *file_name; |
||
2127 | #if LWIP_HTTPD_MAX_REQUEST_URI_LEN |
||
2128 | if (copy_len > 0) { |
||
2129 | size_t len_left = sizeof(http_uri_buf) - copy_len - 1; |
||
2130 | if (len_left > 0) { |
||
2131 | size_t name_len = strlen(g_psDefaultFilenames[loop].name); |
||
2132 | size_t name_copy_len = LWIP_MIN(len_left, name_len); |
||
2133 | MEMCPY(&http_uri_buf[copy_len], g_psDefaultFilenames[loop].name, name_copy_len); |
||
2134 | } |
||
2135 | file_name = http_uri_buf; |
||
2136 | } else |
||
2137 | #endif /* LWIP_HTTPD_MAX_REQUEST_URI_LEN */ |
||
2138 | { |
||
2139 | file_name = g_psDefaultFilenames[loop].name; |
||
2140 | } |
||
2141 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Looking for %s...\n", file_name)); |
||
2142 | err = fs_open(&hs->file_handle, file_name); |
||
2143 | if (err == ERR_OK) { |
||
2144 | uri = file_name; |
||
2145 | file = &hs->file_handle; |
||
2146 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opened.\n")); |
||
2147 | #if LWIP_HTTPD_SSI |
||
2148 | tag_check = g_psDefaultFilenames[loop].shtml; |
||
2149 | #endif /* LWIP_HTTPD_SSI */ |
||
2150 | break; |
||
2151 | } |
||
2152 | } |
||
2153 | } |
||
2154 | if (file == NULL) { |
||
2155 | /* No - we've been asked for a specific file. */ |
||
2156 | /* First, isolate the base URI (without any parameters) */ |
||
2157 | params = (char *)strchr(uri, '?'); |
||
2158 | if (params != NULL) { |
||
2159 | /* URI contains parameters. NULL-terminate the base URI */ |
||
2160 | *params = '\0'; |
||
2161 | params++; |
||
2162 | } |
||
2163 | |||
2164 | #if LWIP_HTTPD_CGI |
||
2165 | http_cgi_paramcount = -1; |
||
2166 | /* Does the base URI we have isolated correspond to a CGI handler? */ |
||
2167 | if (g_iNumCGIs && g_pCGIs) { |
||
2168 | for (i = 0; i < g_iNumCGIs; i++) { |
||
2169 | if (strcmp(uri, g_pCGIs[i].pcCGIName) == 0) { |
||
2170 | /* |
||
2171 | * We found a CGI that handles this URI so extract the |
||
2172 | * parameters and call the handler. |
||
2173 | */ |
||
2174 | http_cgi_paramcount = extract_uri_parameters(hs, params); |
||
2175 | uri = g_pCGIs[i].pfnCGIHandler(i, http_cgi_paramcount, hs->params, |
||
2176 | hs->param_vals); |
||
2177 | break; |
||
2178 | } |
||
2179 | } |
||
2180 | } |
||
2181 | #endif /* LWIP_HTTPD_CGI */ |
||
2182 | |||
2183 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opening %s\n", uri)); |
||
2184 | |||
2185 | err = fs_open(&hs->file_handle, uri); |
||
2186 | if (err == ERR_OK) { |
||
2187 | file = &hs->file_handle; |
||
2188 | } else { |
||
2189 | file = http_get_404_file(hs, &uri); |
||
2190 | } |
||
2191 | #if LWIP_HTTPD_SSI |
||
2192 | if (file != NULL) { |
||
2193 | /* See if we have been asked for an shtml file and, if so, |
||
2194 | enable tag checking. */ |
||
2195 | const char *ext = NULL, *sub; |
||
2196 | char *param = (char *)strstr(uri, "?"); |
||
2197 | if (param != NULL) { |
||
2198 | /* separate uri from parameters for now, set back later */ |
||
2199 | *param = 0; |
||
2200 | } |
||
2201 | sub = uri; |
||
2202 | ext = uri; |
||
2203 | for (sub = strstr(sub, "."); sub != NULL; sub = strstr(sub, ".")) { |
||
2204 | ext = sub; |
||
2205 | sub++; |
||
2206 | } |
||
2207 | tag_check = 0; |
||
2208 | for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) { |
||
2209 | if (!lwip_stricmp(ext, g_pcSSIExtensions[loop])) { |
||
2210 | tag_check = 1; |
||
2211 | break; |
||
2212 | } |
||
2213 | } |
||
2214 | if (param != NULL) { |
||
2215 | *param = '?'; |
||
2216 | } |
||
2217 | } |
||
2218 | #endif /* LWIP_HTTPD_SSI */ |
||
2219 | } |
||
2220 | if (file == NULL) { |
||
2221 | /* None of the default filenames exist so send back a 404 page */ |
||
2222 | file = http_get_404_file(hs, &uri); |
||
2223 | } |
||
2224 | return http_init_file(hs, file, is_09, uri, tag_check, params); |
||
2225 | } |
||
2226 | |||
2227 | /** Initialize a http connection with a file to send (if found). |
||
2228 | * Called by http_find_file and http_find_error_file. |
||
2229 | * |
||
2230 | * @param hs http connection state |
||
2231 | * @param file file structure to send (or NULL if not found) |
||
2232 | * @param is_09 1 if the request is HTTP/0.9 (no HTTP headers in response) |
||
2233 | * @param uri the HTTP header URI |
||
2234 | * @param tag_check enable SSI tag checking |
||
2235 | * @param params != NULL if URI has parameters (separated by '?') |
||
2236 | * @return ERR_OK if file was found and hs has been initialized correctly |
||
2237 | * another err_t otherwise |
||
2238 | */ |
||
2239 | static err_t |
||
2240 | http_init_file(struct http_state *hs, struct fs_file *file, int is_09, const char *uri, |
||
2241 | u8_t tag_check, char *params) |
||
2242 | { |
||
2243 | if (file != NULL) { |
||
2244 | /* file opened, initialise struct http_state */ |
||
2245 | #if LWIP_HTTPD_SSI |
||
2246 | if (tag_check) { |
||
2247 | struct http_ssi_state *ssi = http_ssi_state_alloc(); |
||
2248 | if (ssi != NULL) { |
||
2249 | ssi->tag_index = 0; |
||
2250 | ssi->tag_state = TAG_NONE; |
||
2251 | ssi->parsed = file->data; |
||
2252 | ssi->parse_left = file->len; |
||
2253 | ssi->tag_end = file->data; |
||
2254 | hs->ssi = ssi; |
||
2255 | } |
||
2256 | } |
||
2257 | #else /* LWIP_HTTPD_SSI */ |
||
2258 | LWIP_UNUSED_ARG(tag_check); |
||
2259 | #endif /* LWIP_HTTPD_SSI */ |
||
2260 | hs->handle = file; |
||
2261 | hs->file = file->data; |
||
2262 | LWIP_ASSERT("File length must be positive!", (file->len >= 0)); |
||
2263 | #if LWIP_HTTPD_CUSTOM_FILES |
||
2264 | if (file->is_custom_file && (file->data == NULL)) { |
||
2265 | /* custom file, need to read data first (via fs_read_custom) */ |
||
2266 | hs->left = 0; |
||
2267 | } else |
||
2268 | #endif /* LWIP_HTTPD_CUSTOM_FILES */ |
||
2269 | { |
||
2270 | hs->left = (u32_t)file->len; |
||
2271 | } |
||
2272 | hs->retries = 0; |
||
2273 | #if LWIP_HTTPD_TIMING |
||
2274 | hs->time_started = sys_now(); |
||
2275 | #endif /* LWIP_HTTPD_TIMING */ |
||
2276 | #if !LWIP_HTTPD_DYNAMIC_HEADERS |
||
2277 | LWIP_ASSERT("HTTP headers not included in file system", |
||
2278 | (hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) != 0); |
||
2279 | #endif /* !LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
2280 | #if LWIP_HTTPD_SUPPORT_V09 |
||
2281 | if (is_09 && ((hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) != 0)) { |
||
2282 | /* HTTP/0.9 responses are sent without HTTP header, |
||
2283 | search for the end of the header. */ |
||
2284 | char *file_start = lwip_strnstr(hs->file, CRLF CRLF, hs->left); |
||
2285 | if (file_start != NULL) { |
||
2286 | int diff = file_start + 4 - hs->file; |
||
2287 | hs->file += diff; |
||
2288 | hs->left -= (u32_t)diff; |
||
2289 | } |
||
2290 | } |
||
2291 | #endif /* LWIP_HTTPD_SUPPORT_V09*/ |
||
2292 | #if LWIP_HTTPD_CGI_SSI |
||
2293 | if (params != NULL) { |
||
2294 | /* URI contains parameters, call generic CGI handler */ |
||
2295 | int count; |
||
2296 | #if LWIP_HTTPD_CGI |
||
2297 | if (http_cgi_paramcount >= 0) { |
||
2298 | count = http_cgi_paramcount; |
||
2299 | } else |
||
2300 | #endif |
||
2301 | { |
||
2302 | count = extract_uri_parameters(hs, params); |
||
2303 | } |
||
2304 | httpd_cgi_handler(uri, count, http_cgi_params, http_cgi_param_vals |
||
2305 | #if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE |
||
2306 | , hs->handle->state |
||
2307 | #endif /* LWIP_HTTPD_FILE_STATE */ |
||
2308 | ); |
||
2309 | } |
||
2310 | #else /* LWIP_HTTPD_CGI_SSI */ |
||
2311 | LWIP_UNUSED_ARG(params); |
||
2312 | #endif /* LWIP_HTTPD_CGI_SSI */ |
||
2313 | } else { |
||
2314 | hs->handle = NULL; |
||
2315 | hs->file = NULL; |
||
2316 | hs->left = 0; |
||
2317 | hs->retries = 0; |
||
2318 | } |
||
2319 | #if LWIP_HTTPD_DYNAMIC_HEADERS |
||
2320 | /* Determine the HTTP headers to send based on the file extension of |
||
2321 | * the requested URI. */ |
||
2322 | if ((hs->handle == NULL) || ((hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) == 0)) { |
||
2323 | get_http_headers(hs, uri); |
||
2324 | } |
||
2325 | #else /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
2326 | LWIP_UNUSED_ARG(uri); |
||
2327 | #endif /* LWIP_HTTPD_DYNAMIC_HEADERS */ |
||
2328 | #if LWIP_HTTPD_SUPPORT_11_KEEPALIVE |
||
2329 | if (hs->keepalive) { |
||
2330 | #if LWIP_HTTPD_SSI |
||
2331 | if (hs->ssi != NULL) { |
||
2332 | hs->keepalive = 0; |
||
2333 | } else |
||
2334 | #endif /* LWIP_HTTPD_SSI */ |
||
2335 | { |
||
2336 | if ((hs->handle != NULL) && |
||
2337 | ((hs->handle->flags & (FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT)) == FS_FILE_FLAGS_HEADER_INCLUDED)) { |
||
2338 | hs->keepalive = 0; |
||
2339 | } |
||
2340 | } |
||
2341 | } |
||
2342 | #endif /* LWIP_HTTPD_SUPPORT_11_KEEPALIVE */ |
||
2343 | return ERR_OK; |
||
2344 | } |
||
2345 | |||
2346 | /** |
||
2347 | * The pcb had an error and is already deallocated. |
||
2348 | * The argument might still be valid (if != NULL). |
||
2349 | */ |
||
2350 | static void |
||
2351 | http_err(void *arg, err_t err) |
||
2352 | { |
||
2353 | struct http_state *hs = (struct http_state *)arg; |
||
2354 | LWIP_UNUSED_ARG(err); |
||
2355 | |||
2356 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_err: %s", lwip_strerr(err))); |
||
2357 | |||
2358 | if (hs != NULL) { |
||
2359 | http_state_free(hs); |
||
2360 | } |
||
2361 | } |
||
2362 | |||
2363 | /** |
||
2364 | * Data has been sent and acknowledged by the remote host. |
||
2365 | * This means that more data can be sent. |
||
2366 | */ |
||
2367 | static err_t |
||
2368 | http_sent(void *arg, struct altcp_pcb *pcb, u16_t len) |
||
2369 | { |
||
2370 | struct http_state *hs = (struct http_state *)arg; |
||
2371 | |||
2372 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_sent %p\n", (void *)pcb)); |
||
2373 | |||
2374 | LWIP_UNUSED_ARG(len); |
||
2375 | |||
2376 | if (hs == NULL) { |
||
2377 | return ERR_OK; |
||
2378 | } |
||
2379 | |||
2380 | hs->retries = 0; |
||
2381 | |||
2382 | http_send(pcb, hs); |
||
2383 | |||
2384 | return ERR_OK; |
||
2385 | } |
||
2386 | |||
2387 | /** |
||
2388 | * The poll function is called every 2nd second. |
||
2389 | * If there has been no data sent (which resets the retries) in 8 seconds, close. |
||
2390 | * If the last portion of a file has not been sent in 2 seconds, close. |
||
2391 | * |
||
2392 | * This could be increased, but we don't want to waste resources for bad connections. |
||
2393 | */ |
||
2394 | static err_t |
||
2395 | http_poll(void *arg, struct altcp_pcb *pcb) |
||
2396 | { |
||
2397 | struct http_state *hs = (struct http_state *)arg; |
||
2398 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n", |
||
2399 | (void *)pcb, (void *)hs, tcp_debug_state_str(altcp_dbg_get_tcp_state(pcb)))); |
||
2400 | |||
2401 | if (hs == NULL) { |
||
2402 | err_t closed; |
||
2403 | /* arg is null, close. */ |
||
2404 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n")); |
||
2405 | closed = http_close_conn(pcb, NULL); |
||
2406 | LWIP_UNUSED_ARG(closed); |
||
2407 | #if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR |
||
2408 | if (closed == ERR_MEM) { |
||
2409 | altcp_abort(pcb); |
||
2410 | return ERR_ABRT; |
||
2411 | } |
||
2412 | #endif /* LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR */ |
||
2413 | return ERR_OK; |
||
2414 | } else { |
||
2415 | hs->retries++; |
||
2416 | if (hs->retries == HTTPD_MAX_RETRIES) { |
||
2417 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n")); |
||
2418 | http_close_conn(pcb, hs); |
||
2419 | return ERR_OK; |
||
2420 | } |
||
2421 | |||
2422 | /* If this connection has a file open, try to send some more data. If |
||
2423 | * it has not yet received a GET request, don't do this since it will |
||
2424 | * cause the connection to close immediately. */ |
||
2425 | if (hs && (hs->handle)) { |
||
2426 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n")); |
||
2427 | if (http_send(pcb, hs)) { |
||
2428 | /* If we wrote anything to be sent, go ahead and send it now. */ |
||
2429 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n")); |
||
2430 | altcp_output(pcb); |
||
2431 | } |
||
2432 | } |
||
2433 | } |
||
2434 | |||
2435 | return ERR_OK; |
||
2436 | } |
||
2437 | |||
2438 | /** |
||
2439 | * Data has been received on this pcb. |
||
2440 | * For HTTP 1.0, this should normally only happen once (if the request fits in one packet). |
||
2441 | */ |
||
2442 | static err_t |
||
2443 | http_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err) |
||
2444 | { |
||
2445 | struct http_state *hs = (struct http_state *)arg; |
||
2446 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: pcb=%p pbuf=%p err=%s\n", (void *)pcb, |
||
2447 | (void *)p, lwip_strerr(err))); |
||
2448 | |||
2449 | if ((err != ERR_OK) || (p == NULL) || (hs == NULL)) { |
||
2450 | /* error or closed by other side? */ |
||
2451 | if (p != NULL) { |
||
2452 | /* Inform TCP that we have taken the data. */ |
||
2453 | altcp_recved(pcb, p->tot_len); |
||
2454 | pbuf_free(p); |
||
2455 | } |
||
2456 | if (hs == NULL) { |
||
2457 | /* this should not happen, only to be robust */ |
||
2458 | LWIP_DEBUGF(HTTPD_DEBUG, ("Error, http_recv: hs is NULL, close\n")); |
||
2459 | } |
||
2460 | http_close_conn(pcb, hs); |
||
2461 | return ERR_OK; |
||
2462 | } |
||
2463 | |||
2464 | #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND |
||
2465 | if (hs->no_auto_wnd) { |
||
2466 | hs->unrecved_bytes += p->tot_len; |
||
2467 | } else |
||
2468 | #endif /* LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND */ |
||
2469 | { |
||
2470 | /* Inform TCP that we have taken the data. */ |
||
2471 | altcp_recved(pcb, p->tot_len); |
||
2472 | } |
||
2473 | |||
2474 | #if LWIP_HTTPD_SUPPORT_POST |
||
2475 | if (hs->post_content_len_left > 0) { |
||
2476 | /* reset idle counter when POST data is received */ |
||
2477 | hs->retries = 0; |
||
2478 | /* this is data for a POST, pass the complete pbuf to the application */ |
||
2479 | http_post_rxpbuf(hs, p); |
||
2480 | /* pbuf is passed to the application, don't free it! */ |
||
2481 | if (hs->post_content_len_left == 0) { |
||
2482 | /* all data received, send response or close connection */ |
||
2483 | http_send(pcb, hs); |
||
2484 | } |
||
2485 | return ERR_OK; |
||
2486 | } else |
||
2487 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
2488 | { |
||
2489 | if (hs->handle == NULL) { |
||
2490 | err_t parsed = http_parse_request(p, hs, pcb); |
||
2491 | LWIP_ASSERT("http_parse_request: unexpected return value", parsed == ERR_OK |
||
2492 | || parsed == ERR_INPROGRESS || parsed == ERR_ARG || parsed == ERR_USE); |
||
2493 | #if LWIP_HTTPD_SUPPORT_REQUESTLIST |
||
2494 | if (parsed != ERR_INPROGRESS) { |
||
2495 | /* request fully parsed or error */ |
||
2496 | if (hs->req != NULL) { |
||
2497 | pbuf_free(hs->req); |
||
2498 | hs->req = NULL; |
||
2499 | } |
||
2500 | } |
||
2501 | #endif /* LWIP_HTTPD_SUPPORT_REQUESTLIST */ |
||
2502 | pbuf_free(p); |
||
2503 | if (parsed == ERR_OK) { |
||
2504 | #if LWIP_HTTPD_SUPPORT_POST |
||
2505 | if (hs->post_content_len_left == 0) |
||
2506 | #endif /* LWIP_HTTPD_SUPPORT_POST */ |
||
2507 | { |
||
2508 | LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: data %p len %"S32_F"\n", (const void *)hs->file, hs->left)); |
||
2509 | http_send(pcb, hs); |
||
2510 | } |
||
2511 | } else if (parsed == ERR_ARG) { |
||
2512 | /* @todo: close on ERR_USE? */ |
||
2513 | http_close_conn(pcb, hs); |
||
2514 | } |
||
2515 | } else { |
||
2516 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_recv: already sending data\n")); |
||
2517 | /* already sending but still receiving data, we might want to RST here? */ |
||
2518 | pbuf_free(p); |
||
2519 | } |
||
2520 | } |
||
2521 | return ERR_OK; |
||
2522 | } |
||
2523 | |||
2524 | /** |
||
2525 | * A new incoming connection has been accepted. |
||
2526 | */ |
||
2527 | static err_t |
||
2528 | http_accept(void *arg, struct altcp_pcb *pcb, err_t err) |
||
2529 | { |
||
2530 | struct http_state *hs; |
||
2531 | LWIP_UNUSED_ARG(err); |
||
2532 | LWIP_UNUSED_ARG(arg); |
||
2533 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept %p / %p\n", (void *)pcb, arg)); |
||
2534 | |||
2535 | if ((err != ERR_OK) || (pcb == NULL)) { |
||
2536 | return ERR_VAL; |
||
2537 | } |
||
2538 | |||
2539 | /* Set priority */ |
||
2540 | altcp_setprio(pcb, HTTPD_TCP_PRIO); |
||
2541 | |||
2542 | /* Allocate memory for the structure that holds the state of the |
||
2543 | connection - initialized by that function. */ |
||
2544 | hs = http_state_alloc(); |
||
2545 | if (hs == NULL) { |
||
2546 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n")); |
||
2547 | return ERR_MEM; |
||
2548 | } |
||
2549 | hs->pcb = pcb; |
||
2550 | |||
2551 | /* Tell TCP that this is the structure we wish to be passed for our |
||
2552 | callbacks. */ |
||
2553 | altcp_arg(pcb, hs); |
||
2554 | |||
2555 | /* Set up the various callback functions */ |
||
2556 | altcp_recv(pcb, http_recv); |
||
2557 | altcp_err(pcb, http_err); |
||
2558 | altcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL); |
||
2559 | altcp_sent(pcb, http_sent); |
||
2560 | |||
2561 | return ERR_OK; |
||
2562 | } |
||
2563 | |||
2564 | static void |
||
2565 | httpd_init_pcb(struct altcp_pcb *pcb, u16_t port) |
||
2566 | { |
||
2567 | err_t err; |
||
2568 | |||
2569 | if (pcb) { |
||
2570 | altcp_setprio(pcb, HTTPD_TCP_PRIO); |
||
2571 | /* set SOF_REUSEADDR here to explicitly bind httpd to multiple interfaces */ |
||
2572 | err = altcp_bind(pcb, IP_ANY_TYPE, port); |
||
2573 | LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */ |
||
2574 | LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK); |
||
2575 | pcb = altcp_listen(pcb); |
||
2576 | LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL); |
||
2577 | altcp_accept(pcb, http_accept); |
||
2578 | } |
||
2579 | } |
||
2580 | |||
2581 | /** |
||
2582 | * @ingroup httpd |
||
2583 | * Initialize the httpd: set up a listening PCB and bind it to the defined port |
||
2584 | */ |
||
2585 | void |
||
2586 | httpd_init(void) |
||
2587 | { |
||
2588 | struct altcp_pcb *pcb; |
||
2589 | |||
2590 | #if HTTPD_USE_MEM_POOL |
||
2591 | LWIP_MEMPOOL_INIT(HTTPD_STATE); |
||
2592 | #if LWIP_HTTPD_SSI |
||
2593 | LWIP_MEMPOOL_INIT(HTTPD_SSI_STATE); |
||
2594 | #endif |
||
2595 | #endif |
||
2596 | LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n")); |
||
2597 | |||
2598 | pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY); |
||
2599 | LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL); |
||
2600 | httpd_init_pcb(pcb, HTTPD_SERVER_PORT); |
||
2601 | } |
||
2602 | |||
2603 | #if HTTPD_ENABLE_HTTPS |
||
2604 | /** |
||
2605 | * @ingroup httpd |
||
2606 | * Initialize the httpd: set up a listening PCB and bind it to the defined port. |
||
2607 | * Also set up TLS connection handling (HTTPS). |
||
2608 | */ |
||
2609 | void |
||
2610 | httpd_inits(struct altcp_tls_config *conf) |
||
2611 | { |
||
2612 | #if LWIP_ALTCP_TLS |
||
2613 | struct altcp_pcb *pcb_tls; |
||
2614 | struct altcp_pcb *pcb_tcp = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY); |
||
2615 | LWIP_ASSERT("httpd_init: tcp_new failed", pcb_tcp != NULL); |
||
2616 | pcb_tls = altcp_tls_new(conf, pcb_tcp); |
||
2617 | LWIP_ASSERT("httpd_init: altcp_tls_new failed", pcb_tls != NULL); |
||
2618 | httpd_init_pcb(pcb_tls, HTTPD_SERVER_PORT_HTTPS); |
||
2619 | #else /* LWIP_ALTCP_TLS */ |
||
2620 | LWIP_UNUSED_ARG(conf); |
||
2621 | #endif /* LWIP_ALTCP_TLS */ |
||
2622 | } |
||
2623 | #endif /* HTTPD_ENABLE_HTTPS */ |
||
2624 | |||
2625 | #if LWIP_HTTPD_SSI |
||
2626 | /** |
||
2627 | * Set the SSI handler function. |
||
2628 | * |
||
2629 | * @param ssi_handler the SSI handler function |
||
2630 | * @param tags an array of SSI tag strings to search for in SSI-enabled files |
||
2631 | * @param num_tags number of tags in the 'tags' array |
||
2632 | */ |
||
2633 | void |
||
2634 | http_set_ssi_handler(tSSIHandler ssi_handler, const char **tags, int num_tags) |
||
2635 | { |
||
2636 | LWIP_DEBUGF(HTTPD_DEBUG, ("http_set_ssi_handler\n")); |
||
2637 | |||
2638 | LWIP_ASSERT("no ssi_handler given", ssi_handler != NULL); |
||
2639 | g_pfnSSIHandler = ssi_handler; |
||
2640 | |||
2641 | #if LWIP_HTTPD_SSI_RAW |
||
2642 | LWIP_UNUSED_ARG(tags); |
||
2643 | LWIP_UNUSED_ARG(num_tags); |
||
2644 | #else /* LWIP_HTTPD_SSI_RAW */ |
||
2645 | LWIP_ASSERT("no tags given", tags != NULL); |
||
2646 | LWIP_ASSERT("invalid number of tags", num_tags > 0); |
||
2647 | |||
2648 | g_ppcTags = tags; |
||
2649 | g_iNumTags = num_tags; |
||
2650 | #endif /* !LWIP_HTTPD_SSI_RAW */ |
||
2651 | } |
||
2652 | #endif /* LWIP_HTTPD_SSI */ |
||
2653 | |||
2654 | #if LWIP_HTTPD_CGI |
||
2655 | /** |
||
2656 | * Set an array of CGI filenames/handler functions |
||
2657 | * |
||
2658 | * @param cgis an array of CGI filenames/handler functions |
||
2659 | * @param num_handlers number of elements in the 'cgis' array |
||
2660 | */ |
||
2661 | void |
||
2662 | http_set_cgi_handlers(const tCGI *cgis, int num_handlers) |
||
2663 | { |
||
2664 | LWIP_ASSERT("no cgis given", cgis != NULL); |
||
2665 | LWIP_ASSERT("invalid number of handlers", num_handlers > 0); |
||
2666 | |||
2667 | g_pCGIs = cgis; |
||
2668 | g_iNumCGIs = num_handlers; |
||
2669 | } |
||
2670 | #endif /* LWIP_HTTPD_CGI */ |
||
2671 | |||
2672 | #endif /* LWIP_TCP && LWIP_CALLBACK_API */ |