sync tinyweb codes
authorchengtao.liu <chengtaox.liu@intel.com>
Fri, 14 Feb 2014 05:47:11 +0000 (13:47 +0800)
committerchengtao.liu <chengtaox.liu@intel.com>
Fri, 14 Feb 2014 05:57:26 +0000 (13:57 +0800)
12 files changed:
Makefile
README.md [new file with mode: 0644]
VERSION
cgi-getcookie.c [new file with mode: 0644]
cgi-getfield.c [new file with mode: 0644]
echo.c
mongoose.c
mongoose.h
packaging/tinyweb.spec [deleted file]
tinyweb.c
webservice.c [new file with mode: 0644]
websocket.c [new file with mode: 0644]

index 70423281b6773ad896e9eef65ece677c7d5c708c..6e35bd45b0565f28e7fc83fea4e4029431081ef5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,9 @@
-OBJ=tinyweb
-CFLAGS=        -W -Wall -I.. -pthread
+CFLAGS=        -W -Wall -I.. -pthread -g
 
-all: $(OBJ)
-
-tinyweb: echo
-       $(CC) tinyweb.c -ldl -lmongoose -L./ -o tinyweb;
-
-echo: mongoose
-       $(CC) -shared -fPIC -lmongoose -L./ echo.c -o echo.so
-
-mongoose:
-       $(CC) -shared -fPIC $(CFLAGS) -DUSE_WEBSOCKET mongoose.c -ldl -o libmongoose.so;
-
-install: $(OBJ)
-       install -d $(DESTDIR)/usr/bin/
-       install -m 755 $(OBJ) $(DESTDIR)/usr/bin/
-       install -d $(DESTDIR)/usr/lib
-       install -m 755 libmongoose.so $(DESTDIR)/usr/lib/
-       install -m 755 echo.so $(DESTDIR)/usr/lib/
-       install -m 755 -d $(DESTDIR)/usr/share/$(OBJ)
-       install -m 755 server.pem $(DESTDIR)/usr/share/$(OBJ)
-       ln -s /usr/lib/libssl.so.1.0.0 /usr/lib/libssl.so
-       ln -s /usr/lib/libcrypto.so.1.0.0 /usr/lib/libcrypto.so
+all:
+       $(CC) websocket.c tinyweb.c mongoose.c $(CFLAGS) -DUSE_WEBSOCKET -ldl -lutil -o tinyweb;
+       $(CC) cgi-getfield.c -o cgi-getfield;
+       $(CC) cgi-getcookie.c -o cgi-getcookie;
 
 clean:
