Parcourir la source

http_server: Add a flag to enable using control frames in user handlers

David Cermak il y a 5 ans
Parent
commit
5e1e5f8be9

+ 6 - 0
components/esp_http_server/include/esp_http_server.h

@@ -406,6 +406,12 @@ typedef struct httpd_uri {
      * If this flag is true, then method must be HTTP_GET. Otherwise the handshake will not be handled.
      */
     bool is_websocket;
+
+    /**
+     * Flag indicating that control frames (PING, PONG, CLOSE) are also passed to the handler
+     * This is used if a custom processing of the control frames is needed
+     */
+    bool handle_ws_control_frames;
 #endif
 } httpd_uri_t;
 

+ 1 - 0
components/esp_http_server/src/esp_httpd_priv.h

@@ -75,6 +75,7 @@ struct sock_db {
     bool ws_handshake_done;                 /*!< True if it has done WebSocket handshake (if this socket is a valid WS) */
     bool ws_close;                          /*!< Set to true to close the socket later (when WS Close frame received) */
     esp_err_t (*ws_handler)(httpd_req_t *r);   /*!< WebSocket handler, leave to null if it's not WebSocket */
+    bool ws_control_frames;                         /*!< WebSocket flag indicating that control frames should be passed to user handlers */
 #endif
 };
 

+ 3 - 2
components/esp_http_server/src/httpd_parse.c

@@ -772,8 +772,9 @@ esp_err_t httpd_req_new(struct httpd_data *hd, struct sock_db *sd)
             ESP_LOGD(TAG, LOG_FMT("Received PONG frame"));
         }
 
-        /* Call handler if it's a non-control frame */
-        if (ret == ESP_OK && ra->ws_type <= HTTPD_WS_TYPE_PONG) {
+        /* Call handler if it's a non-control frame (or if handler requests control frames, as well) */
+        if (ret == ESP_OK &&
+            (ra->ws_type < HTTPD_WS_TYPE_CLOSE || sd->ws_control_frames)) {
             ret = sd->ws_handler(r);
         }
 

+ 2 - 0
components/esp_http_server/src/httpd_uri.c

@@ -174,6 +174,7 @@ esp_err_t httpd_register_uri_handler(httpd_handle_t handle,
             hd->hd_calls[i]->user_ctx = uri_handler->user_ctx;
 #ifdef CONFIG_HTTPD_WS_SUPPORT
             hd->hd_calls[i]->is_websocket = uri_handler->is_websocket;
+            hd->hd_calls[i]->handle_ws_control_frames = uri_handler->handle_ws_control_frames;
 #endif
             ESP_LOGD(TAG, LOG_FMT("[%d] installed %s"), i, uri_handler->uri);
             return ESP_OK;
@@ -322,6 +323,7 @@ esp_err_t httpd_uri(struct httpd_data *hd)
 
         aux->sd->ws_handshake_done = true;
         aux->sd->ws_handler = uri->handler;
+        aux->sd->ws_control_frames = uri->handle_ws_control_frames;
 
         /* Return immediately after handshake, no need to call handler here */
         return ESP_OK;

+ 2 - 1
examples/protocols/https_server/wss_server/main/wss_server_example.c

@@ -84,7 +84,8 @@ static const httpd_uri_t ws = {
         .method     = HTTP_GET,
         .handler    = ws_handler,
         .user_ctx   = NULL,
-        .is_websocket = true
+        .is_websocket = true,
+        .handle_ws_control_frames = true
 };