X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=webserver.c;h=555afbf704685f985435d073231ae9d3cdba396d;hb=refs%2Fheads%2Fmaster;hp=e0128e66aa243485633890fd4a6693d00876c33d;hpb=2bec4aa77cff1669f0c29d1ca426e4fa714b0928;p=webserver.git diff --git a/webserver.c b/webserver.c index e0128e6..555afbf 100644 --- a/webserver.c +++ b/webserver.c @@ -6,9 +6,11 @@ #include #include #include +#include #include #include "debug.h" +#include "file.h" #include "http.h" #include "server.h" @@ -18,6 +20,7 @@ #define BUFFER_SIZE 4096 #define ROOT_DIR "webroot" +#define TEMP_DIR "tmp" #define SERVER_NAME "localhost" #define CHARSET "iso-8859-1" @@ -28,6 +31,7 @@ char *progname = NULL; int port = 8080; char *root = ROOT_DIR; +char *temp = TEMP_DIR; char *servername = SERVER_NAME; char *charset = CHARSET; @@ -37,9 +41,12 @@ int usage (int ret) { FILE *fid = ret ? stderr : stdout; fprintf (fid, "usage: %s\n", progname); + fprintf (fid, " -c : charset name (%s)\n", charset); 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, " -s : server name (%s)\n", servername); + fprintf (fid, " -t : temporay directory (%s)\n", temp); fprintf (fid, " -v : verbose level (%d)\n", verbose); return ret; @@ -73,7 +80,15 @@ int main (int argc, char *argv[]) } char c = arg[1]; switch (c) { - case 'p': + case 'c': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, PERROR ("%s: missing charset name\n", progname); usage (1)); + return 1; + } + charset = arg; + break; + case 'p': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg == NULL) { VERBOSE (ERROR, PERROR ("%s: missing port number\n", progname); usage (1)); @@ -93,6 +108,22 @@ int main (int argc, char *argv[]) } root = arg; break; + case 's': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, PERROR ("%s: missing server name\n", progname); usage (1)); + return 1; + } + servername = arg; + break; + case 't': + 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; + } + temp = arg; + break; case 'v': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg == NULL) { @@ -107,17 +138,37 @@ int main (int argc, char *argv[]) } } + /* init seed */ + srand (time (NULL)); + /* check root directory */ VERBOSE (DEBUG, PRINT ("Check web root\n")); - DIR *pdir = opendir (root); - if (pdir == NULL) { + DIR *prootdir = opendir (root); + if (prootdir == NULL) { VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root)); return 1; } - closedir (pdir); + closedir (prootdir); + + /* check temp directory */ + VERBOSE (DEBUG, PRINT ("Check temp dirweb root\n")); + DIR *ptempdir = opendir (temp); + if (ptempdir == NULL) { + VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", temp)); + return 1; + } + char *ntemp = tempname (temp, NULL); + FILE *ftemp = fopen (ntemp, "w"); + if (ftemp == NULL) { + VERBOSE (ERROR, PERROR ("Can't write temporary file (%s)\n", ntemp)); + return 1; + } + fclose (ftemp); + unlink (ntemp); + free (ntemp); /* configuration */ - conf_t conf = {root, servername, charset}; + conf_t conf = {root, temp, servername, charset}; /* init network stack */ VERBOSE (DEBUG, PRINT ("Initializing socket\n")); @@ -183,10 +234,17 @@ int main (int argc, char *argv[]) // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }' // test: webserver.exe error 2>&1 | grep -q 'invalid option' // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level' +// test: webserver.exe -c 2>&1 | grep -q 'missing charset name' // test: webserver.exe -p 2>&1 | grep -q 'missing port number' // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number' -// test: webserver.exe > test.log & pid=$!; sleep 1; kill -QUIT $pid; grep -q 'Listening socket on port 8080' test.log -// test: webserver.exe -p 8000 > test.log & pid=$!; sleep 1; kill -ABRT $pid; grep -q 'Listening socket on port 8000' test.log -// test: webserver.exe & pid=$!; sleep 1; kill -TERM $pid; ps aux | grep -q [w]ebserver.exe && kill -9 $pid || rc=1; test x$rc = x1 +// test: webserver.exe -s 2>&1 | grep -q 'missing server name' +// test: webserver.exe -r 2>&1 | grep -q 'missing directory name' +// test: webserver.exe >& test.log & pid=$!; sleep 1; kill -ABRT $pid; sleep 1; grep -q 'Listening socket on port 8080' test.log +// test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -TERM $pid; sleep 1; grep -q 'Listening socket on port 8000' test.log +// test: webserver.exe -p 8001 -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8001/index.html > test.log; kill -TERM $pid; sleep 1; grep -q 'Test' test.log +// test: webserver.exe -v 3 -p 8002 >&/dev/null & pid=$!; sleep 1; curl -v http://localhost:8002/index.html >& test.log; kill -TERM $pid; sleep 1; grep -q 200 test.log +// test: webserver.exe -v 3 -p 8003 >&/dev/null & pid=$!; sleep 1; curl -v http://localhost:8003/not_found.html >& test.log; kill -TERM $pid; sleep 1; grep -q 404 test.log +// test: webserver.exe -v 3 -p 8004 >&/dev/null & pid=$!; sleep 1; curl -v -I http://localhost:8004/index.html >& test.log; kill -TERM $pid; sleep 1; grep -q Content-Length test.log; test $? -eq 1 +// test: webserver.exe -v 3 -p 8005 >&/dev/null & pid=$!; sleep 1; curl -v -d '' http://localhost:8005/index.html >& test.log; kill -TERM $pid; sleep 1; grep -q 'Test' test.log /* vim: set ts=4 sw=4 et: */