annotate src/c/http.c @ 899:25a038a9194b

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