-       rm -rf tinyweb *.so
+       rm -rf tinyweb cgi-getcookie cgi-getfield
diff --git a/README.md b/README.md
new file mode 100644 (file)
index 0000000..df3950d
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+Tinyweb
diff --git a/VERSION b/VERSION
index e3462940604b0ed68ebec6603d5b0a0cabd3c4b3..7d385d419cc848ee6394256288edbcd8f4f7522a 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-0.22
+0.25
diff --git a/cgi-getcookie.c b/cgi-getcookie.c
new file mode 100644 (file)
index 0000000..d036f33
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+
+Authors:
+        Wang, Jing J <jing.j.wang@intel.com>
+
+*/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+static char *
+scanspaces(char *p) {
+    while (*p == ' ' || *p == '\t') {
+        p++;
+    }
+    return p;
+}
+
+static char* scan_cookie(char *p, char **items) {
+    int quote = 0;
+    items[0] = p = scanspaces(p);
+    while (*p != '=' && *p != 0) {
+        p++;
+    }
+    if (*p != '=' || p == items[0]) {
+        return 0;
+    }
+    *p++ = 0;
+    if (*p == '"' || *p == '\'' || *p == '`') {
+        quote = *p++;
+    }
+    items[1] = p;
+    if (quote != 0) {
+        while(*p != quote && *p != 0) {
+            p++;
+        }
+        if (*p != quote) {
+            return 0;
+        }
+        *p++ = 0;
+        if (*p == ';') {
+            p++;
+        }
+    }
+    else {
+        while (*p != ';' && *p != ' ' && *p != '\t' &&
+            *p != '\r' && *p != '\n' && *p != 0)
+        {
+            p++;
+        }
+        if (*p != 0) {
+            *p++ = 0;
+        }
+    }
+    return p;
+}
+
+int main(int argc, char **argv) {
+    char *items[2];
+    char buf[4096], *p;
+    int len = -1;
+    if (argc != 2)
+        return -1;
+    char *field = argv[1];
+    if ((len = fread(buf, 1, 4096, stdin)) > 0) {
+        buf[len] = 0;
+        p = buf;
+        while ((p = scan_cookie(p, items)) != 0) {
+            if (strcmp(items[0], field) == 0) {
+                printf("%s", items[1]);
+                return 0;
+            }
+        }
+    }
+    return -1;
+}
diff --git a/cgi-getfield.c b/cgi-getfield.c
new file mode 100644 (file)
index 0000000..a25309c
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+
+Authors:
+        Wang, Jing J <jing.j.wang@intel.com>
+
+*/
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+#define MAX_QUERY_SIZE 1024
+static int
+hex(int digit) {
+    switch(digit) {
+
+    case '0': case '1': case '2': case '3': case '4':
+    case '5': case '6': case '7': case '8': case '9':
+        return digit - '0';
+
+    case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
+        return 10 + digit - 'A';
+
+    case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
+        return 10 + digit - 'a';
+
+    default:
+        return -1;
+    }
+}
+
+
+int decode_query(const char *query, char *pkey, char *pval, int *psize) {
+    char buf[256], *name, *value;
+    int i, k, j, L, R, done;
+
+    if (query == 0) {
+        return -1;
+    }
+    name = value = 0;
+    for (i = j = k = done = 0; done == 0; i++) {
+        switch (query[i]) {
+        case '=':
+            if (name != 0) {
+                break;  /* treat extraneous '=' as data */
+            }
+            if (name == 0 && k > 0) {
+                name = buf;
+                buf[k++] = 0;
+                j = k;
+                value = buf + k;
+            }
+            continue;
+
+        case 0:
+            done = 1;  /* fall through */
+
+        case '&':
+            buf[k] = 0;
+            if (name == 0 && k > 0) {
+                name = buf;
+                value = buf + k;
+            }
+            if (name != 0) {
+                if (strncmp(name, pkey, strlen(pkey)) == 0) {
+                    *psize = k - j;
+                    memcpy(pval, value, k - j);
+                    return 0;          
+                }
+            }
+            k = 0;
+            name = value = 0;
+            continue;
+
+        case '+':
+            buf[k++] = ' ';
+            continue;
+
+        case '%':
+            if ((L = hex(query[i + 1])) >= 0 &&
+                (R = hex(query[i + 2])) >= 0)
+            {
+                buf[k++] = (L << 4) + R;
+                i += 2;
+                continue;
+            }
+            break;  /* treat extraneous '%' as data */
+        }
+        buf[k++] = query[i];
+    }
+    return -1;
+}
+
+int
+main(int argc, char **argv) {
+    char qbuf[MAX_QUERY_SIZE], val[MAX_QUERY_SIZE];
+    int len = MAX_QUERY_SIZE;
+    int size = 0;
+    if (argc < 2)
+        return -1;
+    char *key = argv[1];
+    if (argc > 2)
+        len = atoi(argv[2]);  
+    if ((len = fread(qbuf, 1, len, stdin)) > 0) {
+        qbuf[len] = 0;
+        if (decode_query(qbuf, key, val, &size) == 0) {
+            fwrite(val, 1, size, stdout);
+            return 0;
+        }
+    }
+    return -1;
+}
diff --git a/echo.c b/echo.c
index 72e46252334361c5f7411fa27b52e79c615294e7..6136ce8f65b0d108b1349ff35e297b89ab7dda3b 100644 (file)
--- a/echo.c
+++ b/echo.c
@@ -1,32 +1,17 @@
 /*\r
-Copyright (c) 2013 Intel Corporation.\r
-\r
-Redistribution and use in source and binary forms, with or without modification,\r
-are permitted provided that the following conditions are met:\r
-\r
-* Redistributions of works must retain the original copyright notice, this list\r
-  of conditions and the following disclaimer.\r
-* Redistributions in binary form must reproduce the original copyright notice,\r
-  this list of conditions and the following disclaimer in the documentation\r
-  and/or other materials provided with the distribution.\r
-* Neither the name of Intel Corporation nor the names of its contributors\r
-  may be used to endorse or promote products derived from this work without\r
-  specific prior written permission.\r
-\r
-THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS"\r
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT,\r
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
-OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
-EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU General Public License,\r
+ * version 2, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+ * more details.\r
 \r
 Authors:\r
         Wang, Jing J <jing.j.wang@intel.com>\r
-\r
 */\r
 \r
 #include <stdio.h>\r
