From 6a3fa5c180497b26cb207e94fc9f2ba436b9d485 Mon Sep 17 00:00:00 2001 From: Anurag Kar Date: Tue, 9 Oct 2018 18:07:38 +0530 Subject: [PATCH] HTTP Server Examples : Updated examples to manually return 408 and 500 errors --- .../http_server/advanced_tests/main/tests.c | 16 +++++++++++--- .../advanced_tests/scripts/test.py | 22 +++++++++---------- .../persistent_sockets/main/main.c | 10 +++++++-- .../protocols/http_server/simple/main/main.c | 11 ++++++++-- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/examples/protocols/http_server/advanced_tests/main/tests.c b/examples/protocols/http_server/advanced_tests/main/tests.c index 1aa64bcbc..0d7d8327f 100644 --- a/examples/protocols/http_server/advanced_tests/main/tests.c +++ b/examples/protocols/http_server/advanced_tests/main/tests.c @@ -50,13 +50,17 @@ esp_err_t echo_post_handler(httpd_req_t *req) int ret; if (!buf) { + httpd_resp_send_500(req); return ESP_FAIL; } while (off < req->content_len) { /* Read data received in the request */ ret = httpd_req_recv(req, buf + off, req->content_len - off); - if (ret < 0) { + if (ret <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } free (buf); return ESP_FAIL; } @@ -105,7 +109,10 @@ esp_err_t adder_post_handler(httpd_req_t *req) /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf)); - if (ret < 0) { + if (ret <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } return ESP_FAIL; } @@ -137,7 +144,10 @@ esp_err_t leftover_data_post_handler(httpd_req_t *req) /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf) - 1); - if (ret < 0) { + if (ret <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } return ESP_FAIL; } diff --git a/examples/protocols/http_server/advanced_tests/scripts/test.py b/examples/protocols/http_server/advanced_tests/scripts/test.py index f1fcedaec..4f8f0fd4d 100644 --- a/examples/protocols/http_server/advanced_tests/scripts/test.py +++ b/examples/protocols/http_server/advanced_tests/scripts/test.py @@ -350,7 +350,7 @@ def get_hello(dut, port): def put_hello(dut, port): # PUT /hello returns 405' - Utility.console_log("[test] PUT /hello returns 405' =>", end=' ') + Utility.console_log("[test] PUT /hello returns 405 =>", end=' ') conn = http.client.HTTPConnection(dut, int(port), timeout=15) conn.request("PUT", "/hello", "Hello") resp = conn.getresponse() @@ -363,7 +363,7 @@ def put_hello(dut, port): def post_hello(dut, port): # POST /hello returns 405' - Utility.console_log("[test] POST /hello returns 404' =>", end=' ') + Utility.console_log("[test] POST /hello returns 404 =>", end=' ') conn = http.client.HTTPConnection(dut, int(port), timeout=15) conn.request("POST", "/hello", "Hello") resp = conn.getresponse() @@ -376,7 +376,7 @@ def post_hello(dut, port): def post_echo(dut, port): # POST /echo echoes data' - Utility.console_log("[test] POST /echo echoes data' =>", end=' ') + Utility.console_log("[test] POST /echo echoes data =>", end=' ') conn = http.client.HTTPConnection(dut, int(port), timeout=15) conn.request("POST", "/echo", "Hello") resp = conn.getresponse() @@ -392,7 +392,7 @@ def post_echo(dut, port): def put_echo(dut, port): # PUT /echo echoes data' - Utility.console_log("[test] PUT /echo echoes data' =>", end=' ') + Utility.console_log("[test] PUT /echo echoes data =>", end=' ') conn = http.client.HTTPConnection(dut, int(port), timeout=15) conn.request("PUT", "/echo", "Hello") resp = conn.getresponse() @@ -408,7 +408,7 @@ def put_echo(dut, port): def get_echo(dut, port): # GET /echo returns 404' - Utility.console_log("[test] GET /echo returns 405' =>", end=' ') + Utility.console_log("[test] GET /echo returns 405 =>", end=' ') conn = http.client.HTTPConnection(dut, int(port), timeout=15) conn.request("GET", "/echo") resp = conn.getresponse() @@ -595,7 +595,7 @@ def packet_size_limit_test(dut, port, test_size): s.request("POST", url=path, body=random_data) resp = s.getresponse() if not test_val("Error", "200", str(resp.status)): - if test_val("Error", "408", str(resp.status)): + if test_val("Error", "500", str(resp.status)): Utility.console_log("Data too large to be allocated") test_size = test_size//10 else: @@ -619,14 +619,12 @@ def packet_size_limit_test(dut, port, test_size): def code_500_server_error_test(dut, port): Utility.console_log("[test] 500 Server Error test =>", end=' ') s = Session(dut, port) - s.client.sendall(b"abcdefgh\0") + # Sending a very large content length will cause malloc to fail + content_len = 2**31 + s.client.sendall(("POST /echo HTTP/1.1\r\nHost: " + dut + "\r\nContent-Length: " + str(content_len) + "\r\n\r\nABCD").encode()) s.read_resp_hdrs() resp = s.read_resp_data() - # Presently server sends back 400 Bad Request - #if not test_val("Server Error", "500", s.status): - #s.close() - #return False - if not test_val("Server Error", "400", s.status): + if not test_val("Server Error", "500", s.status): s.close() return False s.close() diff --git a/examples/protocols/http_server/persistent_sockets/main/main.c b/examples/protocols/http_server/persistent_sockets/main/main.c index 996a05afe..ea6bea5b2 100644 --- a/examples/protocols/http_server/persistent_sockets/main/main.c +++ b/examples/protocols/http_server/persistent_sockets/main/main.c @@ -48,7 +48,10 @@ esp_err_t adder_post_handler(httpd_req_t *req) /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf)); - if (ret < 0) { + if (ret <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } return ESP_FAIL; } @@ -111,7 +114,10 @@ esp_err_t adder_put_handler(httpd_req_t *req) /* Read data received in the request */ ret = httpd_req_recv(req, buf, sizeof(buf)); - if (ret < 0) { + if (ret <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } return ESP_FAIL; } diff --git a/examples/protocols/http_server/simple/main/main.c b/examples/protocols/http_server/simple/main/main.c index 7e2bd4907..5c8d266a5 100644 --- a/examples/protocols/http_server/simple/main/main.c +++ b/examples/protocols/http_server/simple/main/main.c @@ -122,7 +122,11 @@ esp_err_t echo_post_handler(httpd_req_t *req) while (remaining > 0) { /* Read the data for the request */ if ((ret = httpd_req_recv(req, buf, - MIN(remaining, sizeof(buf)))) < 0) { + MIN(remaining, sizeof(buf)))) <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + /* Retry receiving if timeout occurred */ + continue; + } return ESP_FAIL; } @@ -156,7 +160,10 @@ esp_err_t ctrl_put_handler(httpd_req_t *req) char buf; int ret; - if ((ret = httpd_req_recv(req, &buf, 1)) < 0) { + if ((ret = httpd_req_recv(req, &buf, 1)) <= 0) { + if (ret == HTTPD_SOCK_ERR_TIMEOUT) { + httpd_resp_send_408(req); + } return ESP_FAIL; }