Merge branch 'bugfix/httpd_accept_conn_v3.2' into 'release/v3.2'

esp_http_server : Only accept new connections if server has capacity to handle more (backport v3.2)

See merge request idf/esp-idf!4722
This commit is contained in:
Jiang Jiang Jian 2019-04-28 11:47:14 +08:00
commit df874cd070
2 changed files with 42 additions and 1 deletions

View file

@ -156,7 +156,12 @@ static esp_err_t httpd_server(struct httpd_data *hd)
{
fd_set read_set;
FD_ZERO(&read_set);
FD_SET(hd->listen_fd, &read_set);
if (hd->config.lru_purge_enable || httpd_is_sess_available(hd)) {
/* Only listen for new connections if server has capacity to
* handle more (or when LRU purge is enabled, in which case
* older connections will be closed) */
FD_SET(hd->listen_fd, &read_set);
}
FD_SET(hd->ctrl_fd, &read_set);
int tmp_max_fd;
@ -335,6 +340,23 @@ esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config)
return ESP_ERR_INVALID_ARG;
}
/* Sanity check about whether LWIP is configured for providing the
* maximum number of open sockets sufficient for the server. Though,
* this check doesn't guarantee that many sockets will actually be
* available at runtime as other processes may use up some sockets.
* Note that server also uses 3 sockets for its internal use :
* 1) listening for new TCP connections
* 2) for sending control messages over UDP
* 3) for receiving control messages over UDP
* So the total number of required sockets is max_open_sockets + 3
*/
if (CONFIG_LWIP_MAX_SOCKETS < config->max_open_sockets + 3) {
ESP_LOGE(TAG, "Configuration option max_open_sockets is too large (max allowed %d)\n\t"
"Either decrease this or configure LWIP_MAX_SOCKETS to a larger value",
CONFIG_LWIP_MAX_SOCKETS - 3);
return ESP_ERR_INVALID_ARG;
}
struct httpd_data *hd = httpd_create(config);
if (hd == NULL) {
/* Failed to allocate memory */

View file

@ -167,3 +167,22 @@ TEST_CASE("Basic Functionality Tests", "[HTTP SERVER]")
test_handler_limit(hd);
TEST_ASSERT(httpd_stop(hd) == ESP_OK);
}
TEST_CASE("Max Allowed Sockets Test", "[HTTP SERVER]")
{
test_case_uses_tcpip();
httpd_handle_t hd;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
/* Starting server with default config options should pass */
TEST_ASSERT(httpd_start(&hd, &config) == ESP_OK);
TEST_ASSERT(httpd_stop(hd) == ESP_OK);
/* Default value of max_open_sockets is already set as per
* maximum limit imposed by LWIP. Increasing this beyond the
* maximum allowed value, without increasing LWIP limit,
* should fail */
config.max_open_sockets += 1;
TEST_ASSERT(httpd_start(&hd, &config) != ESP_OK);
}