#include "lwip/init.h"
#include "lwip/apps/httpd.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/apps/fs.h"
#include "httpd_structs.h"
#include "lwip/def.h"
#include "lwip/altcp.h"
#include "lwip/altcp_tcp.h"
#if HTTPD_ENABLE_HTTPS
#include "lwip/altcp_tls.h"
#endif
#ifdef LWIP_HOOK_FILENAME
#include LWIP_HOOK_FILENAME
#endif
#if LWIP_HTTPD_TIMING
#include "lwip/sys.h"
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#if LWIP_TCP && LWIP_CALLBACK_API
#define MIN_REQ_LEN 7
#define CRLF "\r\n"
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
#define HTTP11_CONNECTIONKEEPALIVE "Connection: keep-alive"
#define HTTP11_CONNECTIONKEEPALIVE2 "Connection: Keep-Alive"
#endif
#if LWIP_HTTPD_DYNAMIC_FILE_READ
#define HTTP_IS_DYNAMIC_FILE(hs) ((hs)->buf != NULL)
#else
#define HTTP_IS_DYNAMIC_FILE(hs) 0
#endif
#ifndef HTTP_IS_DATA_VOLATILE
#define HTTP_IS_DATA_VOLATILE(hs) (HTTP_IS_DYNAMIC_FILE(hs) ? TCP_WRITE_FLAG_COPY : 0)
#endif
#ifndef HTTP_IS_HDR_VOLATILE
#define HTTP_IS_HDR_VOLATILE(hs, ptr) 0
#endif
#define HTTP_DATA_TO_SEND_FREED 3
#define HTTP_DATA_TO_SEND_BREAK 2
#define HTTP_DATA_TO_SEND_CONTINUE 1
#define HTTP_NO_DATA_TO_SEND 0
typedef struct {
const char *name;
u8_t shtml;
} default_filename;
static const default_filename httpd_default_filenames[] = {
{"/index.shtml", 1 },
{"/index.ssi", 1 },
{"/index.shtm", 1 },
{"/index.html", 0 },
{"/index.htm", 0 }
};
#define NUM_DEFAULT_FILENAMES LWIP_ARRAYSIZE(httpd_default_filenames)
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
static char httpd_req_buf[LWIP_HTTPD_MAX_REQ_LENGTH + 1];
#endif
#if LWIP_HTTPD_SUPPORT_POST
#if LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN > LWIP_HTTPD_MAX_REQUEST_URI_LEN
#define LWIP_HTTPD_URI_BUF_LEN LWIP_HTTPD_POST_MAX_RESPONSE_URI_LEN
#endif
#endif
#ifndef LWIP_HTTPD_URI_BUF_LEN
#define LWIP_HTTPD_URI_BUF_LEN LWIP_HTTPD_MAX_REQUEST_URI_LEN
#endif
#if LWIP_HTTPD_URI_BUF_LEN
static char http_uri_buf[LWIP_HTTPD_URI_BUF_LEN + 1];
#endif
#if LWIP_HTTPD_DYNAMIC_HEADERS
#define NUM_FILE_HDR_STRINGS 5
#define HDR_STRINGS_IDX_HTTP_STATUS 0
#define HDR_STRINGS_IDX_SERVER_NAME 1
#define HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE 2
#define HDR_STRINGS_IDX_CONTENT_LEN_NR 3
#define HDR_STRINGS_IDX_CONTENT_TYPE 4
#define LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET 3
#ifndef LWIP_HTTPD_MAX_CONTENT_LEN_SIZE
#define LWIP_HTTPD_MAX_CONTENT_LEN_SIZE (9 + LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET)
#endif
#endif
#if LWIP_HTTPD_SSI
#define HTTPD_LAST_TAG_PART 0xFFFF
enum tag_check_state {
TAG_NONE,
TAG_LEADIN,
TAG_FOUND,
TAG_LEADOUT,
TAG_SENDING
};
struct http_ssi_state {
const char *parsed;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
const char *tag_started;
#endif
const char *tag_end;
u32_t parse_left;
u16_t tag_index;
u16_t tag_insert_len;
#if LWIP_HTTPD_SSI_MULTIPART
u16_t tag_part;
#endif
u8_t tag_type;
u8_t tag_name_len;
char tag_name[LWIP_HTTPD_MAX_TAG_NAME_LEN + 1];
char tag_insert[LWIP_HTTPD_MAX_TAG_INSERT_LEN + 1];
enum tag_check_state tag_state;
};
struct http_ssi_tag_description {
const char *lead_in;
const char *lead_out;
};
#endif
struct http_state {
#if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
struct http_state *next;
#endif
struct fs_file file_handle;
struct fs_file *handle;
const char *file;
struct altcp_pcb *pcb;
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
struct pbuf *req;
#endif
#if LWIP_HTTPD_DYNAMIC_FILE_READ
char *buf;
int buf_len;
#endif
u32_t left;
u8_t retries;
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
u8_t keepalive;
#endif
#if LWIP_HTTPD_SSI
struct http_ssi_state *ssi;
#endif
#if LWIP_HTTPD_CGI
char *params[LWIP_HTTPD_MAX_CGI_PARAMETERS];
char *param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS];
#endif
#if LWIP_HTTPD_DYNAMIC_HEADERS
const char *hdrs[NUM_FILE_HDR_STRINGS];
char hdr_content_len[LWIP_HTTPD_MAX_CONTENT_LEN_SIZE];
u16_t hdr_pos;
u16_t hdr_index;
#endif
#if LWIP_HTTPD_TIMING
u32_t time_started;
#endif
#if LWIP_HTTPD_SUPPORT_POST
u32_t post_content_len_left;
#if LWIP_HTTPD_POST_MANUAL_WND
u32_t unrecved_bytes;
u8_t no_auto_wnd;
u8_t post_finished;
#endif
#endif
};
#if HTTPD_USE_MEM_POOL
LWIP_MEMPOOL_DECLARE(HTTPD_STATE, MEMP_NUM_PARALLEL_HTTPD_CONNS, sizeof(struct http_state), "HTTPD_STATE")
#if LWIP_HTTPD_SSI
LWIP_MEMPOOL_DECLARE(HTTPD_SSI_STATE, MEMP_NUM_PARALLEL_HTTPD_SSI_CONNS, sizeof(struct http_ssi_state), "HTTPD_SSI_STATE")
#define HTTP_FREE_SSI_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_SSI_STATE, (x))
#define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)LWIP_MEMPOOL_ALLOC(HTTPD_SSI_STATE)
#endif
#define HTTP_ALLOC_HTTP_STATE() (struct http_state *)LWIP_MEMPOOL_ALLOC(HTTPD_STATE)
#define HTTP_FREE_HTTP_STATE(x) LWIP_MEMPOOL_FREE(HTTPD_STATE, (x))
#else
#define HTTP_ALLOC_HTTP_STATE() (struct http_state *)mem_malloc(sizeof(struct http_state))
#define HTTP_FREE_HTTP_STATE(x) mem_free(x)
#if LWIP_HTTPD_SSI
#define HTTP_ALLOC_SSI_STATE() (struct http_ssi_state *)mem_malloc(sizeof(struct http_ssi_state))
#define HTTP_FREE_SSI_STATE(x) mem_free(x)
#endif
#endif
static err_t http_close_conn(struct altcp_pcb *pcb, struct http_state *hs);
static err_t http_close_or_abort_conn(struct altcp_pcb *pcb, struct http_state *hs, u8_t abort_conn);
static err_t http_find_file(struct http_state *hs, const char *uri, int is_09);
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);
static err_t http_poll(void *arg, struct altcp_pcb *pcb);
static u8_t http_check_eof(struct altcp_pcb *pcb, struct http_state *hs);
#if LWIP_HTTPD_FS_ASYNC_READ
static void http_continue(void *connection);
#endif
#if LWIP_HTTPD_SSI
static tSSIHandler httpd_ssi_handler;
#if !LWIP_HTTPD_SSI_RAW
static int httpd_num_tags;
static const char **httpd_tags;
#endif
const struct http_ssi_tag_description http_ssi_tag_desc[] = {
{"<!--#", "-->"},
{"/*#", "*/"}
};
#endif
#if LWIP_HTTPD_CGI
static const tCGI *httpd_cgis;
static int httpd_num_cgis;
static int http_cgi_paramcount;
#define http_cgi_params hs->params
#define http_cgi_param_vals hs->param_vals
#elif LWIP_HTTPD_CGI_SSI
static char *http_cgi_params[LWIP_HTTPD_MAX_CGI_PARAMETERS];
static char *http_cgi_param_vals[LWIP_HTTPD_MAX_CGI_PARAMETERS];
#endif
#if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
static struct http_state *http_connections;
static void
http_add_connection(struct http_state *hs)
{
hs->next = http_connections;
http_connections = hs;
}
static void
http_remove_connection(struct http_state *hs)
{
if (http_connections) {
if (http_connections == hs) {
http_connections = hs->next;
} else {
struct http_state *last;
for (last = http_connections; last->next != NULL; last = last->next) {
if (last->next == hs) {
last->next = hs->next;
break;
}
}
}
}
}
static void
http_kill_oldest_connection(u8_t ssi_required)
{
struct http_state *hs = http_connections;
struct http_state *hs_free_next = NULL;
while (hs && hs->next) {
#if LWIP_HTTPD_SSI
if (ssi_required) {
if (hs->next->ssi != NULL) {
hs_free_next = hs;
}
} else
#else
LWIP_UNUSED_ARG(ssi_required);
#endif
{
hs_free_next = hs;
}
LWIP_ASSERT("broken list", hs != hs->next);
hs = hs->next;
}
if (hs_free_next != NULL) {
LWIP_ASSERT("hs_free_next->next != NULL", hs_free_next->next != NULL);
LWIP_ASSERT("hs_free_next->next->pcb != NULL", hs_free_next->next->pcb != NULL);
http_close_or_abort_conn(hs_free_next->next->pcb, hs_free_next->next, 1);
}
}
#else
#define http_add_connection(hs)
#define http_remove_connection(hs)
#endif
#if LWIP_HTTPD_SSI
static struct http_ssi_state *
http_ssi_state_alloc(void)
{
struct http_ssi_state *ret = HTTP_ALLOC_SSI_STATE();
#if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
if (ret == NULL) {
http_kill_oldest_connection(1);
ret = HTTP_ALLOC_SSI_STATE();
}
#endif
if (ret != NULL) {
memset(ret, 0, sizeof(struct http_ssi_state));
}
return ret;
}
static void
http_ssi_state_free(struct http_ssi_state *ssi)
{
if (ssi != NULL) {
HTTP_FREE_SSI_STATE(ssi);
}
}
#endif
static void
http_state_init(struct http_state *hs)
{
memset(hs, 0, sizeof(struct http_state));
#if LWIP_HTTPD_DYNAMIC_HEADERS
hs->hdr_index = NUM_FILE_HDR_STRINGS;
#endif
}
static struct http_state *
http_state_alloc(void)
{
struct http_state *ret = HTTP_ALLOC_HTTP_STATE();
#if LWIP_HTTPD_KILL_OLD_ON_CONNECTIONS_EXCEEDED
if (ret == NULL) {
http_kill_oldest_connection(0);
ret = HTTP_ALLOC_HTTP_STATE();
}
#endif
if (ret != NULL) {
http_state_init(ret);
http_add_connection(ret);
}
return ret;
}
static void
http_state_close_post(struct http_state* hs)
{
#if LWIP_HTTPD_SUPPORT_POST
if (hs != NULL) {
if ((hs->post_content_len_left != 0)
#if LWIP_HTTPD_POST_MANUAL_WND
|| ((hs->no_auto_wnd != 0) && (hs->unrecved_bytes != 0))
#endif
) {
hs->post_content_len_left = 0;
#if LWIP_HTTPD_POST_MANUAL_WND
hs->unrecved_bytes = 0;
#endif
http_uri_buf[0] = 0;
httpd_post_finished(hs, http_uri_buf, LWIP_HTTPD_URI_BUF_LEN);
}
}
#else
LWIP_UNUSED_ARG(hs);
#endif
}
static void
http_state_eof(struct http_state *hs)
{
if (hs->handle) {
#if LWIP_HTTPD_TIMING
u32_t ms_needed = sys_now() - hs->time_started;
u32_t needed = LWIP_MAX(1, (ms_needed / 100));
LWIP_DEBUGF(HTTPD_DEBUG_TIMING, ("httpd: needed %"U32_F" ms to send file of %d bytes -> %"U32_F" bytes/sec\n",
ms_needed, hs->handle->len, ((((u32_t)hs->handle->len) * 10) / needed)));
#endif
fs_close(hs->handle);
hs->handle = NULL;
}
#if LWIP_HTTPD_DYNAMIC_FILE_READ
if (hs->buf != NULL) {
mem_free(hs->buf);
hs->buf = NULL;
}
#endif
#if LWIP_HTTPD_SSI
if (hs->ssi) {
http_ssi_state_free(hs->ssi);
hs->ssi = NULL;
}
#endif
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
if (hs->req) {
pbuf_free(hs->req);
hs->req = NULL;
}
#endif
http_state_close_post(hs);
}
static void
http_state_free(struct http_state *hs)
{
if (hs != NULL) {
http_state_eof(hs);
http_remove_connection(hs);
HTTP_FREE_HTTP_STATE(hs);
}
}
static err_t
http_write(struct altcp_pcb *pcb, const void *ptr, u16_t *length, u8_t apiflags)
{
u16_t len, max_len;
err_t err;
LWIP_ASSERT("length != NULL", length != NULL);
len = *length;
if (len == 0) {
return ERR_OK;
}
max_len = altcp_sndbuf(pcb);
if (max_len < len) {
len = max_len;
}
#ifdef HTTPD_MAX_WRITE_LEN
max_len = HTTPD_MAX_WRITE_LEN(pcb);
if (len > max_len) {
len = max_len;
}
#endif
do {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Trying to send %d bytes\n", len));
err = altcp_write(pcb, ptr, len, apiflags);
if (err == ERR_MEM) {
if ((altcp_sndbuf(pcb) == 0) ||
(altcp_sndqueuelen(pcb) >= TCP_SND_QUEUELEN)) {
len = 1;
} else {
len /= 2;
}
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE,
("Send failed, trying less (%d bytes)\n", len));
}
} while ((err == ERR_MEM) && (len > 1));
if (err == ERR_OK) {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Sent %d bytes\n", len));
*length = len;
} else {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Send failed with err %d (\"%s\")\n", err, lwip_strerr(err)));
*length = 0;
}
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
altcp_nagle_enable(pcb);
#endif
return err;
}
static err_t
http_close_or_abort_conn(struct altcp_pcb *pcb, struct http_state *hs, u8_t abort_conn)
{
err_t err;
LWIP_DEBUGF(HTTPD_DEBUG, ("Closing connection %p\n", (void *)pcb));
http_state_close_post(hs);
altcp_arg(pcb, NULL);
altcp_recv(pcb, NULL);
altcp_err(pcb, NULL);
altcp_poll(pcb, NULL, 0);
altcp_sent(pcb, NULL);
if (hs != NULL) {
http_state_free(hs);
}
if (abort_conn) {
altcp_abort(pcb);
return ERR_OK;
}
err = altcp_close(pcb);
if (err != ERR_OK) {
LWIP_DEBUGF(HTTPD_DEBUG, ("Error %d closing %p\n", err, (void *)pcb));
altcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
}
return err;
}
static err_t
http_close_conn(struct altcp_pcb *pcb, struct http_state *hs)
{
return http_close_or_abort_conn(pcb, hs, 0);
}
static void
http_eof(struct altcp_pcb *pcb, struct http_state *hs)
{
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
if (hs->keepalive) {
http_remove_connection(hs);
http_state_eof(hs);
http_state_init(hs);
hs->pcb = pcb;
hs->keepalive = 1;
http_add_connection(hs);
altcp_nagle_disable(pcb);
} else
#endif
{
http_close_conn(pcb, hs);
}
}
#if LWIP_HTTPD_CGI || LWIP_HTTPD_CGI_SSI
static int
extract_uri_parameters(struct http_state *hs, char *params)
{
char *pair;
char *equals;
int loop;
LWIP_UNUSED_ARG(hs);
if (!params || (params[0] == '\0')) {
return (0);
}
pair = params;
for (loop = 0; (loop < LWIP_HTTPD_MAX_CGI_PARAMETERS) && pair; loop++) {
http_cgi_params[loop] = pair;
equals = pair;
pair = strchr(pair, '&');
if (pair) {
*pair = '\0';
pair++;
} else {
pair = strchr(equals, ' ');
if (pair) {
*pair = '\0';
}
pair = NULL;
}
equals = strchr(equals, '=');
if (equals) {
*equals = '\0';
http_cgi_param_vals[loop] = equals + 1;
} else {
http_cgi_param_vals[loop] = NULL;
}
}
return loop;
}
#endif
#if LWIP_HTTPD_SSI
static void
get_tag_insert(struct http_state *hs)
{
#if LWIP_HTTPD_SSI_RAW
const char *tag;
#else
int tag;
#endif
size_t len;
struct http_ssi_state *ssi;
#if LWIP_HTTPD_SSI_MULTIPART
u16_t current_tag_part;
#endif
LWIP_ASSERT("hs != NULL", hs != NULL);
ssi = hs->ssi;
LWIP_ASSERT("ssi != NULL", ssi != NULL);
#if LWIP_HTTPD_SSI_MULTIPART
current_tag_part = ssi->tag_part;
ssi->tag_part = HTTPD_LAST_TAG_PART;
#endif
#if LWIP_HTTPD_SSI_RAW
tag = ssi->tag_name;
#endif
if (httpd_ssi_handler
#if !LWIP_HTTPD_SSI_RAW
&& httpd_tags && httpd_num_tags
#endif
) {
#if LWIP_HTTPD_SSI_RAW
{
#else
for (tag = 0; tag < httpd_num_tags; tag++) {
if (strcmp(ssi->tag_name, httpd_tags[tag]) == 0)
#endif
{
ssi->tag_insert_len = httpd_ssi_handler(tag, ssi->tag_insert,
LWIP_HTTPD_MAX_TAG_INSERT_LEN
#if LWIP_HTTPD_SSI_MULTIPART
, current_tag_part, &ssi->tag_part
#endif
#if LWIP_HTTPD_FILE_STATE
, (hs->handle ? hs->handle->state : NULL)
#endif
);
#if LWIP_HTTPD_SSI_RAW
if (ssi->tag_insert_len != HTTPD_SSI_TAG_UNKNOWN)
#endif
{
return;
}
}
}
}
#define UNKNOWN_TAG1_TEXT "<b>***UNKNOWN TAG "
#define UNKNOWN_TAG1_LEN 18
#define UNKNOWN_TAG2_TEXT "***</b>"
#define UNKNOWN_TAG2_LEN 7
len = LWIP_MIN(sizeof(ssi->tag_name), LWIP_MIN(strlen(ssi->tag_name),
LWIP_HTTPD_MAX_TAG_INSERT_LEN - (UNKNOWN_TAG1_LEN + UNKNOWN_TAG2_LEN)));
MEMCPY(ssi->tag_insert, UNKNOWN_TAG1_TEXT, UNKNOWN_TAG1_LEN);
MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN], ssi->tag_name, len);
MEMCPY(&ssi->tag_insert[UNKNOWN_TAG1_LEN + len], UNKNOWN_TAG2_TEXT, UNKNOWN_TAG2_LEN);
ssi->tag_insert[UNKNOWN_TAG1_LEN + len + UNKNOWN_TAG2_LEN] = 0;
len = strlen(ssi->tag_insert);
LWIP_ASSERT("len <= 0xffff", len <= 0xffff);
ssi->tag_insert_len = (u16_t)len;
}
#endif
#if LWIP_HTTPD_DYNAMIC_HEADERS
static void
get_http_headers(struct http_state *hs, const char *uri)
{
size_t content_type;
char *tmp;
char *ext;
char *vars;
hs->hdrs[HDR_STRINGS_IDX_SERVER_NAME] = g_psHTTPHeaderStrings[HTTP_HDR_SERVER];
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] = NULL;
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_NR] = NULL;
if (uri == NULL) {
hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
if (hs->keepalive) {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaderStrings[DEFAULT_404_HTML_PERSISTENT];
} else
#endif
{
hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaderStrings[DEFAULT_404_HTML];
}
hs->hdr_index = 0;
hs->hdr_pos = 0;
return;
}
if (memcmp(uri, "/404.", 5) == 0) {
hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_FOUND];
} else if (memcmp(uri, "/400.", 5) == 0) {
hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_BAD_REQUEST];
} else if (memcmp(uri, "/501.", 5) == 0) {
hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_NOT_IMPL];
} else {
hs->hdrs[HDR_STRINGS_IDX_HTTP_STATUS] = g_psHTTPHeaderStrings[HTTP_HDR_OK];
}
vars = strchr(uri, '?');
if (vars) {
*vars = '\0';
}
ext = NULL;
tmp = strchr(uri, '.');
while (tmp) {
ext = tmp + 1;
tmp = strchr(ext, '.');
}
if (ext != NULL) {
for (content_type = 0; content_type < NUM_HTTP_HEADERS; content_type++) {
if (!lwip_stricmp(g_psHTTPHeaders[content_type].extension, ext)) {
break;
}
}
} else {
content_type = NUM_HTTP_HEADERS;
}
if (vars) {
*vars = '?';
}
#if LWIP_HTTPD_OMIT_HEADER_FOR_EXTENSIONLESS_URI
if (!ext) {
hs->hdr_index = NUM_FILE_HDR_STRINGS;
return;
}
#endif
if (content_type < NUM_HTTP_HEADERS) {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = g_psHTTPHeaders[content_type].content_type;
} else if (!ext) {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = HTTP_HDR_APP;
} else {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_TYPE] = HTTP_HDR_DEFAULT_TYPE;
}
hs->hdr_index = 0;
hs->hdr_pos = 0;
}
static void
get_http_content_length(struct http_state *hs)
{
u8_t add_content_len = 0;
LWIP_ASSERT("already been here?", hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] == NULL);
add_content_len = 0;
#if LWIP_HTTPD_SSI
if (hs->ssi == NULL)
#endif
{
if ((hs->handle != NULL) && (hs->handle->flags & FS_FILE_FLAGS_HEADER_PERSISTENT)) {
add_content_len = 1;
}
}
if (add_content_len) {
size_t len;
lwip_itoa(hs->hdr_content_len, (size_t)LWIP_HTTPD_MAX_CONTENT_LEN_SIZE,
hs->handle->len);
len = strlen(hs->hdr_content_len);
if (len <= LWIP_HTTPD_MAX_CONTENT_LEN_SIZE - LWIP_HTTPD_MAX_CONTENT_LEN_OFFSET) {
SMEMCPY(&hs->hdr_content_len[len], CRLF, 3);
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_NR] = hs->hdr_content_len;
} else {
add_content_len = 0;
}
}
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
if (add_content_len) {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_KEEPALIVE_LEN];
} else {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_CONN_CLOSE];
hs->keepalive = 0;
}
#else
if (add_content_len) {
hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] = g_psHTTPHeaderStrings[HTTP_HDR_CONTENT_LENGTH];
}
#endif
}
static u8_t
http_send_headers(struct altcp_pcb *pcb, struct http_state *hs)
{
err_t err;
u16_t len;
u8_t data_to_send = HTTP_NO_DATA_TO_SEND;
u16_t hdrlen, sendlen;
if (hs->hdrs[HDR_STRINGS_IDX_CONTENT_LEN_KEEPALIVE] == NULL) {
get_http_content_length(hs);
}
len = altcp_sndbuf(pcb);
sendlen = len;
while (len && (hs->hdr_index < NUM_FILE_HDR_STRINGS) && sendlen) {
const void *ptr;
u16_t old_sendlen;
u8_t apiflags;
hdrlen = (u16_t)strlen(hs->hdrs[hs->hdr_index]);
sendlen = (len < (hdrlen - hs->hdr_pos)) ? len : (hdrlen - hs->hdr_pos);
ptr = (const void *)(hs->hdrs[hs->hdr_index] + hs->hdr_pos);
old_sendlen = sendlen;
apiflags = HTTP_IS_HDR_VOLATILE(hs, ptr);
if (hs->hdr_index == HDR_STRINGS_IDX_CONTENT_LEN_NR) {
apiflags |= TCP_WRITE_FLAG_COPY;
}
if (hs->hdr_index < NUM_FILE_HDR_STRINGS - 1) {
apiflags |= TCP_WRITE_FLAG_MORE;
}
err = http_write(pcb, ptr, &sendlen, apiflags);
if ((err == ERR_OK) && (old_sendlen != sendlen)) {
data_to_send = HTTP_DATA_TO_SEND_CONTINUE;
} else if (err != ERR_OK) {
sendlen = 0;
}
hs->hdr_pos += sendlen;
len -= sendlen;
if (hs->hdr_pos == hdrlen) {
hs->hdr_index++;
while ((hs->hdr_index < NUM_FILE_HDR_STRINGS) &&
(hs->hdrs[hs->hdr_index] == NULL)) {
hs->hdr_index++;
}
hs->hdr_pos = 0;
}
}
if ((hs->hdr_index >= NUM_FILE_HDR_STRINGS) && (hs->file == NULL)) {
if (http_check_eof(pcb, hs)) {
data_to_send = HTTP_DATA_TO_SEND_BREAK;
} else {
return HTTP_DATA_TO_SEND_FREED;
}
}
if ((hs->hdr_index < NUM_FILE_HDR_STRINGS) || !hs->file) {
LWIP_DEBUGF(HTTPD_DEBUG, ("tcp_output\n"));
return HTTP_DATA_TO_SEND_BREAK;
}
return data_to_send;
}
#endif
static u8_t
http_check_eof(struct altcp_pcb *pcb, struct http_state *hs)
{
int bytes_left;
#if LWIP_HTTPD_DYNAMIC_FILE_READ
int count;
#ifdef HTTPD_MAX_WRITE_LEN
int max_write_len;
#endif
#endif
if (hs->handle == NULL) {
http_eof(pcb, hs);
return 0;
}
bytes_left = fs_bytes_left(hs->handle);
if (bytes_left <= 0) {
LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
http_eof(pcb, hs);
return 0;
}
#if LWIP_HTTPD_DYNAMIC_FILE_READ
if (hs->buf) {
count = LWIP_MIN(hs->buf_len, bytes_left);
} else {
count = altcp_sndbuf(pcb);
if (bytes_left < count) {
count = bytes_left;
}
#ifdef HTTPD_MAX_WRITE_LEN
max_write_len = HTTPD_MAX_WRITE_LEN(pcb);
if (count > max_write_len) {
count = max_write_len;
}
#endif
do {
hs->buf = (char *)mem_malloc((mem_size_t)count);
if (hs->buf != NULL) {
hs->buf_len = count;
break;
}
count = count / 2;
} while (count > 100);
if (hs->buf == NULL) {
LWIP_DEBUGF(HTTPD_DEBUG, ("No buff\n"));
return 0;
}
}
LWIP_DEBUGF(HTTPD_DEBUG, ("Trying to read %d bytes.\n", count));
#if LWIP_HTTPD_FS_ASYNC_READ
count = fs_read_async(hs->handle, hs->buf, count, http_continue, hs);
#else
count = fs_read(hs->handle, hs->buf, count);
#endif
if (count < 0) {
if (count == FS_READ_DELAYED) {
return 0;
}
LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
http_eof(pcb, hs);
return 0;
}
LWIP_DEBUGF(HTTPD_DEBUG, ("Read %d bytes.\n", count));
hs->left = count;
hs->file = hs->buf;
#if LWIP_HTTPD_SSI
if (hs->ssi) {
hs->ssi->parse_left = count;
hs->ssi->parsed = hs->buf;
}
#endif
#else
LWIP_ASSERT("SSI and DYNAMIC_HEADERS turned off but eof not reached", 0);
#endif
return 1;
}
static u8_t
http_send_data_nonssi(struct altcp_pcb *pcb, struct http_state *hs)
{
err_t err;
u16_t len;
u8_t data_to_send = 0;
len = (u16_t)LWIP_MIN(hs->left, 0xffff);
err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
if (err == ERR_OK) {
data_to_send = 1;
hs->file += len;
hs->left -= len;
}
return data_to_send;
}
#if LWIP_HTTPD_SSI
static u8_t
http_send_data_ssi(struct altcp_pcb *pcb, struct http_state *hs)
{
err_t err = ERR_OK;
u16_t len;
u8_t data_to_send = 0;
u8_t tag_type;
struct http_ssi_state *ssi = hs->ssi;
LWIP_ASSERT("ssi != NULL", ssi != NULL);
len = altcp_sndbuf(pcb);
if (ssi->parsed > hs->file) {
len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff);
err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
if (err == ERR_OK) {
data_to_send = 1;
hs->file += len;
hs->left -= len;
}
if (altcp_sndbuf(pcb) == 0) {
return data_to_send;
}
}
LWIP_DEBUGF(HTTPD_DEBUG, ("State %d, %d left\n", ssi->tag_state, (int)ssi->parse_left));
while (((ssi->tag_state == TAG_SENDING) || ssi->parse_left) && (err == ERR_OK)) {
if (len == 0) {
return data_to_send;
}
switch (ssi->tag_state) {
case TAG_NONE:
for (tag_type = 0; tag_type < LWIP_ARRAYSIZE(http_ssi_tag_desc); tag_type++) {
if (*ssi->parsed == http_ssi_tag_desc[tag_type].lead_in[0]) {
ssi->tag_type = tag_type;
ssi->tag_state = TAG_LEADIN;
ssi->tag_index = 1;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
ssi->tag_started = ssi->parsed;
#endif
break;
}
}
ssi->parse_left--;
ssi->parsed++;
break;
case TAG_LEADIN:
if (http_ssi_tag_desc[ssi->tag_type].lead_in[ssi->tag_index] == 0) {
ssi->tag_index = 0;
ssi->tag_state = TAG_FOUND;
} else {
if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_in[ssi->tag_index]) {
ssi->tag_index++;
} else {
ssi->tag_state = TAG_NONE;
}
#if LWIP_HTTPD_DYNAMIC_FILE_READ && !LWIP_HTTPD_SSI_INCLUDE_TAG
if ((ssi->tag_state == TAG_NONE) &&
(ssi->parsed - hs->file < ssi->tag_index)) {
for(u16_t i = 0;i < ssi->tag_index;i++) {
ssi->tag_insert[i] = http_ssi_tag_desc[ssi->tag_type].lead_in[i];
}
ssi->tag_insert_len = ssi->tag_index;
hs->file += ssi->parsed - hs->file;
hs->left -= ssi->parsed - hs->file;
ssi->tag_end = hs->file;
ssi->tag_index = 0;
ssi->tag_state = TAG_SENDING;
break;
}
#endif
ssi->parse_left--;
ssi->parsed++;
}
break;
case TAG_FOUND:
if ((ssi->tag_index == 0) && ((*ssi->parsed == ' ') ||
(*ssi->parsed == '\t') || (*ssi->parsed == '\n') ||
(*ssi->parsed == '\r'))) {
ssi->parse_left--;
ssi->parsed++;
break;
}
if ((*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[0]) ||
(*ssi->parsed == ' ') || (*ssi->parsed == '\t') ||
(*ssi->parsed == '\n') || (*ssi->parsed == '\r')) {
if (ssi->tag_index == 0) {
ssi->tag_state = TAG_NONE;
} else {
ssi->tag_state = TAG_LEADOUT;
LWIP_ASSERT("ssi->tag_index <= 0xff", ssi->tag_index <= 0xff);
ssi->tag_name_len = (u8_t)ssi->tag_index;
ssi->tag_name[ssi->tag_index] = '\0';
if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[0]) {
ssi->tag_index = 1;
} else {
ssi->tag_index = 0;
}
}
} else {
if (ssi->tag_index < LWIP_HTTPD_MAX_TAG_NAME_LEN) {
ssi->tag_name[ssi->tag_index++] = *ssi->parsed;
} else {
ssi->tag_state = TAG_NONE;
}
}
ssi->parse_left--;
ssi->parsed++;
break;
case TAG_LEADOUT:
if ((ssi->tag_index == 0) && ((*ssi->parsed == ' ') ||
(*ssi->parsed == '\t') || (*ssi->parsed == '\n') ||
(*ssi->parsed == '\r'))) {
ssi->parse_left--;
ssi->parsed++;
break;
}
if (*ssi->parsed == http_ssi_tag_desc[ssi->tag_type].lead_out[ssi->tag_index]) {
ssi->parse_left--;
ssi->parsed++;
ssi->tag_index++;
if (http_ssi_tag_desc[ssi->tag_type].lead_out[ssi->tag_index] == 0) {
#if LWIP_HTTPD_SSI_MULTIPART
ssi->tag_part = 0;
#endif
get_tag_insert(hs);
ssi->tag_index = 0;
ssi->tag_state = TAG_SENDING;
ssi->tag_end = ssi->parsed;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
ssi->parsed = ssi->tag_started;
#endif
if (ssi->tag_end > hs->file) {
#if LWIP_HTTPD_SSI_INCLUDE_TAG
len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff);
#else
len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff);
#endif
err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
if (err == ERR_OK) {
data_to_send = 1;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
if (ssi->tag_started <= hs->file) {
len += (u16_t)(ssi->tag_end - ssi->tag_started);
}
#endif
hs->file += len;
hs->left -= len;
}
}
}
} else {
ssi->parse_left--;
ssi->parsed++;
ssi->tag_state = TAG_NONE;
}
break;
case TAG_SENDING:
if (ssi->tag_end > hs->file) {
#if LWIP_HTTPD_SSI_INCLUDE_TAG
len = (u16_t)LWIP_MIN(ssi->tag_end - hs->file, 0xffff);
#else
LWIP_ASSERT("hs->started >= hs->file", ssi->tag_started >= hs->file);
len = (u16_t)LWIP_MIN(ssi->tag_started - hs->file, 0xffff);
#endif
if (len != 0) {
err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
} else {
err = ERR_OK;
}
if (err == ERR_OK) {
data_to_send = 1;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
if (ssi->tag_started <= hs->file) {
len += (u16_t)(ssi->tag_end - ssi->tag_started);
}
#endif
hs->file += len;
hs->left -= len;
}
} else {
#if LWIP_HTTPD_SSI_MULTIPART
if (ssi->tag_index >= ssi->tag_insert_len) {
if (ssi->tag_part != HTTPD_LAST_TAG_PART) {
ssi->tag_index = 0;
get_tag_insert(hs);
}
}
#endif
if (ssi->tag_index < ssi->tag_insert_len) {
len = (ssi->tag_insert_len - ssi->tag_index);
err = http_write(pcb, &(ssi->tag_insert[ssi->tag_index]), &len,
HTTP_IS_TAG_VOLATILE(hs));
if (err == ERR_OK) {
data_to_send = 1;
ssi->tag_index += len;
}
} else {
#if LWIP_HTTPD_SSI_MULTIPART
if (ssi->tag_part == HTTPD_LAST_TAG_PART)
#endif
{
LWIP_DEBUGF(HTTPD_DEBUG, ("Everything sent.\n"));
ssi->tag_index = 0;
ssi->tag_state = TAG_NONE;
#if !LWIP_HTTPD_SSI_INCLUDE_TAG
ssi->parsed = ssi->tag_end;
#endif
}
}
break;
default:
break;
}
}
}
if ((ssi->tag_state != TAG_SENDING) && (ssi->parsed > hs->file)) {
#if LWIP_HTTPD_DYNAMIC_FILE_READ && !LWIP_HTTPD_SSI_INCLUDE_TAG
if ((ssi->tag_state != TAG_NONE) && (ssi->tag_started > ssi->tag_end)) {
len = (u16_t)(ssi->tag_started - hs->file);
hs->left -= (ssi->parsed - ssi->tag_started);
ssi->parsed = ssi->tag_started;
ssi->tag_started = hs->buf;
} else
#endif
{
len = (u16_t)LWIP_MIN(ssi->parsed - hs->file, 0xffff);
}
err = http_write(pcb, hs->file, &len, HTTP_IS_DATA_VOLATILE(hs));
if (err == ERR_OK) {
data_to_send = 1;
hs->file += len;
hs->left -= len;
}
}
return data_to_send;
}
#endif
static u8_t
http_send(struct altcp_pcb *pcb, struct http_state *hs)
{
u8_t data_to_send = HTTP_NO_DATA_TO_SEND;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_send: pcb=%p hs=%p left=%d\n", (void *)pcb,
(void *)hs, hs != NULL ? (int)hs->left : 0));
#if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
if (hs->unrecved_bytes != 0) {
return 0;
}
#endif
if (hs == NULL) {
return 0;
}
#if LWIP_HTTPD_FS_ASYNC_READ
if (!fs_is_file_ready(hs->handle, http_continue, hs)) {
return 0;
}
#endif
#if LWIP_HTTPD_DYNAMIC_HEADERS
if (hs->hdr_index < NUM_FILE_HDR_STRINGS) {
data_to_send = http_send_headers(pcb, hs);
if ((data_to_send == HTTP_DATA_TO_SEND_FREED) ||
((data_to_send != HTTP_DATA_TO_SEND_CONTINUE) &&
(hs->hdr_index < NUM_FILE_HDR_STRINGS))) {
return data_to_send;
}
}
#endif
#if LWIP_HTTPD_SSI
if (hs->ssi && (hs->ssi->tag_state == TAG_SENDING)) {
} else
#endif
if (hs->left == 0) {
if (!http_check_eof(pcb, hs)) {
return 0;
}
}
#if LWIP_HTTPD_SSI
if (hs->ssi) {
data_to_send = http_send_data_ssi(pcb, hs);
if (hs->ssi->tag_state == TAG_SENDING) {
return data_to_send;
}
} else
#endif
{
data_to_send = http_send_data_nonssi(pcb, hs);
}
if ((hs->left == 0) && (fs_bytes_left(hs->handle) <= 0)) {
LWIP_DEBUGF(HTTPD_DEBUG, ("End of file.\n"));
http_eof(pcb, hs);
return 0;
}
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("send_data end.\n"));
return data_to_send;
}
#if LWIP_HTTPD_SUPPORT_EXTSTATUS
static err_t
http_find_error_file(struct http_state *hs, u16_t error_nr)
{
const char *uri, *uri1, *uri2, *uri3;
if (error_nr == 501) {
uri1 = "/501.html";
uri2 = "/501.htm";
uri3 = "/501.shtml";
} else {
uri1 = "/400.html";
uri2 = "/400.htm";
uri3 = "/400.shtml";
}
if (fs_open(&hs->file_handle, uri1) == ERR_OK) {
uri = uri1;
} else if (fs_open(&hs->file_handle, uri2) == ERR_OK) {
uri = uri2;
} else if (fs_open(&hs->file_handle, uri3) == ERR_OK) {
uri = uri3;
} else {
LWIP_DEBUGF(HTTPD_DEBUG, ("Error page for error %"U16_F" not found\n",
error_nr));
return ERR_ARG;
}
return http_init_file(hs, &hs->file_handle, 0, uri, 0, NULL);
}
#else
#define http_find_error_file(hs, error_nr) ERR_ARG
#endif
static struct fs_file *
http_get_404_file(struct http_state *hs, const char **uri)
{
err_t err;
*uri = "/404.html";
err = fs_open(&hs->file_handle, *uri);
if (err != ERR_OK) {
*uri = "/404.htm";
err = fs_open(&hs->file_handle, *uri);
if (err != ERR_OK) {
*uri = "/404.shtml";
err = fs_open(&hs->file_handle, *uri);
if (err != ERR_OK) {
*uri = NULL;
return NULL;
}
}
}
return &hs->file_handle;
}
#if LWIP_HTTPD_SUPPORT_POST
static err_t
http_handle_post_finished(struct http_state *hs)
{
#if LWIP_HTTPD_POST_MANUAL_WND
if (hs->post_finished) {
return ERR_OK;
}
hs->post_finished = 1;
#endif
http_uri_buf[0] = 0;
httpd_post_finished(hs, http_uri_buf, LWIP_HTTPD_URI_BUF_LEN);
return http_find_file(hs, http_uri_buf, 0);
}
static err_t
http_post_rxpbuf(struct http_state *hs, struct pbuf *p)
{
err_t err;
if (p != NULL) {
if (hs->post_content_len_left < p->tot_len) {
hs->post_content_len_left = 0;
} else {
hs->post_content_len_left -= p->tot_len;
}
}
#if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
hs->unrecved_bytes++;
#endif
if (p != NULL) {
err = httpd_post_receive_data(hs, p);
} else {
err = ERR_OK;
}
#if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
hs->unrecved_bytes--;
#endif
if (err != ERR_OK) {
hs->post_content_len_left = 0;
}
if (hs->post_content_len_left == 0) {
#if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
if (hs->unrecved_bytes != 0) {
return ERR_OK;
}
#endif
return http_handle_post_finished(hs);
}
return ERR_OK;
}
static err_t
http_post_request(struct pbuf *inp, struct http_state *hs,
char *data, u16_t data_len, char *uri, char *uri_end)
{
err_t err;
char *crlfcrlf = lwip_strnstr(uri_end + 1, CRLF CRLF, data_len - (uri_end + 1 - data));
if (crlfcrlf != NULL) {
#define HTTP_HDR_CONTENT_LEN "Content-Length: "
#define HTTP_HDR_CONTENT_LEN_LEN 16
#define HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN 10
char *scontent_len = lwip_strnistr(uri_end + 1, HTTP_HDR_CONTENT_LEN, crlfcrlf - (uri_end + 1));
if (scontent_len != NULL) {
char *scontent_len_end = lwip_strnstr(scontent_len + HTTP_HDR_CONTENT_LEN_LEN, CRLF, HTTP_HDR_CONTENT_LEN_DIGIT_MAX_LEN);
if (scontent_len_end != NULL) {
int content_len;
char *content_len_num = scontent_len + HTTP_HDR_CONTENT_LEN_LEN;
content_len = atoi(content_len_num);
if (content_len == 0) {
if ((content_len_num[0] != '0') || (content_len_num[1] != '\r')) {
content_len = -1;
}
}
if (content_len >= 0) {
const char *hdr_start_after_uri = uri_end + 1;
u16_t hdr_len = (u16_t)LWIP_MIN(data_len, crlfcrlf + 4 - data);
u16_t hdr_data_len = (u16_t)LWIP_MIN(data_len, crlfcrlf + 4 - hdr_start_after_uri);
u8_t post_auto_wnd = 1;
http_uri_buf[0] = 0;
*crlfcrlf = 0;
err = httpd_post_begin(hs, uri, hdr_start_after_uri, hdr_data_len, content_len,
http_uri_buf, LWIP_HTTPD_URI_BUF_LEN, &post_auto_wnd);
if (err == ERR_OK) {
struct pbuf *q = inp;
u16_t start_offset = hdr_len;
#if LWIP_HTTPD_POST_MANUAL_WND
hs->no_auto_wnd = !post_auto_wnd;
#endif
hs->post_content_len_left = (u32_t)content_len;
while ((q != NULL) && (q->len <= start_offset)) {
start_offset -= q->len;
q = q->next;
}
if (q != NULL) {
pbuf_remove_header(q, start_offset);
#if LWIP_HTTPD_POST_MANUAL_WND
if (!post_auto_wnd) {
hs->unrecved_bytes = q->tot_len;
}
#endif
pbuf_ref(q);
return http_post_rxpbuf(hs, q);
} else if (hs->post_content_len_left == 0) {
q = pbuf_alloc(PBUF_RAW, 0, PBUF_REF);
return http_post_rxpbuf(hs, q);
} else {
return ERR_OK;
}
} else {
return http_find_file(hs, http_uri_buf, 0);
}
} else {
LWIP_DEBUGF(HTTPD_DEBUG, ("POST received invalid Content-Length: %s\n",
content_len_num));
return ERR_ARG;
}
}
}
LWIP_DEBUGF(HTTPD_DEBUG, ("Error when parsing Content-Length\n"));
return ERR_ARG;
}
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
return ERR_INPROGRESS;
#else
return ERR_ARG;
#endif
}
#if LWIP_HTTPD_POST_MANUAL_WND
void httpd_post_data_recved(void *connection, u16_t recved_len)
{
struct http_state *hs = (struct http_state *)connection;
if (hs != NULL) {
if (hs->no_auto_wnd) {
u16_t len = recved_len;
if (hs->unrecved_bytes >= recved_len) {
hs->unrecved_bytes -= recved_len;
} else {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_LEVEL_WARNING, ("httpd_post_data_recved: recved_len too big\n"));
len = (u16_t)hs->unrecved_bytes;
hs->unrecved_bytes = 0;
}
if (hs->pcb != NULL) {
if (len != 0) {
altcp_recved(hs->pcb, len);
}
if ((hs->post_content_len_left == 0) && (hs->unrecved_bytes == 0)) {
http_handle_post_finished(hs);
http_send(hs->pcb, hs);
}
}
}
}
}
#endif
#endif
#if LWIP_HTTPD_FS_ASYNC_READ
static void
http_continue(void *connection)
{
struct http_state *hs = (struct http_state *)connection;
LWIP_ASSERT_CORE_LOCKED();
if (hs && (hs->pcb) && (hs->handle)) {
LWIP_ASSERT("hs->pcb != NULL", hs->pcb != NULL);
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("httpd_continue: try to send more data\n"));
if (http_send(hs->pcb, hs)) {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
altcp_output(hs->pcb);
}
}
}
#endif
static err_t
http_parse_request(struct pbuf *inp, struct http_state *hs, struct altcp_pcb *pcb)
{
char *data;
char *crlf;
u16_t data_len;
struct pbuf *p = inp;
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
u16_t clen;
#endif
#if LWIP_HTTPD_SUPPORT_POST
err_t err;
#endif
LWIP_UNUSED_ARG(pcb);
LWIP_ASSERT("p != NULL", p != NULL);
LWIP_ASSERT("hs != NULL", hs != NULL);
if ((hs->handle != NULL) || (hs->file != NULL)) {
LWIP_DEBUGF(HTTPD_DEBUG, ("Received data while sending a file\n"));
return ERR_USE;
}
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
LWIP_DEBUGF(HTTPD_DEBUG, ("Received %"U16_F" bytes\n", p->tot_len));
if (hs->req == NULL) {
LWIP_DEBUGF(HTTPD_DEBUG, ("First pbuf\n"));
hs->req = p;
} else {
LWIP_DEBUGF(HTTPD_DEBUG, ("pbuf enqueued\n"));
pbuf_cat(hs->req, p);
}
pbuf_ref(p);
if (hs->req->next != NULL) {
data_len = LWIP_MIN(hs->req->tot_len, LWIP_HTTPD_MAX_REQ_LENGTH);
pbuf_copy_partial(hs->req, httpd_req_buf, data_len, 0);
data = httpd_req_buf;
} else
#endif
{
data = (char *)p->payload;
data_len = p->len;
if (p->len != p->tot_len) {
LWIP_DEBUGF(HTTPD_DEBUG, ("Warning: incomplete header due to chained pbufs\n"));
}
}
if (data_len >= MIN_REQ_LEN) {
crlf = lwip_strnstr(data, CRLF, data_len);
if (crlf != NULL) {
#if LWIP_HTTPD_SUPPORT_POST
int is_post = 0;
#endif
int is_09 = 0;
char *sp1, *sp2;
u16_t left_len, uri_len;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("CRLF received, parsing request\n"));
if (!strncmp(data, "GET ", 4)) {
sp1 = data + 3;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received GET request\"\n"));
#if LWIP_HTTPD_SUPPORT_POST
} else if (!strncmp(data, "POST ", 5)) {
is_post = 1;
sp1 = data + 4;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Received POST request\n"));
#endif
} else {
data[4] = 0;
LWIP_DEBUGF(HTTPD_DEBUG, ("Unsupported request method (not implemented): \"%s\"\n",
data));
return http_find_error_file(hs, 501);
}
left_len = (u16_t)(data_len - ((sp1 + 1) - data));
sp2 = lwip_strnstr(sp1 + 1, " ", left_len);
#if LWIP_HTTPD_SUPPORT_V09
if (sp2 == NULL) {
sp2 = lwip_strnstr(sp1 + 1, CRLF, left_len);
is_09 = 1;
#if LWIP_HTTPD_SUPPORT_POST
if (is_post) {
goto badrequest;
}
#endif
}
#endif
uri_len = (u16_t)(sp2 - (sp1 + 1));
if ((sp2 != NULL) && (sp2 > sp1)) {
if (lwip_strnstr(data, CRLF CRLF, data_len) != NULL) {
char *uri = sp1 + 1;
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
if (!is_09 && (lwip_strnistr(data, HTTP11_CONNECTIONKEEPALIVE, data_len) ||
lwip_strnistr(data, HTTP11_CONNECTIONKEEPALIVE2, data_len))) {
hs->keepalive = 1;
} else {
hs->keepalive = 0;
}
#endif
*sp1 = 0;
uri[uri_len] = 0;
LWIP_DEBUGF(HTTPD_DEBUG, ("Received \"%s\" request for URI: \"%s\"\n",
data, uri));
#if LWIP_HTTPD_SUPPORT_POST
if (is_post) {
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
struct pbuf *q = hs->req;
#else
struct pbuf *q = inp;
#endif
err = http_post_request(q, hs, data, data_len, uri, sp2);
if (err != ERR_OK) {
*sp1 = ' ';
*sp2 = ' ';
uri[uri_len] = ' ';
}
if (err == ERR_ARG) {
goto badrequest;
}
return err;
} else
#endif
{
return http_find_file(hs, uri, is_09);
}
}
} else {
LWIP_DEBUGF(HTTPD_DEBUG, ("invalid URI\n"));
}
}
}
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
clen = pbuf_clen(hs->req);
if ((hs->req->tot_len <= LWIP_HTTPD_REQ_BUFSIZE) &&
(clen <= LWIP_HTTPD_REQ_QUEUELEN)) {
return ERR_INPROGRESS;
} else
#endif
{
#if LWIP_HTTPD_SUPPORT_POST
badrequest:
#endif
LWIP_DEBUGF(HTTPD_DEBUG, ("bad request\n"));
return http_find_error_file(hs, 400);
}
}
#if LWIP_HTTPD_SSI && (LWIP_HTTPD_SSI_BY_FILE_EXTENSION == 1)
static u8_t
http_uri_is_ssi(struct fs_file *file, const char *uri)
{
size_t loop;
u8_t tag_check = 0;
if (file != NULL) {
const char *ext = NULL, *sub;
char *param = (char *)strstr(uri, "?");
if (param != NULL) {
*param = 0;
}
sub = uri;
ext = uri;
for (sub = strstr(sub, "."); sub != NULL; sub = strstr(sub, ".")) {
ext = sub;
sub++;
}
for (loop = 0; loop < NUM_SHTML_EXTENSIONS; loop++) {
if (!lwip_stricmp(ext, g_pcSSIExtensions[loop])) {
tag_check = 1;
break;
}
}
if (param != NULL) {
*param = '?';
}
}
return tag_check;
}
#endif
static err_t
http_find_file(struct http_state *hs, const char *uri, int is_09)
{
size_t loop;
struct fs_file *file = NULL;
char *params = NULL;
err_t err;
#if LWIP_HTTPD_CGI
int i;
#endif
#if !LWIP_HTTPD_SSI
const
#endif
u8_t tag_check = 0;
#if LWIP_HTTPD_MAX_REQUEST_URI_LEN
size_t uri_len = strlen(uri);
if ((uri_len > 0) && (uri[uri_len - 1] == '/') &&
((uri != http_uri_buf) || (uri_len == 1))) {
size_t copy_len = LWIP_MIN(sizeof(http_uri_buf) - 1, uri_len - 1);
if (copy_len > 0) {
MEMCPY(http_uri_buf, uri, copy_len);
http_uri_buf[copy_len] = 0;
}
#else
if ((uri[0] == '/') && (uri[1] == 0)) {
#endif
for (loop = 0; loop < NUM_DEFAULT_FILENAMES; loop++) {
const char *file_name;
#if LWIP_HTTPD_MAX_REQUEST_URI_LEN
if (copy_len > 0) {
size_t len_left = sizeof(http_uri_buf) - copy_len - 1;
if (len_left > 0) {
size_t name_len = strlen(httpd_default_filenames[loop].name);
size_t name_copy_len = LWIP_MIN(len_left, name_len);
MEMCPY(&http_uri_buf[copy_len], httpd_default_filenames[loop].name, name_copy_len);
http_uri_buf[copy_len + name_copy_len] = 0;
}
file_name = http_uri_buf;
} else
#endif
{
file_name = httpd_default_filenames[loop].name;
}
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Looking for %s...\n", file_name));
err = fs_open(&hs->file_handle, file_name);
if (err == ERR_OK) {
uri = file_name;
file = &hs->file_handle;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opened.\n"));
#if LWIP_HTTPD_SSI
tag_check = httpd_default_filenames[loop].shtml;
#endif
break;
}
}
}
if (file == NULL) {
params = (char *)strchr(uri, '?');
if (params != NULL) {
*params = '\0';
params++;
}
#if LWIP_HTTPD_CGI
http_cgi_paramcount = -1;
if (httpd_num_cgis && httpd_cgis) {
for (i = 0; i < httpd_num_cgis; i++) {
if (strcmp(uri, httpd_cgis[i].pcCGIName) == 0) {
http_cgi_paramcount = extract_uri_parameters(hs, params);
uri = httpd_cgis[i].pfnCGIHandler(i, http_cgi_paramcount, hs->params,
hs->param_vals);
break;
}
}
}
#endif
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("Opening %s\n", uri));
err = fs_open(&hs->file_handle, uri);
if (err == ERR_OK) {
file = &hs->file_handle;
} else {
file = http_get_404_file(hs, &uri);
}
#if LWIP_HTTPD_SSI
if (file != NULL) {
if (file->flags & FS_FILE_FLAGS_SSI) {
tag_check = 1;
} else {
#if LWIP_HTTPD_SSI_BY_FILE_EXTENSION
tag_check = http_uri_is_ssi(file, uri);
#endif
}
}
#endif
}
if (file == NULL) {
file = http_get_404_file(hs, &uri);
}
return http_init_file(hs, file, is_09, uri, tag_check, params);
}
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)
{
#if !LWIP_HTTPD_SUPPORT_V09
LWIP_UNUSED_ARG(is_09);
#endif
if (file != NULL) {
#if !LWIP_HTTPD_DYNAMIC_FILE_READ
LWIP_ASSERT("file->data != NULL", file->data != NULL);
#endif
#if LWIP_HTTPD_SSI
if (tag_check) {
struct http_ssi_state *ssi = http_ssi_state_alloc();
if (ssi != NULL) {
ssi->tag_index = 0;
ssi->tag_state = TAG_NONE;
ssi->parsed = file->data;
ssi->parse_left = file->len;
ssi->tag_end = file->data;
hs->ssi = ssi;
}
}
#else
LWIP_UNUSED_ARG(tag_check);
#endif
hs->handle = file;
#if LWIP_HTTPD_CGI_SSI
if (params != NULL) {
int count;
#if LWIP_HTTPD_CGI
if (http_cgi_paramcount >= 0) {
count = http_cgi_paramcount;
} else
#endif
{
count = extract_uri_parameters(hs, params);
}
httpd_cgi_handler(file, uri, count, http_cgi_params, http_cgi_param_vals
#if defined(LWIP_HTTPD_FILE_STATE) && LWIP_HTTPD_FILE_STATE
, file->state
#endif
);
}
#else
LWIP_UNUSED_ARG(params);
#endif
hs->file = file->data;
LWIP_ASSERT("File length must be positive!", (file->len >= 0));
#if LWIP_HTTPD_CUSTOM_FILES
if (((file->flags & FS_FILE_FLAGS_CUSTOM) != 0) && (file->data == NULL)) {
hs->left = 0;
} else
#endif
{
hs->left = (u32_t)file->len;
}
hs->retries = 0;
#if LWIP_HTTPD_TIMING
hs->time_started = sys_now();
#endif
#if !LWIP_HTTPD_DYNAMIC_HEADERS
LWIP_ASSERT("HTTP headers not included in file system",
(hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) != 0);
#endif
#if LWIP_HTTPD_SUPPORT_V09
if (is_09 && ((hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) != 0)) {
char *file_start = lwip_strnstr(hs->file, CRLF CRLF, hs->left);
if (file_start != NULL) {
size_t diff = file_start + 4 - hs->file;
hs->file += diff;
hs->left -= (u32_t)diff;
}
}
#endif
} else {
hs->handle = NULL;
hs->file = NULL;
hs->left = 0;
hs->retries = 0;
}
#if LWIP_HTTPD_DYNAMIC_HEADERS
if ((hs->handle == NULL) || ((hs->handle->flags & FS_FILE_FLAGS_HEADER_INCLUDED) == 0)) {
get_http_headers(hs, uri);
}
#else
LWIP_UNUSED_ARG(uri);
#endif
#if LWIP_HTTPD_SUPPORT_11_KEEPALIVE
if (hs->keepalive) {
#if LWIP_HTTPD_SSI
if (hs->ssi != NULL) {
hs->keepalive = 0;
} else
#endif
{
if ((hs->handle != NULL) &&
((hs->handle->flags & (FS_FILE_FLAGS_HEADER_INCLUDED | FS_FILE_FLAGS_HEADER_PERSISTENT)) == FS_FILE_FLAGS_HEADER_INCLUDED)) {
hs->keepalive = 0;
}
}
}
#endif
return ERR_OK;
}
static void
http_err(void *arg, err_t err)
{
struct http_state *hs = (struct http_state *)arg;
LWIP_UNUSED_ARG(err);
LWIP_DEBUGF(HTTPD_DEBUG, ("http_err: %s\n", lwip_strerr(err)));
if (hs != NULL) {
http_state_free(hs);
}
}
static err_t
http_sent(void *arg, struct altcp_pcb *pcb, u16_t len)
{
struct http_state *hs = (struct http_state *)arg;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_sent %p\n", (void *)pcb));
LWIP_UNUSED_ARG(len);
if (hs == NULL) {
return ERR_OK;
}
hs->retries = 0;
http_send(pcb, hs);
return ERR_OK;
}
static err_t
http_poll(void *arg, struct altcp_pcb *pcb)
{
struct http_state *hs = (struct http_state *)arg;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: pcb=%p hs=%p pcb_state=%s\n",
(void *)pcb, (void *)hs, tcp_debug_state_str(altcp_dbg_get_tcp_state(pcb))));
if (hs == NULL) {
err_t closed;
LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: arg is NULL, close\n"));
closed = http_close_conn(pcb, NULL);
LWIP_UNUSED_ARG(closed);
#if LWIP_HTTPD_ABORT_ON_CLOSE_MEM_ERROR
if (closed == ERR_MEM) {
altcp_abort(pcb);
return ERR_ABRT;
}
#endif
return ERR_OK;
} else {
hs->retries++;
if (hs->retries == HTTPD_MAX_RETRIES) {
LWIP_DEBUGF(HTTPD_DEBUG, ("http_poll: too many retries, close\n"));
http_close_conn(pcb, hs);
return ERR_OK;
}
if (hs->handle) {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_poll: try to send more data\n"));
if (http_send(pcb, hs)) {
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("tcp_output\n"));
altcp_output(pcb);
}
}
}
return ERR_OK;
}
static err_t
http_recv(void *arg, struct altcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct http_state *hs = (struct http_state *)arg;
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: pcb=%p pbuf=%p err=%s\n", (void *)pcb,
(void *)p, lwip_strerr(err)));
if ((err != ERR_OK) || (p == NULL) || (hs == NULL)) {
if (p != NULL) {
altcp_recved(pcb, p->tot_len);
pbuf_free(p);
}
if (hs == NULL) {
LWIP_DEBUGF(HTTPD_DEBUG, ("Error, http_recv: hs is NULL, close\n"));
}
http_close_conn(pcb, hs);
return ERR_OK;
}
#if LWIP_HTTPD_SUPPORT_POST && LWIP_HTTPD_POST_MANUAL_WND
if (hs->no_auto_wnd) {
hs->unrecved_bytes += p->tot_len;
} else
#endif
{
altcp_recved(pcb, p->tot_len);
}
#if LWIP_HTTPD_SUPPORT_POST
if (hs->post_content_len_left > 0) {
hs->retries = 0;
http_post_rxpbuf(hs, p);
if (hs->post_content_len_left == 0) {
http_send(pcb, hs);
}
return ERR_OK;
} else
#endif
{
if (hs->handle == NULL) {
err_t parsed = http_parse_request(p, hs, pcb);
LWIP_ASSERT("http_parse_request: unexpected return value", parsed == ERR_OK
|| parsed == ERR_INPROGRESS || parsed == ERR_ARG || parsed == ERR_USE);
#if LWIP_HTTPD_SUPPORT_REQUESTLIST
if (parsed != ERR_INPROGRESS) {
if (hs->req != NULL) {
pbuf_free(hs->req);
hs->req = NULL;
}
}
#endif
pbuf_free(p);
if (parsed == ERR_OK) {
#if LWIP_HTTPD_SUPPORT_POST
if (hs->post_content_len_left == 0)
#endif
{
LWIP_DEBUGF(HTTPD_DEBUG | LWIP_DBG_TRACE, ("http_recv: data %p len %"S32_F"\n", (const void *)hs->file, hs->left));
http_send(pcb, hs);
}
} else if (parsed == ERR_ARG) {
http_close_conn(pcb, hs);
}
} else {
LWIP_DEBUGF(HTTPD_DEBUG, ("http_recv: already sending data\n"));
pbuf_free(p);
}
}
return ERR_OK;
}
static err_t
http_accept(void *arg, struct altcp_pcb *pcb, err_t err)
{
struct http_state *hs;
LWIP_UNUSED_ARG(err);
LWIP_UNUSED_ARG(arg);
LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept %p / %p\n", (void *)pcb, arg));
if ((err != ERR_OK) || (pcb == NULL)) {
return ERR_VAL;
}
altcp_setprio(pcb, HTTPD_TCP_PRIO);
hs = http_state_alloc();
if (hs == NULL) {
LWIP_DEBUGF(HTTPD_DEBUG, ("http_accept: Out of memory, RST\n"));
return ERR_MEM;
}
hs->pcb = pcb;
altcp_arg(pcb, hs);
altcp_recv(pcb, http_recv);
altcp_err(pcb, http_err);
altcp_poll(pcb, http_poll, HTTPD_POLL_INTERVAL);
altcp_sent(pcb, http_sent);
return ERR_OK;
}
static void
httpd_init_pcb(struct altcp_pcb *pcb, u16_t port)
{
err_t err;
if (pcb) {
altcp_setprio(pcb, HTTPD_TCP_PRIO);
err = altcp_bind(pcb, IP_ANY_TYPE, port);
LWIP_UNUSED_ARG(err);
LWIP_ASSERT("httpd_init: tcp_bind failed", err == ERR_OK);
pcb = altcp_listen(pcb);
LWIP_ASSERT("httpd_init: tcp_listen failed", pcb != NULL);
altcp_accept(pcb, http_accept);
}
}
void
httpd_init(void)
{
struct altcp_pcb *pcb;
#if HTTPD_USE_MEM_POOL
LWIP_MEMPOOL_INIT(HTTPD_STATE);
#if LWIP_HTTPD_SSI
LWIP_MEMPOOL_INIT(HTTPD_SSI_STATE);
#endif
#endif
LWIP_DEBUGF(HTTPD_DEBUG, ("httpd_init\n"));
pcb = altcp_tcp_new_ip_type(IPADDR_TYPE_ANY);
LWIP_ASSERT("httpd_init: tcp_new failed", pcb != NULL);
httpd_init_pcb(pcb, HTTPD_SERVER_PORT);
}
#if HTTPD_ENABLE_HTTPS
void
httpd_inits(struct altcp_tls_config *conf)
{
#if LWIP_ALTCP_TLS
struct altcp_pcb *pcb_tls = altcp_tls_new(conf, IPADDR_TYPE_ANY);
LWIP_ASSERT("httpd_init: altcp_tls_new failed", pcb_tls != NULL);
httpd_init_pcb(pcb_tls, HTTPD_SERVER_PORT_HTTPS);
#else
LWIP_UNUSED_ARG(conf);
#endif
}
#endif
#if LWIP_HTTPD_SSI
void
http_set_ssi_handler(tSSIHandler ssi_handler, const char **tags, int num_tags)
{
LWIP_DEBUGF(HTTPD_DEBUG, ("http_set_ssi_handler\n"));
LWIP_ASSERT("no ssi_handler given", ssi_handler != NULL);
httpd_ssi_handler = ssi_handler;
#if LWIP_HTTPD_SSI_RAW
LWIP_UNUSED_ARG(tags);
LWIP_UNUSED_ARG(num_tags);
#else
LWIP_ASSERT("no tags given", tags != NULL);
LWIP_ASSERT("invalid number of tags", num_tags > 0);
httpd_tags = tags;
httpd_num_tags = num_tags;
#endif
}
#endif
#if LWIP_HTTPD_CGI
void
http_set_cgi_handlers(const tCGI *cgis, int num_handlers)
{
LWIP_ASSERT("no cgis given", cgis != NULL);
LWIP_ASSERT("invalid number of handlers", num_handlers > 0);
httpd_cgis = cgis;
httpd_num_cgis = num_handlers;
}
#endif
#endif