Add config options in menuconfig to enable/disable https and examples

parent baf849bf
menu "HTTPD Server"
choice HTTPS_SERVER_MODE
prompt "http or https"
default HTTPS_SERVER_DISABLED
help
Wheter the http server is secure or not
config HTTPS_SERVER_ENABLED
bool "Https enabled"
config HTTPS_SERVER_DISABLED
bool "Https disabled"
endchoice
config HTTPS_SERVER
bool
default y if HTTPS_SERVER_ENABLED
default n if HTTPS_SERVER_DISABLED
config HTTPD_EXAMPLE
bool "Enable HTTP POST/GET Example functions"
default n
...@@ -51,6 +51,8 @@ ...@@ -51,6 +51,8 @@
#define MBEDTLS_EXAMPLE_RECV_BUF_LEN 1024 #define MBEDTLS_EXAMPLE_RECV_BUF_LEN 1024
#define HTTPD_EXAMPLE CONFIG_HTTPD_EXAMPLE
typedef enum { typedef enum {
HTTP_PARSING_URI, //!< HTTP_PARSING_URI HTTP_PARSING_URI, //!< HTTP_PARSING_URI
HTTP_PARSING_HEADER_NAME, //!< HTTP_PARSING_HEADER_NAME HTTP_PARSING_HEADER_NAME, //!< HTTP_PARSING_HEADER_NAME
...@@ -102,7 +104,7 @@ struct http_context_ { ...@@ -102,7 +104,7 @@ struct http_context_ {
const char* data_ptr; const char* data_ptr;
size_t data_size; size_t data_size;
http_header_list_t request_args; http_header_list_t request_args;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
mbedtls_ssl_context *ssl_conn; mbedtls_ssl_context *ssl_conn;
mbedtls_net_context *client_fd; mbedtls_net_context *client_fd;
#else #else
...@@ -118,7 +120,7 @@ struct http_server_context_ { ...@@ -118,7 +120,7 @@ struct http_server_context_ {
SLIST_HEAD(, http_handler_t) handlers; SLIST_HEAD(, http_handler_t) handlers;
_lock_t handlers_lock; _lock_t handlers_lock;
struct http_context_ connection_context; struct http_context_ connection_context;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
mbedtls_net_context *listen_fd; mbedtls_net_context *listen_fd;
mbedtls_entropy_context *entropy; mbedtls_entropy_context *entropy;
mbedtls_ctr_drbg_context *ctr_drbg; mbedtls_ctr_drbg_context *ctr_drbg;
...@@ -552,7 +554,7 @@ static esp_err_t http_send_response_headers(http_context_t http_ctx) ...@@ -552,7 +554,7 @@ static esp_err_t http_send_response_headers(http_context_t http_ctx)
headers_list_clear(&http_ctx->response_headers); headers_list_clear(&http_ctx->response_headers);
#ifdef HTTPS_SERVER #if HTTPS_SERVER
int ret; int ret;
int actual_len; int actual_len;
ESP_LOGI(TAG, "Writing response headers..." ); ESP_LOGI(TAG, "Writing response headers..." );
...@@ -636,7 +638,7 @@ esp_err_t http_response_write(http_context_t http_ctx, const http_buffer_t* buff ...@@ -636,7 +638,7 @@ esp_err_t http_response_write(http_context_t http_ctx, const http_buffer_t* buff
} }
} }
len = buffer->size ? buffer->size : strlen((const char*) buffer->data); len = buffer->size ? buffer->size : strlen((const char*) buffer->data);
#ifdef HTTPS_SERVER #if HTTPS_SERVER
ESP_LOGI(TAG, "Writing to client:" ); ESP_LOGI(TAG, "Writing to client:" );
ret = 0; ret = 0;
do do
...@@ -776,7 +778,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn) ...@@ -776,7 +778,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn)
/* Initialize context */ /* Initialize context */
ctx->state = HTTP_PARSING_URI; ctx->state = HTTP_PARSING_URI;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
#else #else
struct netbuf *inbuf = NULL; struct netbuf *inbuf = NULL;
u16_t buflen; u16_t buflen;
...@@ -796,7 +798,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn) ...@@ -796,7 +798,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn)
.on_message_complete = &http_message_done_cb .on_message_complete = &http_message_done_cb
}; };
#ifdef HTTPS_SERVER #if HTTPS_SERVER
int ret; int ret;
size_t parsed_bytes = 0; size_t parsed_bytes = 0;
/* /*
...@@ -860,7 +862,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn) ...@@ -860,7 +862,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn)
} }
#endif #endif
#ifdef HTTPS_SERVER #if HTTPS_SERVER
if (ret > 0) { if (ret > 0) {
ctx->state = HTTP_COLLECTING_RESPONSE_HEADERS; ctx->state = HTTP_COLLECTING_RESPONSE_HEADERS;
if (ctx->handler == NULL) { if (ctx->handler == NULL) {
...@@ -891,7 +893,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn) ...@@ -891,7 +893,7 @@ static void http_handle_connection(http_server_t server, void *arg_conn)
ctx->uri = NULL; ctx->uri = NULL;
ctx->handler = NULL; ctx->handler = NULL;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
ESP_LOGI(TAG, "Closing the connection..." ); ESP_LOGI(TAG, "Closing the connection..." );
while( ( ret = mbedtls_ssl_close_notify( server->connection_context.ssl_conn) ) < 0 ) while( ( ret = mbedtls_ssl_close_notify( server->connection_context.ssl_conn) ) < 0 )
{ {
...@@ -934,7 +936,7 @@ static void http_server(void *arg) ...@@ -934,7 +936,7 @@ static void http_server(void *arg)
//If server has not successfully been started yet, //If server has not successfully been started yet,
if (!(bits & SERVER_STARTED_BIT)) { if (!(bits & SERVER_STARTED_BIT)) {
#ifdef HTTPS_SERVER #if HTTPS_SERVER
char *error_buf; char *error_buf;
ESP_LOGV(TAG, "Declaring local mbedTLS context on task..."); ESP_LOGV(TAG, "Declaring local mbedTLS context on task...");
int ret; int ret;
...@@ -1291,7 +1293,7 @@ esp_err_t http_server_start(const http_server_options_t* options, http_server_t* ...@@ -1291,7 +1293,7 @@ esp_err_t http_server_start(const http_server_options_t* options, http_server_t*
esp_err_t http_server_stop(http_server_t server) esp_err_t http_server_stop(http_server_t server)
{ {
/* FIXME: figure out a thread safe way to do this */ /* FIXME: figure out a thread safe way to do this */
#ifdef HTTPS_SERVER #if HTTPS_SERVER
/* FIXME: Add function to stop HTTPS */ /* FIXME: Add function to stop HTTPS */
#else #else
netconn_close(server->server_conn); netconn_close(server->server_conn);
...@@ -1301,6 +1303,8 @@ esp_err_t http_server_stop(http_server_t server) ...@@ -1301,6 +1303,8 @@ esp_err_t http_server_stop(http_server_t server)
return ESP_OK; return ESP_OK;
} }
#if HTTPD_EXAMPLE
static void cb_GET_method(http_context_t http_ctx, void* ctx) static void cb_GET_method(http_context_t http_ctx, void* ctx)
{ {
size_t response_size = strlen(index_html); size_t response_size = strlen(index_html);
...@@ -1313,7 +1317,7 @@ static void cb_GET_method(http_context_t http_ctx, void* ctx) ...@@ -1313,7 +1317,7 @@ static void cb_GET_method(http_context_t http_ctx, void* ctx)
esp_err_t simple_GET_method_example(void) esp_err_t simple_GET_method_example(void)
{ {
http_server_t server; http_server_t server;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
http_server_options_t http_options = HTTPS_SERVER_OPTIONS_DEFAULT(); http_server_options_t http_options = HTTPS_SERVER_OPTIONS_DEFAULT();
#else #else
http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT(); http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT();
...@@ -1361,7 +1365,7 @@ static void cb_POST_method(http_context_t http_ctx, void* ctx) ...@@ -1361,7 +1365,7 @@ static void cb_POST_method(http_context_t http_ctx, void* ctx)
esp_err_t simple_POST_method_example(void) esp_err_t simple_POST_method_example(void)
{ {
http_server_t server; http_server_t server;
#ifdef HTTPS_SERVER #if HTTPS_SERVER
http_server_options_t http_options = HTTPS_SERVER_OPTIONS_DEFAULT(); http_server_options_t http_options = HTTPS_SERVER_OPTIONS_DEFAULT();
#else #else
http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT(); http_server_options_t http_options = HTTP_SERVER_OPTIONS_DEFAULT();
...@@ -1380,3 +1384,4 @@ esp_err_t simple_POST_method_example(void) ...@@ -1380,3 +1384,4 @@ esp_err_t simple_POST_method_example(void)
return res; return res;
} }
#endif // HTTPD_EXAMPLE
...@@ -43,8 +43,10 @@ extern "C" { ...@@ -43,8 +43,10 @@ extern "C" {
/** Error buffer length */ /** Error buffer length */
#define ERROR_BUF_LENGTH 100 #define ERROR_BUF_LENGTH 100
/** Uncomment to enable secure server */ #define HTTPS_SERVER CONFIG_HTTPS_SERVER
#define HTTPS_SERVER
#define HTTPD_EXAMPLE CONFIG_HTTPD_EXAMPLE
/** Opaque type representing single HTTP connection */ /** Opaque type representing single HTTP connection */
typedef struct http_context_* http_context_t; typedef struct http_context_* http_context_t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment