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