annotate src/c/http.c @ 1417:d0786ff9bb22

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