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@466
|
76 uw_context ctx = uw_init(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@138
|
109 int sock;
|
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@138
|
141
|
adamc@475
|
142 s[2] = 0;
|
adamc@144
|
143
|
adamc@138
|
144 if (!(s = strstr(buf, "\r\n"))) {
|
adamc@138
|
145 fprintf(stderr, "No newline in buf\n");
|
adamc@138
|
146 break;
|
adamc@138
|
147 }
|
adamc@138
|
148
|
adamc@138
|
149 *s = 0;
|
adamc@457
|
150 headers = s + 2;
|
adamc@138
|
151 cmd = s = buf;
|
adamc@401
|
152
|
adamc@401
|
153 printf("Read: %s\n", buf);
|
adamc@138
|
154
|
adamc@138
|
155 if (!strsep(&s, " ")) {
|
adamc@138
|
156 fprintf(stderr, "No first space in HTTP command\n");
|
adamc@138
|
157 break;
|
adamc@138
|
158 }
|
adamc@138
|
159
|
adamc@138
|
160 if (strcmp(cmd, "GET")) {
|
adamc@138
|
161 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
|
adamc@138
|
162 break;
|
adamc@138
|
163 }
|
adamc@138
|
164
|
adamc@138
|
165 path = s;
|
adamc@138
|
166 if (!strsep(&s, " ")) {
|
adamc@138
|
167 fprintf(stderr, "No second space in HTTP command\n");
|
adamc@138
|
168 break;
|
adamc@138
|
169 }
|
adamc@138
|
170
|
adamc@144
|
171 if (inputs = strchr(path, '?')) {
|
adamc@144
|
172 char *name, *value;
|
adamc@144
|
173 *inputs++ = 0;
|
adamc@144
|
174
|
adamc@144
|
175 while (*inputs) {
|
adamc@144
|
176 name = inputs;
|
adamc@145
|
177 if (inputs = strchr(inputs, '&'))
|
adamc@145
|
178 *inputs++ = 0;
|
adamc@145
|
179 else
|
adamc@145
|
180 inputs = strchr(name, 0);
|
adamc@145
|
181
|
adamc@145
|
182 if (value = strchr(name, '=')) {
|
adamc@144
|
183 *value++ = 0;
|
adamc@311
|
184 uw_set_input(ctx, name, value);
|
adamc@144
|
185 }
|
adamc@145
|
186 else
|
adamc@311
|
187 uw_set_input(ctx, name, "");
|
adamc@144
|
188 }
|
adamc@144
|
189 }
|
adamc@144
|
190
|
adamc@138
|
191 printf("Serving URI %s....\n", path);
|
adamc@138
|
192
|
adamc@458
|
193 uw_set_headers(ctx, headers);
|
adamc@458
|
194
|
adamc@167
|
195 while (1) {
|
adamc@500
|
196 uw_write(ctx, "<html>");
|
adamc@500
|
197
|
adamc@424
|
198 if (uw_db_begin(ctx)) {
|
adamc@424
|
199 printf("Error running SQL BEGIN\n");
|
adamc@424
|
200 if (retries_left)
|
adamc@424
|
201 --retries_left;
|
adamc@424
|
202 else {
|
adamc@424
|
203 fk = FATAL;
|
adamc@424
|
204 uw_reset(ctx);
|
adamc@464
|
205 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
206 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
|
adamc@424
|
207 uw_write(ctx, "Error running SQL BEGIN\n");
|
adamc@424
|
208
|
adamc@424
|
209 break;
|
adamc@424
|
210 }
|
adamc@424
|
211 }
|
adamc@167
|
212
|
adamc@462
|
213 uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
|
adamc@462
|
214 uw_write_header(ctx, "Content-type: text/html\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@464
|
219 uw_write(ctx, "</html>");
|
adamc@464
|
220
|
adamc@424
|
221 if (uw_db_commit(ctx)) {
|
adamc@424
|
222 fk = FATAL;
|
adamc@424
|
223
|
adamc@424
|
224 printf("Error running SQL COMMIT\n");
|
adamc@424
|
225 uw_reset(ctx);
|
adamc@464
|
226 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
227 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@424
|
228 uw_write(ctx, "Error running SQL COMMIT\n");
|
adamc@424
|
229 }
|
adamc@424
|
230
|
adamc@167
|
231 break;
|
adamc@167
|
232 } else if (fk == BOUNDED_RETRY) {
|
adamc@167
|
233 if (retries_left) {
|
adamc@311
|
234 printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
|
adamc@167
|
235 --retries_left;
|
adamc@167
|
236 }
|
adamc@167
|
237 else {
|
adamc@311
|
238 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
|
adamc@167
|
239
|
adamc@311
|
240 uw_reset_keep_error_message(ctx);
|
adamc@464
|
241 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
242 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@311
|
243 uw_write(ctx, "Fatal error (out of retries): ");
|
adamc@311
|
244 uw_write(ctx, uw_error_message(ctx));
|
adamc@311
|
245 uw_write(ctx, "\n");
|
adamc@424
|
246
|
adamc@424
|
247 try_rollback(ctx);
|
adamc@424
|
248
|
adamc@424
|
249 break;
|
adamc@167
|
250 }
|
adamc@167
|
251 } else if (fk == UNLIMITED_RETRY)
|
adamc@311
|
252 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
|
adamc@167
|
253 else if (fk == FATAL) {
|
adamc@311
|
254 printf("Fatal error: %s\n", uw_error_message(ctx));
|
adamc@167
|
255
|
adamc@311
|
256 uw_reset_keep_error_message(ctx);
|
adamc@464
|
257 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
|
adamc@464
|
258 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@311
|
259 uw_write(ctx, "Fatal error: ");
|
adamc@311
|
260 uw_write(ctx, uw_error_message(ctx));
|
adamc@311
|
261 uw_write(ctx, "\n");
|
adamc@167
|
262
|
adamc@424
|
263 try_rollback(ctx);
|
adamc@424
|
264
|
adamc@167
|
265 break;
|
adamc@167
|
266 } else {
|
adamc@311
|
267 printf("Unknown uw_handle return code!\n");
|
adamc@167
|
268
|
adamc@311
|
269 uw_reset_keep_request(ctx);
|
adamc@464
|
270 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
|
adamc@464
|
271 uw_write_header(ctx, "Content-type: text/plain\r\n");
|
adamc@311
|
272 uw_write(ctx, "Unknown uw_handle return code!\n");
|
adamc@167
|
273
|
adamc@424
|
274 try_rollback(ctx);
|
adamc@424
|
275
|
adamc@167
|
276 break;
|
adamc@167
|
277 }
|
adamc@167
|
278
|
adamc@311
|
279 uw_reset_keep_request(ctx);
|
adamc@424
|
280
|
adamc@424
|
281 if (try_rollback(ctx))
|
adamc@424
|
282 break;
|
adamc@167
|
283 }
|
adamc@138
|
284
|
adamc@311
|
285 uw_send(ctx, sock);
|
adamc@138
|
286
|
adamc@138
|
287 printf("Done with client.\n\n");
|
adamc@324
|
288 uw_memstats(ctx);
|
adamc@138
|
289 break;
|
adamc@138
|
290 }
|
adamc@116
|
291 }
|
adamc@116
|
292
|
adamc@138
|
293 close(sock);
|
adamc@311
|
294 uw_reset(ctx);
|
adamc@116
|
295 }
|
adamc@116
|
296 }
|
adamc@102
|
297
|
adamc@477
|
298 static void help(char *cmd) {
|
adamc@477
|
299 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
|
adamc@477
|
300 }
|
adamc@477
|
301
|
adamc@502
|
302 static void sigint(int signum) {
|
adamc@502
|
303 printf("Exiting....\n");
|
adamc@502
|
304 exit(0);
|
adamc@502
|
305 }
|
adamc@502
|
306
|
adamc@138
|
307 int main(int argc, char *argv[]) {
|
adamc@116
|
308 // The skeleton for this function comes from Beej's sockets tutorial.
|
adamc@138
|
309 int sockfd; // listen on sock_fd
|
adamc@116
|
310 struct sockaddr_in my_addr;
|
adamc@116
|
311 struct sockaddr_in their_addr; // connector's address information
|
adamc@116
|
312 int sin_size, yes = 1;
|
adamc@472
|
313 int uw_port = 8080, nthreads = 1, i, *names, opt;
|
adamc@502
|
314
|
adamc@502
|
315 signal(SIGINT, sigint);
|
adamc@505
|
316 signal(SIGPIPE, SIG_IGN);
|
adamc@505
|
317
|
adamc@477
|
318 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
|
adamc@472
|
319 switch (opt) {
|
adamc@472
|
320 case '?':
|
adamc@472
|
321 fprintf(stderr, "Unknown command-line option");
|
adamc@477
|
322 help(argv[0]);
|
adamc@472
|
323 return 1;
|
adamc@138
|
324
|
adamc@477
|
325 case 'h':
|
adamc@477
|
326 help(argv[0]);
|
adamc@477
|
327 return 0;
|
adamc@477
|
328
|
adamc@472
|
329 case 'p':
|
adamc@472
|
330 uw_port = atoi(optarg);
|
adamc@472
|
331 if (uw_port <= 0) {
|
adamc@472
|
332 fprintf(stderr, "Invalid port number\n");
|
adamc@477
|
333 help(argv[0]);
|
adamc@472
|
334 return 1;
|
adamc@472
|
335 }
|
adamc@472
|
336 break;
|
adamc@472
|
337
|
adamc@472
|
338 case 't':
|
adamc@472
|
339 nthreads = atoi(optarg);
|
adamc@472
|
340 if (nthreads <= 0) {
|
adamc@472
|
341 fprintf(stderr, "Invalid thread count\n");
|
adamc@477
|
342 help(argv[0]);
|
adamc@472
|
343 return 1;
|
adamc@472
|
344 }
|
adamc@472
|
345 break;
|
adamc@472
|
346
|
adamc@472
|
347 default:
|
adamc@472
|
348 fprintf(stderr, "Unexpected getopt() behavior\n");
|
adamc@472
|
349 return 1;
|
adamc@472
|
350 }
|
adamc@138
|
351 }
|
adamc@138
|
352
|
adamc@138
|
353 names = calloc(nthreads, sizeof(int));
|
adamc@116
|
354
|
adamc@116
|
355 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
|
adamc@116
|
356
|
adamc@116
|
357 if (sockfd < 0) {
|
adamc@116
|
358 fprintf(stderr, "Listener socket creation failed\n");
|
adamc@116
|
359 return 1;
|
adamc@116
|
360 }
|
adamc@116
|
361
|
adamc@116
|
362 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
|
adamc@116
|
363 fprintf(stderr, "Listener socket option setting failed\n");
|
adamc@116
|
364 return 1;
|
adamc@116
|
365 }
|
adamc@116
|
366
|
adamc@116
|
367 my_addr.sin_family = AF_INET; // host byte order
|
adamc@311
|
368 my_addr.sin_port = htons(uw_port); // short, network byte order
|
adamc@116
|
369 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
|
adamc@116
|
370 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
|
adamc@116
|
371
|
adamc@116
|
372 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
|
adamc@116
|
373 fprintf(stderr, "Listener socket bind failed\n");
|
adamc@116
|
374 return 1;
|
adamc@116
|
375 }
|
adamc@116
|
376
|
adamc@311
|
377 if (listen(sockfd, uw_backlog) < 0) {
|
adamc@116
|
378 fprintf(stderr, "Socket listen failed\n");
|
adamc@116
|
379 return 1;
|
adamc@116
|
380 }
|
adamc@116
|
381
|
adamc@116
|
382 sin_size = sizeof their_addr;
|
adamc@116
|
383
|
adamc@311
|
384 printf("Listening on port %d....\n", uw_port);
|
adamc@116
|
385
|
adamc@138
|
386 for (i = 0; i < nthreads; ++i) {
|
adamc@138
|
387 pthread_t thread;
|
adamc@138
|
388 names[i] = i;
|
adamc@138
|
389 if (pthread_create(&thread, NULL, worker, &names[i])) {
|
adamc@138
|
390 fprintf(stderr, "Error creating worker thread #%d\n", i);
|
adamc@138
|
391 return 1;
|
adamc@138
|
392 }
|
adamc@138
|
393 }
|
adamc@138
|
394
|
adamc@116
|
395 while (1) {
|
adamc@138
|
396 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
|
adamc@116
|
397
|
adamc@116
|
398 if (new_fd < 0) {
|
adamc@116
|
399 fprintf(stderr, "Socket accept failed\n");
|
adamc@116
|
400 return 1;
|
adamc@116
|
401 }
|
adamc@116
|
402
|
adamc@116
|
403 printf("Accepted connection.\n");
|
adamc@138
|
404
|
adamc@138
|
405 pthread_mutex_lock(&queue_mutex);
|
adamc@138
|
406 enqueue(new_fd);
|
adamc@139
|
407 pthread_cond_broadcast(&queue_cond);
|
adamc@138
|
408 pthread_mutex_unlock(&queue_mutex);
|
adamc@116
|
409 }
|
adamc@102
|
410 }
|