Add support for a new --contentdir command line argument which allows
authorRusty Lynch <rusty.lynch@intel.com>
Mon, 20 Aug 2012 19:51:02 +0000 (12:51 -0700)
committerRusty Lynch <rusty.lynch@intel.com>
Mon, 20 Aug 2012 19:51:02 +0000 (12:51 -0700)
the binary to be run from a different directory then where the HTML
content is compiled to be located

main.c

diff --git a/main.c b/main.c
index eed39b6..2a0685b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -36,6 +36,8 @@ E_DBus_Connection *bus;
 json_t *app_list = NULL;
 char *apps_directory = NULL;
 
+char *content_directory = NULL;
+
 json_t *event_queue = NULL;
 
 char *voicecallmanager_path = NULL;
@@ -132,15 +134,20 @@ callback_http(struct libwebsocket_context *context,
                 libwebsockets_serve_http_file(wsi, path, 
                                               (const char *)efreet_mime_type_get((char *)path));
             } else {
-                snprintf(filename, 256, "%s/%s", LOCAL_RESOURCE_PATH, (char *)in + 1);
-                if (strlen((char *)in) == 1)
-                    /* serve up default content */
-                    libwebsockets_serve_http_file(wsi, DEFAULTCONTENT, "text/html");
-                else if (!stat(filename, &info) && S_ISREG(info.st_mode))
+                snprintf(filename, 256, "%s/%s", content_directory, (char *)in + 1);
+                if (!stat(filename, &info) && S_ISDIR(info.st_mode))
+                    /* try serving up index.html */
+                    snprintf(filename, 256, "%s/%s/index.html", 
+                             content_directory, (char *)in + 1);
+
+                if (!stat(filename, &info) && S_ISREG(info.st_mode)) {
                     libwebsockets_serve_http_file(wsi, filename, 
-                                                  (const char *)efreet_mime_type_get((char *)in));
-                else
-                    fprintf(stderr, "Failed to find content: %s\n", (char *)in);
+                                                  (const char *)efreet_mime_type_get(filename));
+                    printf("serving: %s\n", filename);
+                } else {
+                    /* TODO: Serve up error html page */
+                    fprintf(stderr, "Unable to find %s\n", filename);
+                }
             }
         } else {
             fprintf(stderr, "Invalid request \'%s\'\n", (char *)in);
@@ -704,9 +711,10 @@ get_modems_reply(void *data, DBusMessage *reply, DBusError *error)
 }
 
 static struct option options[] = {
-    { "help",     no_argument,         NULL, 'h' },
-    { "port",     required_argument,   NULL, 'p' },
-    { "directory", required_argument,   NULL, 'd' },
+    { "help",      no_argument,       NULL, 'h' },
+    { "port",      required_argument, NULL, 'p' },
+    { "appsdir",    required_argument, NULL, 'a' },
+    { "contentdir", required_argument, NULL, 'c' },
     { NULL, 0, 0, 0 }
 };
 
@@ -730,20 +738,26 @@ int main(int argc, char **argv)
         if (n < 0)
             continue;
         switch (n) {
-        case 'd':
+        case 'a':
             apps_directory = strdup(optarg);
             break;
+        case 'c':
+            content_directory = strdup(optarg);
+            break;
         case 'p':
             port = atoi(optarg);
             break;
         case 'h':
-            fprintf(stderr, "Usage: %s [--port=<p>] [--directory=<d>]\n", argv[0]);
+        default:
+            fprintf(stderr, "Usage: %s [--port=<p>] [--appsdir=<a>] [--contentdir=<c>]\n", argv[0]);
             exit(-1);
         }
     }
     if (!apps_directory)
         apps_directory = APPDIR;
-    
+    if (!content_directory)
+        content_directory = LOCAL_RESOURCE_PATH;
+
     /* Initialize and build up the application list */
     app_list = json_array();
     build_app_list(apps_directory);