| 1 | /* depend: */ |
| 2 | /* cflags: */ |
| 3 | /* linker: color.o debug.o file.o http.o server.o */ |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <dirent.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <time.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include "debug.h" |
| 13 | #include "file.h" |
| 14 | #include "http.h" |
| 15 | #include "server.h" |
| 16 | |
| 17 | /* types */ |
| 18 | |
| 19 | /* constants */ |
| 20 | |
| 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" |
| 26 | |
| 27 | /* macros */ |
| 28 | |
| 29 | /* gobal variables */ |
| 30 | |
| 31 | char *progname = NULL; |
| 32 | int port = 8080; |
| 33 | char *root = ROOT_DIR; |
| 34 | char *temp = TEMP_DIR; |
| 35 | char *servername = SERVER_NAME; |
| 36 | char *charset = CHARSET; |
| 37 | |
| 38 | /* help function */ |
| 39 | |
| 40 | int usage (int ret) |
| 41 | { |
| 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); |
| 51 | |
| 52 | return ret; |
| 53 | } |
| 54 | |
| 55 | /* main function */ |
| 56 | |
| 57 | int main (int argc, char *argv[]) |
| 58 | { |
| 59 | int i = 0; |
| 60 | |
| 61 | /* program name */ |
| 62 | |
| 63 | progname = argv[0]; |
| 64 | while (progname[i] != '\0') { |
| 65 | if ((progname[i] == '/') || (progname[i] == '\\')) { |
| 66 | progname += i + 1; |
| 67 | i = 0; |
| 68 | } else { |
| 69 | i++; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | /* argument processing */ |
| 74 | |
| 75 | while (argc-- > 1) { |
| 76 | char *arg = *(++argv); |
| 77 | if (arg[0] != '-') { |
| 78 | VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1)); |
| 79 | return 1; |
| 80 | } |
| 81 | char c = arg[1]; |
| 82 | switch (c) { |
| 83 | case 'c': |
| 84 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 85 | if (arg == NULL) { |
| 86 | VERBOSE (ERROR, PERROR ("%s: missing charset name\n", progname); usage (1)); |
| 87 | return 1; |
| 88 | } |
| 89 | charset = arg; |
| 90 | break; |
| 91 | case 'p': |
| 92 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 93 | if (arg == NULL) { |
| 94 | VERBOSE (ERROR, PERROR ("%s: missing port number\n", progname); usage (1)); |
| 95 | return 1; |
| 96 | } |
| 97 | port = atoi (arg); |
| 98 | if (port <= 0) { |
| 99 | VERBOSE (ERROR, PERROR ("%s: incorrect port number (%s)\n", progname, arg); usage (1)); |
| 100 | return 1; |
| 101 | } |
| 102 | break; |
| 103 | case 'r': |
| 104 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 105 | if (arg == NULL) { |
| 106 | VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1)); |
| 107 | return 1; |
| 108 | } |
| 109 | root = arg; |
| 110 | break; |
| 111 | case 's': |
| 112 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 113 | if (arg == NULL) { |
| 114 | VERBOSE (ERROR, PERROR ("%s: missing server name\n", progname); usage (1)); |
| 115 | return 1; |
| 116 | } |
| 117 | servername = arg; |
| 118 | break; |
| 119 | case 't': |
| 120 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 121 | if (arg == NULL) { |
| 122 | VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1)); |
| 123 | return 1; |
| 124 | } |
| 125 | temp = arg; |
| 126 | break; |
| 127 | case 'v': |
| 128 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
| 129 | if (arg == NULL) { |
| 130 | VERBOSE (ERROR, PERROR ("%s: missing verbose level\n", progname); usage (1)); |
| 131 | return 1; |
| 132 | } |
| 133 | verbose = atoi (arg); |
| 134 | break; |
| 135 | case 'h': |
| 136 | default: |
| 137 | return usage (c != 'h'); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* init seed */ |
| 142 | srand (time (NULL)); |
| 143 | |
| 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)); |
| 149 | return 1; |
| 150 | } |
| 151 | closedir (prootdir); |
| 152 | |
| 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)); |
| 158 | return 1; |
| 159 | } |
| 160 | char *ntemp = tempname (temp, NULL); |
| 161 | FILE *ftemp = fopen (ntemp, "w"); |
| 162 | if (ftemp == NULL) { |
| 163 | VERBOSE (ERROR, PERROR ("Can't write temporary file (%s)\n", ntemp)); |
| 164 | return 1; |
| 165 | } |
| 166 | fclose (ftemp); |
| 167 | unlink (ntemp); |
| 168 | free (ntemp); |
| 169 | |
| 170 | /* configuration */ |
| 171 | conf_t conf = {root, temp, servername, charset}; |
| 172 | |
| 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")); |
| 178 | return 1; |
| 179 | } |
| 180 | VERBOSE (INFO, PRINT ("Listening socket on port %d\n", port)); |
| 181 | |
| 182 | /* main loop */ |
| 183 | while (1) { |
| 184 | if (accept_incoming_connection () == 0) { |
| 185 | usleep (1e5); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n")); |
| 190 | |
| 191 | char *data = NULL; |
| 192 | char *output = NULL; |
| 193 | |
| 194 | int len = receive_data (&data); |
| 195 | if (len == 0) { |
| 196 | VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n")); |
| 197 | } else if (len < 0) { |
| 198 | VERBOSE (WARNING, PRINT ("Connection in error (rx)\n")); |
| 199 | } else { |
| 200 | VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len)); |
| 201 | |
| 202 | // processing |
| 203 | VERBOSE (DEBUG, PRINT ("Processing %s\n", data)); |
| 204 | len = processing (data, len, &conf, &output); |
| 205 | |
| 206 | VERBOSE (DEBUG, PRINT ("Sending data (%d)\n%s\n", len, data)); |
| 207 | int rc = send_data (output, len); |
| 208 | if (rc == 0) { |
| 209 | VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n")); |
| 210 | } else if (rc < 0) { |
| 211 | VERBOSE (WARNING, PRINT ("Connection in error (tx)\n")); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | VERBOSE (DEBUG, PRINT ("Closing connection\n")); |
| 216 | if (data) { |
| 217 | free (data); |
| 218 | } |
| 219 | if (output) { |
| 220 | free (output); |
| 221 | } |
| 222 | close_connection (); |
| 223 | } |
| 224 | |
| 225 | VERBOSE (DEBUG, PRINT ("Closing socket\n")); |
| 226 | terminate_network_context (); |
| 227 | |
| 228 | return 2; |
| 229 | } |
| 230 | |
| 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 |
| 249 | |
| 250 | /* vim: set ts=4 sw=4 et: */ |