index f1c59d16eceb6a58252e414b70e6f9272c3d174e..57f90714128208b6b22242c35b3f099f237c28f3 100644 (file)
@@ -1,33 +1,19 @@
 /*\r
-Copyright (c) 2013 Intel Corporation.\r
-\r
-Redistribution and use in source and binary forms, with or without modification,\r
-are permitted provided that the following conditions are met:\r
-\r
-* Redistributions of works must retain the original copyright notice, this list\r
-  of conditions and the following disclaimer.\r
-* Redistributions in binary form must reproduce the original copyright notice,\r
-  this list of conditions and the following disclaimer in the documentation\r
-  and/or other materials provided with the distribution.\r
-* Neither the name of Intel Corporation nor the names of its contributors\r
-  may be used to endorse or promote products derived from this work without\r
-  specific prior written permission.\r
-\r
-THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS"\r
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT,\r
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
-OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
-EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU General Public License,\r
+ * version 2, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+ * more details.\r
 \r
 Authors:\r
         Wang, Jing J <jing.j.wang@intel.com>\r
-\r
 */\r
+\r
 #if defined(_WIN32)\r
 #if !defined(_CRT_SECURE_NO_WARNINGS)\r
 #define _CRT_SECURE_NO_WARNINGS // Disable deprecation warning in VS2005\r
@@ -450,7 +436,7 @@ enum {
   ACCESS_LOG_FILE, ENABLE_DIRECTORY_LISTING, ERROR_LOG_FILE,\r
   GLOBAL_PASSWORDS_FILE, INDEX_FILES, ENABLE_KEEP_ALIVE, ACCESS_CONTROL_LIST,\r
   EXTRA_MIME_TYPES, LISTENING_PORTS, DOCUMENT_ROOT, SSL_CERTIFICATE,\r
-  NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT,\r
+  NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT, PID_FILE,\r
   NUM_OPTIONS\r
 };\r
 \r
@@ -480,6 +466,7 @@ static const char *config_options[] = {
   "url_rewrite_patterns", NULL,\r
   "hide_files_patterns", NULL,\r
   "request_timeout_ms", "30000",\r
+  "pidfile", NULL,\r
   NULL\r
 };\r
 \r
index 00c33f1bd871dc6757b0da1943e9f6e06062bf40..c51f3410e2717da90d07aaef9e0245cd3355e912 100644 (file)
@@ -1,32 +1,17 @@
 /*\r
-Copyright (c) 2013 Intel Corporation.\r
-\r
-Redistribution and use in source and binary forms, with or without modification,\r
-are permitted provided that the following conditions are met:\r
-\r
-* Redistributions of works must retain the original copyright notice, this list\r
-  of conditions and the following disclaimer.\r
-* Redistributions in binary form must reproduce the original copyright notice,\r
-  this list of conditions and the following disclaimer in the documentation\r
-  and/or other materials provided with the distribution.\r
-* Neither the name of Intel Corporation nor the names of its contributors\r
-  may be used to endorse or promote products derived from this work without\r
-  specific prior written permission.\r
-\r
-THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS"\r
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT,\r
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
-OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
-EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU General Public License,\r
+ * version 2, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+ * more details.\r
 \r
 Authors:\r
         Wang, Jing J <jing.j.wang@intel.com>\r
-\r
 */\r
 \r
 #ifndef MONGOOSE_HEADER_INCLUDED\r
