/* depend: */
/* cflags: */
-/* linker: color.o debug.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 <time.h>
+#include <unistd.h>
#include "debug.h"
+#include "file.h"
+#include "http.h"
+#include "server.h"
+
+/* types */
/* constants */
#define BUFFER_SIZE 4096
+#define ROOT_DIR "webroot"
+#define TEMP_DIR "tmp"
+#define SERVER_NAME "localhost"
+#define CHARSET "iso-8859-1"
/* macros */
/* gobal variables */
char *progname = NULL;
+int port = 8080;
+char *root = ROOT_DIR;
+char *temp = TEMP_DIR;
+char *servername = SERVER_NAME;
+char *charset = CHARSET;
/* help function */
{
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;
int main (int argc, char *argv[])
{
int i = 0;
- int ret = 0;
/* program name */
/* argument processing */
- while (argc-- > 1) {
+ while (argc-- > 1) {
char *arg = *(++argv);
if (arg[0] != '-') {
- VERBOSE (ERROR, fprintf (stderr, "%s: invalid option -- '%s'\n", progname, arg); usage (1));
+ VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1));
return 1;
}
char c = arg[1];
switch (c) {
-/*
- case 'i':
+ 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));
+ return 1;
+ }
+ port = atoi (arg);
+ if (port <= 0) {
+ VERBOSE (ERROR, PERROR ("%s: incorrect port number (%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 '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, fprintf (stderr, "%s: missing input prompt\n", progname); usage (1));
+ VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1));
return 1;
}
- iprompt = arg;
+ temp = arg;
break;
-*/
case 'v':
arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
if (arg == NULL) {
- VERBOSE (ERROR, fprintf (stderr, "%s: missing verbose level\n", progname); usage (1));
+ VERBOSE (ERROR, PERROR ("%s: missing verbose level\n", progname); usage (1));
return 1;
}
verbose = atoi (arg);
}
}
- return ret;
+ /* init seed */
+ srand (time (NULL));
+
+ /* check root directory */
+ VERBOSE (DEBUG, PRINT ("Check web root\n"));
+ DIR *prootdir = opendir (root);
+ if (prootdir == NULL) {
+ VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root));
+ return 1;
+ }
+ 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, temp, servername, charset};
+
+ /* init network stack */
+ VERBOSE (DEBUG, PRINT ("Initializing socket\n"));
+ init_network_context ();
+ if (open_listening_socket (port) == 0) {
+ VERBOSE (ERROR, PERROR ("Can't open listening socket\n"));
+ return 1;
+ }
+ VERBOSE (INFO, PRINT ("Listening socket on port %d\n", port));
+
+ /* main loop */
+ while (1) {
+ if (accept_incoming_connection () == 0) {
+ usleep (1e5);
+ continue;
+ }
+
+ VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n"));
+
+ char *data = NULL;
+ char *output = NULL;
+
+ int len = receive_data (&data);
+ if (len == 0) {
+ VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n"));
+ } else if (len < 0) {
+ VERBOSE (WARNING, PRINT ("Connection in error (rx)\n"));
+ } else {
+ VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len));
+
+ // processing
+ VERBOSE (DEBUG, PRINT ("Processing %s\n", data));
+ len = processing (data, len, &conf, &output);
+
+ VERBOSE (DEBUG, PRINT ("Sending data (%d)\n%s\n", len, data));
+ int rc = send_data (output, len);
+ if (rc == 0) {
+ VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n"));
+ } else if (rc < 0) {
+ VERBOSE (WARNING, PRINT ("Connection in error (tx)\n"));
+ }
+ }
+
+ VERBOSE (DEBUG, PRINT ("Closing connection\n"));
+ if (data) {
+ free (data);
+ }
+ if (output) {
+ free (output);
+ }
+ close_connection ();
+ }
+
+ VERBOSE (DEBUG, PRINT ("Closing socket\n"));
+ terminate_network_context ();
+
+ return 2;
}
// test: webserver.exe -h
// test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
-// test: echo 1 | webserver.exe -v3 | grep -q value
-// test: webserver.exe -_ 2> /dev/null | awk 'END { if (NR == 0) { exit(0) } else exit (1) }'
+// test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
// 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 -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 '<title>Test</title>' 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 '<title>Test</title>' test.log
/* vim: set ts=4 sw=4 et: */