Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions components/esp_http_server/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,16 @@ menu "HTTP Server"
It internally uses a counting semaphore with count set to `LWIP_UDP_RECVMBOX_SIZE` to achieve this.
This config will slightly change API behavior to block until message gets delivered on control socket.

config HTTPD_ENABLE_EVENTS
bool "Enable ESP_HTTP_SERVER_EVENT"
default y
help
This enables the ESP_HTTP_SERVER_EVENT event type. Generating these eventes adds some overhead.
If you are not using this event type, you can disable it to save some memory and add performance.

config HTTPD_SERVER_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
depends on HTTPD_ENABLE_EVENTS
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
Expand Down
2 changes: 2 additions & 0 deletions components/esp_http_server/include/esp_http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ extern "C" {

#define ESP_HTTPD_DEF_CTRL_PORT (32768) /*!< HTTP Server control socket port*/

#ifdef CONFIG_HTTPD_ENABLE_EVENTS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
#if CONFIG_HTTPD_ENABLE_EVENTS || __DOXYGEN__

ESP_EVENT_DECLARE_BASE(ESP_HTTP_SERVER_EVENT);

/**
Expand All @@ -45,6 +46,7 @@ typedef struct {
int fd; /*!< Session socket file descriptor */
int data_len; /*!< Data length */
} esp_http_server_event_data;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#endif // CONFIG_HTTPD_ENABLE_EVENTS


/*
note: esp_https_server.h includes a customized copy of this
Expand Down
8 changes: 8 additions & 0 deletions components/esp_http_server/src/httpd_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct {

static const char *TAG = "httpd";

#ifdef CONFIG_HTTPD_ENABLE_EVENTS
ESP_EVENT_DEFINE_BASE(ESP_HTTP_SERVER_EVENT);

void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, size_t event_data_size)
Expand All @@ -47,6 +48,7 @@ void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, si
ESP_LOGE(TAG, "Failed to post esp_http_server event: %s", esp_err_to_name(err));
}
}
#endif

static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd)
{
Expand Down Expand Up @@ -127,7 +129,9 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd)
}
ESP_LOGD(TAG, LOG_FMT("complete"));
hd->http_server_state = HTTP_SERVER_EVENT_ON_CONNECTED;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ON_CONNECTED, &new_fd, sizeof(int));
#endif
return ESP_OK;
exit:
close(new_fd);
Expand Down Expand Up @@ -532,7 +536,9 @@ esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config)

*handle = (httpd_handle_t)hd;
hd->http_server_state = HTTP_SERVER_EVENT_START;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_START, NULL, 0);
#endif

return ESP_OK;
}
Expand Down Expand Up @@ -583,7 +589,9 @@ esp_err_t httpd_stop(httpd_handle_t handle)
vSemaphoreDelete(hd->ctrl_sock_semaphore);
#endif
httpd_delete(hd);
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_STOP, NULL, 0);
#endif
return ESP_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions components/esp_http_server/src/httpd_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ static esp_err_t cb_headers_complete(http_parser *parser)
ra->remaining_len = r->content_len;
struct httpd_data *hd = (struct httpd_data *) r->handle;
hd->http_server_state = HTTP_SERVER_EVENT_ON_HEADER;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ON_HEADER, &(ra->sd->fd), sizeof(int));
#endif
return ESP_OK;
}

Expand Down
2 changes: 2 additions & 0 deletions components/esp_http_server/src/httpd_sess.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ void httpd_sess_delete(struct httpd_data *hd, struct sock_db *session)
close(session->fd);
}
hd->http_server_state = HTTP_SERVER_EVENT_DISCONNECTED;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_DISCONNECTED, &session->fd, sizeof(int));
#endif

// clear all contexts
httpd_sess_clear_ctx(session);
Expand Down
10 changes: 10 additions & 0 deletions components/esp_http_server/src/httpd_txrx.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,24 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len)
}
struct httpd_data *hd = (struct httpd_data *) r->handle;
hd->http_server_state = HTTP_SERVER_EVENT_HEADERS_SENT;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_HEADERS_SENT, &(ra->sd->fd), sizeof(int));
#endif

/* Sending content */
if (buf && buf_len) {
if (httpd_send_all(r, buf, buf_len) != ESP_OK) {
return ESP_ERR_HTTPD_RESP_SEND;
}
}
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_event_data evt_data = {
.fd = ra->sd->fd,
.data_len = buf_len,
};
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data));
#endif
return ESP_OK;
}

