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
, " -c : charset name (%s)\n", charset
);
41 fprintf (fid
, " -h : help message\n");
42 fprintf (fid
, " -p : port number (%d)\n", port
);
43 fprintf (fid
, " -r : web root directory (%s)\n", root
);
44 fprintf (fid
, " -s : server name (%s)\n", servername
);
45 fprintf (fid
, " -v : verbose level (%d)\n", verbose
);
52 int main (int argc
, char *argv
[])
59 while (progname
[i
] != '\0') {
60 if ((progname
[i
] == '/') || (progname
[i
] == '\\')) {
68 /* argument processing */
71 char *arg
= *(++argv
);
73 VERBOSE (ERROR
, PERROR ("%s: invalid option -- '%s'\n", progname
, arg
); usage (1));
79 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
81 VERBOSE (ERROR
, PERROR ("%s: missing charset name\n", progname
); usage (1));
87 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
89 VERBOSE (ERROR
, PERROR ("%s: missing port number\n", progname
); usage (1));
94 VERBOSE (ERROR
, PERROR ("%s: incorrect port number (%s)\n", progname
, arg
); usage (1));
99 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
101 VERBOSE (ERROR
, PERROR ("%s: missing server name\n", progname
); usage (1));
107 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
109 VERBOSE (ERROR
, PERROR ("%s: missing directory name\n", progname
); usage (1));
115 arg
= (arg
[2]) ? arg
+ 2 : (--argc
> 0) ? *(++argv
) : NULL
;
117 VERBOSE (ERROR
, PERROR ("%s: missing verbose level\n", progname
); usage (1));
120 verbose
= atoi (arg
);
124 return usage (c
!= 'h');
128 /* check root directory */
129 VERBOSE (DEBUG
, PRINT ("Check web root\n"));
130 DIR *pdir
= opendir (root
);
132 VERBOSE (ERROR
, PERROR ("Can't read directory (%s)\n", root
));
138 conf_t conf
= {root
, servername
, charset
};
140 /* init network stack */
141 VERBOSE (DEBUG
, PRINT ("Initializing socket\n"));
142 init_network_context ();
143 if (open_listening_socket (port
) == 0) {
144 VERBOSE (ERROR
, PERROR ("Can't open listening socket\n"));
147 VERBOSE (INFO
, PRINT ("Listening socket on port %d\n", port
));
151 if (accept_incoming_connection () == 0) {
156 VERBOSE (DEBUG
, PRINT ("Server connected, waiting for data\n"));
161 int len
= receive_data (&data
);
163 VERBOSE (WARNING
, PRINT ("Connection closed by peer (rx)\n"));
164 } else if (len
< 0) {
165 VERBOSE (WARNING
, PRINT ("Connection in error (rx)\n"));
167 VERBOSE (DEBUG
, PRINT ("Received %d bytes\n", len
));
170 VERBOSE (DEBUG
, PRINT ("Processing %s\n", data
));
171 len
= processing (data
, len
, &conf
, &output
);
173 VERBOSE (DEBUG
, PRINT ("Sending data (%d)\n%s\n", len
, data
));
174 int rc
= send_data (output
, len
);
176 VERBOSE (WARNING
, PRINT ("Connection closed by peer (tx)\n"));
178 VERBOSE (WARNING
, PRINT ("Connection in error (tx)\n"));
182 VERBOSE (DEBUG
, PRINT ("Closing connection\n"));
192 VERBOSE (DEBUG
, PRINT ("Closing socket\n"));
193 terminate_network_context ();
198 // test: webserver.exe -h
199 // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
200 // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
201 // test: webserver.exe -_ 2>&1 | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
202 // test: webserver.exe error 2>&1 | grep -q 'invalid option'
203 // test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
204 // test: webserver.exe -c 2>&1 | grep -q 'missing charset name'
205 // test: webserver.exe -p 2>&1 | grep -q 'missing port number'
206 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
207 // test: webserver.exe -s 2>&1 | grep -q 'missing server name'
208 // test: webserver.exe -r 2>&1 | grep -q 'missing directory name'
209 // test: webserver.exe >& test.log & pid=$!; sleep 1; kill -INT $pid; grep -q 'Listening socket on port 8080' test.log
210 // test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -INT $pid; grep -q 'Listening socket on port 8000' test.log
211 // test: webserver.exe -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8080/index.html > test; kill -TERM $pid; grep -q '<title>Test</title>' test
212 // test: webserver.exe -v 3 -p 8001 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8001/index.html > test; kill -TERM $pid
213 // test: webserver.exe -v 3 -p 8002 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8002/not_found.html > test; kill -TERM $pid; grep -q '404' test
214 // test: webserver.exe -v 3 -p 8003 >&/dev/null & pid=$!; sleep 1; curl -I http://localhost:8003/index.html > test; kill -TERM $pid
217 /* vim: set ts=4 sw=4 et: */