93 #define HTTPD_DEBUG LWIP_DBG_OFF
101 #ifndef HTTPD_USE_MEM_POOL
102 #define HTTPD_USE_MEM_POOL 0
106 #ifndef HTTPD_SERVER_PORT
107 #define HTTPD_SERVER_PORT 80
114 #ifndef HTTPD_MAX_RETRIES
115 #define HTTPD_MAX_RETRIES 4
119 #ifndef HTTPD_POLL_INTERVAL
120 #define HTTPD_POLL_INTERVAL 4
126 #ifndef HTTPD_TCP_PRIO
127 #define HTTPD_TCP_PRIO TCP_PRIO_MIN
131 #ifndef LWIP_HTTPD_TIMING
132 #define LWIP_HTTPD_TIMING 0
134 #ifndef HTTPD_DEBUG_TIMING
135 #define HTTPD_DEBUG_TIMING LWIP_DBG_OFF
139 #ifndef LWIP_HTTPD_STRNSTR_PRIVATE
140 #define LWIP_HTTPD_STRNSTR_PRIVATE 1
145 #ifndef LWIP_HTTPD_SUPPORT_EXTSTATUS
146 #define LWIP_HTTPD_SUPPORT_EXTSTATUS 0
150 #ifndef LWIP_HTTPD_SUPPORT_V09
151 #define LWIP_HTTPD_SUPPORT_V09 1
155 #ifndef LWIP_HTTPD_SUPPORT_REQUESTLIST
156 #define LWIP_HTTPD_SUPPORT_REQUESTLIST 0
159 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
162 #ifndef LWIP_HTTPD_REQ_QUEUELEN
163 #define LWIP_HTTPD_REQ_QUEUELEN 10
168 #ifndef LWIP_HTTPD_REQ_BUFSIZE
169 #define LWIP_HTTPD_REQ_BUFSIZE LWIP_HTTPD_MAX_REQ_LENGTH
175 #ifndef LWIP_HTTPD_MAX_REQ_LENGTH
176 #define LWIP_HTTPD_MAX_REQ_LENGTH 1023
183 #ifndef LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN
184 #define LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN 63
189 #ifndef LWIP_HTTPD_SSI_INCLUDE_TAG
190 #define LWIP_HTTPD_SSI_INCLUDE_TAG 1
196 #ifndef LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
197 #define LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR 0
201 #define true ((u8_t)1)
205 #define false ((u8_t)0)
209 #define MIN_REQ_LEN 7
217 #ifndef HTTP_IS_DATA_VOLATILE
220 #define HTTP_IS_DATA_VOLATILE(hs) ((hs)->tag_check ? TCP_WRITE_FLAG_COPY : 0)
223 #define HTTP_IS_DATA_VOLATILE(hs) (((hs->file != NULL) && (hs->handle != NULL) && (hs->file == \
224 (char*)hs->handle->data + hs->handle->len - hs->left)) \
225 ? 0 : TCP_WRITE_FLAG_COPY)
230 #ifndef HTTP_IS_HDR_VOLATILE
231 #define HTTP_IS_HDR_VOLATILE(hs, ptr) 0
236 #ifndef HTTP_IS_TAG_VOLATILE
237 #define HTTP_IS_TAG_VOLATILE(ptr) TCP_WRITE_FLAG_COPY
247 const default_filename g_psDefaultFilenames[] = {
248 {
"/index.shtml",
true },
249 {
"/index.ssi",
true },
250 {
"/index.shtm",
true },
251 {
"/index.html",
false },
252 {
"/index.htm",
false }
255 #define NUM_DEFAULT_FILENAMES (sizeof(g_psDefaultFilenames) / \
256 sizeof(default_filename))
258 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
260 static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH+1];
263 #if LWIP_HTTPD_SUPPORT_POST
265 static char http_post_response_filename[LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN+1];
268 #if LWIP_HTTPD_DYNAMIC_HEADERS
272 #define NUM_FILE_HDR_STRINGS 3
277 #define HTTPD_LAST_TAG_PART 0xFFFF
279 const char *g_pcSSIExtensions[] = {
280 ".shtml",
".shtm",
".ssi",
".xml"
283 #define NUM_SHTML_EXTENSIONS (sizeof(g_pcSSIExtensions) / sizeof(const char *))
285 enum tag_check_state {
298 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
302 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
310 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
311 const char *tag_started;
316 u16_t tag_insert_len;
317 #if LWIP_HTTPD_SSI_MULTIPART
322 char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1];
323 char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1];
324 enum tag_check_state tag_state;
330 #if LWIP_HTTPD_DYNAMIC_HEADERS
331 const char *hdrs[NUM_FILE_HDR_STRINGS];
336 #if LWIP_HTTPD_TIMING
339 #if LWIP_HTTPD_SUPPORT_POST
340 u32_t post_content_len_left;
341 #if LWIP_HTTPD_POST_MANUAL_WND
342 u32_t unrecved_bytes;
349 static err_t http_find_file(
struct http_state *hs,
const char *uri,
int is_09);
350 static err_t http_init_file(
struct http_state *hs,
struct webfs_file *file,
int is_09,
const char *uri);
351 static err_t http_poll(
void *arg,
struct tcp_pcb *pcb);
355 tSSIHandler g_pfnSSIHandler =
NULL;
357 const char **g_ppcTags =
NULL;
359 #define LEN_TAG_LEAD_IN 5
360 const char *
const g_pcTagLeadIn =
"<!--#";
362 #define LEN_TAG_LEAD_OUT 3
363 const char *
const g_pcTagLeadOut =
"-->";
372 #if LWIP_HTTPD_STRNSTR_PRIVATE
375 strnstr(
const char* buffer,
const char* token,
size_t n)
378 int tokenlen = (int)strlen(token);
380 return (
char *)buffer;
382 for (p = buffer; *p && (p + tokenlen <= buffer + n); p++) {
383 if ((*p == *token) && (strncmp(p, token, tokenlen) == 0)) {
392 static struct http_state*
393 http_state_alloc(
void)
395 struct http_state *ret;
396 #if HTTPD_USE_MEM_POOL
397 ret = (
struct http_state *)
memp_malloc(MEMP_HTTPD_STATE);
399 ret = (
struct http_state *)
mem_malloc(
sizeof(
struct http_state));
403 memset(ret, 0,
sizeof(
struct http_state));
404 #if LWIP_HTTPD_DYNAMIC_HEADERS
406 ret->hdr_index = NUM_FILE_HDR_STRINGS;
416 http_state_free(
struct http_state *hs)
420 #if LWIP_HTTPD_TIMING
424 ms_needed, hs->handle->len, ((((
u32_t)hs->handle->len) * 10) / needed)));
429 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
430 if (hs->buf !=
NULL) {
435 #if HTTPD_USE_MEM_POOL
453 http_write(
struct tcp_pcb *pcb,
const void* ptr,
u16_t *length,
u8_t apiflags)
461 err = tcp_write(pcb, ptr, len, apiflags);
463 if ((tcp_sndbuf(pcb) == 0) ||
471 (
"Send failed, trying less (%d bytes)\n", len));
473 }
while ((err ==
ERR_MEM) && (len > 1));
493 http_close_conn(
struct tcp_pcb *pcb,
struct http_state *hs)
498 #if LWIP_HTTPD_SUPPORT_POST
500 if ((hs->post_content_len_left != 0)
501 #
if LWIP_HTTPD_POST_MANUAL_WND
502 || ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0))
506 http_post_response_filename[0] = 0;
507 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
516 tcp_poll(pcb,
NULL, 0);
522 err = tcp_close(pcb);
526 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
541 extract_uri_parameters(
struct http_state *hs,
char *params)
548 if(!params || (params[0] ==
'\0')) {
560 hs->params[loop] = pair;
567 pair = strchr(pair,
'&');
574 pair = strchr(equals,
' ');
585 equals = strchr(equals,
'=');
588 hs->param_vals[loop] = equals + 1;
590 hs->param_vals[loop] =
NULL;
610 get_tag_insert(
struct http_state *hs)
614 #if LWIP_HTTPD_SSI_MULTIPART
615 u16_t current_tag_part = hs->tag_part;
616 hs->tag_part = HTTPD_LAST_TAG_PART;
619 if(g_pfnSSIHandler && g_ppcTags && g_iNumTags) {
622 for(loop = 0; loop < g_iNumTags; loop++) {
623 if(strcmp(hs->tag_name, g_ppcTags[loop]) == 0) {
624 hs->tag_insert_len = g_pfnSSIHandler(loop, hs->tag_insert,
625 LWIP_HTTPD_MAX_TAG_INSERT_LEN
626 #
if LWIP_HTTPD_SSI_MULTIPART
627 , current_tag_part, &hs->tag_part
629 #
if LWIP_HTTPD_FILE_STATE
641 #define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG "
642 #define UNKNOWN_TAG1_LEN 18
643 #define UNKNOWN_TAG2_TEXT "***</b>"
644 #define UNKNOWN_TAG2_LEN 7
645 len =
LWIP_MIN(strlen(hs->tag_name),
646 LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN));
647 MEMCPY(hs->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN);
648 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN], hs->tag_name, len);
649 MEMCPY(&hs->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN);
650 hs->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0;
652 len = strlen(hs->tag_insert);
654 hs->tag_insert_len = (
u16_t)len;
658 #if LWIP_HTTPD_DYNAMIC_HEADERS
664 get_http_headers(
struct http_state *pState,
char *pszURI)
676 pState->hdrs[1] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER];
680 if (pszURI ==
NULL) {
681 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
682 pState->hdrs[2] = g_psHTTPHeaderStrings[DEFAULT_404_HTML];
685 pState->hdr_index = 0;
693 if (strstr(pszURI,
"404")) {
694 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
695 }
else if (strstr(pszURI,
"400")) {
696 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
697 }
else if (strstr(pszURI,
"501")) {
698 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
700 pState->hdrs[0] = g_psHTTPHeaderStrings[HTTP_HDR_OK];
705 pszVars = strchr(pszURI,
'?');
713 pszWork = strchr(pszURI,
'.');
715 pszExt = pszWork + 1;
716 pszWork = strchr(pszExt,
'.');
720 for(iLoop = 0; (iLoop < NUM_HTTP_HEADERS) && pszExt; iLoop++) {
722 if(!strcmp(g_psHTTPHeaders[iLoop].extension, pszExt)) {
724 g_psHTTPHeaderStrings[g_psHTTPHeaders[iLoop].headerIndex];
741 pState->hdr_index = NUM_FILE_HDR_STRINGS;
744 if(iLoop == NUM_HTTP_HEADERS) {
746 pState->hdrs[2] = g_psHTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE];
750 pState->hdr_index = 0;
763 http_send_data(
struct tcp_pcb *pcb,
struct http_state *hs)
768 u8_t data_to_send =
false;
769 #if LWIP_HTTPD_DYNAMIC_HEADERS
770 u16_t hdrlen, sendlen;
774 (
void*)hs, hs !=
NULL ? hs->left : 0));
776 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
777 if (hs->unrecved_bytes != 0) {
782 #if LWIP_HTTPD_DYNAMIC_HEADERS
792 if(hs->hdr_index < NUM_FILE_HDR_STRINGS) {
794 len = tcp_sndbuf(pcb);
797 while(len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) {
801 hdrlen = (
u16_t)strlen(hs->hdrs[hs->hdr_index]);
804 sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos);
808 ptr = (
const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos);
809 old_sendlen = sendlen;
810 err = http_write(pcb, ptr, &sendlen, HTTP_IS_HDR_VOLATILE(hs, ptr));
811 if ((err ==
ERR_OK) && (old_sendlen != sendlen)) {
814 }
else if (err !=
ERR_OK) {
820 hs->hdr_pos += sendlen;
824 if(hs->hdr_pos == hdrlen) {
835 if((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) {
848 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
853 if (hs->handle ==
NULL) {
855 http_close_conn(pcb, hs);
862 http_close_conn(pcb, hs);
865 #if LWIP_HTTPD_SSI || LWIP_HTTPD_DYNAMIC_HEADERS
872 count = 2 * tcp_mss(pcb);
875 if (hs->buf !=
NULL) {
880 }
while (count > 100);
883 if (hs->buf ==
NULL) {
892 count =
webfs_read(hs->handle, hs->buf, count);
897 http_close_conn(pcb, hs);
906 hs->parse_left = count;
907 hs->parsed = hs->buf;
910 LWIP_ASSERT(
"SSI and DYNAMIC_HEADERS turned off but eof not reached", 0);
922 if (tcp_sndbuf(pcb) < hs->left) {
923 len = tcp_sndbuf(pcb);
925 len = (
u16_t)hs->left;
926 LWIP_ASSERT(
"hs->left did not fit into u16_t!", (len == hs->left));
929 if(len > (2 * mss)) {
933 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
947 len = tcp_sndbuf(pcb);
950 if(hs->parsed > hs->file) {
953 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
954 len = tcp_sndbuf(pcb);
957 (hs->parsed - hs->file) <= 0xffff);
958 len = (
u16_t)(hs->parsed - hs->file);
961 if(len > (2 * mss)) {
965 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
973 if(tcp_sndbuf(pcb) == 0) {
982 while((hs->parse_left) && (err ==
ERR_OK)) {
987 switch(hs->tag_state) {
991 if(*hs->parsed == g_pcTagLeadIn[0]) {
994 hs->tag_state = TAG_LEADIN;
996 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
997 hs->tag_started = hs->parsed;
1011 if(hs->tag_index == LEN_TAG_LEAD_IN) {
1013 hs->tag_state = TAG_FOUND;
1016 if(*hs->parsed == g_pcTagLeadIn[hs->tag_index]) {
1023 hs->tag_state = TAG_NONE;
1038 if((hs->tag_index == 0) && ((*hs->parsed ==
' ') ||
1039 (*hs->parsed ==
'\t') || (*hs->parsed ==
'\n') ||
1040 (*hs->parsed ==
'\r'))) {
1049 if((*hs->parsed == g_pcTagLeadOut[0]) ||
1050 (*hs->parsed ==
' ') || (*hs->parsed ==
'\t') ||
1051 (*hs->parsed ==
'\n') || (*hs->parsed ==
'\r')) {
1053 if(hs->tag_index == 0) {
1055 hs->tag_state = TAG_NONE;
1059 hs->tag_state = TAG_LEADOUT;
1060 LWIP_ASSERT(
"hs->tag_index <= 0xff", hs->tag_index <= 0xff);
1061 hs->tag_name_len = (
u8_t)hs->tag_index;
1062 hs->tag_name[hs->tag_index] =
'\0';
1063 if(*hs->parsed == g_pcTagLeadOut[0]) {
1071 if(hs->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) {
1072 hs->tag_name[hs->tag_index++] = *hs->parsed;
1075 hs->tag_state = TAG_NONE;
1089 if((hs->tag_index == 0) && ((*hs->parsed ==
' ') ||
1090 (*hs->parsed ==
'\t') || (*hs->parsed ==
'\n') ||
1091 (*hs->parsed ==
'\r'))) {
1099 if(*hs->parsed == g_pcTagLeadOut[hs->tag_index]) {
1108 if(hs->tag_index == (LEN_TAG_LEAD_OUT - 1)) {
1111 #if LWIP_HTTPD_SSI_MULTIPART
1120 hs->tag_state = TAG_SENDING;
1121 hs->tag_end = hs->parsed;
1122 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1123 hs->parsed = hs->tag_started;
1128 if (hs->tag_end > hs->file) {
1130 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1131 if(len > hs->tag_end - hs->file) {
1132 len = (
u16_t)(hs->tag_end - hs->file);
1135 if(len > hs->tag_started - hs->file) {
1137 len = (
u16_t)(hs->tag_started - hs->file);
1141 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1143 data_to_send =
true;
1144 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1145 if(hs->tag_started <= hs->file) {
1147 len += hs->tag_end - hs->tag_started;
1162 hs->tag_state = TAG_NONE;
1174 if(hs->tag_end > hs->file) {
1176 #if LWIP_HTTPD_SSI_INCLUDE_TAG
1177 if(len > hs->tag_end - hs->file) {
1178 len = (
u16_t)(hs->tag_end - hs->file);
1181 LWIP_ASSERT(
"hs->started >= hs->file", hs->tag_started >= hs->file);
1182 if (len > hs->tag_started - hs->file) {
1184 len = (
u16_t)(hs->tag_started - hs->file);
1188 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1193 data_to_send =
true;
1194 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1195 if(hs->tag_started <= hs->file) {
1197 len += hs->tag_end - hs->tag_started;
1204 #if LWIP_HTTPD_SSI_MULTIPART
1205 if(hs->tag_index >= hs->tag_insert_len) {
1207 if (hs->tag_part != HTTPD_LAST_TAG_PART) {
1216 if(hs->tag_index < hs->tag_insert_len) {
1219 if(len > (hs->tag_insert_len - hs->tag_index)) {
1220 len = (hs->tag_insert_len - hs->tag_index);
1227 err = http_write(pcb, &(hs->tag_insert[hs->tag_index]), &len,
1228 HTTP_IS_TAG_VOLATILE(hs));
1230 data_to_send =
true;
1231 hs->tag_index += len;
1239 hs->tag_state = TAG_NONE;
1240 #if !LWIP_HTTPD_SSI_INCLUDE_TAG
1241 hs->parsed = hs->tag_end;
1252 if((hs->tag_state != TAG_SENDING) && (hs->parsed > hs->file)) {
1255 if (tcp_sndbuf(pcb) < (hs->parsed - hs->file)) {
1256 len = tcp_sndbuf(pcb);
1259 (hs->parsed - hs->file) <= 0xffff);
1260 len = (
u16_t)(hs->parsed - hs->file);
1262 if(len > (2 * tcp_mss(pcb))) {
1263 len = 2 * tcp_mss(pcb);
1266 err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
1268 data_to_send =
true;
1281 http_close_conn(pcb, hs);
1285 return data_to_send;
1288 #if LWIP_HTTPD_SUPPORT_EXTSTATUS
1297 http_find_error_file(
struct http_state *hs,
u16_t error_nr)
1299 const char *uri1, *uri2, *uri3;
1302 if (error_nr == 501) {
1305 uri3 =
"/501.shtml";
1310 uri3 =
"/400.shtml";
1324 return http_init_file(hs, file, 0,
NULL);
1327 #define http_find_error_file(hs, error_nr) ERR_ARG
1338 http_get_404_file(
const char **uri)
1350 *uri =
"/404.shtml";
1364 #if LWIP_HTTPD_SUPPORT_POST
1366 http_handle_post_finished(
struct http_state *hs)
1370 http_post_response_filename[0] = 0;
1371 httpd_post_finished(hs, http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN);
1372 return http_find_file(hs, http_post_response_filename, 0);
1385 http_post_rxpbuf(
struct http_state *hs,
struct pbuf *p)
1390 if (hs->post_content_len_left < p->
tot_len) {
1391 hs->post_content_len_left = 0;
1393 hs->post_content_len_left -= p->
tot_len;
1395 err = httpd_post_receive_data(hs, p);
1396 if ((err !=
ERR_OK) || (hs->post_content_len_left == 0)) {
1397 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
1398 if (hs->unrecved_bytes != 0) {
1403 return http_handle_post_finished(hs);
1425 http_post_request(
struct tcp_pcb *pcb,
struct pbuf **inp,
struct http_state *hs,
1426 char *
data,
u16_t data_len,
char *uri,
char *uri_end)
1430 char* crlfcrlf = strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data));
1432 #if LWIP_HTTPD_POST_MANUAL_WND
1438 if (crlfcrlf !=
NULL) {
1440 #define HTTP_HDR_CONTENT_LEN "Content-Length: "
1441 #define HTTP_HDR_CONTENT_LEN_LEN 16
1442 #define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10
1443 char *scontent_len = strnstr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1));
1444 if (scontent_len !=
NULL) {
1445 char *scontent_len_end = strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN);
1446 if (scontent_len_end !=
NULL) {
1448 char *conten_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN;
1449 *scontent_len_end = 0;
1450 content_len = atoi(conten_len_num);
1451 if (content_len > 0) {
1453 const char *hdr_start_after_uri = uri_end + 1;
1455 u16_t hdr_data_len =
LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri);
1456 u8_t post_auto_wnd = 1;
1457 http_post_response_filename[0] = 0;
1458 err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len,
1459 http_post_response_filename, LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN, &post_auto_wnd);
1462 struct pbuf *q = *inp;
1463 u16_t start_offset = hdr_len;
1464 #if LWIP_HTTPD_POST_MANUAL_WND
1465 hs->no_auto_wnd = !post_auto_wnd;
1468 hs->post_content_len_left = (
u32_t)content_len;
1471 while((q !=
NULL) && (q->
len <= start_offset)) {
1472 struct pbuf *head = q;
1473 start_offset -= q->
len;
1483 #if LWIP_HTTPD_POST_MANUAL_WND
1484 if (!post_auto_wnd) {
1486 hs->unrecved_bytes = q->
tot_len;
1489 return http_post_rxpbuf(hs, q);
1495 return http_find_file(hs, http_post_response_filename, 0);
1506 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1513 #if LWIP_HTTPD_POST_MANUAL_WND
1522 void httpd_post_data_recved(
void *connection,
u16_t recved_len)
1524 struct http_state *hs = (
struct http_state*)connection;
1526 if (hs->no_auto_wnd) {
1527 u16_t len = recved_len;
1528 if (hs->unrecved_bytes >= recved_len) {
1529 hs->unrecved_bytes -= recved_len;
1532 len = (
u16_t)hs->unrecved_bytes;
1533 hs->unrecved_bytes = 0;
1535 if (hs->pcb !=
NULL) {
1537 tcp_recved(hs->pcb, len);
1539 if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) {
1541 http_handle_post_finished(hs);
1542 http_send_data(hs->pcb, hs);
1564 http_parse_request(
struct pbuf **inp,
struct http_state *hs,
struct tcp_pcb *pcb)
1569 struct pbuf *p = *inp;
1570 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1573 #if LWIP_HTTPD_SUPPORT_POST
1581 if ((hs->handle !=
NULL) || (hs->file !=
NULL)) {
1588 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1595 if (hs->req ==
NULL) {
1603 if (hs->req->next !=
NULL) {
1604 data_len =
LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH);
1606 data = httpd_req_buf;
1618 if (data_len >= MIN_REQ_LEN) {
1620 crlf = strnstr(data, CRLF, data_len);
1622 #if LWIP_HTTPD_SUPPORT_POST
1627 u16_t left_len, uri_len;
1630 if (!strncmp(data,
"GET ", 4)) {
1634 #if LWIP_HTTPD_SUPPORT_POST
1635 }
else if (!strncmp(data,
"POST ", 5)) {
1648 return http_find_error_file(hs, 501);
1651 left_len = data_len - ((sp1 +1) - data);
1652 sp2 = strnstr(sp1 + 1,
" ", left_len);
1653 #if LWIP_HTTPD_SUPPORT_V09
1656 sp2 = strnstr(sp1 + 1, CRLF, left_len);
1658 #if LWIP_HTTPD_SUPPORT_POST
1666 uri_len = sp2 - (sp1 + 1);
1667 if ((sp2 != 0) && (sp2 > sp1)) {
1668 char *uri = sp1 + 1;
1674 #if LWIP_HTTPD_SUPPORT_POST
1676 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1677 struct pbuf **q = &hs->req;
1679 struct pbuf **q = inp;
1681 err = http_post_request(pcb, q, hs, data, data_len, uri, sp2);
1695 return http_find_file(hs, uri, is_09);
1703 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
1705 if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) &&
1706 (clen <= LWIP_HTTPD_REQ_QUEUELEN)) {
1712 #if LWIP_HTTPD_SUPPORT_POST
1717 return http_find_error_file(hs, 400);
1731 http_find_file(
struct http_state *hs,
const char *uri,
int is_09)
1746 hs->tag_check =
false;
1750 if((uri[0] ==
'/') && (uri[1] == 0)) {
1753 for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
1755 file =
webfs_open((
char *)g_psDefaultFilenames[loop].name);
1756 uri = (
char *)g_psDefaultFilenames[loop].name;
1760 hs->tag_check = g_psDefaultFilenames[loop].shtml;
1767 file = http_get_404_file(&uri);
1769 hs->tag_check =
false;
1775 params = (
char *)strchr(uri,
'?');
1776 if (params !=
NULL) {
1784 if (g_iNumCGIs && g_pCGIs) {
1785 for (i = 0; i < g_iNumCGIs; i++) {
1786 if (strcmp(uri, g_pCGIs[i].pcCGIName) == 0) {
1791 count = extract_uri_parameters(hs, params);
1804 file = http_get_404_file(&uri);
1812 hs->tag_check =
false;
1813 for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
1814 if (strstr(uri, g_pcSSIExtensions[loop])) {
1815 hs->tag_check =
true;
1822 return http_init_file(hs, file, is_09, uri);
1836 http_init_file(
struct http_state *hs,
struct webfs_file *file,
int is_09,
const char *uri)
1842 hs->tag_state = TAG_NONE;
1843 hs->parsed = file->
data;
1844 hs->parse_left = file->
len;
1845 hs->tag_end = file->
data;
1848 hs->file = (
char*)file->
data;
1850 hs->left = file->
len;
1852 #if LWIP_HTTPD_TIMING
1855 #if !LWIP_HTTPD_DYNAMIC_HEADERS
1856 LWIP_ASSERT(
"HTTP headers not included in file system", hs->handle->http_header_included);
1858 #if LWIP_HTTPD_SUPPORT_V09
1859 if (hs->handle->http_header_included && is_09) {
1862 char *file_start = strnstr(hs->file, CRLF CRLF, hs->left);
1863 if (file_start !=
NULL) {
1864 size_t diff = file_start + 4 - hs->file;
1866 hs->left -= (
u32_t)diff;
1876 #if LWIP_HTTPD_DYNAMIC_HEADERS
1879 if ((hs->handle ==
NULL) || !hs->handle->http_header_included) {
1880 get_http_headers(hs, (
char*)uri);
1893 http_err(
void *arg,
err_t err)
1895 struct http_state *hs = (
struct http_state *)arg;
1901 http_state_free(hs);
1910 http_sent(
void *arg,
struct tcp_pcb *pcb,
u16_t len)
1912 struct http_state *hs = (
struct http_state *)arg;
1924 http_send_data(pcb, hs);
1937 http_poll(
void *arg,
struct tcp_pcb *pcb)
1939 struct http_state *hs = (
struct http_state *)arg;
1941 (
void*)pcb, (
void*)hs, tcp_debug_state_str(pcb->state)));
1947 closed = http_close_conn(pcb, hs);
1949 #if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
1958 if (hs->retries == HTTPD_MAX_RETRIES) {
1960 http_close_conn(pcb, hs);
1967 if(hs && (hs->handle)) {
1969 if(http_send_data(pcb, hs)) {
1985 http_recv(
void *arg,
struct tcp_pcb *pcb,
struct pbuf *p,
err_t err)
1988 struct http_state *hs = (
struct http_state *)arg;
2003 http_close_conn(pcb, hs);
2007 #if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
2008 if (hs->no_auto_wnd) {
2009 hs->unrecved_bytes += p->
tot_len;
2017 #if LWIP_HTTPD_SUPPORT_POST
2018 if (hs->post_content_len_left > 0) {
2022 http_post_rxpbuf(hs, p);
2024 if (hs->post_content_len_left == 0) {
2026 http_send_data(pcb, hs);
2032 if (hs->handle ==
NULL) {
2033 parsed = http_parse_request(&p, hs, pcb);
2039 #if LWIP_HTTPD_SUPPORT_REQUESTLIST
2042 if (hs->req !=
NULL) {
2054 #if LWIP_HTTPD_SUPPORT_POST
2055 if (hs->post_content_len_left == 0)
2059 http_send_data(pcb, hs);
2061 }
else if (parsed ==
ERR_ARG) {
2063 http_close_conn(pcb, hs);
2073 http_accept(
void *arg,
struct tcp_pcb *pcb,
err_t err)
2075 struct http_state *hs;
2076 struct tcp_pcb_listen *lpcb = (
struct tcp_pcb_listen*)arg;
2083 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2087 hs = http_state_alloc();
2098 tcp_recv(pcb, http_recv);
2099 tcp_err(pcb, http_err);
2100 tcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
2101 tcp_sent(pcb, http_sent);
2112 struct tcp_pcb *pcb;
2117 tcp_setprio(pcb, HTTPD_TCP_PRIO);
2119 err = tcp_bind(pcb, local_addr, HTTPD_SERVER_PORT);
2121 pcb = tcp_listen(pcb);
2125 tcp_accept(pcb, http_accept);
2134 #if HTTPD_USE_MEM_POOL
2135 LWIP_ASSERT(
"memp_sizes[MEMP_HTTPD_STATE] >= sizeof(http_state)",
2136 memp_sizes[MEMP_HTTPD_STATE] >=
sizeof(http_state));
2153 http_set_ssi_handler(tSSIHandler ssi_handler,
const char **tags,
int num_tags)
2159 LWIP_ASSERT(
"invalid number of tags", num_tags > 0);
2161 g_pfnSSIHandler = ssi_handler;
2163 g_iNumTags = num_tags;
2178 LWIP_ASSERT(
"invalid number of handlers", num_handlers > 0);
2181 g_iNumCGIs = num_handlers;