diff --git a/packaging/tinyweb.spec b/packaging/tinyweb.spec
deleted file mode 100644 (file)
index 3a40ac5..0000000
+++ /dev/null
@@ -1,55 +0,0 @@
-Name:       tinyweb
-Summary:    test
-Version:    0.22
-Release:    1
-Group:      Development/Debug
-License:    GPL v2 only
-URL:        http://www.tizen.org/
-Source0:    %{name}-%{version}.tar.gz
-BuildRoot:  %{_tmppath}/%{name}-%{version}-build
-
-%description
-A lightweight web server. It support http directory, websocket, cgi etc.
-
-
-%prep
-%setup -q -n %{name}-%{version}
-# >> setup
-# << setup
-
-%build
-# >> build pre
-# << build pre
-
-
-# Call make instruction with smp support
-make %{?jobs:-j%jobs}
-
-# >> build post
-# << build post
-%install
-rm -rf %{buildroot}
-# >> install pre
-# << install pre
-%make_install
-
-# >> install post
-# << install post
-
-%clean
-rm -rf %{buildroot}
-
-%files
-%defattr(-,root,root,-)
-# >> files
-%{_bindir}/tinyweb
-%{_libdir}/echo.so
-%{_libdir}/libmongoose.so
-%{_datadir}/%{name}/server.pem
-
-%{_libdir}/
-# << files
-
-%changelog
-* Tue Mar  21 2013 jing wang <jing.j.wang@intel.com> 0.22
-- create for tct2.1 build
index 10909b438e5a1277758e7390331165572adc11b8..2dc1364d760b1ac163674f6ed66e1909feb74ba2 100644 (file)
--- a/tinyweb.c
+++ b/tinyweb.c
@@ -1,32 +1,17 @@
 /*\r
-Copyright (c) 2013 Intel Corporation.\r
-\r
-Redistribution and use in source and binary forms, with or without modification,\r
-are permitted provided that the following conditions are met:\r
-\r
-* Redistributions of works must retain the original copyright notice, this list\r
-  of conditions and the following disclaimer.\r
-* Redistributions in binary form must reproduce the original copyright notice,\r
-  this list of conditions and the following disclaimer in the documentation\r
-  and/or other materials provided with the distribution.\r
-* Neither the name of Intel Corporation nor the names of its contributors\r
-  may be used to endorse or promote products derived from this work without\r
-  specific prior written permission.\r
-\r
-THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION "AS IS"\r
-AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
-IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE\r
-ARE DISCLAIMED. IN NO EVENT SHALL INTEL CORPORATION BE LIABLE FOR ANY DIRECT,\r
-INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,\r
-BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY\r
-OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
-NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
-EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU General Public License,\r
+ * version 2, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+ * more details.\r
 \r
 Authors:\r
         Wang, Jing J <jing.j.wang@intel.com>\r
-\r
 */\r
 \r
 #define _XOPEN_SOURCE 600  // For PATH_MAX on linux\r
@@ -53,73 +38,19 @@ Authors:
 \r
 #define MAX_OPTIONS 40\r
 #define MAX_CONF_FILE_LINE_SIZE (8 * 1024)\r
-\r
+extern void websocket_ready_handler(struct mg_connection *);\r
+extern int websocket_data_handler(struct mg_connection *, int, char *, size_t);\r
 static int exit_flag;\r
 static char server_name[40];        // Set by init_server_name()\r
 static char config_file[PATH_MAX];  // Set by process_command_line_arguments()\r
 static struct mg_context *ctx;      // Set by start_server()\r
-\r
 #if !defined(CONFIG_FILE)\r
 #define CONFIG_FILE "mongoose.conf"\r
 #endif /* !CONFIG_FILE */\r
-\r
-static void *get_app(struct mg_connection *conn) {\r
-  char app_name[128];\r
-  const char *uri = mg_get_request_info(conn)->uri;\r
-  if (strcmp(uri, "/") == 0){\r
-    snprintf(app_name, sizeof(app_name), "/usr/lib%secho.so", uri);\r
-  }else{\r
-    snprintf(app_name, sizeof(app_name), "/usr/lib%s.so", uri);\r
-  }\r
-//  printf("app_name=%s\n", app_name);\r
-  void *ws_handle = NULL;\r
-  if ((ws_handle = dlopen(app_name, RTLD_LAZY)) == NULL) {\r
-    fprintf(stderr, "%s: cannot load %s\n", __func__, app_name);\r
-    return NULL;\r
-  }\r
-  return ws_handle;\r
-}\r
-\r
-static void websocket_ready_handler(struct mg_connection *conn){\r
-  const char *prot = mg_get_header(conn, "Sec-WebSocket-Protocol");\r
-  char buf[100];\r
-  if (prot){\r
-      char *p = NULL;\r
-      snprintf(buf, sizeof(buf), "%s", prot);\r
-      if ((p = strrchr(buf, ',')) != NULL) {\r
-        *p = '\0';\r
-      } \r
-      mg_printf(conn, "Sec-WebSocket-Protocol: %s\r\n", buf);\r
-  }\r
-  mg_printf(conn, "\r\n");\r
-}\r
-\r
-\r
-// Arguments:\r
-//   flags: first byte of websocket frame, see websocket RFC,\r
-//          http://tools.ietf.org/html/rfc6455, section 5.2\r
-//   data, data_len: payload data. Mask, if any, is already applied.\r
-static int websocket_data_handler(struct mg_connection *conn, int flags,\r
-                                  char *data, size_t data_len){\r
-  void *ws_handle = get_app(conn);\r
-  if (ws_handle == NULL) {\r
-    return 0;\r
-  }\r
-  int (*func)(struct mg_connection *, int, char *, size_t) = dlsym(ws_handle, "websocket_data");\r
-  if (func == NULL){\r
-    dlclose(ws_handle);\r
-    return 0; \r
-  }\r
-  int ret = (*func)(conn, flags, data, data_len);\r
-  dlclose(ws_handle);\r
-  return ret; \r
-}\r
-\r
 static void WINCDECL signal_handler(int sig_num) {\r
-  exit_flag = sig_num;\r
+    exit_flag = sig_num;\r
 }\r
 \r
