Skip to content

Commit fe2dccf

Browse files
committed
feat(esp_http_server): Make HTTP(S)_SERVER_EVENT events optional
Make it possible to disable http(s) server events. This improves performance of the server, as http server creates events on every signle read or write to the socket.
1 parent ff97953 commit fe2dccf

File tree

9 files changed

+52
-5
lines changed

9 files changed

+52
-5
lines changed

components/esp_http_server/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,16 @@ menu "HTTP Server"
5757
It internally uses a counting semaphore with count set to `LWIP_UDP_RECVMBOX_SIZE` to achieve this.
5858
This config will slightly change API behavior to block until message gets delivered on control socket.
5959

60+
config HTTPD_ENABLE_EVENTS
61+
bool "Enable ESP_HTTP_SERVER_EVENT"
62+
default y
63+
help
64+
This enables the ESP_HTTP_SERVER_EVENT event type. Generating these eventes adds some overhead.
65+
If you are not using this event type, you can disable it to save some memory and add performance.
66+
6067
config HTTPD_SERVER_EVENT_POST_TIMEOUT
6168
int "Time in millisecond to wait for posting event"
69+
depends on HTTPD_ENABLE_EVENTS
6270
default 2000
6371
help
6472
This config option helps in setting the time in millisecond to wait for event to be posted to the

components/esp_http_server/include/esp_http_server.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ extern "C" {
2323

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

26-
ESP_EVENT_DECLARE_BASE(ESP_HTTP_SERVER_EVENT);
27-
2826
/**
2927
* @brief HTTP Server events id
3028
*/
@@ -40,11 +38,15 @@ typedef enum {
4038
HTTP_SERVER_EVENT_STOP, /*!< This event occurs when HTTP Server is stopped */
4139
} esp_http_server_event_id_t;
4240

41+
#if CONFIG_HTTPD_ENABLE_EVENTS || __DOXYGEN_
42+
ESP_EVENT_DECLARE_BASE(ESP_HTTP_SERVER_EVENT);
43+
4344
/** Argument structure for HTTP_SERVER_EVENT_ON_DATA and HTTP_SERVER_EVENT_SENT_DATA event */
4445
typedef struct {
4546
int fd; /*!< Session socket file descriptor */
4647
int data_len; /*!< Data length */
4748
} esp_http_server_event_data;
49+
#endif // CONFIG_HTTPD_ENABLE_EVENTS || __DOXYGEN_
4850

4951
/*
5052
note: esp_https_server.h includes a customized copy of this

components/esp_http_server/src/esp_httpd_priv.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <esp_http_server.h>
1919
#include "osal.h"
20+
#include "sdkconfig.h"
2021

2122
#ifdef __cplusplus
2223
extern "C" {
@@ -587,12 +588,18 @@ esp_err_t httpd_sess_trigger_close_(httpd_handle_t handle, struct sock_db *sessi
587588
#define ESP_HTTP_SERVER_EVENT_POST_TIMEOUT pdMS_TO_TICKS(CONFIG_HTTPD_SERVER_EVENT_POST_TIMEOUT)
588589
#endif
589590

591+
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
590592
/**
591593
* @brief Function to dispatch events in default event loop
592594
*
593595
*/
594596
void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, size_t event_data_size);
595597

598+
#else // CONFIG_HTTPD_ENABLE_EVENTS
599+
#define esp_http_server_dispatch_event(event_id, event_data, event_data_size) do {} while(0)
600+
#endif // CONFIG_HTTPD_ENABLE_EVENTS
601+
602+
596603
#ifdef __cplusplus
597604
}
598605
#endif

components/esp_http_server/src/httpd_main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct {
3838

3939
static const char *TAG = "httpd";
4040

41+
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
4142
ESP_EVENT_DEFINE_BASE(ESP_HTTP_SERVER_EVENT);
4243

4344
void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, size_t event_data_size)
@@ -47,6 +48,7 @@ void esp_http_server_dispatch_event(int32_t event_id, const void* event_data, si
4748
ESP_LOGE(TAG, "Failed to post esp_http_server event: %s", esp_err_to_name(err));
4849
}
4950
}
51+
#endif // CONFIG_HTTPD_ENABLE_EVENTS
5052

5153
static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd)
5254
{

components/esp_http_server/src/httpd_txrx.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,14 @@ esp_err_t httpd_resp_send(httpd_req_t *r, const char *buf, ssize_t buf_len)
312312
return ESP_ERR_HTTPD_RESP_SEND;
313313
}
314314
}
315+
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
316+
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
315317
esp_http_server_event_data evt_data = {
316318
.fd = ra->sd->fd,
317319
.data_len = buf_len,
318320
};
319-
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
320321
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data));
322+
#endif // CONFIG_HTTPD_ENABLE_EVENTS
321323
return ESP_OK;
322324
}
323325

@@ -412,12 +414,14 @@ esp_err_t httpd_resp_send_chunk(httpd_req_t *r, const char *buf, ssize_t buf_len
412414
if (httpd_send_all(r, "\r\n", strlen("\r\n")) != ESP_OK) {
413415
return ESP_ERR_HTTPD_RESP_SEND;
414416
}
417+
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
418+
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
415419
esp_http_server_event_data evt_data = {
416420
.fd = ra->sd->fd,
417421
.data_len = buf_len,
418422
};
419-
hd->http_server_state = HTTP_SERVER_EVENT_SENT_DATA;
420423
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_SENT_DATA, &evt_data, sizeof(esp_http_server_event_data));
424+
#endif // CONFIG_HTTPD_ENABLE_EVENTS
421425

