comparison src/c/driver.c @ 116:c5d7ce9ddd57

Basic web server
author Adam Chlipala <adamc@hcoop.net>
date Sun, 13 Jul 2008 13:38:23 -0400
parents 5f04adf47f48
children 94856a3b4752
comparison
equal deleted inserted replaced
115:ff13d390ec60 116:c5d7ce9ddd57
1 void lw_handle(void); 1 #include <stdio.h>
2
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/socket.h>
6 #include <netinet/in.h>
7
8 int lw_port = 8080;
9 int lw_backlog = 10;
10 int lw_bufsize = 1024;
11
12 void lw_handle(char*);
13
14 static void worker(int sock) {
15 char buf[lw_bufsize+1], *back = buf, *s;
16
17 while (1) {
18 int r = recv(sock, back, lw_bufsize - (back - buf), 0);
19
20 if (r < 0) {
21 fprintf(stderr, "Recv failed\n");
22 close(sock);
23 return;
24 }
25
26 if (r == 0) {
27 printf("Connection closed.\n");
28 close(sock);
29 return;
30 }
31
32 printf("Received %d bytes.\n", r);
33
34 back += r;
35 *back = 0;
36
37 if (s = strstr(buf, "\r\n\r\n")) {
38 char *cmd, *path;
39
40 *s = 0;
41
42 if (!(s = strstr(buf, "\n"))) {
43 fprintf(stderr, "No newline in buf\n");
44 close(sock);
45 return;
46 }
47
48 *s = 0;
49 cmd = s = buf;
50
51 if (!strsep(&s, " ")) {
52 fprintf(stderr, "No first space in HTTP command\n");
53 close(sock);
54 return;
55 }
56
57 if (strcmp(cmd, "GET")) {
58 fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
59 close(sock);
60 return;
61 }
62
63 path = s;
64 if (!strsep(&s, " ")) {
65 fprintf(stderr, "No second space in HTTP command\n");
66 close(sock);
67 return;
68 }
69
70 printf("Serving URI %s....\n", path);
71 puts("Content-type: text/html\n\n");
72 puts("<html>");
73 lw_handle(path);
74 puts("</html>");
75
76 printf("Done with client.\n\n");
77 close(sock);
78 return;
79 }
80 }
81 }
2 82
3 int main() { 83 int main() {
4 puts("<html>"); 84 // The skeleton for this function comes from Beej's sockets tutorial.
5 lw_handle(); 85 int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
6 puts("</html>"); 86 struct sockaddr_in my_addr;
7 return 0; 87 struct sockaddr_in their_addr; // connector's address information
88 int sin_size, yes = 1;
89
90 sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
91
92 if (sockfd < 0) {
93 fprintf(stderr, "Listener socket creation failed\n");
94 return 1;
95 }
96
97 if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
98 fprintf(stderr, "Listener socket option setting failed\n");
99 return 1;
100 }
101
102 my_addr.sin_family = AF_INET; // host byte order
103 my_addr.sin_port = htons(lw_port); // short, network byte order
104 my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
105 memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
106
107 if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
108 fprintf(stderr, "Listener socket bind failed\n");
109 return 1;
110 }
111
112 if (listen(sockfd, lw_backlog) < 0) {
113 fprintf(stderr, "Socket listen failed\n");
114 return 1;
115 }
116
117 sin_size = sizeof their_addr;
118
119 printf("Listening on port %d....\n", lw_port);
120
121 while (1) {
122 new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
123
124 if (new_fd < 0) {
125 fprintf(stderr, "Socket accept failed\n");
126 return 1;
127 }
128
129 printf("Accepted connection.\n");
130 worker(new_fd);
131 }
8 } 132 }