annotate src/c/http.c @ 855:28e42b22424d

Initial implementation of protocols in Settings
author Adam Chlipala <adamc@hcoop.net>
date Tue, 23 Jun 2009 15:56:04 -0400
parents src/c/driver.c@158d980889ac
children 86ec89baee01
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@854 57 static char *get_header(void *data, const char *h) {
adamc@854 58 char *s = data;
adamc@854 59 int len = strlen(h);
adamc@854 60 char *p;
adamc@854 61
adamc@854 62 while (p = strchr(s, ':')) {
adamc@854 63 if (p - s == len && !strncasecmp(s, h, len)) {
adamc@854 64 return p + 2;
adamc@854 65 } else {
adamc@854 66 if ((s = strchr(p, 0)) && s[1] != 0)
adamc@854 67 s += 2;
adamc@854 68 else
adamc@854 69 return NULL;
adamc@854 70 }
adamc@854 71 }
adamc@854 72
adamc@854 73 return NULL;
adamc@854 74 }
adamc@854 75
adamc@853 76 static void *worker(void *data) {
adamc@853 77 int me = *(int *)data;
adamc@853 78 uw_context ctx = uw_request_new_context();
adamc@853 79 size_t buf_size = 2;
adamc@853 80 char *buf = malloc(buf_size);
adamc@853 81 uw_request_context rc = uw_new_request_context();
adamc@698 82
adamc@272 83 while (1) {
adamc@853 84 char *back = buf;
adamc@853 85 int sock;
adamc@116 86
adamc@138 87 pthread_mutex_lock(&queue_mutex);
adamc@138 88 while (empty())
adamc@138 89 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 90 sock = dequeue();
adamc@138 91 pthread_mutex_unlock(&queue_mutex);
adamc@138 92
adamc@138 93 printf("Handling connection with thread #%d.\n", me);
adamc@138 94
adamc@138 95 while (1) {
adamc@737 96 int r;
adamc@854 97 char *method, *path, *query_string, *headers, *body, *s, *s2;
adamc@737 98
adamc@742 99 if (back - buf == buf_size - 1) {
adamc@737 100 char *new_buf;
adamc@737 101 buf_size *= 2;
adamc@737 102 new_buf = realloc(buf, buf_size);
adamc@737 103 back = new_buf + (back - buf);
adamc@737 104 buf = new_buf;
adamc@737 105 }
adamc@737 106
adamc@742 107 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@138 108
adamc@138 109 if (r < 0) {
adamc@138 110 fprintf(stderr, "Recv failed\n");
adamc@138 111 break;
adamc@138 112 }
adamc@138 113
adamc@138 114 if (r == 0) {
adamc@138 115 printf("Connection closed.\n");
adamc@138 116 break;
adamc@138 117 }
adamc@138 118
adamc@138 119 back += r;
adamc@138 120 *back = 0;
adamc@730 121
adamc@854 122 if ((body = strstr(buf, "\r\n\r\n"))) {
adamc@853 123 request_result rr;
adamc@138 124
adamc@854 125 body[0] = body[1] = 0;
adamc@854 126 body += 4;
adamc@854 127
adamc@854 128 if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) {
adamc@853 129 int clen;
adamc@737 130
adamc@854 131 if (sscanf(s + 18, "%d\r\n", &clen) != 1) {
adamc@853 132 fprintf(stderr, "Malformed Content-Length header\n");
adamc@167 133 break;
adamc@167 134 }
adamc@167 135
adamc@854 136 while (back - body < clen) {
adamc@854 137 if (back - buf == buf_size - 1) {
adamc@854 138 char *new_buf;
adamc@854 139 buf_size *= 2;
adamc@854 140 new_buf = realloc(buf, buf_size);
adamc@854 141
adamc@854 142 back = new_buf + (back - buf);
adamc@854 143 body = new_buf + (body - buf);
adamc@854 144 s = new_buf + (s - buf);
adamc@854 145
adamc@854 146 buf = new_buf;
adamc@854 147 }
adamc@854 148
adamc@854 149 r = recv(sock, back, buf_size - 1 - (back - buf), 0);
adamc@854 150
adamc@854 151 if (r < 0) {
adamc@854 152 fprintf(stderr, "Recv failed\n");
adamc@854 153 close(sock);
adamc@854 154 goto done;
adamc@854 155 }
adamc@854 156
adamc@854 157 if (r == 0) {
adamc@854 158 fprintf(stderr, "Connection closed.\n");
adamc@854 159 close(sock);
adamc@854 160 goto done;
adamc@854 161 }
adamc@854 162
adamc@854 163 back += r;
adamc@854 164 *back = 0;
adamc@854 165 }
adamc@167 166 }
adamc@138 167
adamc@854 168 if (!(s = strstr(buf, "\r\n"))) {
adamc@854 169 fprintf(stderr, "No newline in request\n");
adamc@854 170 close(sock);
adamc@854 171 goto done;
adamc@854 172 }
adamc@854 173
adamc@854 174 *s = 0;
adamc@854 175 headers = s + 2;
adamc@854 176 method = s = buf;
adamc@854 177
adamc@854 178 if (!strsep(&s, " ")) {
adamc@854 179 fprintf(stderr, "No first space in HTTP command\n");
adamc@854 180 close(sock);
adamc@854 181 goto done;
adamc@854 182 }
adamc@854 183 path = s;
adamc@854 184
adamc@854 185 if (s = strchr(path, ' '))
adamc@854 186 *s = 0;
adamc@854 187
adamc@854 188 if (s = strchr(path, '?')) {
adamc@854 189 *s = 0;
adamc@854 190 query_string = s+1;
adamc@854 191 }
adamc@854 192 else
adamc@854 193 query_string = NULL;
adamc@854 194
adamc@854 195 s = headers;
adamc@854 196 while (s2 = strchr(s, '\r')) {
adamc@854 197 s = s2;
adamc@854 198
adamc@854 199 if (s[1] == 0)
adamc@854 200 break;
adamc@854 201
adamc@854 202 *s = 0;
adamc@854 203 s += 2;
adamc@854 204 }
adamc@854 205
adamc@854 206 uw_set_headers(ctx, get_header, headers);
adamc@854 207
adamc@854 208 rr = uw_request(rc, ctx, method, path, query_string, body, back - body, sock);
adamc@311 209 uw_send(ctx, sock);
adamc@138 210
adamc@853 211 if (rr == SERVED || rr == FAILED)
adamc@853 212 close(sock);
adamc@853 213 else if (rr != KEEP_OPEN)
adamc@853 214 fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
adamc@853 215
adamc@138 216 break;
adamc@138 217 }
adamc@116 218 }
adamc@116 219
adamc@854 220 done:
adamc@311 221 uw_reset(ctx);
adamc@116 222 }
adamc@116 223 }
adamc@102 224
adamc@477 225 static void help(char *cmd) {
adamc@477 226 printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
adamc@477 227 }
adamc@477 228
adamc@502 229 static void sigint(int signum) {
adamc@502 230 printf("Exiting....\n");
adamc@502 231 exit(0);
adamc@502 232 }
adamc@502 233
adamc@138 234 int main(int argc, char *argv[]) {
adamc@116 235 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 236 int sockfd; // listen on sock_fd
adamc@116 237 struct sockaddr_in my_addr;
adamc@116 238 struct sockaddr_in their_addr; // connector's address information
adamc@116 239 int sin_size, yes = 1;
adamc@472 240 int uw_port = 8080, nthreads = 1, i, *names, opt;
adamc@502 241
adamc@502 242 signal(SIGINT, sigint);
adamc@505 243 signal(SIGPIPE, SIG_IGN);
adamc@505 244
adamc@477 245 while ((opt = getopt(argc, argv, "hp:t:")) != -1) {
adamc@472 246 switch (opt) {
adamc@472 247 case '?':
adamc@472 248 fprintf(stderr, "Unknown command-line option");
adamc@477 249 help(argv[0]);
adamc@472 250 return 1;
adamc@138 251
adamc@477 252 case 'h':
adamc@477 253 help(argv[0]);
adamc@477 254 return 0;
adamc@477 255
adamc@472 256 case 'p':
adamc@472 257 uw_port = atoi(optarg);
adamc@472 258 if (uw_port <= 0) {
adamc@472 259 fprintf(stderr, "Invalid port number\n");
adamc@477 260 help(argv[0]);
adamc@472 261 return 1;
adamc@472 262 }
adamc@472 263 break;
adamc@472 264
adamc@472 265 case 't':
adamc@472 266 nthreads = atoi(optarg);
adamc@472 267 if (nthreads <= 0) {
adamc@472 268 fprintf(stderr, "Invalid thread count\n");
adamc@477 269 help(argv[0]);
adamc@472 270 return 1;
adamc@472 271 }
adamc@472 272 break;
adamc@472 273
adamc@472 274 default:
adamc@472 275 fprintf(stderr, "Unexpected getopt() behavior\n");
adamc@472 276 return 1;
adamc@472 277 }
adamc@138 278 }
adamc@138 279
adamc@853 280 uw_request_init();
adamc@687 281
adamc@138 282 names = calloc(nthreads, sizeof(int));
adamc@116 283
adamc@116 284 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 285
adamc@116 286 if (sockfd < 0) {
adamc@116 287 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 288 return 1;
adamc@116 289 }
adamc@116 290
adamc@116 291 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 292 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 293 return 1;
adamc@116 294 }
adamc@116 295
adamc@116 296 my_addr.sin_family = AF_INET; // host byte order
adamc@311 297 my_addr.sin_port = htons(uw_port); // short, network byte order
adamc@116 298 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 299 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 300
adamc@116 301 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 302 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 303 return 1;
adamc@116 304 }
adamc@116 305
adamc@311 306 if (listen(sockfd, uw_backlog) < 0) {
adamc@116 307 fprintf(stderr, "Socket listen failed\n");
adamc@116 308 return 1;
adamc@116 309 }
adamc@116 310
adamc@116 311 sin_size = sizeof their_addr;
adamc@116 312
adamc@311 313 printf("Listening on port %d....\n", uw_port);
adamc@116 314
adamc@667 315 {
adamc@667 316 pthread_t thread;
adamc@667 317 int name;
adamc@667 318
adamc@667 319 if (pthread_create(&thread, NULL, client_pruner, &name)) {
adamc@667 320 fprintf(stderr, "Error creating pruner thread\n");
adamc@667 321 return 1;
adamc@667 322 }
adamc@667 323 }
adamc@667 324
adamc@138 325 for (i = 0; i < nthreads; ++i) {
adamc@138 326 pthread_t thread;
adamc@138 327 names[i] = i;
adamc@138 328 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 329 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 330 return 1;
adamc@138 331 }
adamc@138 332 }
adamc@138 333
adamc@116 334 while (1) {
adamc@138 335 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 336
adamc@116 337 if (new_fd < 0) {
adamc@116 338 fprintf(stderr, "Socket accept failed\n");
adamc@116 339 return 1;
adamc@116 340 }
adamc@116 341
adamc@116 342 printf("Accepted connection.\n");
adamc@138 343
adamc@138 344 pthread_mutex_lock(&queue_mutex);
adamc@138 345 enqueue(new_fd);
adamc@139 346 pthread_cond_broadcast(&queue_cond);
adamc@138 347 pthread_mutex_unlock(&queue_mutex);
adamc@116 348 }
adamc@102 349 }