adamc@116
|
1 #include <stdio.h>
|
adamc@116
|
2
|
adamc@116
|
3 #include <string.h>
|
adamc@502
|
4 #include <stdlib.h>
|
adamc@116
|
5 #include <sys/types.h>
|
adamc@116
|
6 #include <sys/socket.h>
|
adamc@116
|
7 #include <netinet/in.h>
|
adamc@472
|
8 #include <unistd.h>
|
adamc@502
|
9 #include <signal.h>
|
adamc@116
|
10
|
adamc@138
|
11 #include <pthread.h>
|
adamc@138
|
12
|
adamc@244
|
13 #include "urweb.h"
|
adamc@117
|
14
|
adamc@311
|
15 int uw_backlog = 10;
|
adamc@311
|
16 int uw_bufsize = 1024;
|
adamc@116
|
17
|
adamc@138
|
18 typedef struct node {
|
adamc@138
|
19 int fd;
|
adamc@138
|
20 struct node *next;
|
adamc@138
|
21 } *node;
|
adamc@138
|
22
|
adamc@138
|
23 static node front = NULL, back = NULL;
|
adamc@138
|
24
|
adamc@138
|
25 static int empty() {
|
adamc@138
|
26 return front == NULL;
|
adamc@138
|
27 }
|
adamc@138
|
28
|
adamc@138
|
29 static void enqueue(int fd) {
|
adamc@138
|
30 node n = malloc(sizeof(struct node));
|
adamc@138
|
31
|
adamc@138
|
32 n->fd = fd;
|
adamc@138
|
33 n->next = NULL;
|
adamc@138
|
34 if (back)
|
adamc@138
|
35 back->next = n;
|
adamc@138
|
36 else
|
adamc@138
|
37 front = n;
|
adamc@138
|
38 back = n;
|
adamc@138
|
39 }
|
adamc@138
|
40
|
adamc@138
|
41 static int dequeue() {
|
adamc@138
|
42 int ret = front->fd;
|
adamc@138
|
43
|
adamc@138
|
44 front = front->next;
|
adamc@138
|
45 if (!front)
|
adamc@138
|
46 back = NULL;
|
adamc@138
|
47
|
adamc@138
|
48 return ret;
|
adamc@138
|
49 }
|
adamc@138
|
50
|
adamc@138
|
51 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
|
adamc@138
|
52 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
|
adamc@138
|
53
|
adamc@167
|
54 #define MAX_RETRIES 5
|
adamc@167
|
55
|
adamc@424
|
56 int uw_db_begin(uw_context);
|
adamc@424
|
57 int uw_db_commit(uw_context);
|
adamc@424
|
58 int uw_db_rollback(uw_context);
|
adamc@424
|
59
|
adamc@424
|
60 static int try_rollback(uw_context ctx) {
|
adamc@424
|
61 int r = uw_db_rollback(ctx);
|
adamc@424
|
62
|
adamc@424
|
63 if (r) {
|
adamc@424
|
64 printf("Error running SQL ROLLBACK\n");
|
adamc@424
|
65 uw_reset(ctx);
|
adamc@424
|
66 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@424
|
67 uw_write(ctx, "Content-type: text/plain\r\n\r\n");
|
adamc@424
|
68 uw_write(ctx, "Error running SQL ROLLBACK\n");
|
adamc@424
|
69 }
|
adamc@424
|
70
|
adamc@424
|
71 return r;
|
adamc@424
|
72 }
|
adamc@424
|
73
|
adamc@138
|
74 static void *worker(void *data) {
|
adamc@324
|
75 int me = *(int *)data, retries_left = MAX_RETRIES;
|
adamc@562
|
76 uw_context ctx = uw_init(0, 0, 1024, 0);
|
adamc@272
|
77
|
adamc@272
|
78 while (1) {
|
adamc@311
|
79 failure_kind fk = uw_begin_init(ctx);
|
adamc@272
|
80
|
adamc@272
|
81 if (fk == SUCCESS) {
|
adamc@311
|
82 uw_db_init(ctx);
|
adamc@272
|
83 printf("Database connection initialized.\n");
|
adamc@272
|
84 break;
|
adamc@272
|
85 } else if (fk == BOUNDED_RETRY) {
|
adamc@272
|
86 if (retries_left) {
|
adamc@311
|
87 printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@272
|
88 --retries_left;
|
adamc@272
|
89 } else {
|
adamc@311
|
90 printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@311
|
91 uw_free(ctx);
|
adamc@272
|
92 return NULL;
|
adamc@272
|
93 }
|
adamc@272
|
94 } else if (fk == UNLIMITED_RETRY)
|
adamc@311
|
95 printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@272
|
96 else if (fk == FATAL) {
|
adamc@311
|
97 printf("Fatal initialization error: %s\n", uw_error_message(ctx));
|
adamc@311
|
98 uw_free(ctx);
|
adamc@272
|
99 return NULL;
|
adamc@272
|
100 } else {
|
adamc@311
|
101 printf("Unknown uw_handle return code!\n");
|
adamc@311
|
102 uw_free(ctx);
|
adamc@272
|
103 return NULL;
|
adamc@272
|
104 }
|
adamc@272
|
105 }
|
adamc@116
|
106
|
adamc@116
|
107 while (1) {
|
adamc@311
|
108 char buf[uw_bufsize+1], *back = buf, *s;
|
adamc@667
|
109 int sock, dont_close = 0;
|
adamc@116
|
110
|
adamc@138
|
111 pthread_mutex_lock(&queue_mutex);
|
adamc@138
|
112 while (empty())
|
adamc@138
|
113 pthread_cond_wait(&queue_cond, &queue_mutex);
|
adamc@138
|
114 sock = dequeue();
|
adamc@138
|
115 pthread_mutex_unlock(&queue_mutex);
|
adamc@138
|
116
|
adamc@138
|
117 printf("Handling connection with thread #%d.\n", me);
|
adamc@138
|
118
|
adamc@138
|
119 while (1) {
|
adamc@167
|
120 unsigned retries_left = MAX_RETRIES;
|
adamc@311
|
121 int r = recv(sock, back, uw_bufsize - (back - buf), 0);
|
adamc@138
|
122
|
adamc@138
|
123 if (r < 0) {
|
adamc@138
|
124 fprintf(stderr, "Recv failed\n");
|
adamc@138
|
125 break;
|
adamc@138
|
126 }
|
adamc@138
|
127
|
adamc@138
|
128 if (r == 0) {
|
adamc@138
|
129 printf("Connection closed.\n");
|
adamc@138
|
130 break;
|
adamc@138
|
131 }
|
adamc@138
|
132
|
adamc@138
|
133 printf("Received %d bytes.\n", r);
|
adamc@138
|
134
|
adamc@138
|
135 back += r;
|
adamc@138
|
136 *back = 0;
|
adamc@138
|
137
|
adamc@138
|
138 if (s = strstr(buf, "\r\n\r\n")) {
|
adamc@424
|
139 failure_kind fk;
|
adamc@457
|
140 char *cmd, *path, *headers, path_copy[uw_bufsize+1], *inputs;
|
adamc@667
|
141 int id, pass;
|
adamc@138
|
142
|
adamc@475
|
143 s[2] = 0;
|
adamc@144
|
144
|
adamc@138
|
145 if (!(s = strstr(buf, "\r\n"))) {
|
adamc@138
|
146 fprintf(stderr, "No newline in buf\n");
|
adamc@138
|
147 break;
|
adamc@138
|
148 }
|
adamc@138
|
149
|
adamc@138
|
150 *s = 0;
|
adamc@457
|
151 headers = s + 2;
|
adamc@138
|
152 cmd = s = buf;
|
adamc@401
|
153
|
adamc@401
|
154 printf("Read: %s\n", buf);
|
adamc@138
|
155
|
adamc@138
|
156 if (!strsep(&s, " ")) {
|
adamc@138
|
157 fprintf(stderr, "No first space in HTTP command\n");
|
adamc@138
|
158 break;
|
adamc@138
|
159 }
|
adamc@138
|
160
|
adamc@138
|
161 if (strcmp(cmd, "GET")) {
|
adamc@138
|
162 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
|
adamc@138
|
163 break;
|
adamc@138
|
164 }
|
adamc@138
|
165
|
adamc@138
|
166 path = s;
|
adamc@138
|
167 if (!strsep(&s, " ")) {
|
adamc@138
|
168 fprintf(stderr, "No second space in HTTP command\n");
|
adamc@138
|
169 break;
|
adamc@138
|
170 }
|
adamc@138
|
171
|
adamc@667
|
172 if (sscanf(path, "/.msgs/%d/%d", &id, &pass) == 2) {
|
adamc@667
|
173 uw_client_connect(id, pass, sock);
|
adamc@667
|
174 dont_close = 1;
|
adamc@667
|
175 break;
|
adamc@667
|
176 }
|
adamc@667
|
177
|
adamc@144
|
178 if (inputs = strchr(path, '?')) {
|
adamc@144
|
179 char *name, *value;
|
adamc@144
|
180 *inputs++ = 0;
|
adamc@144
|
181
|
adamc@144
|
182 while (*inputs) {
|
adamc@144
|
183 name = inputs;
|
adamc@145
|
184 if (inputs = strchr(inputs, '&'))
|
adamc@145
|
185 *inputs++ = 0;
|
adamc@145
|
186 else
|
adamc@145
|
187 inputs = strchr(name, 0);
|
adamc@145
|
188
|
adamc@145
|
189 if (value = strchr(name, '=')) {
|
adamc@144
|
190 *value++ = 0;
|
adamc@311
|
191 uw_set_input(ctx, name, value);
|
adamc@144
|
192 }
|
adamc@145
|
193 else
|
adamc@311
|
194 uw_set_input(ctx, name, "");
|
adamc@144
|
195 }
|
adamc@144
|
196 }
|
adamc@144
|
197
|
adamc@138
|
198 printf("Serving URI %s....\n", path);
|
adamc@138
|
199
|
adamc@458
|
200 uw_set_headers(ctx, headers);
|
adamc@458
|
201
|
adamc@167
|
202 while (1) {
|
adamc@424
|
203 if (uw_db_begin(ctx)) {
|
adamc@424
|
204 printf("Error running SQL BEGIN\n");
|
adamc@424
|
205 if (retries_left)
|
adamc@424
|
206 --retries_left;
|
adamc@424
|
207 else {
|
adamc@424
|
208 fk = FATAL;
|
adamc@424
|
209 uw_reset(ctx);
|
adamc@464
|
210 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
211 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
|
adamc@424
|
212 uw_write(ctx, "Error running SQL BEGIN\n");
|
adamc@424
|
213
|
adamc@424
|
214 break;
|
adamc@424
|
215 }
|
adamc@424
|
216 }
|
adamc@167
|
217
|
adamc@462
|
218 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
|
adamc@167
|
219
|
adamc@400
|
220 strcpy(path_copy, path);
|
adamc@458
|
221 fk = uw_begin(ctx, path_copy);
|
adamc@167
|
222 if (fk == SUCCESS) {
|
adamc@424
|
223 if (uw_db_commit(ctx)) {
|
adamc@424
|
224 fk = FATAL;
|
adamc@424
|
225
|
adamc@424
|
226 printf("Error running SQL COMMIT\n");
|
adamc@424
|
227 uw_reset(ctx);
|
adamc@464
|
228 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
229 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@424
|
230 uw_write(ctx, "Error running SQL COMMIT\n");
|
adamc@424
|
231 }
|
adamc@424
|
232
|
adamc@167
|
233 break;
|
adamc@167
|
234 } else if (fk == BOUNDED_RETRY) {
|
adamc@167
|
235 if (retries_left) {
|
adamc@311
|
236 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@167
|
237 --retries_left;
|
adamc@167
|
238 }
|
adamc@167
|
239 else {
|
adamc@311
|
240 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@167
|
241
|
adamc@311
|
242 uw_reset_keep_error_message(ctx);
|
adamc@464
|
243 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
244 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@311
|
245 uw_write(ctx, "Fatal error (out of retries): ");
|
adamc@311
|
246 uw_write(ctx, uw_error_message(ctx));
|
adamc@311
|
247 uw_write(ctx, "\n");
|
adamc@424
|
248
|
adamc@424
|
249 try_rollback(ctx);
|
adamc@424
|
250
|
adamc@424
|
251 break;
|
adamc@167
|
252 }
|
adamc@167
|
253 } else if (fk == UNLIMITED_RETRY)
|
adamc@311
|
254 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@167
|
255 else if (fk == FATAL) {
|
adamc@311
|
256 printf("Fatal error: %s\n", uw_error_message(ctx));
|
adamc@167
|
257
|
adamc@311
|
258 uw_reset_keep_error_message(ctx);
|
adamc@464
|
259 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
|
adamc@548
|
260 uw_write_header(ctx, "Content-type: text/html\r\n");
|
adamc@548
|
261 uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
|
adamc@311
|
262 uw_write(ctx, "Fatal error: ");
|
adamc@311
|
263 uw_write(ctx, uw_error_message(ctx));
|
adamc@548
|
264 uw_write(ctx, "\n</body></html>");
|
adamc@167
|
265
|
adamc@424
|
266 try_rollback(ctx);
|
adamc@424
|
267
|
adamc@167
|
268 break;
|
adamc@167
|
269 } else {
|
adamc@311
|
270 printf("Unknown uw_handle return code!\n");
|
adamc@167
|
271
|
adamc@311
|
272 uw_reset_keep_request(ctx);
|
adamc@464
|
273 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
274 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@311
|
275 uw_write(ctx, "Unknown uw_handle return code!\n");
|
adamc@167
|
276
|
adamc@424
|
277 try_rollback(ctx);
|
adamc@424
|
278
|
adamc@167
|
279 break;
|
adamc@167
|
280 }
|
adamc@167
|
281
|
adamc@311
|
282 uw_reset_keep_request(ctx);
|
adamc@424
|
283
|
adamc@424
|
284 if (try_rollback(ctx))
|
adamc@424
|
285 break;
|
adamc@167
|
286 }
|
adamc@138
|
287
|
adamc@311
|
288 uw_send(ctx, sock);
|
adamc@138
|
289
|
adamc@138
|
290 printf("Done with client.\n\n");
|
adamc@324
|
291 uw_memstats(ctx);
|
adamc@138
|
292 break;
|
adamc@138
|
293 }
|
adamc@116
|
294 }
|
adamc@116
|
295
|
adamc@667
|
296 if (!dont_close)
|
adamc@667
|
297 close(sock);
|
adamc@311
|
298 uw_reset(ctx);
|
adamc@116
|
299 }
|
adamc@116
|
300 }
|
adamc@102
|
301
|
adamc@667
|
302 static void *client_pruner(void *data) {
|
adamc@667
|
303 while (1) {
|
adamc@667
|
304 uw_prune_clients(5);
|
adamc@667
|
305 sleep(5);
|
adamc@667
|
306 }
|
adamc@667
|
307 }
|
adamc@667
|
308
|
adamc@477
|
309 static void help(char *cmd) {
|
adamc@477
|
310 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
|
adamc@477
|
311 }
|
adamc@477
|
312
|
adamc@502
|
313 static void sigint(int signum) {
|
adamc@502
|
314 printf("Exiting....\n");
|
adamc@502
|
315 exit(0);
|
adamc@502
|
316 }
|
adamc@502
|
317
|
adamc@138
|
318 int main(int argc, char *argv[]) {
|
adamc@116
|
319 // The skeleton for this function comes from Beej's sockets tutorial.
|
adamc@138
|
320 int sockfd; // listen on sock_fd
|
adamc@116
|
321 struct sockaddr_in my_addr;
|
adamc@116
|
322 struct sockaddr_in their_addr; // connector's address information
|
adamc@116
|
323 int sin_size, yes = 1;
|
adamc@472
|
324 int uw_port = 8080, nthreads = 1, i, *names, opt;
|
adamc@502
|
325
|
adamc@502
|
326 signal(SIGINT, sigint);
|
adamc@505
|
327 signal(SIGPIPE, SIG_IGN);
|
adamc@505
|
328
|
adamc@477
|
329 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
|
adamc@472
|
330 switch (opt) {
|
adamc@472
|
331 case '?':
|
adamc@472
|
332 fprintf(stderr, "Unknown command-line option");
|
adamc@477
|
333 help(argv[0]);
|
adamc@472
|
334 return 1;
|
adamc@138
|
335
|
adamc@477
|
336 case 'h':
|
adamc@477
|
337 help(argv[0]);
|
adamc@477
|
338 return 0;
|
adamc@477
|
339
|
adamc@472
|
340 case 'p':
|
adamc@472
|
341 uw_port = atoi(optarg);
|
adamc@472
|
342 if (uw_port <= 0) {
|
adamc@472
|
343 fprintf(stderr, "Invalid port number\n");
|
adamc@477
|
344 help(argv[0]);
|
adamc@472
|
345 return 1;
|
adamc@472
|
346 }
|
adamc@472
|
347 break;
|
adamc@472
|
348
|
adamc@472
|
349 case 't':
|
adamc@472
|
350 nthreads = atoi(optarg);
|
adamc@472
|
351 if (nthreads <= 0) {
|
adamc@472
|
352 fprintf(stderr, "Invalid thread count\n");
|
adamc@477
|
353 help(argv[0]);
|
adamc@472
|
354 return 1;
|
adamc@472
|
355 }
|
adamc@472
|
356 break;
|
adamc@472
|
357
|
adamc@472
|
358 default:
|
adamc@472
|
359 fprintf(stderr, "Unexpected getopt() behavior\n");
|
adamc@472
|
360 return 1;
|
adamc@472
|
361 }
|
adamc@138
|
362 }
|
adamc@138
|
363
|
adamc@138
|
364 names = calloc(nthreads, sizeof(int));
|
adamc@116
|
365
|
adamc@116
|
366 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
|
adamc@116
|
367
|
adamc@116
|
368 if (sockfd < 0) {
|
adamc@116
|
369 fprintf(stderr, "Listener socket creation failed\n");
|
adamc@116
|
370 return 1;
|
adamc@116
|
371 }
|
adamc@116
|
372
|
adamc@116
|
373 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
|
adamc@116
|
374 fprintf(stderr, "Listener socket option setting failed\n");
|
adamc@116
|
375 return 1;
|
adamc@116
|
376 }
|
adamc@116
|
377
|
adamc@116
|
378 my_addr.sin_family = AF_INET; // host byte order
|
adamc@311
|
379 my_addr.sin_port = htons(uw_port); // short, network byte order
|
adamc@116
|
380 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
|
adamc@116
|
381 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
|
adamc@116
|
382
|
adamc@116
|
383 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
|
adamc@116
|
384 fprintf(stderr, "Listener socket bind failed\n");
|
adamc@116
|
385 return 1;
|
adamc@116
|
386 }
|
adamc@116
|
387
|
adamc@311
|
388 if (listen(sockfd, uw_backlog) < 0) {
|
adamc@116
|
389 fprintf(stderr, "Socket listen failed\n");
|
adamc@116
|
390 return 1;
|
adamc@116
|
391 }
|
adamc@116
|
392
|
adamc@116
|
393 sin_size = sizeof their_addr;
|
adamc@116
|
394
|
adamc@667
|
395 uw_global_init();
|
adamc@667
|
396
|
adamc@311
|
397 printf("Listening on port %d....\n", uw_port);
|
adamc@116
|
398
|
adamc@667
|
399 {
|
adamc@667
|
400 pthread_t thread;
|
adamc@667
|
401 int name;
|
adamc@667
|
402
|
adamc@667
|
403 if (pthread_create(&thread, NULL, client_pruner, &name)) {
|
adamc@667
|
404 fprintf(stderr, "Error creating pruner thread\n");
|
adamc@667
|
405 return 1;
|
adamc@667
|
406 }
|
adamc@667
|
407 }
|
adamc@667
|
408
|
adamc@138
|
409 for (i = 0; i < nthreads; ++i) {
|
adamc@138
|
410 pthread_t thread;
|
adamc@138
|
411 names[i] = i;
|
adamc@138
|
412 if (pthread_create(&thread, NULL, worker, &names[i])) {
|
adamc@138
|
413 fprintf(stderr, "Error creating worker thread #%d\n", i);
|
adamc@138
|
414 return 1;
|
adamc@138
|
415 }
|
adamc@138
|
416 }
|
adamc@138
|
417
|
adamc@116
|
418 while (1) {
|
adamc@138
|
419 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
|
adamc@116
|
420
|
adamc@116
|
421 if (new_fd < 0) {
|
adamc@116
|
422 fprintf(stderr, "Socket accept failed\n");
|
adamc@116
|
423 return 1;
|
adamc@116
|
424 }
|
adamc@116
|
425
|
adamc@116
|
426 printf("Accepted connection.\n");
|
adamc@138
|
427
|
adamc@138
|
428 pthread_mutex_lock(&queue_mutex);
|
adamc@138
|
429 enqueue(new_fd);
|
adamc@139
|
430 pthread_cond_broadcast(&queue_cond);
|
adamc@138
|
431 pthread_mutex_unlock(&queue_mutex);
|
adamc@116
|
432 }
|
adamc@102
|
433 }
|