From be8dbb0dc7613d6ef55eba0258cac6740da7e74a Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Mon, 1 Jul 2024 14:38:27 -0300 Subject: [PATCH] fix 411 not draining header data --- mongoose.c | 1 + src/http.c | 1 + test/unit_test.c | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/mongoose.c b/mongoose.c index d65ddf1a..e8006f3b 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3258,6 +3258,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) { // contain a Content-length header. Other requests can also contain a // body, but their content has no defined semantics (RFC 7231) require_content_len = true; + ofs += (size_t) n; // this request has been processed } else if (is_response) { // HTTP spec 7.2 Entity body: All other responses must include a body // or Content-Length header field defined with a value of 0. diff --git a/src/http.c b/src/http.c index dcbfdd81..044426ef 100644 --- a/src/http.c +++ b/src/http.c @@ -1042,6 +1042,7 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) { // contain a Content-length header. Other requests can also contain a // body, but their content has no defined semantics (RFC 7231) require_content_len = true; + ofs += (size_t) n; // this request has been processed } else if (is_response) { // HTTP spec 7.2 Entity body: All other responses must include a body // or Content-Length header field defined with a value of 0. diff --git a/test/unit_test.c b/test/unit_test.c index a816520d..42870a87 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -865,7 +865,8 @@ static void fprcb(struct mg_connection *c, int ev, void *ev_data) { struct fpr_data *fd = (struct fpr_data *) c->fn_data; if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; - if (atoi(hm->uri.buf) == 200) { + int code = atoi(hm->uri.buf); + if (code == 200) { snprintf(fd->buf + fd->len, FETCH_BUF_SIZE - (unsigned int) fd->len, "%.*s", (int) hm->message.len, hm->message.buf); fd->len += (int) hm->message.len; @@ -874,6 +875,9 @@ static void fprcb(struct mg_connection *c, int ev, void *ev_data) { fd->closed = 1; c->is_closing = 1; } + } else { // allow testing for other codes and catching wrong responses + MG_INFO(("reqs: %d, code: %d", fd->reqs, code)); + fd->reqs += code; } (void) c; } else if (ev == MG_EV_CLOSE) { @@ -1434,6 +1438,8 @@ static void test_http_no_content_length(void) { ASSERT(fetch(&mgr, buf, url, "HTTP/1.1 304\r\n\r\n") != 411); ASSERT(fetch(&mgr, buf, url, "HTTP/1.1 305\r\n\r\n") == 411); ASSERT(fetch(&mgr, buf, url, post_req) != 411); + // Check it is processed only once (see #2811) + ASSERT(fpr(&mgr, buf, url, "POST / HTTP/1.1\r\n\r\n") == 411); mg_mgr_free(&mgr); ASSERT(mgr.conns == NULL); }