diff --git a/components/esp_http_server/src/esp_httpd_priv.h b/components/esp_http_server/src/esp_httpd_priv.h index b0019e48f7..423749afc7 100644 --- a/components/esp_http_server/src/esp_httpd_priv.h +++ b/components/esp_http_server/src/esp_httpd_priv.h @@ -197,7 +197,7 @@ esp_err_t httpd_sess_new(struct httpd_data *hd, int newfd); * @brief Processes incoming HTTP requests * * @param[in] hd Server instance data - * @param[in] session session + * @param[in] session Session * * @return * - ESP_OK : on successfully receiving, parsing and responding to a request diff --git a/components/esp_http_server/src/httpd_main.c b/components/esp_http_server/src/httpd_main.c index 3030e1878b..6cb8787cfa 100644 --- a/components/esp_http_server/src/httpd_main.c +++ b/components/esp_http_server/src/httpd_main.c @@ -25,8 +25,8 @@ #include "ctrl_sock.h" typedef struct { - fd_set *fdset; - struct httpd_data *hd; + fd_set *fdset; + struct httpd_data *hd; } process_session_context_t; static const char *TAG = "httpd"; @@ -44,7 +44,7 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd) * therefore httpd_accept_conn() will be called again, but this time * with space available for one session */ - } + } } struct sockaddr_in addr_from; @@ -60,12 +60,12 @@ static esp_err_t httpd_accept_conn(struct httpd_data *hd, int listen_fd) /* Set recv timeout of this fd as per config */ tv.tv_sec = hd->config.recv_wait_timeout; tv.tv_usec = 0; - setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)); + setsockopt(new_fd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof(tv)); /* Set send timeout of this fd as per config */ tv.tv_sec = hd->config.send_wait_timeout; tv.tv_usec = 0; - setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, (const char*)&tv, sizeof(tv)); + setsockopt(new_fd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof(tv)); if (ESP_OK != httpd_sess_new(hd, new_fd)) { ESP_LOGW(TAG, LOG_FMT("session creation failed")); @@ -170,24 +170,24 @@ static void httpd_process_ctrl_msg(struct httpd_data *hd) // Called for each session from httpd_server static int httpd_process_session(struct sock_db *session, void *context) { - if ((!session) || (!context)) { + if ((!session) || (!context)) { return 0; - } + } - if (session->fd < 0) { + if (session->fd < 0) { return 1; - } + } - process_session_context_t * ctx=(process_session_context_t*)context; - int fd=session->fd; + process_session_context_t *ctx = (process_session_context_t *)context; + int fd = session->fd; - if (FD_ISSET(fd, ctx->fdset) || httpd_sess_pending(ctx->hd,session)) { - ESP_LOGD(TAG, LOG_FMT("processing socket %d"), fd); - if (httpd_sess_process(ctx->hd, session) != ESP_OK) { - httpd_sess_delete(ctx->hd, session); // Delete session - } - } - return 1; + if (FD_ISSET(fd, ctx->fdset) || httpd_sess_pending(ctx->hd, session)) { + ESP_LOGD(TAG, LOG_FMT("processing socket %d"), fd); + if (httpd_sess_process(ctx->hd, session) != ESP_OK) { + httpd_sess_delete(ctx->hd, session); // Delete session + } + } + return 1; } /* Manage in-coming connection or data requests */ @@ -417,8 +417,8 @@ esp_err_t httpd_start(httpd_handle_t *handle, const httpd_config_t *config) */ 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); + "Either decrease this or configure LWIP_MAX_SOCKETS to a larger value", + CONFIG_LWIP_MAX_SOCKETS - 3); return ESP_ERR_INVALID_ARG; } diff --git a/components/esp_http_server/src/httpd_sess.c b/components/esp_http_server/src/httpd_sess.c index 4826dd6b5e..462c6521c0 100644 --- a/components/esp_http_server/src/httpd_sess.c +++ b/components/esp_http_server/src/httpd_sess.c @@ -76,59 +76,59 @@ static int enum_function(struct sock_db *session, void *context) enum_context_t *ctx = (enum_context_t *) context; int found = 0; switch (ctx->task) { - // Initialize session - case HTTPD_TASK_INIT: - session->fd = -1; - session->ctx = NULL; - break; - // Get active session - case HTTPD_TASK_GET_ACTIVE: - found = (session->fd != -1); - break; - // Get free slot - case HTTPD_TASK_GET_FREE: - found = (session->fd < 0); - break; - // Find fd - case HTTPD_TASK_FIND_FD: - found = (session->fd == ctx->fd); - break; - // Set descriptor - case HTTPD_TASK_SET_DESCRIPTOR: - if (session->fd != -1) { - FD_SET(session->fd, ctx->fdset); - if (session->fd > ctx->max_fd) { - ctx->max_fd = session->fd; - } + // Initialize session + case HTTPD_TASK_INIT: + session->fd = -1; + session->ctx = NULL; + break; + // Get active session + case HTTPD_TASK_GET_ACTIVE: + found = (session->fd != -1); + break; + // Get free slot + case HTTPD_TASK_GET_FREE: + found = (session->fd < 0); + break; + // Find fd + case HTTPD_TASK_FIND_FD: + found = (session->fd == ctx->fd); + break; + // Set descriptor + case HTTPD_TASK_SET_DESCRIPTOR: + if (session->fd != -1) { + FD_SET(session->fd, ctx->fdset); + if (session->fd > ctx->max_fd) { + ctx->max_fd = session->fd; } - break; - // Delete invalid session - case HTTPD_TASK_DELETE_INVALID: - if (!fd_is_valid(session->fd)) { - ESP_LOGW(TAG, LOG_FMT("Closing invalid socket %d"), session->fd); - httpd_sess_delete(ctx->hd, session); - } - break; - // Find lowest lru - case HTTPD_TASK_FIND_LOWEST_LRU: - // Found free slot - no need to check other sessions - if (session->fd == -1) { - return 0; - } - // Check/update lowest lru - if (session->lru_counter < ctx->lru_counter) { - ctx->lru_counter = session->lru_counter; - ctx->session = session; - } - break; - case HTTPD_TASK_CLOSE: - if (session->fd!=-1) { - ESP_LOGD(TAG, LOG_FMT("cleaning up socket %d"), session->fd); - httpd_sess_delete(ctx->hd, session); - } - break; - default: + } + break; + // Delete invalid session + case HTTPD_TASK_DELETE_INVALID: + if (!fd_is_valid(session->fd)) { + ESP_LOGW(TAG, LOG_FMT("Closing invalid socket %d"), session->fd); + httpd_sess_delete(ctx->hd, session); + } + break; + // Find lowest lru + case HTTPD_TASK_FIND_LOWEST_LRU: + // Found free slot - no need to check other sessions + if (session->fd == -1) { return 0; + } + // Check/update lowest lru + if (session->lru_counter < ctx->lru_counter) { + ctx->lru_counter = session->lru_counter; + ctx->session = session; + } + break; + case HTTPD_TASK_CLOSE: + if (session->fd != -1) { + ESP_LOGD(TAG, LOG_FMT("cleaning up socket %d"), session->fd); + httpd_sess_delete(ctx->hd, session); + } + break; + default: + return 0; } if (found) { ctx->session = session; @@ -155,9 +155,9 @@ static void httpd_sess_close(void *arg) struct sock_db *httpd_sess_get_free(struct httpd_data *hd) { - if ((!hd) || (hd->hd_sd_active_count == hd->config.max_open_sockets)) { - return NULL; - } + if ((!hd) || (hd->hd_sd_active_count == hd->config.max_open_sockets)) { + return NULL; + } enum_context_t context = { .task = HTTPD_TASK_GET_FREE }; @@ -179,7 +179,7 @@ struct sock_db *httpd_sess_get(struct httpd_data *hd, int sockfd) // Check if called inside a request handler, and the session sockfd in use is same as the parameter // => Just return the pointer to the sock_db corresponding to the request if ((hd->hd_req_aux.sd) && (hd->hd_req_aux.sd->fd == sockfd)) { - return hd->hd_req_aux.sd; + return hd->hd_req_aux.sd; } enum_context_t context = { @@ -236,8 +236,7 @@ void httpd_sess_free_ctx(void **ctx, httpd_free_ctx_fn_t free_fn) } if (free_fn) { free_fn(*ctx); - } - else { + } else { free(*ctx); } *ctx = NULL; @@ -272,7 +271,7 @@ void *httpd_sess_get_ctx(httpd_handle_t handle, int sockfd) // Check if the function has been called from inside a // request handler, in which case fetch the context from // the httpd_req_t structure - struct httpd_data * hd = (struct httpd_data *) handle; + struct httpd_data *hd = (struct httpd_data *) handle; if (hd->hd_req_aux.sd == session) { return hd->hd_req.sess_ctx; } @@ -357,7 +356,7 @@ void httpd_sess_delete_invalid(struct httpd_data *hd) void httpd_sess_delete(struct httpd_data *hd, struct sock_db *session) { - if ((!hd) || (!session) || (session->fd<0)) { + if ((!hd) || (!session) || (session->fd < 0)) { return; } @@ -366,8 +365,7 @@ void httpd_sess_delete(struct httpd_data *hd, struct sock_db *session) // Call close function if defined if (hd->config.close_fn) { hd->config.close_fn(hd, session->fd); - } - else { + } else { close(session->fd); } @@ -477,18 +475,18 @@ esp_err_t httpd_sess_trigger_close_(httpd_handle_t handle, struct sock_db *sessi esp_err_t httpd_sess_trigger_close(httpd_handle_t handle, int sockfd) { - struct sock_db *session = httpd_sess_get(handle, sockfd); - if (!session) { - return ESP_ERR_NOT_FOUND; - } - return httpd_sess_trigger_close_(handle,session); + struct sock_db *session = httpd_sess_get(handle, sockfd); + if (!session) { + return ESP_ERR_NOT_FOUND; + } + return httpd_sess_trigger_close_(handle, session); } void httpd_sess_close_all(struct httpd_data *hd) { - enum_context_t context = { + enum_context_t context = { .task = HTTPD_TASK_CLOSE, .hd = hd }; - httpd_sess_enum(hd, enum_function, &context); + httpd_sess_enum(hd, enum_function, &context); }