From ea9a60a59a27b909926ad7fbe053d3967e9c2cf2 Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Sun, 2 Mar 2014 12:16:09 +0000
Subject: [PATCH] Updated examples to conform to the evented API

---
 examples/Makefile         | 31 ++++++++++++++++++++-----------
 examples/auth.c           | 28 ++++++++++++++++------------
 examples/hello.c          | 18 ++++++++++++------
 examples/multi_threaded.c | 23 +++++++++++++----------
 examples/post.c           | 20 ++++++++++++++++----
 examples/server.c         |  2 +-
 examples/upload.c         | 16 ++++++++++++----
 examples/websocket.c      | 36 ++++++++++++++++++++++++------------
 8 files changed, 114 insertions(+), 60 deletions(-)

diff --git a/examples/Makefile b/examples/Makefile
index d22d677db..14e56c075 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,41 +1,50 @@
 CFLAGS    = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA)
 RM        = rm -rf
-MSVC      = ../../vc6
-CL        = $(MSVC)/bin/cl
-CLFLAGS   = /MD /TC /nologo $(CFLAGS_EXTRA) /W3 \
-            /I$(MSVC)/include /I.. /Dsnprintf=_snprintf
-LFLAGS    = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
-
 
 ifeq ($(OS),Windows_NT)
+  MSVC      = ../../vc6
   RM = del /q /f
   CC = $(MSVC)/bin/cl $(CLFLAGS)
+  CLFLAGS   = /MD /TC /nologo $(CFLAGS_EXTRA) /W3 \
+              /I$(MSVC)/include /I.. /Dsnprintf=_snprintf
+  LFLAGS    = /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
 else
   UNAME_S := $(shell uname -s)
-  DLL_FLAGS += -shared
 
   ifeq ($(UNAME_S),Linux)
     CFLAGS += -ldl
   endif
 
   ifeq ($(UNAME_S),Darwin)
-    #  DLL_FLAGS += -bundle -undefined dynamic_lookup -dynamiclib
-    DLL_FLAGS += -flat_namespace -undefined suppress -dynamiclib
   endif
 endif
 
+all: hello websocket server post multi_threaded upload auth
+
 # To enable Lua in a server, uncomment following lines
 LUA    = ../lua-5.2.3/src
 #CFLAGS += -I$(LUA) -L$(LUA) -llua
 
-all: websocket_html.c
+hello: hello.c ../mongoose.c
 	$(CC) hello.c ../mongoose.c  -o hello $(CFLAGS)
+
+websocket: websocket_html.c websocket.c ../mongoose.c
 	$(CC) websocket.c websocket_html.c ../mongoose.c  -o websocket $(CFLAGS)
+
+server: server.c ../mongoose.c
+	$(CC) server.c ../mongoose.c -o server $(CFLAGS)
+
+post: post.c ../mongoose.c
 	$(CC) post.c ../mongoose.c -o post $(CFLAGS)
+
+multi_threaded: multi_threaded.c ../mongoose.c
 	$(CC) multi_threaded.c ../mongoose.c -o multi_threaded $(CFLAGS)
+
+upload: upload.c ../mongoose.c
 	$(CC) upload.c ../mongoose.c -o upload $(CFLAGS)
+
+auth: auth.c ../mongoose.c
 	$(CC) auth.c ../mongoose.c -o auth $(CFLAGS)
-	$(CC) server.c ../mongoose.c -o server $(CFLAGS)
 
 websocket_html.c: websocket.html
 	perl mkdata.pl $< > $@
diff --git a/examples/auth.c b/examples/auth.c
index 21d13c642..18835c729 100644
--- a/examples/auth.c
+++ b/examples/auth.c
@@ -2,25 +2,29 @@
 #include <string.h>
 #include "mongoose.h"
 
