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

This commit is contained in:
David Cermak
2020-09-07 16:24:26 +02:00
parent 76ca826758
commit 5e1e5f8be9
5 changed files with 14 additions and 3 deletions

View File

@@ -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. * If this flag is true, then method must be HTTP_GET. Otherwise the handshake will not be handled.
*/ */
bool is_websocket; 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 #endif
} httpd_uri_t; } httpd_uri_t;

View File

@@ -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_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) */ 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 */ 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 #endif
}; };

View File

@@ -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")); ESP_LOGD(TAG, LOG_FMT("Received PONG frame"));
} }
/* Call handler if it's a non-control frame */ /* 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_PONG) { if (ret == ESP_OK &&
(ra->ws_type < HTTPD_WS_TYPE_CLOSE || sd->ws_control_frames)) {
ret = sd->ws_handler(r); ret = sd->ws_handler(r);
} }

View File

@@ -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; hd->hd_calls[i]->user_ctx = uri_handler->user_ctx;
#ifdef CONFIG_HTTPD_WS_SUPPORT #ifdef CONFIG_HTTPD_WS_SUPPORT
hd->hd_calls[i]->is_websocket = uri_handler->is_websocket; 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 #endif
ESP_LOGD(TAG, LOG_FMT("[%d] installed %s"), i, uri_handler->uri); ESP_LOGD(TAG, LOG_FMT("[%d] installed %s"), i, uri_handler->uri);
return ESP_OK; 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_handshake_done = true;
aux->sd->ws_handler = uri->handler; 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 immediately after handshake, no need to call handler here */
return ESP_OK; return ESP_OK;

View File

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