-\r
 static void die(const char *fmt, ...) {\r
   va_list ap;\r
   char msg[200];\r
@@ -319,7 +250,8 @@ static void start_server(int argc, char *argv[]) {
 }\r
 \r
 int main(int argc, char *argv[]) {\r
-  init_server_name();\r
+  signal(SIGCHLD, SIG_IGN);\r
+  signal(SIGHUP, SIG_IGN);\r
   pid_t pid = 0;\r
   pid = fork();\r
   if (pid < 0) {\r
@@ -327,23 +259,34 @@ int main(int argc, char *argv[]) {
     exit(1);\r
   }\r
   if (pid > 0) {\r
-    fprintf(stderr, "Daemonize\n");\r
+    sleep(1);\r
     exit(0);\r
   }\r
   umask(0);\r
   setsid();\r
+  chdir("/");\r
+\r
+  init_server_name();\r
   start_server(argc, argv);\r
-  printf("%s started on port(s) %s with web root [%s]\n",\r
+  pid = getpid();\r
+  printf("%s started on port(s) %s with document root [%s], pid [%d]\n",\r
          server_name, mg_get_option(ctx, "listening_ports"),\r
-         mg_get_option(ctx, "document_root"));\r
+         mg_get_option(ctx, "document_root"), pid);\r
+\r
+  char *pidfile = (char *)mg_get_option(ctx, "pidfile");\r
+  if (*pidfile) {\r
+    FILE *fp = fopen(pidfile, "w+");\r
+    fprintf(fp, "%d", pid);\r
+    fclose(fp);\r
+  }\r
+\r
   while (exit_flag == 0) {\r
     sleep(1);\r
   }\r
   printf("Exiting on signal %d, waiting for all threads to finish...",\r
          exit_flag);\r
+  printf("%s", " done.\n");\r
   fflush(stdout);\r
   mg_stop(ctx);\r
-  printf("%s", " done.\n");\r
-\r
   return EXIT_SUCCESS;\r
 }\r
diff --git a/webservice.c b/webservice.c
new file mode 100644 (file)
index 0000000..8327f4c
--- /dev/null
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "mongoose.h"
+
+extern struct mg_context *ctx;      // Set by start_server()
+int webservice_handler(struct mg_connection *conn) {
+  const struct mg_request_info *ri = mg_get_request_info(conn);
+  char *post_data, fname[256], *data;
+  int post_data_len;
+  #define POST_SIZE 4*1024*1024
+  if (!strcmp(ri->uri, "/save_file")) {
+      char fpath[256], *ptr;
+      //Apply space to contain post data
+      post_data = (char *)malloc(POST_SIZE);
+      // User has submitted a form, show submitted data and a variable value
+      post_data_len = mg_read(conn, post_data, POST_SIZE);
+
+      data = (char *)malloc(POST_SIZE);
+      // Parse post data.
+      mg_get_var(post_data, post_data_len, "filename", fname, sizeof(fname));
+      mg_get_var(post_data, post_data_len, "data", data, POST_SIZE);
+      free(post_data);
+      ptr = (char *)mg_get_option(ctx, "outdir");
+      if (*ptr) {
+        sprintf(fpath, "%s/%s", ptr, fname);
+        FILE *fp = fopen(fpath, "w+");
+        fputs(data, fp);
+        fclose(fp);
+      } else
+        printf("data=%s\n", data);
+      // Send Ok reply to the client.
+      mg_printf(conn, "HTTP/1.0 200 OK\r\n");
+      free(data);
+      return 1;  // Mark event as processed
+  } 
+
+  return 0;  // Mark request as processed
+}
diff --git a/websocket.c b/websocket.c
new file mode 100644 (file)
index 0000000..8274982
--- /dev/null
@@ -0,0 +1,116 @@
+/*\r
+ * Copyright (c) 2013-2014 Intel Corporation, All Rights Reserved\r
+ *\r
+ * This program is free software; you can redistribute it and/or modify it\r
+ * under the terms and conditions of the GNU General Public License,\r
+ * version 2, as published by the Free Software Foundation.\r
+ *\r
+ * This program is distributed in the hope it will be useful, but WITHOUT\r
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
+ * more details.\r
+\r
+Authors:\r
+        Wang, Jing J <jing.j.wang@intel.com>\r
+*/\r
+\r
+#include <sys/stat.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <signal.h>\r
+#include <string.h>\r
+#include <errno.h>\r
+#include <limits.h>\r
+#include <stddef.h>\r
+#include <stdarg.h>\r
+#include <ctype.h>\r
+#include <dlfcn.h>\r
+#include "mongoose.h"\r
+\r
+#include <sys/wait.h>\r
+#include <unistd.h>\r
+#include <sys/stat.h>\r
+\r
+// Arguments:\r
+//   flags: first byte of websocket frame, see websocket RFC,\r
+//          http://tools.ietf.org/html/rfc6455, section 5.2\r
+//   data, data_len: payload data. Mask, if any, is already applied.\r
+static int websocket_data(struct mg_connection *conn, int flags,\r
+                                  char *data, size_t data_len) {\r
+  unsigned char reply[4];\r
+  int offset = 2;\r
+  int ret = 1;\r
+\r
+//  (void) flags;\r
+\r
+  // Truncate echoed message, to simplify output code.\r
+  if (data_len > 65536) {\r
+    data_len = 65536;\r
+  }  \r
+  printf("data_len=%d\n",data_len);\r
+  int opcode = flags & 0x0f;\r
+  if ((opcode == 0x08) || memcmp(data, ".close", 6)==0){\r
+//    printf("close frame");\r
+    reply[0] = 0x88; // close, FIN set\r
+    reply[1] = data_len;\r
+    // Status code\r
+    if (data_len == 0){\r
+      reply[2] = 3;\r
+      reply[3] = 232;\r
+    }else{\r
+      reply[2] = data[0];\r
+      reply[3] = data[1];\r
+    }\r
+    ret = 0;\r
+  } else {\r
+    reply[0] = 0x80|opcode;  // FIN set\r
+    if (data_len <= 125){\r
+      reply[1] = data_len;\r
+    }\r
+    else{\r
+      reply[1] = 126;\r
+      reply[2] = data_len / 256;\r
+      reply[3] = data_len % 256;\r
+      offset = 4;\r
+    }\r
+  }\r
+  // Echo the message back to the client\r
+  mg_write(conn, reply, offset);\r
+  if (data_len > 0)\r
+    mg_write(conn, data, data_len);\r
+  // Returning zero means stoping websocket conversation.\r
+  // Close the conversation if client has sent us "exit" string.\r
+  return ret;\r
+}\r
+\r
+void websocket_ready_handler(struct mg_connection *conn){\r
+  const char *prot = mg_get_header(conn, "Sec-WebSocket-Protocol");\r
+  char buf[100];\r
+  if (prot){\r
+    char *p = NULL;\r
+    snprintf(buf, sizeof(buf), "%s", prot);\r
+    if ((p = strrchr(buf, ',')) != NULL) {\r
+      *p = '\0';\r
+    } \r
+    mg_printf(conn, "Sec-WebSocket-Protocol: %s\r\n", buf);\r
+  }\r
+  mg_printf(conn, "\r\n");\r
+}\r
+\r
+\r
+// Arguments:\r
+//   flags: first byte of websocket frame, see websocket RFC,\r
+//          http://tools.ietf.org/html/rfc6455, section 5.2\r
+//   data, data_len: payload data. Mask, if any, is already applied.\r
+int websocket_data_handler(struct mg_connection *conn, int flags,\r
+                                  char *data, size_t data_len){\r
+  const char *prot = mg_get_header(conn, "Sec-WebSocket-Protocol");\r
+  if (prot && (strncmp(prot, "foobar", 6) == 0)){\r
+    return 0;\r
+  }\r
+  const char *uri = mg_get_request_info(conn)->uri;\r
+  if (strcmp(uri, "/") == 0 || strcmp(uri, "/echo") == 0){\r
+      return websocket_data(conn, flags, data, data_len);\r
+  }\r
+  return 0; \r
+}\r