3 /* linker: color.o debug.o file.o http.o server.o */
19 #define BUFFER_SIZE 4096
20 #define ROOT_DIR "webroot"
21 #define SERVER_NAME "localhost"
22 #define CHARSET "iso-8859-1"
28 char *progname
= NULL
;
30 char *root
= ROOT_DIR
;
31 char *servername
= SERVER_NAME
;
32 char *charset
= CHARSET
;
38 FILE *fid
= ret
? stderr
: stdout
;
39 fprintf (fid
, "usage: %s\n", progname
);
40 fprintf (fid
, " -h : help message\n");
41 fprintf (fid
, " -p : port number (%d)\n", port
);
42 fprintf (fid
, " -r : web root directory (%s)\n", root
);
43 fprintf (fid
, " -v : verbose level (%d)\n", verbose
);
50 int main (int argc
, char *argv
[])
57 while (progname
[i
] != '\0') {
58 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
66 /* argument processing */
69 char *arg
= *(++argv
);
71 VERBOSE (ERROR
, PERROR ("%s: invalid option -- '%s'\n", progname
, arg
); usage (1));
77 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
79 VERBOSE (ERROR
, PERROR ("%s: missing port number\n", progname
); usage (1));
84 VERBOSE (ERROR
, PERROR ("%s: incorrect port number (%s)\n", progname
, arg
); usage (1));
89 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
91 VERBOSE (ERROR
, PERROR ("%s: missing directory name\n", progname
); usage (1));
97 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
99 VERBOSE (ERROR
, PERROR ("%s: missing verbose level\n", progname
); usage (1));
102 verbose
= atoi (arg
);
106 return usage (c
!= 'h');
110 /* check root directory */
111 VERBOSE (DEBUG
, PRINT ("Check web root\n"));
112 DIR *pdir
= opendir (root
);
114 VERBOSE (ERROR
, PERROR ("Can't read directory (%s)\n", root
));
120 conf_t conf
= {root
, servername
, charset
};
122 /* init network stack */
123 VERBOSE (DEBUG
, PRINT ("Initializing socket\n"));
124 init_network_context ();
125 if (open_listening_socket (port
) == 0) {
126 VERBOSE (ERROR
, PERROR ("Can't open listening socket\n"));
129 VERBOSE (INFO
, PRINT ("Listening socket on port %d\n", port
));
133 if (accept_incoming_connection () == 0) {
138 VERBOSE (DEBUG
, PRINT ("Server connected, waiting for data\n"));
143 int len
= receive_data (&data
);
145 VERBOSE (WARNING
, PRINT ("Connection closed by peer (rx)\n"));
146 } else if (len
< 0) {
147 VERBOSE (WARNING
, PRINT ("Connection in error (rx)\n"));
149 VERBOSE (DEBUG
, PRINT ("Received %d bytes\n", len
));
152 VERBOSE (DEBUG
, PRINT ("Processing %s\n", data
));
153 len
= processing (data
, len
, &conf
, &output
);
155 VERBOSE (DEBUG
, PRINT ("Sending data (%d)\n%s\n", len
, data
));
156 int rc
= send_data (output
, len
);
158 VERBOSE (WARNING
, PRINT ("Connection closed by peer (tx)\n"));
160 VERBOSE (WARNING
, PRINT ("Connection in error (tx)\n"));
164 VERBOSE (DEBUG
, PRINT ("Closing connection\n"));
174 VERBOSE (DEBUG
, PRINT ("Closing socket\n"));
175 terminate_network_context ();
180 // test: webserver.exe -h
181 // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
182 // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
183 // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
184 // test: webserver.exe error 2>&1 | grep -q 'invalid option'
185 // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
186 // test: webserver.exe -p 2>&1 | grep -q 'missing port number'
187 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
188 // test: webserver.exe > test.log & pid=$!; sleep 1; kill -QUIT $pid; grep -q 'Listening socket on port 8080' test.log
189 // test: webserver.exe -p 8000 > test.log & pid=$!; sleep 1; kill -ABRT $pid; grep -q 'Listening socket on port 8000' test.log
190 // 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
192 /* vim: set ts=4 sw=4 et: */