Parcourir la source

esp_http_server: Fix examples parse ws keep-alive packet fail.

yuanjm il y a 4 ans
Parent
commit
3874c20b5d

+ 17 - 15
examples/protocols/http_server/ws_echo_server/main/ws_echo_server.c

@@ -72,6 +72,7 @@ static esp_err_t echo_handler(httpd_req_t *req)
         return ESP_OK;
     }
     httpd_ws_frame_t ws_pkt;
+    uint8_t *buf = NULL;
     memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
     ws_pkt.type = HTTPD_WS_TYPE_TEXT;
     /* Set max_len = 0 to get the frame len */
@@ -81,25 +82,27 @@ static esp_err_t echo_handler(httpd_req_t *req)
         return ret;
     }
     ESP_LOGI(TAG, "frame len is %d", ws_pkt.len);
-    /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
-    uint8_t *buf = calloc(1, ws_pkt.len + 1);
-    if (buf == NULL) {
-        ESP_LOGE(TAG, "Failed to calloc memory for buf");
-        return ESP_ERR_NO_MEM;
+    if (ws_pkt.len) {
+        /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
+        buf = calloc(1, ws_pkt.len + 1);
+        if (buf == NULL) {
+            ESP_LOGE(TAG, "Failed to calloc memory for buf");
+            return ESP_ERR_NO_MEM;
+        }
+        ws_pkt.payload = buf;
+        /* Set max_len = ws_pkt.len to get the frame payload */
+        ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
+        if (ret != ESP_OK) {
+            ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret);
+            free(buf);
+            return ret;
+        }
+        ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload);
     }
-    ws_pkt.payload = buf;
-    /* Set max_len = ws_pkt.len to get the frame payload */
-    ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
-    if (ret != ESP_OK) {
-        ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret);
-        return ret;
-    }
-    ESP_LOGI(TAG, "Got packet with message: %s", ws_pkt.payload);
     ESP_LOGI(TAG, "Packet type: %d", ws_pkt.type);
     if (ws_pkt.type == HTTPD_WS_TYPE_TEXT &&
         strcmp((char*)ws_pkt.payload,"Trigger async") == 0) {
         free(buf);
-        buf = NULL;
         return trigger_async_send(req->handle, req);
     }
 
@@ -108,7 +111,6 @@ static esp_err_t echo_handler(httpd_req_t *req)
         ESP_LOGE(TAG, "httpd_ws_send_frame failed with %d", ret);
     }
     free(buf);
-    buf = NULL;
     return ret;
 }
 

+ 16 - 16
examples/protocols/https_server/wss_server/main/wss_server_example.c

@@ -38,6 +38,7 @@ static esp_err_t ws_handler(httpd_req_t *req)
         return ESP_OK;
     }
     httpd_ws_frame_t ws_pkt;
+    uint8_t *buf = NULL;
     memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
 
     // First receive the full ws message
@@ -48,25 +49,26 @@ static esp_err_t ws_handler(httpd_req_t *req)
         return ret;
     }
     ESP_LOGI(TAG, "frame len is %d", ws_pkt.len);
-    /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
-    uint8_t *buf = calloc(1, ws_pkt.len + 1);
-    if (buf == NULL) {
-        ESP_LOGE(TAG, "Failed to calloc memory for buf");
-        return ESP_ERR_NO_MEM;
-    }
-    ws_pkt.payload = buf;
-    /* Set max_len = ws_pkt.len to get the frame payload */
-    ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
-    if (ret != ESP_OK) {
-        ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret);
-        return ret;
+    if (ws_pkt.len) {
+        /* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
+        buf = calloc(1, ws_pkt.len + 1);
+        if (buf == NULL) {
+            ESP_LOGE(TAG, "Failed to calloc memory for buf");
+            return ESP_ERR_NO_MEM;
+        }
+        ws_pkt.payload = buf;
+        /* Set max_len = ws_pkt.len to get the frame payload */
+        ret = httpd_ws_recv_frame(req, &ws_pkt, ws_pkt.len);
+        if (ret != ESP_OK) {
+            ESP_LOGE(TAG, "httpd_ws_recv_frame failed with %d", ret);
+            free(buf);
+            return ret;
+        }
     }
-
     // If it was a PONG, update the keep-alive
     if (ws_pkt.type == HTTPD_WS_TYPE_PONG) {
         ESP_LOGD(TAG, "Received PONG message");
         free(buf);
-        buf = NULL;
         return wss_keep_alive_client_is_active(httpd_get_global_user_ctx(req->handle),
                 httpd_req_to_sockfd(req));
 
@@ -80,11 +82,9 @@ static esp_err_t ws_handler(httpd_req_t *req)
         ESP_LOGI(TAG, "ws_handler: httpd_handle_t=%p, sockfd=%d, client_info:%d", req->handle,
                  httpd_req_to_sockfd(req), httpd_ws_get_fd_info(req->handle, httpd_req_to_sockfd(req)));
         free(buf);
-        buf = NULL;
         return ret;
     }
     free(buf);
-    buf = NULL;
     return ESP_OK;
 }