Commit | Line | Data |
---|---|---|
8512671a LM |
1 | /* depend: */ |
2 | /* cflags: */ | |
4baf6839 | 3 | /* linker: color.o debug.o file.o http.o server.o */ |
8512671a | 4 | |
184be781 | 5 | #include <assert.h> |
4baf6839 | 6 | #include <dirent.h> |
8512671a LM |
7 | #include <stdio.h> |
8 | #include <stdlib.h> | |
89f0e084 | 9 | #include <unistd.h> |
8512671a LM |
10 | |
11 | #include "debug.h" | |
96748ca7 | 12 | #include "http.h" |
00afbb65 | 13 | #include "server.h" |
8512671a | 14 | |
184be781 LM |
15 | /* types */ |
16 | ||
8512671a LM |
17 | /* constants */ |
18 | ||
19 | #define BUFFER_SIZE 4096 | |
4baf6839 | 20 | #define ROOT_DIR "webroot" |
2bec4aa7 LM |
21 | #define SERVER_NAME "localhost" |
22 | #define CHARSET "iso-8859-1" | |
8512671a LM |
23 | |
24 | /* macros */ | |
25 | ||
26 | /* gobal variables */ | |
27 | ||
28 | char *progname = NULL; | |
21ac031b | 29 | int port = 8080; |
4baf6839 | 30 | char *root = ROOT_DIR; |
2bec4aa7 LM |
31 | char *servername = SERVER_NAME; |
32 | char *charset = CHARSET; | |
8512671a LM |
33 | |
34 | /* help function */ | |
35 | ||
36 | int usage (int ret) | |
37 | { | |
38 | FILE *fid = ret ? stderr : stdout; | |
39 | fprintf (fid, "usage: %s\n", progname); | |
40 | fprintf (fid, " -h : help message\n"); | |
21ac031b | 41 | fprintf (fid, " -p : port number (%d)\n", port); |
4baf6839 | 42 | fprintf (fid, " -r : web root directory (%s)\n", root); |
8512671a LM |
43 | fprintf (fid, " -v : verbose level (%d)\n", verbose); |
44 | ||
45 | return ret; | |
46 | } | |
47 | ||
48 | /* main function */ | |
49 | ||
50 | int main (int argc, char *argv[]) | |
51 | { | |
52 | int i = 0; | |
8512671a LM |
53 | |
54 | /* program name */ | |
55 | ||
56 | progname = argv[0]; | |
57 | while (progname[i] != '\0') { | |
58 | if ((progname[i] == '/') || (progname[i] == '\\')) { | |
59 | progname += i + 1; | |
60 | i = 0; | |
61 | } else { | |
62 | i++; | |
63 | } | |
64 | } | |
65 | ||
66 | /* argument processing */ | |
67 | ||
4baf6839 | 68 | while (argc-- > 1) { |
8512671a LM |
69 | char *arg = *(++argv); |
70 | if (arg[0] != '-') { | |
163f56b1 | 71 | VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1)); |
8512671a LM |
72 | return 1; |
73 | } | |
74 | char c = arg[1]; | |
75 | switch (c) { | |
21ac031b | 76 | case 'p': |
8512671a LM |
77 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; |
78 | if (arg == NULL) { | |
163f56b1 | 79 | VERBOSE (ERROR, PERROR ("%s: missing port number\n", progname); usage (1)); |
21ac031b LM |
80 | return 1; |
81 | } | |
82 | port = atoi (arg); | |
83 | if (port <= 0) { | |
163f56b1 | 84 | VERBOSE (ERROR, PERROR ("%s: incorrect port number (%s)\n", progname, arg); usage (1)); |
8512671a LM |
85 | return 1; |
86 | } | |
8512671a | 87 | break; |
4baf6839 LM |
88 | case 'r': |
89 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; | |
90 | if (arg == NULL) { | |
91 | VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1)); | |
92 | return 1; | |
93 | } | |
94 | root = arg; | |
95 | break; | |
8512671a LM |
96 | case 'v': |
97 | arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; | |
98 | if (arg == NULL) { | |
163f56b1 | 99 | VERBOSE (ERROR, PERROR ("%s: missing verbose level\n", progname); usage (1)); |
8512671a LM |
100 | return 1; |
101 | } | |
102 | verbose = atoi (arg); | |
103 | break; | |
104 | case 'h': | |
105 | default: | |
106 | return usage (c != 'h'); | |
107 | } | |
108 | } | |
109 | ||
4baf6839 LM |
110 | /* check root directory */ |
111 | VERBOSE (DEBUG, PRINT ("Check web root\n")); | |
112 | DIR *pdir = opendir (root); | |
113 | if (pdir == NULL) { | |
114 | VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root)); | |
115 | return 1; | |
116 | } | |
117 | closedir (pdir); | |
118 | ||
2bec4aa7 LM |
119 | /* configuration */ |
120 | conf_t conf = {root, servername, charset}; | |
121 | ||
4baf6839 | 122 | /* init network stack */ |
163f56b1 | 123 | VERBOSE (DEBUG, PRINT ("Initializing socket\n")); |
89f0e084 | 124 | init_network_context (); |
7d4815a1 | 125 | if (open_listening_socket (port) == 0) { |
163f56b1 | 126 | VERBOSE (ERROR, PERROR ("Can't open listening socket\n")); |
184be781 LM |
127 | return 1; |
128 | } | |
163f56b1 LM |
129 | VERBOSE (INFO, PRINT ("Listening socket on port %d\n", port)); |
130 | ||
04a2223d LM |
131 | /* main loop */ |
132 | while (1) { | |
7d4815a1 | 133 | if (accept_incoming_connection () == 0) { |
89f0e084 | 134 | usleep (1e5); |
04a2223d LM |
135 | continue; |
136 | } | |
137 | ||
163f56b1 | 138 | VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n")); |
04a2223d | 139 | |
135b5dee LM |
140 | char *data = NULL; |
141 | char *output = NULL; | |
04a2223d | 142 | |
7d4815a1 | 143 | int len = receive_data (&data); |
89f0e084 | 144 | if (len == 0) { |
163f56b1 | 145 | VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n")); |
89f0e084 | 146 | } else if (len < 0) { |
163f56b1 | 147 | VERBOSE (WARNING, PRINT ("Connection in error (rx)\n")); |
89f0e084 | 148 | } else { |
96748ca7 | 149 | VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len)); |
04a2223d LM |
150 | |
151 | // processing | |
96748ca7 | 152 | VERBOSE (DEBUG, PRINT ("Processing %s\n", data)); |
2bec4aa7 | 153 | len = processing (data, len, &conf, &output); |
89f0e084 | 154 | |
6add28e9 LM |
155 | VERBOSE (DEBUG, PRINT ("Sending data (%d)\n%s\n", len, data)); |
156 | int rc = send_data (output, len); | |
89f0e084 | 157 | if (rc == 0) { |
163f56b1 | 158 | VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n")); |
89f0e084 | 159 | } else if (rc < 0) { |
163f56b1 | 160 | VERBOSE (WARNING, PRINT ("Connection in error (tx)\n")); |
04a2223d | 161 | } |
89f0e084 | 162 | } |
04a2223d | 163 | |
163f56b1 | 164 | VERBOSE (DEBUG, PRINT ("Closing connection\n")); |
89f0e084 LM |
165 | if (data) { |
166 | free (data); | |
04a2223d | 167 | } |
6add28e9 LM |
168 | if (output) { |
169 | free (output); | |
170 | } | |
7d4815a1 | 171 | close_connection (); |
04a2223d | 172 | } |
89f0e084 | 173 | |
163f56b1 | 174 | VERBOSE (DEBUG, PRINT ("Closing socket\n")); |
89f0e084 | 175 | terminate_network_context (); |
184be781 | 176 | |
06ec8057 | 177 | return 2; |
8512671a LM |
178 | } |
179 | ||
180 | // test: webserver.exe -h | |
181 | // test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }' | |
21ac031b | 182 | // test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 = |
8512671a LM |
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' | |
21ac031b LM |
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' | |
7d4815a1 LM |
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 | |
163f56b1 | 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 |
8512671a LM |
191 | |
192 | /* vim: set ts=4 sw=4 et: */ |