Merge pull request #2604 from cesanta/pipeline

Fix #2592: do not close pipelined connection prematurely
This commit is contained in:
Sergey Lyubka authored and GitHub committed 2024-02-13 05:48:34 +00:00
commit daa47e8272
3 files changed
+30 -7

No files matched your search

+6 -2
View File
@@ -3216,8 +3216,12 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
struct mg_str *te; // Transfer - encoding header struct mg_str *te; // Transfer - encoding header
bool is_chunked = false; bool is_chunked = false;
if (n < 0) { if (n < 0) {
mg_error(c, "HTTP parse, %lu bytes", c->recv.len); // We don't use mg_error() here, to avoid closing pipelined requests
mg_hexdump(c->recv.buf, c->recv.len > 16 ? 16 : c->recv.len); // prematurely, see #2592
MG_ERROR(("HTTP parse, %lu bytes", c->recv.len));
c->is_draining = 1;
mg_hexdump(buf, c->recv.len - ofs > 16 ? 16 : c->recv.len - ofs);
c->recv.len = 0;
return; return;
} }
if (n == 0) break; // Request is not buffered yet if (n == 0) break; // Request is not buffered yet
+6 -2
View File
@@ -996,8 +996,12 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
struct mg_str *te; // Transfer - encoding header struct mg_str *te; // Transfer - encoding header
bool is_chunked = false; bool is_chunked = false;
if (n < 0) { if (n < 0) {
mg_error(c, "HTTP parse, %lu bytes", c->recv.len); // We don't use mg_error() here, to avoid closing pipelined requests
mg_hexdump(c->recv.buf, c->recv.len > 16 ? 16 : c->recv.len); // prematurely, see #2592
MG_ERROR(("HTTP parse, %lu bytes", c->recv.len));
c->is_draining = 1;
mg_hexdump(buf, c->recv.len - ofs > 16 ? 16 : c->recv.len - ofs);
c->recv.len = 0;
return; return;
} }
if (n == 0) break; // Request is not buffered yet if (n == 0) break; // Request is not buffered yet
+18 -3
View File
@@ -1376,18 +1376,33 @@ static void f5(struct mg_connection *c, int ev, void *ev_data) {
} }
} }
static void f6(struct mg_connection *c, int ev, void *ev_data) {
if (ev == MG_EV_HTTP_MSG) {
(*(int *) c->fn_data)++;
(void) ev_data;
}
}
static void test_http_pipeline(void) { static void test_http_pipeline(void) {
struct mg_mgr mgr; struct mg_mgr mgr;
const char *url = "http://127.0.0.1:12377"; const char *url = "http://127.0.0.1:12377";
struct mg_connection *c; struct mg_connection *c;
int i, ok = 0; int i, ok = 0, ok2 = 0;
mg_mgr_init(&mgr); mg_mgr_init(&mgr);
mg_http_listen(&mgr, url, f5, (void *) &ok); mg_http_listen(&mgr, url, f5, (void *) &ok);
c = mg_http_connect(&mgr, url, NULL, NULL); c = mg_http_connect(&mgr, url, f6, &ok2);
mg_printf(c, "POST / HTTP/1.0\nContent-Length: 5\n\n12345GET / HTTP/1.0\n\n"); mg_printf(c, "POST / HTTP/1.0\nContent-Length: 5\n\n12345GET / HTTP/1.0\n\n");
for (i = 0; i < 20; i++) mg_mgr_poll(&mgr, 1); for (i = 0; i < 20; i++) mg_mgr_poll(&mgr, 1);
// MG_INFO(("-----> [%d]", ok));
ASSERT(ok == 2); ASSERT(ok == 2);
ASSERT(ok2 == 2);
// Fire a valid, then invalid request, see #2592. First one should serve
ok = ok2 = 0;
c = mg_http_connect(&mgr, url, f6, (void *) &ok2);
mg_printf(c, "GET / HTTP/1.1\n\nInvalid\n\n");
for (i = 0; i < 20; i++) mg_mgr_poll(&mgr, 1);
ASSERT(ok == 1);
ASSERT(ok2 == 1);
//MG_INFO(("-----> [%d] [%d]", ok, ok2));
mg_mgr_free(&mgr); mg_mgr_free(&mgr);
ASSERT(mgr.conns == NULL); ASSERT(mgr.conns == NULL);
} }