-static int auth_handler(struct mg_connection *conn) {
-  int result = MG_AUTH_FAIL; // Not authorized
-  FILE *fp;
-
-  // To populate passwords file, do
-  // mongoose -A my_passwords.txt mydomain.com admin admin
-  if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
-    result = mg_authorize_digest(conn, fp);
-    fclose(fp);
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+
+  if (ev == MG_AUTH) {
+    int result = MG_FALSE; // Not authorized
+    FILE *fp;
+
+    // To populate passwords file, do
+    // mongoose -A my_passwords.txt mydomain.com admin admin
+    if ((fp = fopen("my_passwords.txt", "r")) != NULL) {
+      result = mg_authorize_digest(conn, fp);
+      fclose(fp);
+    }
+
+    return result;
   }
 
-  return result;
+  return MG_FALSE;
 }
 
 int main(void) {
-  struct mg_server *server = mg_create_server(NULL);
+  struct mg_server *server = mg_create_server(NULL, ev_handler);
   mg_set_option(server, "listening_port", "8080");
   mg_set_option(server, "document_root", ".");
-  mg_set_auth_handler(server, auth_handler);
 
   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
   for (;;) {
diff --git a/examples/hello.c b/examples/hello.c
index 4ed9f4356..58a1e38c7 100644
--- a/examples/hello.c
+++ b/examples/hello.c
@@ -2,19 +2,25 @@
 #include <string.h>
 #include "mongoose.h"
 
-// This function will be called by mongoose on every new request
-static int index_html(struct mg_connection *conn) {
-  mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
-  return MG_REQUEST_PROCESSED;
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+  int result = MG_FALSE;
+
+  if (ev == MG_REQ_BEGIN) {
+    mg_printf_data(conn, "Hello! Requested URI is [%s]", conn->uri);
+    result = MG_TRUE;
+  } else if (ev == MG_AUTH) {
+    result = MG_TRUE;
+  }
+
+  return result;
 }
 
 int main(void) {
   struct mg_server *server;
 
   // Create and configure the server
-  server = mg_create_server(NULL);
+  server = mg_create_server(NULL, ev_handler);
   mg_set_option(server, "listening_port", "8080");
-  mg_set_request_handler(server, index_html);
 
   // Serve request. Hit Ctrl-C to terminate the program
   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
diff --git a/examples/multi_threaded.c b/examples/multi_threaded.c
index 134d88fab..54f1a8f44 100644
--- a/examples/multi_threaded.c
+++ b/examples/multi_threaded.c
@@ -2,11 +2,17 @@
 
 // Start a browser and hit refresh couple of times. The replies will
 // come from both server instances.
-static int request_handler(struct mg_connection *conn) {
-  mg_send_header(conn, "Content-Type", "text/plain");
-  mg_printf_data(conn, "This is a reply from server instance # %s",
-                 (char *) conn->server_param);
-  return MG_REQUEST_PROCESSED;
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+  if (ev == MG_REQ_BEGIN) {
+    mg_send_header(conn, "Content-Type", "text/plain");
+    mg_printf_data(conn, "This is a reply from server instance # %s",
+                   (char *) conn->server_param);
+    return MG_TRUE;
+  } else if (ev == MG_AUTH) {
+    return MG_TRUE;
+  } else {
+    return MG_FALSE;
+  }
 }
 
 static void *serve(void *server) {
@@ -17,11 +23,8 @@ static void *serve(void *server) {
 int main(void) {
   struct mg_server *server1, *server2;
 
-  server1 = mg_create_server((void *) "1");
-  server2 = mg_create_server((void *) "2");
-
-  mg_set_request_handler(server1, request_handler);
-  mg_set_request_handler(server2, request_handler);
+  server1 = mg_create_server((void *) "1", ev_handler);
+  server2 = mg_create_server((void *) "2", ev_handler);
 
   // Make both server1 and server2 listen on the same socket
   mg_set_option(server1, "listening_port", "8080");
diff --git a/examples/post.c b/examples/post.c
index b958b3858..510bac4ad 100644
--- a/examples/post.c
+++ b/examples/post.c
@@ -10,7 +10,7 @@ static const char *html_form =
   "<input type=\"submit\" />"
   "</form></body></html>";
 
-static int handler(struct mg_connection *conn) {
+static void send_reply(struct mg_connection *conn) {
   char var1[500], var2[500];
 
   if (strcmp(conn->uri, "/handle_post_request") == 0) {
@@ -33,18 +33,30 @@ static int handler(struct mg_connection *conn) {
     // Show HTML form.
     mg_send_data(conn, html_form, strlen(html_form));
   }
+}
 
-  return MG_REQUEST_PROCESSED;
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+  if (ev == MG_REQ_BEGIN) {
+    send_reply(conn);
+    return MG_TRUE;
+  } else if (ev == MG_AUTH) {
+    return MG_TRUE;
+  } else {
+    return MG_FALSE;
+  }
 }
 
 int main(void) {
-  struct mg_server *server = mg_create_server(NULL);
+  struct mg_server *server = mg_create_server(NULL, ev_handler);
+
   mg_set_option(server, "listening_port", "8080");
-  mg_set_request_handler(server, handler);
+
   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
   for (;;) {
     mg_poll_server(server, 1000);
   }
+
   mg_destroy_server(&server);
+
   return 0;
 }
diff --git a/examples/server.c b/examples/server.c
index 072f3e975..f21a2ba53 100644
--- a/examples/server.c
+++ b/examples/server.c
@@ -380,7 +380,7 @@ static void start_mongoose(int argc, char *argv[]) {
   char *options[MAX_OPTIONS];
   int i;
 
-  if ((server = mg_create_server(NULL)) == NULL) {
+  if ((server = mg_create_server(NULL, NULL)) == NULL) {
     die("%s", "Failed to start Mongoose.");
   }
 
diff --git a/examples/upload.c b/examples/upload.c
index 468ccc45b..cc38d1efd 100644
--- a/examples/upload.c
+++ b/examples/upload.c
@@ -5,7 +5,7 @@
 #include <string.h>
 #include "mongoose.h"
 
-static int index_html(struct mg_connection *conn) {
+static void send_index_page(struct mg_connection *conn) {
   const char *data;
   int data_len;
   char var_name[100], file_name[100];
@@ -29,17 +29,25 @@ static int index_html(struct mg_connection *conn) {
   }
 
   mg_printf_data(conn, "%s", "</body></html>");
+}
 
-  return MG_REQUEST_PROCESSED;
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+  if (ev == MG_REQ_BEGIN) {
+    send_index_page(conn);
+    return MG_TRUE;
+  } else if (ev == MG_AUTH) {
+    return MG_TRUE;
+  } else {
+    return MG_FALSE;
+  }
 }
 
 int main(void) {
   struct mg_server *server;
 
   // Create and configure the server
-  server = mg_create_server(NULL);
+  server = mg_create_server(NULL, ev_handler);
   mg_set_option(server, "listening_port", "8080");
-  mg_set_request_handler(server, index_html);
 
   // Serve request. Hit Ctrl-C to terminate the program
   printf("Starting on port %s\n", mg_get_option(server, "listening_port"));
diff --git a/examples/websocket.c b/examples/websocket.c
index 1c59b2c2d..08bd79a82 100644
--- a/examples/websocket.c
+++ b/examples/websocket.c
@@ -1,18 +1,20 @@
 #include <string.h>
+#include <time.h>
 #include "mongoose.h"
 
 extern const char *find_embedded_file(const char *, size_t *);
 
-static int iterate_callback(struct mg_connection *c) {
-  if (c->is_websocket) {
+static int iterate_callback(struct mg_connection *c, enum mg_event ev) {
+  static int counter = 0;
+  if (ev == MG_POLL && c->is_websocket) {
     char buf[20];
-    int len = snprintf(buf, sizeof(buf), "%d", * (int *) c->callback_param);
+    int len = snprintf(buf, sizeof(buf), "%d", counter++);
     mg_websocket_write(c, 1, buf, len);
   }
-  return MG_REQUEST_PROCESSED;
+  return MG_TRUE;
 }
 
-static int index_html(struct mg_connection *conn) {
+static int send_reply(struct mg_connection *conn) {
   size_t index_size;
   const char *index_html = find_embedded_file("websocket.html", &index_size);
 
@@ -22,27 +24,37 @@ static int index_html(struct mg_connection *conn) {
     // Echo websocket data back to the client.
     mg_websocket_write(conn, 1, conn->content, conn->content_len);
     return conn->content_len == 4 && !memcmp(conn->content, "exit", 4) ?
-      MG_CLIENT_CLOSE : MG_CLIENT_CONTINUE;
+      MG_FALSE : MG_TRUE;
   } else {
     mg_send_header(conn, "Content-Type", "text/html");
     mg_send_data(conn, index_html, index_size);
-    return MG_REQUEST_PROCESSED;
+    return MG_TRUE;
+  }
+}
+
+static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
+  if (ev == MG_REQ_BEGIN) {
+    return send_reply(conn);
+  } else if (ev == MG_AUTH) {
+    return MG_TRUE;
+  } else {
+    return MG_FALSE;
   }
 }
 
 int main(void) {
-  struct mg_server *server = mg_create_server(NULL);
-  unsigned int current_timer = 0, last_timer = 0;
+  struct mg_server *server = mg_create_server(NULL, ev_handler);
+  time_t current_timer = 0, last_timer = time(NULL);
 
   mg_set_option(server, "listening_port", "8080");
-  mg_set_request_handler(server, index_html);
 
   printf("Started on port %s\n", mg_get_option(server, "listening_port"));
   for (;;) {
-    current_timer = mg_poll_server(server, 100);
+    mg_poll_server(server, 100);
+    current_timer = time(NULL);
     if (current_timer - last_timer > 0) {
       last_timer = current_timer;
-      mg_iterate_over_connections(server, iterate_callback, &current_timer);
+      mg_iterate_over_connections(server, iterate_callback);
     }
   }
 
-- 
GitLab