From e41df3cf9c40104b4afcc5149154247de5f0b2da Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Mon, 3 Mar 2014 10:26:27 +0000
Subject: [PATCH] MG_REQ_BEGIN -> MG_REQUEST, killed MG_REQ_END, add MG_REPLY.
 Restored server test

---
 examples/Makefile         |  2 +-
 examples/hello.c          |  2 +-
 examples/multi_threaded.c |  2 +-
 examples/post.c           |  2 +-
 examples/unit_test.c      | 72 +++++++++++++++++++++------------------
 examples/upload.c         |  2 +-
 examples/websocket.c      |  2 +-
 mongoose.c                | 27 +++++++--------
 mongoose.h                |  8 ++---
 9 files changed, 60 insertions(+), 59 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index b6dfac67b..51d4ac606 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -52,7 +52,7 @@ websocket_html.c: websocket.html
 	perl mkdata.pl $< > $@
 
 u:
-	g++ unit_test.c -Wall -W -pedantic -lssl -g -O0 && ./a.out
+	g++ unit_test.c -Wall -W -pedantic -lssl -g -O0 $(CFLAGS_EXTRA) && ./a.out
 
 clean:
 	-@$(RM) hello upload post websocket auth server multi_threaded websocket_html.c *.exe *.dSYM *.obj .*o
diff --git a/examples/hello.c b/examples/hello.c
index 58a1e38c7..00423db92 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -5,7 +5,7 @@
 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
   int result = MG_FALSE;
 
