3 /* linker: color.o debug.o file.o http.o server.o */
21 #define BUFFER_SIZE 4096
22 #define ROOT_DIR "webroot"
23 #define TEMP_DIR "tmp"
24 #define SERVER_NAME "localhost"
25 #define CHARSET "iso-8859-1"
31 char *progname
= NULL
;
33 char *root
= ROOT_DIR
;
34 char *temp
= TEMP_DIR
;
35 char *servername
= SERVER_NAME
;
36 char *charset
= CHARSET
;
42 FILE *fid
= ret
? stderr
: stdout
;
43 fprintf (fid
, "usage: %s\n", progname
);
44 fprintf (fid
, " -c : charset name (%s)\n", charset
);
45 fprintf (fid
, " -h : help message\n");
46 fprintf (fid
, " -p : port number (%d)\n", port
);
47 fprintf (fid
, " -r : web root directory (%s)\n", root
);
48 fprintf (fid
, " -s : server name (%s)\n", servername
);
49 fprintf (fid
, " -t : temporay directory (%s)\n", temp
);
50 fprintf (fid
, " -v : verbose level (%d)\n", verbose
);
57 int main (int argc
, char *argv
[])
64 while (progname
[i
] != '\0') {
65 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
73 /* argument processing */
76 char *arg
= *(++argv
);
78 VERBOSE (ERROR
, PERROR ("%s: invalid option -- '%s'\n", progname
, arg
); usage (1));
84 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
86 VERBOSE (ERROR
, PERROR ("%s: missing charset name\n", progname
); usage (1));
92 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
94 VERBOSE (ERROR
, PERROR ("%s: missing port number\n", progname
); usage (1));
99 VERBOSE (ERROR
, PERROR ("%s: incorrect port number (%s)\n", progname
, arg
); usage (1));
104 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
106 VERBOSE (ERROR
, PERROR ("%s: missing directory name\n", progname
); usage (1));
112 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
114 VERBOSE (ERROR
, PERROR ("%s: missing server name\n", progname
); usage (1));
120 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
122 VERBOSE (ERROR
, PERROR ("%s: missing directory name\n", progname
); usage (1));
128 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
130 VERBOSE (ERROR
, PERROR ("%s: missing verbose level\n", progname
); usage (1));
133 verbose
= atoi (arg
);
137 return usage (c
!= 'h');
144 /* check root directory */
145 VERBOSE (DEBUG
, PRINT ("Check web root\n"));
146 DIR *prootdir
= opendir (root
);
147 if (prootdir
== NULL
) {
148 VERBOSE (ERROR
, PERROR ("Can't read directory (%s)\n", root
));
153 /* check temp directory */
154 VERBOSE (DEBUG
, PRINT ("Check temp dirweb root\n"));
155 DIR *ptempdir
= opendir (temp
);
156 if (ptempdir
== NULL
) {
157 VERBOSE (ERROR
, PERROR ("Can't read directory (%s)\n", temp
));
160 char *ntemp
= tempname (temp
, NULL
);
161 FILE *ftemp
= fopen (ntemp
, "w");
163 VERBOSE (ERROR
, PERROR ("Can't write temporary file (%s)\n", ntemp
));
171 conf_t conf
= {root
, temp
, servername
, charset
};
173 /* init network stack */
174 VERBOSE (DEBUG
, PRINT ("Initializing socket\n"));
175 init_network_context ();
176 if (open_listening_socket (port
) == 0) {
177 VERBOSE (ERROR
, PERROR ("Can't open listening socket\n"));
180 VERBOSE (INFO
, PRINT ("Listening socket on port %d\n", port
));
184 if (accept_incoming_connection () == 0) {
189 VERBOSE (DEBUG
, PRINT ("Server connected, waiting for data\n"));
194 int len
= receive_data (&data
);
196 VERBOSE (WARNING
, PRINT ("Connection closed by peer (rx)\n"));
197 } else if (len
< 0) {
198 VERBOSE (WARNING
, PRINT ("Connection in error (rx)\n"));
200 VERBOSE (DEBUG
, PRINT ("Received %d bytes\n", len
));
203 VERBOSE (DEBUG
, PRINT ("Processing %s\n", data
));
204 len
= processing (data
, len
, &conf
, &output
);
206 VERBOSE (DEBUG
, PRINT ("Sending data (%d)\n%s\n", len
, data
));
207 int rc
= send_data (output
, len
);
209 VERBOSE (WARNING
, PRINT ("Connection closed by peer (tx)\n"));
211 VERBOSE (WARNING
, PRINT ("Connection in error (tx)\n"));
215 VERBOSE (DEBUG
, PRINT ("Closing connection\n"));
225 VERBOSE (DEBUG
, PRINT ("Closing socket\n"));
226 terminate_network_context ();
231 // test: webserver.exe -h
232 // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
233 // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
234 // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
235 // test: webserver.exe error 2>&1 | grep -q 'invalid option'
236 // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
237 // test: webserver.exe -c 2>&1 | grep -q 'missing charset name'
238 // test: webserver.exe -p 2>&1 | grep -q 'missing port number'
239 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
240 // test: webserver.exe -s 2>&1 | grep -q 'missing server name'
241 // test: webserver.exe -r 2>&1 | grep -q 'missing directory name'
242 // test: webserver.exe >& test.log & pid=$!; sleep 1; kill -ABRT $pid; sleep 1; grep -q 'Listening socket on port 8080' test.log
243 // test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -TERM $pid; sleep 1; grep -q 'Listening socket on port 8000' test.log
244 // 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
245 // 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
246 // 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
247 // 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
248 // 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
250 /* vim: set ts=4 sw=4 et: */