mirror of
https://github.com/espressif/esp-idf.git
synced 2026-04-27 19:13:21 +00:00
Merge branch 'contrib/github_pr_18310' into 'master'
fix(esp_http_server): Dispatch PONG frames to WebSocket handler (GitHub PR) Closes IDFGH-17331 See merge request espressif/esp-idf!46375
This commit is contained in:
@@ -811,9 +811,16 @@ 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 (or if handler requests control frames, as well) */
|
||||
/* Call handler if it's a non-control frame, a PONG frame,
|
||||
* or if handler requests control frames as well.
|
||||
* PONG must be dispatched so that:
|
||||
* 1. User code that sends PINGs can track responses (heartbeat)
|
||||
* 2. The PONG frame bytes are consumed from the socket via
|
||||
* httpd_ws_recv_frame(), preventing TCP stream misalignment */
|
||||
if (ret == ESP_OK &&
|
||||
(ra->ws_type < HTTPD_WS_TYPE_CLOSE || sd->ws_control_frames)) {
|
||||
(ra->ws_type < HTTPD_WS_TYPE_CLOSE ||
|
||||
ra->ws_type == HTTPD_WS_TYPE_PONG ||
|
||||
sd->ws_control_frames)) {
|
||||
ret = sd->ws_handler(r);
|
||||
}
|
||||
|
||||
|
||||
@@ -52,6 +52,18 @@ static void ws_async_send(void *arg)
|
||||
free(resp_arg);
|
||||
}
|
||||
|
||||
static void ws_ping_send(void *arg)
|
||||
{
|
||||
struct async_resp_arg *resp_arg = arg;
|
||||
httpd_handle_t hd = resp_arg->hd;
|
||||
int fd = resp_arg->fd;
|
||||
httpd_ws_frame_t ping_pkt;
|
||||
memset(&ping_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||
ping_pkt.type = HTTPD_WS_TYPE_PING;
|
||||
httpd_ws_send_frame_async(hd, fd, &ping_pkt);
|
||||
free(resp_arg);
|
||||
}
|
||||
|
||||
static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req)
|
||||
{
|
||||
struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));
|
||||
@@ -67,6 +79,22 @@ static esp_err_t trigger_async_send(httpd_handle_t handle, httpd_req_t *req)
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static esp_err_t trigger_ping_send(httpd_handle_t handle, httpd_req_t *req)
|
||||
{
|
||||
struct async_resp_arg *resp_arg = malloc(sizeof(struct async_resp_arg));
|
||||
if (resp_arg == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
resp_arg->hd = req->handle;
|
||||
resp_arg->fd = httpd_req_to_sockfd(req);
|
||||
esp_err_t ret = httpd_queue_work(handle, ws_ping_send, resp_arg);
|
||||
if (ret != ESP_OK) {
|
||||
free(resp_arg);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_EXAMPLE_ENABLE_WS_PRE_HANDSHAKE_CB
|
||||
static esp_err_t ws_pre_handshake_cb(httpd_req_t *req)
|
||||
{
|
||||
@@ -147,10 +175,14 @@ static esp_err_t echo_handler(httpd_req_t *req)
|
||||
}
|
||||
ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type);
|
||||
if (ws_pkt.type == HTTPD_WS_TYPE_TEXT &&
|
||||
ws_pkt.payload != NULL &&
|
||||
strcmp((char*)ws_pkt.payload,"Trigger async") == 0) {
|
||||
free(buf);
|
||||
return trigger_async_send(req->handle, req);
|
||||
ws_pkt.payload != NULL) {
|
||||
if (strncmp((char *)ws_pkt.payload, "Trigger async", strlen("Trigger async")) == 0) {
|
||||
free(buf);
|
||||
return trigger_async_send(req->handle, req);
|
||||
} else if (strncmp((char *)ws_pkt.payload, "Ping", strlen("Ping")) == 0) {
|
||||
free(buf);
|
||||
return trigger_ping_send(req->handle, req);
|
||||
}
|
||||
}
|
||||
|
||||
ret = httpd_ws_send_frame(req, &ws_pkt);
|
||||
@@ -322,7 +354,6 @@ static void connect_handler(void* arg, esp_event_base_t event_base,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
static httpd_handle_t server = NULL;
|
||||
|
||||
@@ -117,6 +117,36 @@ def test_examples_protocol_http_ws_echo_server(dut: Dut) -> None:
|
||||
data = data.decode()
|
||||
if opcode != OPCODE_TEXT or data != 'Async data':
|
||||
raise RuntimeError(f'Failed to receive correct opcode:{opcode} or data:{data}')
|
||||
# Test ping from server
|
||||
logging.info('Testing server-initiated ping')
|
||||
ws.write(data='Ping', opcode=OPCODE_TEXT)
|
||||
# Wait for server to receive the message and send a ping
|
||||
dut.expect(r'Got packet with message: Ping', timeout=10)
|
||||
# Now read the ping response
|
||||
opcode, data = ws.read()
|
||||
logging.info(f'Testing server-initiated ping: Received opcode:{opcode}, data:{data}')
|
||||
data = data.decode()
|
||||
if opcode != OPCODE_PING:
|
||||
raise RuntimeError(f'Failed to receive correct opcode:{opcode}')
|
||||
# Now we should get a pong in response to our ping
|
||||
opcode, data = ws.read()
|
||||
data = data.decode()
|
||||
if opcode != OPCODE_PONG:
|
||||
raise RuntimeError(f'Failed to receive correct opcode:{opcode}')
|
||||
ws.write(data='Ping', opcode=OPCODE_TEXT)
|
||||
# Wait for server to receive the message and send a ping
|
||||
dut.expect(r'Got packet with message: Ping', timeout=10)
|
||||
# Now read the ping response
|
||||
opcode, data = ws.read()
|
||||
logging.info(f'Testing server-initiated ping: Received opcode:{opcode}, data:{data}')
|
||||
data = data.decode()
|
||||
if opcode != OPCODE_PING:
|
||||
raise RuntimeError(f'Failed to receive correct opcode:{opcode}')
|
||||
# Now we should get a pong in response to our ping
|
||||
opcode, data = ws.read()
|
||||
data = data.decode()
|
||||
if opcode != OPCODE_PONG:
|
||||
raise RuntimeError(f'Failed to receive correct opcode:{opcode}')
|
||||
|
||||
|
||||
@pytest.mark.wifi_router
|
||||
|
||||
Reference in New Issue
Block a user