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