]> granicus.if.org Git - esp-idf/commitdiff
HTTP Server Docs : Updated to demonstrate handling of timeout errors
authorAnurag Kar <anurag.kar@espressif.com>
Tue, 9 Oct 2018 14:47:26 +0000 (20:17 +0530)
committerAnurag Kar <anurag.kar@espressif.com>
Wed, 17 Oct 2018 12:06:50 +0000 (17:36 +0530)
docs/en/api-reference/protocols/http_server.rst

index 9016574e1459030a463e61b25d7fd5dc422d9aad..320dee3f8640829c66035467cfb44a5e6751dbbf 100644 (file)
@@ -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;
             }