-  if (ev == MG_REQ_BEGIN) {
+  if (ev == MG_REQUEST) {
     mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
     result = MG_TRUE;
   } else if (ev == MG_AUTH) {
diff --git a/examples/multi_threaded.c b/examples/multi_threaded.c
index 54f1a8f44..b087534c7 100644
--- a/examples/multi_threaded.c
+++ b/examples/multi_threaded.c
@@ -3,7 +3,7 @@
 // Start a browser and hit refresh couple of times. The replies will
 // come from both server instances.
 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
-  if (ev == MG_REQ_BEGIN) {
+  if (ev == MG_REQUEST) {
     mg_send_header(conn, "Content-Type", "text/plain");
     mg_printf_data(conn, "This is a reply from server instance # %s",
                    (char *) conn->server_param);
diff --git a/examples/post.c b/examples/post.c
index 510bac4ad..f6893114d 100644
--- a/examples/post.c
+++ b/examples/post.c
@@ -36,7 +36,7 @@ static void send_reply(struct mg_connection *conn) {
 }
 
 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
-  if (ev == MG_REQ_BEGIN) {
+  if (ev == MG_REQUEST) {
     send_reply(conn);
     return MG_TRUE;
   } else if (ev == MG_AUTH) {
diff --git a/examples/unit_test.c b/examples/unit_test.c
index 82d8c0fba..96d62324b 100644
--- a/examples/unit_test.c
+++ b/examples/unit_test.c
@@ -340,42 +340,44 @@ static const char *test_next_option(void) {
 }
 
 static int evh1(struct mg_connection *conn, enum mg_event ev) {
-  if (ev == MG_HTTP_ERROR) {
-    mg_printf(conn, "HTTP/1.0 404 NF\r\n\r\nERR: %d", conn->status_code);
-    return MG_TRUE;
-  } else if (ev == MG_REQ_BEGIN && !strcmp(conn->uri, "/cb1")) {
-    mg_printf(conn, "%s %s %s",
-              conn->server_param == NULL ? "?" : (char *) conn->server_param,
-              conn->connection_param == NULL ? "?" : "!", conn->remote_ip);
-    return MG_TRUE;
-  }
-  return MG_FALSE;
-}
-
-static int ts1(struct mg_connection *conn) {
-  if (conn->status_code == MG_CONNECT_SUCCESS) {
-    mg_printf(conn, "%s", "GET /cb1 HTTP/1.0\r\n\r\n");
-    return MG_CLIENT_CONTINUE;
-  } else if (conn->status_code == MG_DOWNLOAD_SUCCESS) {
-    sprintf((char *) conn->connection_param, "%.*s",
-            (int) conn->content_len, conn->content);
+  char *buf = (char *) conn->connection_param;
+  int result = MG_FALSE;
+
+  switch (ev) {
+    case MG_CONNECT:
+      mg_printf(conn,  "GET %s HTTP/1.0\r\n\r\n",
+                buf[0] == '1' ? "/cb1" : "/non_exist");
+      result = MG_TRUE;
+      break;
+    case MG_HTTP_ERROR:
+      mg_printf(conn, "HTTP/1.0 404 NF\r\n\r\nERR: %d", conn->status_code);
+      result = MG_TRUE;
+      break;
+    case MG_REQUEST:
+      if (!strcmp(conn->uri, "/cb1")) {
+        mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n%s %s %s",
+                  (char *) conn->server_param,
+                  buf == NULL ? "?" : "!", conn->remote_ip);
+        result = MG_TRUE;
+      }
+      break;
+    case MG_REPLY:
+      if (buf != NULL) {
+        sprintf(buf + 1, "%.*s", (int) conn->content_len, conn->content);
+      }
+      break;
+    case MG_AUTH:
+      result = MG_TRUE;
+      break;
+    default:
+      break;
   }
-  return MG_CLIENT_CLOSE;
-}
 
-static int ts2(struct mg_connection *conn) {
-  if (conn->status_code == MG_CONNECT_SUCCESS) {
-    mg_printf(conn, "%s", "GET /non_exist HTTP/1.0\r\n\r\n");
-    return MG_CLIENT_CONTINUE;
-  } else if (conn->status_code == MG_DOWNLOAD_SUCCESS) {
-    sprintf((char *) conn->connection_param, "%s %.*s",
-            conn->uri, (int) conn->content_len, conn->content);
-  }
-  return MG_CLIENT_CLOSE;
+  return result;
 }
 
 static const char *test_server(void) {
-  char buf1[100] = "", buf2[100] = "";
+  char buf1[100] = "1", buf2[100] = "2";
   struct mg_server *server = mg_create_server((void *) "foo", evh1);
   struct mg_connection *conn;
 
@@ -384,11 +386,13 @@ static const char *test_server(void) {
   ASSERT(mg_set_option(server, "document_root", ".") == NULL);
 
   ASSERT((conn = mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0)) != NULL);
+  conn->connection_param = buf1;
   ASSERT((conn = mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0)) != NULL);
+  conn->connection_param = buf2;
 
   { int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
-  ASSERT(strcmp(buf1, "foo ? 127.0.0.1") == 0);
-  ASSERT(strcmp(buf2, "404 ERR: 404") == 0);
+  ASSERT(strcmp(buf1, "1foo ? 127.0.0.1") == 0);
+  ASSERT(strcmp(buf2, "2ERR: 404") == 0);
 
   ASSERT(strcmp(static_config_options[URL_REWRITES * 2], "url_rewrites") == 0);
   mg_destroy_server(&server);
@@ -596,7 +600,7 @@ static const char *run_all_tests(void) {
   RUN_TEST(test_next_option);
   RUN_TEST(test_parse_multipart);
   RUN_TEST(test_mg_set_option);
-  //RUN_TEST(test_server);
+  RUN_TEST(test_server);
   //RUN_TEST(test_mg_connect);
   //RUN_TEST(test_rewrites);
 #ifdef MONGOOSE_USE_SSL
diff --git a/examples/upload.c b/examples/upload.c
index cc38d1efd..4073afb44 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -32,7 +32,7 @@ static void send_index_page(struct mg_connection *conn) {
 }
 
 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
-  if (ev == MG_REQ_BEGIN) {
+  if (ev == MG_REQUEST) {
     send_index_page(conn);
     return MG_TRUE;
   } else if (ev == MG_AUTH) {
diff --git a/examples/websocket.c b/examples/websocket.c
index 8378d6cc4..1cfb83dc4 100644
--- a/examples/websocket.c
+++ b/examples/websocket.c
@@ -37,7 +37,7 @@ static int send_reply(struct mg_connection *conn) {
 }
 
 static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
-  if (ev == MG_REQ_BEGIN) {
+  if (ev == MG_REQUEST) {
     return send_reply(conn);
   } else if (ev == MG_AUTH) {
     return MG_TRUE;
diff --git a/mongoose.c b/mongoose.c
index 1743b2a6b..6316d3967 100644
--- a/mongoose.c
+++ b/mongoose.c
@@ -650,7 +650,6 @@ static void ns_read_from_socket(struct ns_connection *conn) {
     int ok = 1, ret;
     socklen_t len = sizeof(ok);
 
-    conn->flags &= ~NSF_CONNECTING;
     ret = getsockopt(conn->sock, SOL_SOCKET, SO_ERROR, (char *) &ok, &len);
     (void) ret;
 #ifdef NS_ENABLE_SSL
@@ -659,15 +658,15 @@ static void ns_read_from_socket(struct ns_connection *conn) {
       int ssl_err = SSL_get_error(conn->ssl, res);
       DBG(("%p res %d %d", conn, res, ssl_err));
       if (res == 1) {
-        conn->flags = NSF_SSL_HANDSHAKE_DONE;
+        conn->flags |= NSF_SSL_HANDSHAKE_DONE;
       } else if (res == 0 || ssl_err == 2 || ssl_err == 3) {
-        conn->flags |= NSF_CONNECTING;
         return; // Call us again
       } else {
         ok = 1;
       }
     }
 #endif
+    conn->flags &= ~NSF_CONNECTING;
     DBG(("%p ok=%d", conn, ok));
     if (ok != 0) {
       conn->flags |= NSF_CLOSE_IMMEDIATELY;
@@ -2528,7 +2527,7 @@ static int deliver_websocket_frame(struct connection *conn) {
     }
 
     // Call the handler and remove frame from the iobuf
-    if (call_user(conn, MG_REQ_BEGIN) == MG_FALSE) {
+    if (call_user(conn, MG_REQUEST) == MG_FALSE) {
       conn->ns_conn->flags |= NSF_FINISHED_SENDING_DATA;
     }
     iobuf_remove(&conn->ns_conn->recv_iobuf, frame_len);
@@ -2604,7 +2603,7 @@ static void write_terminating_chunk(struct connection *conn) {
 static int call_request_handler(struct connection *conn) {
   int result;
   conn->mg_conn.content = conn->ns_conn->recv_iobuf.buf;
-  if ((result = call_user(conn, MG_REQ_BEGIN)) == MG_TRUE) {
+  if ((result = call_user(conn, MG_REQUEST)) == MG_TRUE) {
     if (conn->ns_conn->flags & MG_HEADERS_SENT) {
       write_terminating_chunk(conn);
     }
@@ -4301,15 +4300,14 @@ static void process_request(struct connection *conn) {
 #endif
 }
 
-static void call_http_client_handler(struct connection *conn, int code) {
-  conn->mg_conn.status_code = code;
+static void call_http_client_handler(struct connection *conn) {
+  //conn->mg_conn.status_code = code;
   // For responses without Content-Lengh, use the whole buffer
-  if (conn->cl == 0 && code == MG_DOWNLOAD_SUCCESS) {
+  if (conn->cl == 0) {
     conn->mg_conn.content_len = conn->ns_conn->recv_iobuf.len;
   }
   conn->mg_conn.content = conn->ns_conn->recv_iobuf.buf;
-  if (call_user(conn, MG_CONNECT) || code == MG_CONNECT_FAILURE ||
-      code == MG_DOWNLOAD_FAILURE) {
+  if (call_user(conn, MG_REPLY) == MG_FALSE) {
     conn->ns_conn->flags |= NSF_CLOSE_IMMEDIATELY;
   }
   iobuf_remove(&conn->ns_conn->recv_iobuf, conn->mg_conn.content_len);
@@ -4327,10 +4325,9 @@ static void process_response(struct connection *conn) {
        io->len > 40 ? 40 : io->len, io->buf));
   if (conn->request_len < 0 ||
       (conn->request_len == 0 && io->len > MAX_REQUEST_SIZE)) {
-    call_http_client_handler(conn, MG_DOWNLOAD_FAILURE);
-  }
-  if (io->len >= conn->cl) {
-    call_http_client_handler(conn, MG_DOWNLOAD_SUCCESS);
+    call_http_client_handler(conn);
+  } else if (io->len >= conn->cl) {
+    call_http_client_handler(conn);
   }
 }
 
@@ -4755,7 +4752,7 @@ static void mg_ev_handler(struct ns_connection *nc, enum ns_event ev, void *p) {
         DBG(("%p %d closing", conn, conn->endpoint_type));
 
         if (conn->endpoint_type == EP_CLIENT && nc->recv_iobuf.len > 0) {
-          call_http_client_handler(conn, MG_DOWNLOAD_SUCCESS);
+         // call_http_client_handler(conn);
         }
 
         call_user(conn, MG_CLOSE);
diff --git a/mongoose.h b/mongoose.h
index 6a6c881f8..68fbe57c9 100644
--- a/mongoose.h
+++ b/mongoose.h
@@ -60,12 +60,12 @@ struct mg_connection {
 struct mg_server; // Opaque structure describing server instance
 enum mg_result { MG_FALSE, MG_TRUE };
 enum mg_event {
-  MG_POLL,        // Callback return value is ignored
+  MG_POLL = 100,  // Callback return value is ignored
+  MG_CONNECT,     // If callback returns MG_FALSE, connect fails
   MG_AUTH,        // If callback returns MG_FALSE, authentication fails
-  MG_REQ_BEGIN,   // If callback returns MG_FALSE, Mongoose continues with req
-  MG_REQ_END,     // Callback return value is ignored
+  MG_REQUEST,     // If callback returns MG_FALSE, Mongoose continues with req
+  MG_REPLY,       // If callback returns MG_FALSE, Mongoose closes connection
   MG_CLOSE,       // Connection is closed
-  MG_CONNECT,     // If callback returns MG_FALSE, connect fails
   MG_HTTP_ERROR   // If callback returns MG_FALSE, Mongoose continues with err
 };
 typedef int (*mg_handler_t)(struct mg_connection *, enum mg_event);
-- 
GitLab