quick commit
[webserver.git] / webserver.c
index 366973f9ae607636f498fd55c15e963640f06c02..b8c6e8ced64251dadfdfb92d38204d5cfcf21573 100644 (file)
@@ -1,8 +1,9 @@
 /* depend: */
 /* cflags: */
-/* linker: color.o debug.o http.o server.o */
+/* linker: color.o debug.o file.o http.o server.o */
 
 #include <assert.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -16,6 +17,7 @@
 /* constants */
 
 #define BUFFER_SIZE 4096
+#define ROOT_DIR "webroot"
 
 /* macros */
 
@@ -23,6 +25,7 @@
 
 char *progname = NULL;
 int port = 8080;
+char *root = ROOT_DIR;
 
 /* help function */
 
@@ -32,6 +35,7 @@ int usage (int ret)
     fprintf (fid, "usage: %s\n", progname);
     fprintf (fid, " -h : help message\n");
     fprintf (fid, " -p : port number (%d)\n", port);
+    fprintf (fid, " -r : web root directory (%s)\n", root);
     fprintf (fid, " -v : verbose level (%d)\n", verbose);
 
     return ret;
@@ -57,7 +61,7 @@ int main (int argc, char *argv[])
 
     /* argument processing */
 
-     while (argc-- > 1) {
+    while (argc-- > 1) {
         char *arg = *(++argv);
         if (arg[0] != '-') {
             VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1));
@@ -77,6 +81,14 @@ int main (int argc, char *argv[])
                 return 1;
             }
             break;
+        case 'r':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1));
+                return 1;
+            }
+            root = arg;
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -91,6 +103,16 @@ int main (int argc, char *argv[])
         }
     }
 
+    /* check root directory */
+    VERBOSE (DEBUG, PRINT ("Check web root\n"));
+    DIR *pdir = opendir (root);
+    if (pdir == NULL) {
+        VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root));
+        return 1;
+    }
+    closedir (pdir);
+
+    /* init network stack */
     VERBOSE (DEBUG, PRINT ("Initializing socket\n"));
     init_network_context ();
     if (open_listening_socket (port) == 0) {
@@ -120,7 +142,7 @@ int main (int argc, char *argv[])
 
             // processing
             VERBOSE (DEBUG, PRINT ("Processing %s\n", data));
-            len = processing ((char *)data, len, (char **)&data);
+            len = processing ((char *)data, len, root, (char **)&data);
 
             int rc = send_data (data, len);
             if (rc == 0) {