422426
return ESP_OK;
423427
}
@@ -628,11 +632,13 @@ int httpd_req_recv(httpd_req_t *r, char *buf, size_t buf_len)
628632
ESP_LOGD(TAG, LOG_FMT("received length = %d"), ret);
629633
struct httpd_data *hd = (struct httpd_data *) r->handle;
630634
hd->http_server_state = HTTP_SERVER_EVENT_ON_DATA;
635+
#ifdef CONFIG_HTTPD_ENABLE_EVENTS
631636
esp_http_server_event_data evt_data = {
632637
.fd = ra->sd->fd,
633638
.data_len = ret,
634639
};
635640
esp_http_server_dispatch_event(HTTP_SERVER_EVENT_ON_DATA, &evt_data, sizeof(esp_http_server_event_data));
641+
#endif // CONFIG_HTTPD_ENABLE_EVENTS
636642
return ret;
637643
}
638644

components/esp_https_server/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@ menu "ESP HTTPS server"
66
help
77
Enable ESP HTTPS server component
88

9+
config ESP_HTTPS_SERVER_EVENTS
10+
bool "Enable ESP_HTTPS_SERVER_EVENT event type"
11+
depends on ESP_HTTPS_SERVER_ENABLE
12+
default y
13+
help
14+
This enables the ESP_HTTPS_SERVER_EVENT event type. Generating these eventes adds some overhead.
15+
If you are not using this event type, you can disable it to save some memory.
16+
17+
918
config ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT
1019
int "Time in millisecond to wait for posting event"
20+
depends on (ESP_HTTPS_SERVER_ENABLE && ESP_HTTPS_SERVER_EVENTS)
1121
default 2000
1222
help
1323
This config option helps in setting the time in millisecond to wait for event to be posted to the

components/esp_https_server/include/esp_https_server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
extern "C" {
1919
#endif
2020

21+
#if CONFIG_ESP_HTTPS_SERVER_EVENTS || __DOXYGEN__
2122
ESP_EVENT_DECLARE_BASE(ESP_HTTPS_SERVER_EVENT);
2223

2324
typedef enum {
@@ -29,6 +30,7 @@ typedef enum {
2930
HTTPS_SERVER_EVENT_DISCONNECTED, /*!< The connection has been disconnected */
3031
HTTPS_SERVER_EVENT_STOP, /*!< This event occurs when HTTPS Server is stopped */
3132
} esp_https_server_event_id_t;
33+
#endif // CONFIG_ESP_HTTPS_SERVER_EVENTS || __DOXYGEN__
3234

3335
typedef enum {
3436
HTTPD_SSL_TRANSPORT_SECURE, // SSL Enabled

components/esp_https_server/src/https_server.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2018-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -23,7 +23,9 @@ typedef struct httpd_ssl_transport_ctx {
2323
httpd_ssl_ctx_t *global_ctx;
2424
} httpd_ssl_transport_ctx_t;
2525

26+
#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
2627
ESP_EVENT_DEFINE_BASE(ESP_HTTPS_SERVER_EVENT);
28+
#endif
2729

2830
#if CONFIG_ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT == -1
2931
#define ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT portMAX_DELAY
@@ -32,13 +34,17 @@ ESP_EVENT_DEFINE_BASE(ESP_HTTPS_SERVER_EVENT);
3234
#endif
3335

3436

37+
#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
3538
static void http_dispatch_event_to_event_loop(int32_t event_id, const void* event_data, size_t event_data_size)
3639
{
3740
esp_err_t err = esp_event_post(ESP_HTTPS_SERVER_EVENT, event_id, event_data, event_data_size, ESP_HTTPS_SERVER_EVENT_POST_TIMEOUT);
3841
if (err != ESP_OK) {
3942
ESP_LOGE(TAG, "Failed to post http_client event: %"PRId32", error: %s", event_id, esp_err_to_name(err));
4043
}
4144
}
45+
#else // CONFIG_ESP_HTTPS_SERVER_EVENTS
46+
#define http_dispatch_event_to_event_loop(event_id, event_data, event_data_size) do {} while (0)
47+
#endif // CONFIG_ESP_HTTPS_SERVER_EVENTS
4248

4349
/**
4450
* SSL socket close handler

examples/protocols/https_server/simple/main/main.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
static const char *TAG = "example";
3333

34+
#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
3435
/* Event handler for catching system events */
3536
static void event_handler(void* arg, esp_event_base_t event_base,
3637
int32_t event_id, void* event_data)
@@ -42,6 +43,7 @@ static void event_handler(void* arg, esp_event_base_t event_base,
4243
}
4344
}
4445
}
46+
#endif // CONFIG_ESP_HTTPS_SERVER_EVENTS
4547

4648
/* An HTTP GET handler */
4749
static esp_err_t root_get_handler(httpd_req_t *req)
@@ -247,7 +249,9 @@ void app_main(void)
247249
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_ETH_GOT_IP, &connect_handler, &server));
248250
ESP_ERROR_CHECK(esp_event_handler_register(ETH_EVENT, ETHERNET_EVENT_DISCONNECTED, &disconnect_handler, &server));
249251
#endif // CONFIG_EXAMPLE_CONNECT_ETHERNET
252+
#ifdef CONFIG_ESP_HTTPS_SERVER_EVENTS
250253
ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_SERVER_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
254+
#endif // CONFIG_ESP_HTTPS_SERVER_EVENTS
251255

252256
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
253257
* Read "Establishing Wi-Fi or Ethernet Connection" section in

0 commit comments

Comments
 (0)