Procházet zdrojové kódy

Merge branch 'bugfix/httpd_open_fn' into 'master'

HTTP Server : Close new session immediately if `open_fn` fails

Closes IDFGH-1165

See merge request idf/esp-idf!5019
Angus Gratton před 6 roky
rodič
revize
9dd1b2ffb6

+ 5 - 1
components/esp_http_server/include/esp_http_server.h

@@ -100,7 +100,9 @@ typedef void (*httpd_free_ctx_fn_t)(void *ctx);
  *
  *
  * @param[in] hd       server instance
  * @param[in] hd       server instance
  * @param[in] sockfd   session socket file descriptor
  * @param[in] sockfd   session socket file descriptor
- * @return status
+ * @return
+ *  - ESP_OK   : On success
+ *  - Any value other than ESP_OK will signal the server to close the socket immediately
  */
  */
 typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
 typedef esp_err_t (*httpd_open_func_t)(httpd_handle_t hd, int sockfd);
 
 
@@ -201,6 +203,8 @@ typedef struct httpd_config {
      *
      *
      * If a context needs to be maintained between these functions, store it in the session using
      * If a context needs to be maintained between these functions, store it in the session using
      * httpd_sess_set_transport_ctx() and retrieve it later with httpd_sess_get_transport_ctx()
      * httpd_sess_set_transport_ctx() and retrieve it later with httpd_sess_get_transport_ctx()
+     *
+     * Returning a value other than ESP_OK will immediately close the new socket.
      */
      */
     httpd_open_func_t open_fn;
     httpd_open_func_t open_fn;
 
 

+ 5 - 1
components/esp_http_server/src/httpd_sess.c

@@ -77,7 +77,11 @@ esp_err_t httpd_sess_new(struct httpd_data *hd, int newfd)
             /* Call user-defined session opening function */
             /* Call user-defined session opening function */
             if (hd->config.open_fn) {
             if (hd->config.open_fn) {
                 esp_err_t ret = hd->config.open_fn(hd, hd->hd_sd[i].fd);
                 esp_err_t ret = hd->config.open_fn(hd, hd->hd_sd[i].fd);
-                if (ret != ESP_OK) return ret;
+                if (ret != ESP_OK) {
+                    httpd_sess_delete(hd, hd->hd_sd[i].fd);
+                    ESP_LOGD(TAG, LOG_FMT("open_fn failed for fd = %d"), newfd);
+                    return ret;
+                }
             }
             }
             return ESP_OK;
             return ESP_OK;
         }
         }