best makefile
[webserver.git] / webserver.c
CommitLineData
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>
aa628d4e 9#include <time.h>
89f0e084 10#include <unistd.h>
8512671a
LM
11
12#include "debug.h"
aa628d4e 13#include "file.h"
96748ca7 14#include "http.h"
00afbb65 15#include "server.h"
8512671a 16
184be781
LM
17/* types */
18
8512671a
LM
19/* constants */
20
21#define BUFFER_SIZE 4096
4baf6839 22#define ROOT_DIR "webroot"
aa628d4e 23#define TEMP_DIR "tmp"
2bec4aa7
LM
24#define SERVER_NAME "localhost"
25#define CHARSET "iso-8859-1"
8512671a
LM
26
27/* macros */
28
29/* gobal variables */
30
31char *progname = NULL;
21ac031b 32int port = 8080;
4baf6839 33char *root = ROOT_DIR;
aa628d4e 34char *temp = TEMP_DIR;
2bec4aa7
LM
35char *servername = SERVER_NAME;
36char *charset = CHARSET;
8512671a
LM
37
38/* help function */
39
40int usage (int ret)
41{
42 FILE *fid = ret ? stderr : stdout;
43 fprintf (fid, "usage: %s\n", progname);
295b1e68 44 fprintf (fid, " -c : charset name (%s)\n", charset);
8512671a 45 fprintf (fid, " -h : help message\n");
21ac031b 46 fprintf (fid, " -p : port number (%d)\n", port);
4baf6839 47 fprintf (fid, " -r : web root directory (%s)\n", root);
295b1e68 48 fprintf (fid, " -s : server name (%s)\n", servername);
aa628d4e 49 fprintf (fid, " -t : temporay directory (%s)\n", temp);
8512671a
LM
50 fprintf (fid, " -v : verbose level (%d)\n", verbose);
51
52 return ret;
53}
54
55/* main function */
56
57int main (int argc, char *argv[])
58{
59 int i = 0;
8512671a
LM
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
4baf6839 75 while (argc-- > 1) {
8512671a
LM
76 char *arg = *(++argv);
77 if (arg[0] != '-') {
163f56b1 78 VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1));
8512671a
LM
79 return 1;
80 }
81 char c = arg[1];
82 switch (c) {
295b1e68
LM
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':
8512671a
LM
92 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
93 if (arg == NULL) {
163f56b1 94 VERBOSE (ERROR, PERROR ("%s: missing port number\n", progname); usage (1));
21ac031b
LM
95 return 1;
96 }
97 port = atoi (arg);
98 if (port <= 0) {
163f56b1 99 VERBOSE (ERROR, PERROR ("%s: incorrect port number (%s)\n", progname, arg); usage (1));
8512671a
LM
100 return 1;
101 }
8512671a 102 break;
aa628d4e
LM
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;
295b1e68
LM
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;
aa628d4e 119 case 't':
4baf6839
LM
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 }
aa628d4e 125 temp = arg;
4baf6839 126 break;
8512671a
LM
127 case 'v':
128 arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
129 if (arg == NULL) {
163f56b1 130 VERBOSE (ERROR, PERROR ("%s: missing verbose level\n", progname); usage (1));
8512671a
LM
131 return 1;
132 }
133 verbose = atoi (arg);
134 break;
135 case 'h':
136 default:
137 return usage (c != 'h');
138 }
139 }
140
aa628d4e
LM
141 /* init seed */
142 srand (time (NULL));
143
4baf6839
LM
144 /* check root directory */
145 VERBOSE (DEBUG, PRINT ("Check web root\n"));
aa628d4e
LM
146 DIR *prootdir = opendir (root);
147 if (prootdir == NULL) {
4baf6839
LM
148 VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root));
149 return 1;
150 }
aa628d4e
LM
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 }
6ae861c7 160 char *ntemp = tempname (temp, NULL);
aa628d4e
LM
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);
4baf6839 169
2bec4aa7 170 /* configuration */
aa628d4e 171 conf_t conf = {root, temp, servername, charset};
2bec4aa7 172
4baf6839 173 /* init network stack */
163f56b1 174 VERBOSE (DEBUG, PRINT ("Initializing socket\n"));
89f0e084 175 init_network_context ();
7d4815a1 176 if (open_listening_socket (port) == 0) {
163f56b1 177 VERBOSE (ERROR, PERROR ("Can't open listening socket\n"));
184be781
LM
178 return 1;
179 }
163f56b1
LM
180 VERBOSE (INFO, PRINT ("Listening socket on port %d\n", port));
181
04a2223d
LM
182 /* main loop */
183 while (1) {
7d4815a1 184 if (accept_incoming_connection () == 0) {
89f0e084 185 usleep (1e5);
04a2223d
LM
186 continue;
187 }
188
163f56b1 189 VERBOSE (DEBUG, PRINT ("Server connected, waiting for data\n"));
04a2223d 190
135b5dee
LM
191 char *data = NULL;
192 char *output = NULL;
04a2223d 193
7d4815a1 194 int len = receive_data (&data);
89f0e084 195 if (len == 0) {
163f56b1 196 VERBOSE (WARNING, PRINT ("Connection closed by peer (rx)\n"));
89f0e084 197 } else if (len < 0) {
163f56b1 198 VERBOSE (WARNING, PRINT ("Connection in error (rx)\n"));
89f0e084 199 } else {
96748ca7 200 VERBOSE (DEBUG, PRINT ("Received %d bytes\n", len));
04a2223d
LM
201
202 // processing
96748ca7 203 VERBOSE (DEBUG, PRINT ("Processing %s\n", data));
2bec4aa7 204 len = processing (data, len, &conf, &output);
89f0e084 205
6add28e9
LM
206 VERBOSE (DEBUG, PRINT ("Sending data (%d)\n%s\n", len, data));
207 int rc = send_data (output, len);
89f0e084 208 if (rc == 0) {
163f56b1 209 VERBOSE (WARNING, PRINT ("Connection closed by peer (tx)\n"));
89f0e084 210 } else if (rc < 0) {
163f56b1 211 VERBOSE (WARNING, PRINT ("Connection in error (tx)\n"));
04a2223d 212 }
89f0e084 213 }
04a2223d 214
163f56b1 215 VERBOSE (DEBUG, PRINT ("Closing connection\n"));
89f0e084
LM
216 if (data) {
217 free (data);
04a2223d 218 }
6add28e9
LM
219 if (output) {
220 free (output);
221 }
7d4815a1 222 close_connection ();
04a2223d 223 }
89f0e084 224
163f56b1 225 VERBOSE (DEBUG, PRINT ("Closing socket\n"));
89f0e084 226 terminate_network_context ();
184be781 227
06ec8057 228 return 2;
8512671a
LM
229}
230
231// test: webserver.exe -h
232// test: webserver.exe -h | awk '/usage:/ { rc=1 } END { exit (1-rc) }'
21ac031b 233// test: webserver.exe -_ 2> /dev/null | wc -l | xargs test 0 =
8512671a
LM
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'
21ac031b 236// test: webserver.exe -v 2>&1 | grep -q 'missing verbose level'
295b1e68 237// test: webserver.exe -c 2>&1 | grep -q 'missing charset name'
21ac031b
LM
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'
295b1e68
LM
240// test: webserver.exe -s 2>&1 | grep -q 'missing server name'
241// test: webserver.exe -r 2>&1 | grep -q 'missing directory name'
0e6fd2e9
LM
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
8512671a
LM
249
250/* vim: set ts=4 sw=4 et: */