Expand Down Expand Up @@ -412,12 +416,14 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len
if (httpd_send_all(r, "\r\n", strlen("\r\n")) != ESP_OK) {
return ESP_ERR_HTTPD_RESP_SEND;
}
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_event_data evt_data = {
.fd = ra->sd->fd,
.data_len = buf_len,
};
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data));
#endif

return ESP_OK;
}
Expand Down Expand Up @@ -519,7 +525,9 @@ esp_err_t httpd_resp_send_err(httpd_req_t *req, httpd_err_code_t error, const ch
}
}
#endif
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ERROR, &error, sizeof(httpd_err_code_t));
#endif

return ret;
}
Expand Down Expand Up @@ -628,11 +636,13 @@ int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len)
ESP_LOGD(TAG, LOG_FMT("received length = %d"), ret);
struct httpd_data *hd = (struct httpd_data *) r->handle;
hd->http_server_state = HTTP_SERVER_EVENT_ON_DATA;
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
esp_http_server_event_data evt_data = {
.fd = ra->sd->fd,
.data_len = ret,
};
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ON_DATA, &evt_data, sizeof(esp_http_server_event_data));
#endif
return ret;
}

Expand Down
10 changes: 10 additions & 0 deletions components/esp_https_server/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,18 @@ menu "ESP HTTPS server"
help
Enable ESP HTTPS server component

config ESP_HTTPS_SERVER_EVENTS
bool "Enable ESP_HTTPS_SERVER_EVENT event type"
depends on ESP_HTTPS_SERVER_ENABLE
default y
help
This enables the ESP_HTTPS_SERVER_EVENT event type. Generating these eventes adds some overhead.
If you are not using this event type, you can disable it to save some memory.


config ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT
int "Time in millisecond to wait for posting event"
depends on (ESP_HTTPS_SERVER_ENABLE && ESP_HTTPS_SERVER_EVENTS)
default 2000
help
This config option helps in setting the time in millisecond to wait for event to be posted to the
Expand Down
2 changes: 2 additions & 0 deletions components/esp_https_server/include/esp_https_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
extern "C" {
#endif

#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
#if CONFIG_ESP_HTTPS_SERVER_EVENTS || __DOXYGEN__

ESP_EVENT_DECLARE_BASE(ESP_HTTPS_SERVER_EVENT);

typedef enum {
Expand All @@ -29,6 +30,7 @@ typedef enum {
HTTPS_SERVER_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
HTTPS_SERVER_EVENT_STOP, /*!< This event occurs when HTTPS Server is stopped */
} esp_https_server_event_id_t;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#endif
#endif // CONFIG_ESP_HTTPS_SERVER_EVENTS


typedef enum {
HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled
Expand Down
8 changes: 7 additions & 1 deletion components/esp_https_server/src/https_server.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2018-2025 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -23,7 +23,9 @@ typedef struct httpd_ssl_transport_ctx {
httpd_ssl_ctx_t *global_ctx;
} httpd_ssl_transport_ctx_t;

#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
ESP_EVENT_DEFINE_BASE(ESP_HTTPS_SERVER_EVENT);
#endif

#if CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT == -1
#define ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT portMAX_DELAY
Expand All @@ -32,13 +34,17 @@ ESP_EVENT_DEFINE_BASE(ESP_HTTPS_SERVER_EVENT);
#endif


#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
static void http_dispatch_event_to_event_loop(int32_t event_id, const void* event_data, size_t event_data_size)
{
esp_err_t err = esp_event_post(ESP_HTTPS_SERVER_EVENT, event_id, event_data, event_data_size, ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Failed to post http_client event: %"PRId32", error: %s", event_id, esp_err_to_name(err));
}
}
#else
#define http_dispatch_event_to_event_loop(event_id, event_data, event_data_size) do {} while (0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use similar approach (stub out the implementation) in HTTP server component as well?

#endif

/**
* SSL socket close handler
Expand Down