From 4c10d1f356cd7c66d701f42da5cacd86619229ad Mon Sep 17 00:00:00 2001
From: Sergey Lyubka <valenok@gmail.com>
Date: Fri, 10 Oct 2014 13:58:45 +0100
Subject: [PATCH] mongoose_server -> web_server

---
 examples/Makefile                             | 88 ++-----------------
 examples/csharp/example.cs                    | 43 +++++++++
 examples/csharp/mongoose.cs                   | 68 ++++++++++++++
 .../multi_threaded_server.c                   |  2 +-
 .../web_server.c}                             |  0
 5 files changed, 119 insertions(+), 82 deletions(-)
 create mode 100644 examples/csharp/example.cs
 create mode 100644 examples/csharp/mongoose.cs
 rename examples/{mongoose_server/mongoose_server.c => web_server/web_server.c} (100%)

diff --git a/examples/Makefile b/examples/Makefile
index bf650efcd..2964d37e9 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -1,89 +1,15 @@
 # Copyright (c) 2014 Cesanta Software
 # All rights reserved
-#
-# Makefile for Mongoose web server examples
 
-CFLAGS = -W -Wall -I.. -pthread -g -pipe $(CFLAGS_EXTRA)
-RM = rm -rf
-OUT = -o $@
-ALL_PROGS = server post
-NS = ../../net_skeleton
-SW = ../../ssl_wrapper
+SUBDIRS = $(sort $(filter-out csharp/, $(dir $(wildcard */))))
+X = $(SUBDIRS)
 
-ifeq ($(OS),Windows_NT)
-  ifndef MINGW
-    MSVC = ../../vc6
-    RM = del /q /f
-    OUT =
-    CC = $(MSVC)/bin/cl
-    CFLAGS = /MD /TC /nologo /W3 /I$(MSVC)/include /I..
-    CFLAGS += /link /incremental:no /libpath:$(MSVC)/lib /machine:IX86
-    CFLAGS += $(CFLAGS_EXTRA)
-  else
-    CC = g++
-    CFLAGS = -W -Wall -Wno-unused-parameter -I.. -O0 -g -pipe $(CFLAGS_EXTRA) -lwsock32
-  endif
-else
-  UNAME_S := $(shell uname -s)
-  CC = g++
-  CFLAGS += -lssl -g -O0
+.PHONY: $(SUBDIRS)
 
-  ifeq ($(UNAME_S),Linux)
-    CFLAGS += -ldl
-  endif
+all: $(SUBDIRS)
 
-  ifeq ($(UNAME_S),Darwin)
-  endif
-endif
-
-all: $(ALL_PROGS)
-
-# To enable Lua in a server, uncomment following lines
-LUA    = ../lua-5.2.3/src
-#CFLAGS += -I$(LUA) -L$(LUA) -llua -DMONGOOSE_USE_LUA
-
-server: server.c ../mongoose.c
-	$(CC) server.c ../mongoose.c $(OUT)  $(CFLAGS)
-
-hello: hello.c ../mongoose.c
-	$(CC) hello.c ../mongoose.c $(OUT) $(CFLAGS)
-
-websocket: websocket_html.c websocket.c ../mongoose.c
-	$(CC) websocket.c websocket_html.c ../mongoose.c $(OUT)  $(CFLAGS)
-
-post: post.c ../mongoose.c
-	$(CC) post.c ../mongoose.c $(OUT) $(CFLAGS)
-
-multi_threaded: multi_threaded.c ../mongoose.c
-	$(CC) multi_threaded.c ../mongoose.c $(OUT) $(CFLAGS)
-
-upload: upload.c ../mongoose.c
-	$(CC) upload.c ../mongoose.c $(OUT) $(CFLAGS)
-
-auth: auth.c ../mongoose.c
-	$(CC) auth.c ../mongoose.c $(OUT) $(CFLAGS)
-
-file: file.c ../mongoose.c
-	$(CC) file.c ../mongoose.c $(OUT) $(CFLAGS)
-
-form: form.c ../mongoose.c
-	$(CC) form.c ../mongoose.c $(OUT) $(CFLAGS)
-
-mjpg: mjpg.c ../mongoose.c
-	$(CC) mjpg.c ../mongoose.c $(OUT) $(CFLAGS)
-
-pubsub: websocket2.c ../mongoose.c
-	$(CC) websocket2.c ../mongoose.c $(OUT) $(CFLAGS)
-
-proxy: proxy.c ../mongoose.c
-	$(CC) proxy.c ../mongoose.c $(OUT) -I$(NS) -DNS_ENABLE_SSL -lssl $(CFLAGS)
-
-websocket_html.c: websocket.html
-	perl mkdata.pl $< > $@
-
-u:
-	$(CC) unit_test.c $(OUT) $(CFLAGS)
-	./$@
+$(SUBDIRS):
+	@$(MAKE) -C $@
 
 clean:
