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