comparison src/c/driver.c @ 117:94856a3b4752

Serving pages
author Adam Chlipala <adamc@hcoop.net>
date Sun, 13 Jul 2008 15:44:00 -0400
parents c5d7ce9ddd57
children 133fa2d51bb4
comparison
equal deleted inserted replaced
116:c5d7ce9ddd57 117:94856a3b4752
3 #include <string.h> 3 #include <string.h>
4 #include <sys/types.h> 4 #include <sys/types.h>
5 #include <sys/socket.h> 5 #include <sys/socket.h>
6 #include <netinet/in.h> 6 #include <netinet/in.h>
7 7
8 #include "lacweb.h"
9
8 int lw_port = 8080; 10 int lw_port = 8080;
9 int lw_backlog = 10; 11 int lw_backlog = 10;
10 int lw_bufsize = 1024; 12 int lw_bufsize = 1024;
11 13
12 void lw_handle(char*); 14 void lw_handle(lw_context, char*);
13 15
14 static void worker(int sock) { 16 static void worker(int sock) {
15 char buf[lw_bufsize+1], *back = buf, *s; 17 char buf[lw_bufsize+1], *back = buf, *s;
16 18
17 while (1) { 19 while (1) {
34 back += r; 36 back += r;
35 *back = 0; 37 *back = 0;
36 38
37 if (s = strstr(buf, "\r\n\r\n")) { 39 if (s = strstr(buf, "\r\n\r\n")) {
38 char *cmd, *path; 40 char *cmd, *path;
41 lw_context ctx;
39 42
40 *s = 0; 43 *s = 0;
41 44
42 if (!(s = strstr(buf, "\n"))) { 45 if (!(s = strstr(buf, "\r\n"))) {
43 fprintf(stderr, "No newline in buf\n"); 46 fprintf(stderr, "No newline in buf\n");
44 close(sock); 47 close(sock);
45 return; 48 return;
46 } 49 }
47 50
66 close(sock); 69 close(sock);
67 return; 70 return;
68 } 71 }
69 72
70 printf("Serving URI %s....\n", path); 73 printf("Serving URI %s....\n", path);
71 puts("Content-type: text/html\n\n"); 74
72 puts("<html>"); 75 ctx = lw_init(1024);
73 lw_handle(path); 76 lw_write (ctx, "HTTP/1.1 200 OK\r\n");
74 puts("</html>"); 77 lw_write(ctx, "Content-type: text/html\r\n\r\n");
78 lw_write(ctx, "<html>");
79 lw_handle(ctx, path);
80 lw_write(ctx, "</html>");
81
82 lw_send(ctx, sock);
75 83
76 printf("Done with client.\n\n"); 84 printf("Done with client.\n\n");
77 close(sock); 85 close(sock);
78 return; 86 return;
79 } 87 }