/* 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>
/* constants */
#define BUFFER_SIZE 4096
+#define ROOT_DIR "webroot"
/* macros */
char *progname = NULL;
int port = 8080;
+char *root = ROOT_DIR;
/* help function */
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;
/* 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));
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) {
}
}
+ /* 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) {
// 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) {