adamc@737: #define _GNU_SOURCE adamc@737: adamc@116: #include adamc@116: #include adamc@502: #include adamc@116: #include adamc@116: #include adamc@116: #include adamc@472: #include adamc@502: #include adamc@856: #include adamc@116: adamc@138: #include adamc@138: adamc@244: #include "urweb.h" adamc@853: #include "request.h" adamc@859: #include "queue.h" adamc@117: adamc@1094: extern uw_app uw_application; adamc@1094: adamc@311: int uw_backlog = 10; adamc@116: adamc@854: static char *get_header(void *data, const char *h) { adamc@854: char *s = data; adamc@854: int len = strlen(h); adamc@854: char *p; adamc@854: adamc@1134: while ((p = strchr(s, ':'))) { adamc@854: if (p - s == len && !strncasecmp(s, h, len)) { adamc@854: return p + 2; adamc@854: } else { adamc@854: if ((s = strchr(p, 0)) && s[1] != 0) adamc@854: s += 2; adamc@854: else adamc@854: return NULL; adamc@854: } adamc@854: } adamc@854: adamc@854: return NULL; adamc@854: } adamc@854: adamc@856: static void on_success(uw_context ctx) { adamc@856: uw_write_header(ctx, "HTTP/1.1 200 OK\r\n"); adamc@856: } adamc@856: adamc@856: static void on_failure(uw_context ctx) { adamc@856: uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n"); adamc@856: } adamc@856: adamc@856: static void log_error(void *data, const char *fmt, ...) { adamc@856: va_list ap; adamc@856: va_start(ap, fmt); adamc@856: adamc@856: vfprintf(stderr, fmt, ap); adamc@856: } adamc@856: adamc@856: static void log_debug(void *data, const char *fmt, ...) { adamc@856: va_list ap; adamc@856: va_start(ap, fmt); adamc@856: adamc@856: vprintf(fmt, ap); adamc@856: } adamc@856: adamc@853: static void *worker(void *data) { adamc@853: int me = *(int *)data; adamc@1094: uw_context ctx = uw_request_new_context(&uw_application, NULL, log_error, log_debug); adamc@853: size_t buf_size = 2; adamc@853: char *buf = malloc(buf_size); adamc@853: uw_request_context rc = uw_new_request_context(); adamc@698: adamc@272: while (1) { adamc@853: char *back = buf; adamc@859: int sock = uw_dequeue(); adamc@138: adamc@138: printf("Handling connection with thread #%d.\n", me); adamc@138: adamc@138: while (1) { adamc@737: int r; adamc@854: char *method, *path, *query_string, *headers, *body, *s, *s2; adamc@737: adamc@742: if (back - buf == buf_size - 1) { adamc@737: char *new_buf; adamc@737: buf_size *= 2; adamc@737: new_buf = realloc(buf, buf_size); adamc@737: back = new_buf + (back - buf); adamc@737: buf = new_buf; adamc@737: } adamc@737: adamc@742: r = recv(sock, back, buf_size - 1 - (back - buf), 0); adamc@138: adamc@138: if (r < 0) { adamc@138: fprintf(stderr, "Recv failed\n"); adamc@138: break; adamc@138: } adamc@138: adamc@138: if (r == 0) { adamc@138: printf("Connection closed.\n"); adamc@138: break; adamc@138: } adamc@138: adamc@138: back += r; adamc@138: *back = 0; adamc@730: adamc@854: if ((body = strstr(buf, "\r\n\r\n"))) { adamc@853: request_result rr; adamc@138: adamc@854: body[0] = body[1] = 0; adamc@854: body += 4; adamc@854: adamc@854: if ((s = strcasestr(buf, "\r\nContent-Length: ")) && s < body) { adamc@853: int clen; adamc@737: adamc@854: if (sscanf(s + 18, "%d\r\n", &clen) != 1) { adamc@853: fprintf(stderr, "Malformed Content-Length header\n"); adamc@167: break; adamc@167: } adamc@167: adamc@854: while (back - body < clen) { adamc@854: if (back - buf == buf_size - 1) { adamc@854: char *new_buf; adamc@854: buf_size *= 2; adamc@854: new_buf = realloc(buf, buf_size); adamc@854: adamc@854: back = new_buf + (back - buf); adamc@854: body = new_buf + (body - buf); adamc@854: s = new_buf + (s - buf); adamc@854: adamc@854: buf = new_buf; adamc@854: } adamc@854: adamc@854: r = recv(sock, back, buf_size - 1 - (back - buf), 0); adamc@854: adamc@854: if (r < 0) { adamc@854: fprintf(stderr, "Recv failed\n"); adamc@854: close(sock); adamc@854: goto done; adamc@854: } adamc@854: adamc@854: if (r == 0) { adamc@854: fprintf(stderr, "Connection closed.\n"); adamc@854: close(sock); adamc@854: goto done; adamc@854: } adamc@854: adamc@854: back += r; adamc@854: *back = 0; adamc@854: } adamc@167: } adamc@138: adamc@1119: body[-4] = '\r'; adamc@1119: body[-3] = '\n'; adamc@1119: adamc@854: if (!(s = strstr(buf, "\r\n"))) { adamc@854: fprintf(stderr, "No newline in request\n"); adamc@854: close(sock); adamc@854: goto done; adamc@854: } adamc@854: adamc@1119: body[-4] = body[-3] = 0; adamc@1119: adamc@854: *s = 0; adamc@854: headers = s + 2; adamc@854: method = s = buf; adamc@854: adamc@854: if (!strsep(&s, " ")) { adamc@854: fprintf(stderr, "No first space in HTTP command\n"); adamc@854: close(sock); adamc@854: goto done; adamc@854: } adamc@854: path = s; adamc@854: adamc@1134: if ((s = strchr(path, ' '))) adamc@854: *s = 0; adamc@854: adamc@1134: if ((s = strchr(path, '?'))) { adamc@854: *s = 0; adamc@854: query_string = s+1; adamc@854: } adamc@854: else adamc@854: query_string = NULL; adamc@854: adamc@854: s = headers; adamc@1134: while ((s2 = strchr(s, '\r'))) { adamc@854: s = s2; adamc@854: adamc@854: if (s[1] == 0) adamc@854: break; adamc@854: adamc@854: *s = 0; adamc@854: s += 2; adamc@854: } adamc@854: adamc@854: uw_set_headers(ctx, get_header, headers); adamc@854: adamc@856: rr = uw_request(rc, ctx, method, path, query_string, body, back - body, adamc@856: on_success, on_failure, adamc@856: NULL, log_error, log_debug, adamc@863: sock, uw_really_send, close); adamc@311: uw_send(ctx, sock); adamc@138: adamc@853: if (rr == SERVED || rr == FAILED) adamc@853: close(sock); adamc@853: else if (rr != KEEP_OPEN) adamc@853: fprintf(stderr, "Illegal uw_request return code: %d\n", rr); adamc@853: adamc@138: break; adamc@138: } adamc@116: } adamc@116: adamc@854: done: adamc@311: uw_reset(ctx); adamc@116: } adamc@116: } adamc@102: adamc@477: static void help(char *cmd) { adamc@477: printf("Usage: %s [-p ] [-t ]\n", cmd); adamc@477: } adamc@477: adamc@502: static void sigint(int signum) { adamc@502: printf("Exiting....\n"); adamc@502: exit(0); adamc@502: } adamc@502: adamc@1094: static loggers ls = {&uw_application, NULL, log_error, log_debug}; adamc@856: adamc@138: int main(int argc, char *argv[]) { adamc@116: // The skeleton for this function comes from Beej's sockets tutorial. adamc@138: int sockfd; // listen on sock_fd adamc@116: struct sockaddr_in my_addr; adamc@116: struct sockaddr_in their_addr; // connector's address information adamc@1134: socklen_t sin_size; adamc@1134: int yes = 1, uw_port = 8080, nthreads = 1, i, *names, opt; adamc@502: adamc@502: signal(SIGINT, sigint); adamc@505: signal(SIGPIPE, SIG_IGN); adamc@505: adamc@477: while ((opt = getopt(argc, argv, "hp:t:")) != -1) { adamc@472: switch (opt) { adamc@472: case '?': adamc@472: fprintf(stderr, "Unknown command-line option"); adamc@477: help(argv[0]); adamc@472: return 1; adamc@138: adamc@477: case 'h': adamc@477: help(argv[0]); adamc@477: return 0; adamc@477: adamc@472: case 'p': adamc@472: uw_port = atoi(optarg); adamc@472: if (uw_port <= 0) { adamc@472: fprintf(stderr, "Invalid port number\n"); adamc@477: help(argv[0]); adamc@472: return 1; adamc@472: } adamc@472: break; adamc@472: adamc@472: case 't': adamc@472: nthreads = atoi(optarg); adamc@472: if (nthreads <= 0) { adamc@472: fprintf(stderr, "Invalid thread count\n"); adamc@477: help(argv[0]); adamc@472: return 1; adamc@472: } adamc@472: break; adamc@472: adamc@472: default: adamc@472: fprintf(stderr, "Unexpected getopt() behavior\n"); adamc@472: return 1; adamc@472: } adamc@138: } adamc@138: adamc@1094: uw_request_init(&uw_application, NULL, log_error, log_debug); adamc@687: adamc@138: names = calloc(nthreads, sizeof(int)); adamc@116: adamc@116: sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking! adamc@116: adamc@116: if (sockfd < 0) { adamc@116: fprintf(stderr, "Listener socket creation failed\n"); adamc@116: return 1; adamc@116: } adamc@116: adamc@116: if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) { adamc@116: fprintf(stderr, "Listener socket option setting failed\n"); adamc@116: return 1; adamc@116: } adamc@116: adamc@116: my_addr.sin_family = AF_INET; // host byte order adamc@311: my_addr.sin_port = htons(uw_port); // short, network byte order adamc@116: my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP adamc@116: memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero); adamc@116: adamc@116: if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) { adamc@116: fprintf(stderr, "Listener socket bind failed\n"); adamc@116: return 1; adamc@116: } adamc@116: adamc@311: if (listen(sockfd, uw_backlog) < 0) { adamc@116: fprintf(stderr, "Socket listen failed\n"); adamc@116: return 1; adamc@116: } adamc@116: adamc@116: sin_size = sizeof their_addr; adamc@116: adamc@311: printf("Listening on port %d....\n", uw_port); adamc@116: adamc@667: { adamc@667: pthread_t thread; adamc@667: adamc@856: if (pthread_create(&thread, NULL, client_pruner, &ls)) { adamc@667: fprintf(stderr, "Error creating pruner thread\n"); adamc@667: return 1; adamc@667: } adamc@667: } adamc@667: adamc@138: for (i = 0; i < nthreads; ++i) { adamc@138: pthread_t thread; adamc@138: names[i] = i; adamc@138: if (pthread_create(&thread, NULL, worker, &names[i])) { adamc@138: fprintf(stderr, "Error creating worker thread #%d\n", i); adamc@138: return 1; adamc@138: } adamc@138: } adamc@138: adamc@116: while (1) { adamc@138: int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); adamc@116: adamc@116: if (new_fd < 0) { adamc@116: fprintf(stderr, "Socket accept failed\n"); adamc@116: return 1; adamc@116: } adamc@116: adamc@116: printf("Accepted connection.\n"); adamc@138: adamc@859: uw_enqueue(new_fd); adamc@116: } adamc@102: } adamc@1121: adamc@1121: void *uw_init_client_data() { adamc@1121: return NULL; adamc@1121: } adamc@1121: adamc@1121: void uw_free_client_data(void *data) { adamc@1121: } adamc@1121: adamc@1121: void uw_copy_client_data(void *dst, void *src) { adamc@1121: } adamc@1121: adamc@1121: void uw_do_expunge(uw_context ctx, uw_Basis_client cli, void *data) { adamc@1121: if (uw_get_app(ctx)->db_begin(ctx)) adamc@1121: uw_error(ctx, FATAL, "Error running SQL BEGIN"); adamc@1121: uw_get_app(ctx)->expunger(ctx, cli); adamc@1121: if (uw_get_app(ctx)->db_commit(ctx)) adamc@1121: uw_error(ctx, FATAL, "Error running SQL COMMIT"); adamc@1121: } adamc@1121: adamc@1121: void uw_post_expunge(uw_context ctx, void *data) { adamc@1121: }