-	-@$(RM) $(ALL_PROGS) websocket_html.c *.exe *.dSYM *.obj *.exp .*o *.lib u
+	for d in $(SUBDIRS) ; do $(MAKE) -C $$d clean ; done
\ No newline at end of file
diff --git a/examples/csharp/example.cs b/examples/csharp/example.cs
new file mode 100644
index 000000000..3b0ced68d
--- /dev/null
+++ b/examples/csharp/example.cs
@@ -0,0 +1,43 @@
+// This file is part of mongoose web server project,
+// https://github.com/cesanta/mongoose
+
+using System;
+
+public class Program {
+  static private int EventHandler(IntPtr conn_ptr, int ev) {
+		MongooseConnection conn = (MongooseConnection)
+			System.Runtime.InteropServices.Marshal.PtrToStructure(
+				conn_ptr , typeof(MongooseConnection));
+
+		if (ev == 102) {
+			// MG_AUTH
+			return 1;
+		} else if (ev == 103) {
+			// MG_REQUEST
+	    Mongoose.send_data(conn_ptr, "Hello from C#!\n");
+			Mongoose.send_data(conn_ptr, "URI: " + conn.uri + "\n");
+			Mongoose.send_data(conn_ptr, "HTTP Headers:\n");
+			
+			for (int i = 0; i < conn.num_headers; i++) {
+				IntPtr name = conn.http_headers[i].name;
+				IntPtr val = conn.http_headers[i].value;
+				System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name);
+				Mongoose.send_data(conn_ptr, "  " +
+					System.Runtime.InteropServices.Marshal.PtrToStringAnsi(name) + ": " +
+						System.Runtime.InteropServices.Marshal.PtrToStringAnsi(val) + "\n");
+			}			
+      return 1;
+		}
+		return 0;
+  }
+
+  static void Main() {
+    Mongoose web_server = new Mongoose(".", "9001",
+        new MongooseEventHandler(EventHandler));
+
+    Console.WriteLine("Mongoose started, press Ctrl-C to exit.");
+		for (;;) {
+			web_server.poll(1000);
+		}
+  }
+}
diff --git a/examples/csharp/mongoose.cs b/examples/csharp/mongoose.cs
new file mode 100644
index 000000000..efd26581e
--- /dev/null
+++ b/examples/csharp/mongoose.cs
@@ -0,0 +1,68 @@
+// This file is part of mongoose web server project,
+// https://github.com/cesanta/mongoose
+
+using System;
+using System.Runtime.InteropServices;
+
+[StructLayout(LayoutKind.Sequential)] public struct MongooseHeader {
+  [MarshalAs(UnmanagedType.LPTStr)] public IntPtr name;
+  [MarshalAs(UnmanagedType.LPTStr)] public IntPtr value;
+};
+
+// mongoose.h :: struct mg_connection
+[StructLayout(LayoutKind.Sequential)] public struct MongooseConnection {
+  [MarshalAs(UnmanagedType.LPTStr)] public string request_method;
+  [MarshalAs(UnmanagedType.LPTStr)] public string uri;
+  [MarshalAs(UnmanagedType.LPTStr)] public string http_version;
+  [MarshalAs(UnmanagedType.LPTStr)] public string query_string;
+
+	[MarshalAs(UnmanagedType.ByValArray,SizeConst=48)] public char[] remote_ip;
+	[MarshalAs(UnmanagedType.LPTStr)] public string local_ip;
+	[MarshalAs(UnmanagedType.U2)] public short remote_port;
+	[MarshalAs(UnmanagedType.U2)] public short local_port;
+	
+	[MarshalAs(UnmanagedType.SysInt)] public int num_headers;
+  [MarshalAs(UnmanagedType.ByValArray,SizeConst=30)]
+    public MongooseHeader[] http_headers;
+	
+	[MarshalAs(UnmanagedType.LPTStr)] public IntPtr content;
+	[MarshalAs(UnmanagedType.SysInt)] public int content_len;
+	
+	[MarshalAs(UnmanagedType.SysInt)] public int is_websocket;
+	[MarshalAs(UnmanagedType.SysInt)] public int status_code;
+	[MarshalAs(UnmanagedType.SysInt)] public int wsbits;
+};
+
+public delegate int MongooseEventHandler(IntPtr c, int ev);
+
+public class Mongoose {
+  public const string dll_ = "mongoose";
+  private IntPtr server_;
+
+	[DllImport(dll_)] private static extern IntPtr
+		mg_create_server(IntPtr user_data, MongooseEventHandler eh);
+	[DllImport(dll_)] private static extern int
+		mg_poll_server(IntPtr server, int milli);
+  [DllImport(dll_)] private static extern IntPtr
+		mg_set_option(IntPtr server, string name, string value);
+	[DllImport(dll_)] public static extern int
+		mg_send_data(IntPtr conn, string data, int length);	
+
+  public Mongoose(string document_root,
+                  string listening_port,
+                  MongooseEventHandler event_handler) {
+    server_ = mg_create_server(IntPtr.Zero, event_handler);
+		mg_set_option(server_, "document_root", document_root);
+		mg_set_option(server_, "listening_port", listening_port);
+  }
+	
+  public static int send_data(IntPtr conn, string data) {
+    return mg_send_data(conn, data, data.Length);
+  }
+	
+	public void poll(int milli) {
+    mg_poll_server(server_, milli);
+  }
+	
+	// TODO: add destructor and call mg_destroy_server()
+}
diff --git a/examples/multi_threaded_server/multi_threaded_server.c b/examples/multi_threaded_server/multi_threaded_server.c
index b087534c7..9e45fb3cf 100644
--- a/examples/multi_threaded_server/multi_threaded_server.c
+++ b/examples/multi_threaded_server/multi_threaded_server.c
@@ -28,7 +28,7 @@ int main(void) {
 
   // Make both server1 and server2 listen on the same socket
   mg_set_option(server1, "listening_port", "8080");
-  mg_set_listening_socket(server2, mg_get_listening_socket(server1));
+  //mg_set_listening_socket(server2, mg_get_listening_socket(server1));
 
   // server1 goes to separate thread, server 2 runs in main thread.
   // IMPORTANT: NEVER LET DIFFERENT THREADS HANDLE THE SAME SERVER.
diff --git a/examples/mongoose_server/mongoose_server.c b/examples/web_server/web_server.c
similarity index 100%
rename from examples/mongoose_server/mongoose_server.c
rename to examples/web_server/web_server.c
-- 
GitLab