From d16dbc197ff6a07bc8cca4e095029cda95786f10 Mon Sep 17 00:00:00 2001
From: "Serge A. Zaitsev" <zaitsev.serge@gmail.com>
Date: Tue, 20 Mar 2018 10:33:05 +0200
Subject: [PATCH] mongoose: fix http pipeline

mongoose: fix formatting

mongoose: fix formatting

mongoose: remove debugging messages

PUBLISHED_FROM=1194e018001cc5f2b598096593d7aac4ec8dc04d
---
 mongoose.c       |  6 +++++-
 src/mg_http.c    |  6 +++++-
 test/unit_test.c | 12 ++++--------
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/mongoose.c b/mongoose.c
index 25b572601..8e7bd8b28 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -6159,6 +6159,7 @@ void mg_http_handler(struct mg_connection *nc, int ev,
     }
 #endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
 
+  again:
     req_len = mg_parse_http(io->buf, io->len, hm, is_req);
 
     if (req_len > 0 &&
@@ -6252,7 +6253,10 @@ void mg_http_handler(struct mg_connection *nc, int ev,
       /* Whole HTTP message is fully buffered, call event handler */
       mg_http_call_endpoint_handler(nc, trigger_ev, hm);
       mbuf_remove(io, hm->message.len);
-      pd->rcvd = 0;
+      pd->rcvd -= hm->message.len;
+      if (io->len > 0) {
+        goto again;
+      }
     }
   }
 }
diff --git a/src/mg_http.c b/src/mg_http.c
index 068d20830..402b8d3b5 100644
--- a/src/mg_http.c
+++ b/src/mg_http.c
@@ -787,6 +787,7 @@ void mg_http_handler(struct mg_connection *nc, int ev,
     }
 #endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */
 
+  again:
     req_len = mg_parse_http(io->buf, io->len, hm, is_req);
 
     if (req_len > 0 &&
@@ -880,7 +881,10 @@ void mg_http_handler(struct mg_connection *nc, int ev,
       /* Whole HTTP message is fully buffered, call event handler */
       mg_http_call_endpoint_handler(nc, trigger_ev, hm);
       mbuf_remove(io, hm->message.len);
-      pd->rcvd = 0;
+      pd->rcvd -= hm->message.len;
+      if (io->len > 0) {
+        goto again;
+      }
     }
   }
 }
diff --git a/test/unit_test.c b/test/unit_test.c
index 7f587a707..b6f95989a 100644
--- a/test/unit_test.c
+++ b/test/unit_test.c
@@ -2057,20 +2057,19 @@ static void http_pipeline_handler(struct mg_connection *c, int ev,
   int *status = (int *) c->mgr->user_data;
   if (ev == MG_EV_HTTP_REQUEST) {
     /* Server request handler */
-    mg_send_response_line(c, 200, "Content-Type: text/plain\r\n");
+    mg_send_response_line(c, 200,
+                          "Content-Type: text/plain\r\nContent-Length: 5\r\n");
     mg_printf(c, "Hello");
-    c->flags |= MG_F_SEND_AND_CLOSE;
     *status = *status + 1;
   } else if (ev == MG_EV_HTTP_REPLY) {
     /* Client reply handler */
     *status = *status + 10;
-    c->flags |= MG_F_CLOSE_IMMEDIATELY;
   }
 }
 
 static const char *test_http_pipeline(void) {
   struct mg_mgr mgr;
-  struct mg_connection *lc, *nc1, *nc2;
+  struct mg_connection *lc, *nc1;
   const char *local_addr = "127.0.0.1:7777";
   int status = 0;
 
@@ -2079,10 +2078,7 @@ static const char *test_http_pipeline(void) {
   mg_set_protocol_http_websocket(lc);
   ASSERT(nc1 = mg_connect(&mgr, local_addr, http_pipeline_handler));
   mg_set_protocol_http_websocket(nc1);
-  mg_printf(nc1, "GET / HTTP/1.1\r\n\r\n");
-  ASSERT(nc2 = mg_connect(&mgr, local_addr, http_pipeline_handler));
-  mg_set_protocol_http_websocket(nc2);
-  mg_printf(nc2, "GET / HTTP/1.1\r\n\r\n");
+  mg_printf(nc1, "GET / HTTP/1.1\r\n\r\nGET / HTTP/1.1\r\n\r\n");
   poll_until(&mgr, 1, c_int_eq, &status, (void *) 22);
   ASSERT_EQ(status, 22);
   mg_mgr_free(&mgr);
-- 
GitLab