annotate src/c/driver.c @ 853:19fdeef40ada

Factor out common request functionality, in preparation for supporting different protocols
author Adam Chlipala <adamc@hcoop.net>
date Tue, 23 Jun 2009 14:05:12 -0400
parents 5f49a6b759cb
children 158d980889ac
rev   line source
adamc@737 1 #define _GNU_SOURCE
adamc@737 2
adamc@116 3 #include <stdio.h>
adamc@116 4 #include <string.h>
adamc@502 5 #include <stdlib.h>
adamc@116 6 #include <sys/types.h>
adamc@116 7 #include <sys/socket.h>
adamc@116 8 #include <netinet/in.h>
adamc@472 9 #include <unistd.h>
adamc@502 10 #include <signal.h>
adamc@116 11
adamc@138 12 #include <pthread.h>
adamc@138 13
adamc@734 14 #include <mhash.h>
adamc@734 15
adamc@244 16 #include "urweb.h"
adamc@853 17 #include "request.h"
adamc@117 18
adamc@311 19 int uw_backlog = 10;
adamc@116 20
adamc@138 21 typedef struct node {
adamc@138 22 int fd;
adamc@138 23 struct node *next;
adamc@138 24 } *node;
adamc@138 25
adamc@138 26 static node front = NULL, back = NULL;
adamc@138 27
adamc@138 28 static int empty() {
adamc@138 29 return front == NULL;
adamc@138 30 }
adamc@138 31
adamc@138 32 static void enqueue(int fd) {
adamc@138 33 node n = malloc(sizeof(struct node));
adamc@138 34
adamc@138 35 n->fd = fd;
adamc@138 36 n->next = NULL;
adamc@138 37 if (back)
adamc@138 38 back->next = n;
adamc@138 39 else
adamc@138 40 front = n;
adamc@138 41 back = n;
adamc@138 42 }
adamc@138 43
adamc@138 44 static int dequeue() {
adamc@138 45 int ret = front->fd;
adamc@138 46
adamc@138 47 front = front->next;
adamc@138 48 if (!front)
adamc@138 49 back = NULL;
adamc@138 50
adamc@138 51 return ret;
adamc@138 52 }
adamc@138 53
adamc@138 54 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 55 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 56
adamc@853 57 static void *worker(void *data) {
adamc@853 58 int me = *(int *)data;
adamc@853 59 uw_context ctx = uw_request_new_context();
adamc@853 60 size_t buf_size = 2;
adamc@853 61 char *buf = malloc(buf_size);
adamc@853 62 uw_request_context rc = uw_new_request_context();
adamc@698 63
adamc@272 64 while (1) {
adamc@853 65 char *back = buf;
adamc@853 66 int sock;
adamc@116 67
adamc@138 68 pthread_mutex_lock(&queue_mutex);
adamc@138 69 while (empty())
adamc@138 70 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 71 sock = dequeue();
adamc@138 72 pthread_mutex_unlock(&queue_mutex);
adamc@138 73
adamc@138 74 printf("Handling connection with thread #%d.\n", me);
adamc@138 75
adamc@138 76 while (1) {
adamc@737 77 int r;
adamc@853 78 char *s1, *s2;
adamc@737 79
adamc@742 80 if (back - buf == buf_size - 1) {
adamc@737 81 char *new_buf;
adamc@737 82 buf_size *= 2;
adamc@737 83 new_buf = realloc(buf, buf_size);
adamc@737 84 back = new_buf + (back - buf);
adamc@737 85 buf = new_buf;
adamc@737 86 }
adamc@737 87
adamc@742 88 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 89
adamc@138 90 if (r < 0) {
adamc@138 91 fprintf(stderr, "Recv failed\n");
adamc@138 92 break;
adamc@138 93 }
adamc@138 94
adamc@138 95 if (r == 0) {
adamc@138 96 printf("Connection closed.\n");
adamc@138 97 break;
adamc@138 98 }
adamc@138 99
adamc@138 100 back += r;
adamc@138 101 *back = 0;
adamc@730 102
adamc@853 103 if ((s1 = strstr(buf, "\r\n\r\n"))) {
adamc@853 104 request_result rr;
adamc@138 105
adamc@853 106 if ((s2 = strcasestr(buf, "\r\nContent-Length: ")) && s2 < s1) {
adamc@853 107 int clen;
adamc@737 108
adamc@853 109 if (sscanf(s2 + 18, "%d\r\n", &clen) != 1) {
adamc@853 110 fprintf(stderr, "Malformed Content-Length header\n");
adamc@167 111 break;
adamc@167 112 }
adamc@167 113
adamc@853 114 if (s1 + 4 + clen > back)
adamc@853 115 continue;
adamc@167 116 }
adamc@138 117
adamc@853 118 rr = uw_request(rc, ctx, buf, back - buf, sock);
adamc@311 119 uw_send(ctx, sock);
adamc@138 120
adamc@853 121 if (rr == SERVED || rr == FAILED)
adamc@853 122 close(sock);
adamc@853 123 else if (rr != KEEP_OPEN)
adamc@853 124 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 125
adamc@138 126 break;
adamc@138 127 }
adamc@116 128 }
adamc@116 129
adamc@311 130 uw_reset(ctx);
adamc@116 131 }
adamc@116 132 }
adamc@102 133
adamc@477 134 static void help(char *cmd) {
adamc@477 135 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 136 }
adamc@477 137
adamc@502 138 static void sigint(int signum) {
adamc@502 139 printf("Exiting....\n");
adamc@502 140 exit(0);
adamc@502 141 }
adamc@502 142
adamc@138 143 int main(int argc, char *argv[]) {
adamc@116 144 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 145 int sockfd; // listen on sock_fd
adamc@116 146 struct sockaddr_in my_addr;
adamc@116 147 struct sockaddr_in their_addr; // connector's address information
adamc@116 148 int sin_size, yes = 1;
adamc@472 149 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 150
adamc@502 151 signal(SIGINT, sigint);
adamc@505 152 signal(SIGPIPE, SIG_IGN);
adamc@505 153
adamc@477 154 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 155 switch (opt) {
adamc@472 156 case '?':
adamc@472 157 fprintf(stderr, "Unknown command-line option");
adamc@477 158 help(argv[0]);
adamc@472 159 return 1;
adamc@138 160
adamc@477 161 case 'h':
adamc@477 162 help(argv[0]);
adamc@477 163 return 0;
adamc@477 164
adamc@472 165 case 'p':
adamc@472 166 uw_port = atoi(optarg);
adamc@472 167 if (uw_port <= 0) {
adamc@472 168 fprintf(stderr, "Invalid port number\n");
adamc@477 169 help(argv[0]);
adamc@472 170 return 1;
adamc@472 171 }
adamc@472 172 break;
adamc@472 173
adamc@472 174 case 't':
adamc@472 175 nthreads = atoi(optarg);
adamc@472 176 if (nthreads <= 0) {
adamc@472 177 fprintf(stderr, "Invalid thread count\n");
adamc@477 178 help(argv[0]);
adamc@472 179 return 1;
adamc@472 180 }
adamc@472 181 break;
adamc@472 182
adamc@472 183 default:
adamc@472 184 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 185 return 1;
adamc@472 186 }
adamc@138 187 }
adamc@138 188
adamc@853 189 uw_request_init();
adamc@687 190
adamc@138 191 names = calloc(nthreads, sizeof(int));
adamc@116 192
adamc@116 193 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 194
adamc@116 195 if (sockfd < 0) {
adamc@116 196 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 197 return 1;
adamc@116 198 }
adamc@116 199
adamc@116 200 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 201 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 202 return 1;
adamc@116 203 }
adamc@116 204
adamc@116 205 my_addr.sin_family = AF_INET; // host byte order
adamc@311 206 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 207 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 208 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 209
adamc@116 210 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 211 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 212 return 1;
adamc@116 213 }
adamc@116 214
adamc@311 215 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 216 fprintf(stderr, "Socket listen failed\n");
adamc@116 217 return 1;
adamc@116 218 }
adamc@116 219
adamc@116 220 sin_size = sizeof their_addr;
adamc@116 221
adamc@311 222 printf("Listening on port %d....\n", uw_port);
adamc@116 223
adamc@667 224 {
adamc@667 225 pthread_t thread;
adamc@667 226 int name;
adamc@667 227
adamc@667 228 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 229 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 230 return 1;
adamc@667 231 }
adamc@667 232 }
adamc@667 233
adamc@138 234 for (i = 0; i < nthreads; ++i) {
adamc@138 235 pthread_t thread;
adamc@138 236 names[i] = i;
adamc@138 237 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 238 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 239 return 1;
adamc@138 240 }
adamc@138 241 }
adamc@138 242
adamc@116 243 while (1) {
adamc@138 244 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 245
adamc@116 246 if (new_fd < 0) {
adamc@116 247 fprintf(stderr, "Socket accept failed\n");
adamc@116 248 return 1;
adamc@116 249 }
adamc@116 250
adamc@116 251 printf("Accepted connection.\n");
adamc@138 252
adamc@138 253 pthread_mutex_lock(&queue_mutex);
adamc@138 254 enqueue(new_fd);
adamc@139 255 pthread_cond_broadcast(&queue_cond);
adamc@138 256 pthread_mutex_unlock(&queue_mutex);
adamc@116 257 }
adamc@102 258 }