From a0961ad79c73c515d59e24e621ad35f8b82e4d16 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 9 Oct 2018 20:17:26 +0530 Subject: [PATCH] HTTP Server Docs : Updated to demonstrate handling of timeout errors --- docs/en/api-reference/protocols/http_server.rst | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/en/api-reference/protocols/http_server.rst b/docs/en/api-reference/protocols/http_server.rst index 9016574e14..320dee3f86 100644 --- a/docs/en/api-reference/protocols/http_server.rst +++ b/docs/en/api-reference/protocols/http_server.rst @@ -29,15 +29,26 @@ Application Example /* Our URI handler function to be called during POST /uri request */ esp_err_t post_handler(httpd_req_t *req) { - /* Read request content */ + /* Destination buffer for content of HTTP POST request. + * httpd_req_recv() accepts char* only, but content could + * as well be any binary data (needs type casting). + * In case of string data, null termination will be absent, and + * content length would give length of string */ char[100] content; /* Truncate if content length larger than the buffer */ size_t recv_size = MIN(req->content_len, sizeof(content)); int ret = httpd_req_recv(req, content, recv_size); - if (ret < 0) { - /* In case of recv error, returning ESP_FAIL will + if (ret <= 0) { /* 0 return value indicates connection closed */ + /* Check if timeout occurred */ + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + /* In case of timeout one can choose to retry calling + * httpd_req_recv(), but to keep it simple, here we + * respond with an HTTP 408 (Request Timeout) error */ + httpd_resp_send_408(req); + } + /* In case of error, returning ESP_FAIL will * ensure that the underlying socket is closed */ return ESP_FAIL; }