annotate src/c/driver.c @ 144:f0d3402184d1

Simple forms work
author Adam Chlipala <adamc@hcoop.net>
date Tue, 22 Jul 2008 15:12:20 -0400
parents adfa2c7a75da
children b1b33f7cf555
rev   line source
adamc@116 1 #include <stdio.h>
adamc@116 2
adamc@116 3 #include <string.h>
adamc@116 4 #include <sys/types.h>
adamc@116 5 #include <sys/socket.h>
adamc@116 6 #include <netinet/in.h>
adamc@116 7
adamc@138 8 #include <pthread.h>
adamc@138 9
adamc@117 10 #include "lacweb.h"
adamc@117 11
adamc@116 12 int lw_port = 8080;
adamc@116 13 int lw_backlog = 10;
adamc@116 14 int lw_bufsize = 1024;
adamc@116 15
adamc@117 16 void lw_handle(lw_context, char*);
adamc@116 17
adamc@138 18 typedef struct node {
adamc@138 19 int fd;
adamc@138 20 struct node *next;
adamc@138 21 } *node;
adamc@138 22
adamc@138 23 static node front = NULL, back = NULL;
adamc@138 24
adamc@138 25 static int empty() {
adamc@138 26 return front == NULL;
adamc@138 27 }
adamc@138 28
adamc@138 29 static void enqueue(int fd) {
adamc@138 30 node n = malloc(sizeof(struct node));
adamc@138 31
adamc@138 32 n->fd = fd;
adamc@138 33 n->next = NULL;
adamc@138 34 if (back)
adamc@138 35 back->next = n;
adamc@138 36 else
adamc@138 37 front = n;
adamc@138 38 back = n;
adamc@138 39 }
adamc@138 40
adamc@138 41 static int dequeue() {
adamc@138 42 int ret = front->fd;
adamc@138 43
adamc@138 44 front = front->next;
adamc@138 45 if (!front)
adamc@138 46 back = NULL;
adamc@138 47
adamc@138 48 return ret;
adamc@138 49 }
adamc@138 50
adamc@138 51 static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
adamc@138 52 static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
adamc@138 53
adamc@138 54 static void *worker(void *data) {
adamc@138 55 int me = *(int *)data;
adamc@138 56 lw_context ctx = lw_init(1024, 1024);
adamc@116 57
adamc@116 58 while (1) {
adamc@138 59 char buf[lw_bufsize+1], *back = buf, *s;
adamc@138 60 int sock;
adamc@116 61
adamc@138 62 pthread_mutex_lock(&queue_mutex);
adamc@138 63 while (empty())
adamc@138 64 pthread_cond_wait(&queue_cond, &queue_mutex);
adamc@138 65 sock = dequeue();
adamc@138 66 pthread_mutex_unlock(&queue_mutex);
adamc@138 67
adamc@138 68 printf("Handling connection with thread #%d.\n", me);
adamc@138 69
adamc@138 70 while (1) {
adamc@138 71 int r = recv(sock, back, lw_bufsize - (back - buf), 0);
adamc@138 72
adamc@138 73 if (r < 0) {
adamc@138 74 fprintf(stderr, "Recv failed\n");
adamc@138 75 break;
adamc@138 76 }
adamc@138 77
adamc@138 78 if (r == 0) {
adamc@138 79 printf("Connection closed.\n");
adamc@138 80 break;
adamc@138 81 }
adamc@138 82
adamc@138 83 printf("Received %d bytes.\n", r);
adamc@138 84
adamc@138 85 back += r;
adamc@138 86 *back = 0;
adamc@138 87
adamc@138 88 if (s = strstr(buf, "\r\n\r\n")) {
adamc@144 89 char *cmd, *path, *inputs;
adamc@138 90
adamc@138 91 *s = 0;
adamc@144 92
adamc@144 93 printf("Read: %s\n", buf);
adamc@138 94
adamc@138 95 if (!(s = strstr(buf, "\r\n"))) {
adamc@138 96 fprintf(stderr, "No newline in buf\n");
adamc@138 97 break;
adamc@138 98 }
adamc@138 99
adamc@138 100 *s = 0;
adamc@138 101 cmd = s = buf;
adamc@138 102
adamc@138 103 if (!strsep(&s, " ")) {
adamc@138 104 fprintf(stderr, "No first space in HTTP command\n");
adamc@138 105 break;
adamc@138 106 }
adamc@138 107
adamc@138 108 if (strcmp(cmd, "GET")) {
adamc@138 109 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
adamc@138 110 break;
adamc@138 111 }
adamc@138 112
adamc@138 113 path = s;
adamc@138 114 if (!strsep(&s, " ")) {
adamc@138 115 fprintf(stderr, "No second space in HTTP command\n");
adamc@138 116 break;
adamc@138 117 }
adamc@138 118
adamc@144 119 if (inputs = strchr(path, '?')) {
adamc@144 120 char *name, *value;
adamc@144 121 *inputs++ = 0;
adamc@144 122
adamc@144 123 while (*inputs) {
adamc@144 124 name = inputs;
adamc@144 125 if (value = strchr(inputs, '=')) {
adamc@144 126 *value++ = 0;
adamc@144 127 if (inputs = strchr(value, '&'))
adamc@144 128 *inputs++ = 0;
adamc@144 129 else
adamc@144 130 inputs = strchr(value, 0);
adamc@144 131 lw_set_input(ctx, name, value);
adamc@144 132 }
adamc@144 133 else if (inputs = strchr(value, '&')) {
adamc@144 134 *inputs++ = 0;
adamc@144 135 lw_set_input(ctx, name, "");
adamc@144 136 }
adamc@144 137 else {
adamc@144 138 inputs = strchr(value, 0);
adamc@144 139 lw_set_input(ctx, name, "");
adamc@144 140 }
adamc@144 141 }
adamc@144 142 }
adamc@144 143
adamc@138 144 printf("Serving URI %s....\n", path);
adamc@138 145
adamc@138 146 lw_write (ctx, "HTTP/1.1 200 OK\r\n");
adamc@138 147 lw_write(ctx, "Content-type: text/html\r\n\r\n");
adamc@138 148 lw_write(ctx, "<html>");
adamc@138 149 lw_handle(ctx, path);
adamc@138 150 lw_write(ctx, "</html>");
adamc@138 151
adamc@138 152 lw_send(ctx, sock);
adamc@138 153
adamc@138 154 printf("Done with client.\n\n");
adamc@138 155 break;
adamc@138 156 }
adamc@116 157 }
adamc@116 158
adamc@138 159 close(sock);
adamc@138 160 lw_reset(ctx);
adamc@116 161 }
adamc@116 162 }
adamc@102 163
adamc@138 164 int main(int argc, char *argv[]) {
adamc@116 165 // The skeleton for this function comes from Beej's sockets tutorial.
adamc@138 166 int sockfd; // listen on sock_fd
adamc@116 167 struct sockaddr_in my_addr;
adamc@116 168 struct sockaddr_in their_addr; // connector's address information
adamc@116 169 int sin_size, yes = 1;
adamc@138 170 int nthreads, i, *names;
adamc@138 171
adamc@138 172 if (argc < 2) {
adamc@138 173 fprintf(stderr, "No thread count specified\n");
adamc@138 174 return 1;
adamc@138 175 }
adamc@138 176
adamc@138 177 nthreads = atoi(argv[1]);
adamc@138 178 if (nthreads <= 0) {
adamc@138 179 fprintf(stderr, "Invalid thread count\n");
adamc@138 180 return 1;
adamc@138 181 }
adamc@138 182 names = calloc(nthreads, sizeof(int));
adamc@116 183
adamc@116 184 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
adamc@116 185
adamc@116 186 if (sockfd < 0) {
adamc@116 187 fprintf(stderr, "Listener socket creation failed\n");
adamc@116 188 return 1;
adamc@116 189 }
adamc@116 190
adamc@116 191 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
adamc@116 192 fprintf(stderr, "Listener socket option setting failed\n");
adamc@116 193 return 1;
adamc@116 194 }
adamc@116 195
adamc@116 196 my_addr.sin_family = AF_INET; // host byte order
adamc@116 197 my_addr.sin_port = htons(lw_port); // short, network byte order
adamc@116 198 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
adamc@116 199 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
adamc@116 200
adamc@116 201 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
adamc@116 202 fprintf(stderr, "Listener socket bind failed\n");
adamc@116 203 return 1;
adamc@116 204 }
adamc@116 205
adamc@116 206 if (listen(sockfd, lw_backlog) < 0) {
adamc@116 207 fprintf(stderr, "Socket listen failed\n");
adamc@116 208 return 1;
adamc@116 209 }
adamc@116 210
adamc@116 211 sin_size = sizeof their_addr;
adamc@116 212
adamc@116 213 printf("Listening on port %d....\n", lw_port);
adamc@116 214
adamc@138 215 for (i = 0; i < nthreads; ++i) {
adamc@138 216 pthread_t thread;
adamc@138 217 names[i] = i;
adamc@138 218 if (pthread_create(&thread, NULL, worker, &names[i])) {
adamc@138 219 fprintf(stderr, "Error creating worker thread #%d\n", i);
adamc@138 220 return 1;
adamc@138 221 }
adamc@138 222 }
adamc@138 223
adamc@116 224 while (1) {
adamc@138 225 int new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
adamc@116 226
adamc@116 227 if (new_fd < 0) {
adamc@116 228 fprintf(stderr, "Socket accept failed\n");
adamc@116 229 return 1;
adamc@116 230 }
adamc@116 231
adamc@116 232 printf("Accepted connection.\n");
adamc@138 233
adamc@138 234 pthread_mutex_lock(&queue_mutex);
adamc@138 235 enqueue(new_fd);
adamc@139 236 pthread_cond_broadcast(&queue_cond);
adamc@138 237 pthread_mutex_unlock(&queue_mutex);
adamc@116 238 }
adamc@102 239 }