tinywebd

view src/main.c @ 5:def49a046566

serialization of responses
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Apr 2015 01:57:00 +0300
parents 9e054c002489
children 5ec50ca0d071
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <signal.h>
6 #include <errno.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/select.h>
11 #include <sys/mman.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <arpa/inet.h>
15 #include "http.h"
16 #include "mime.h"
18 /* HTTP version */
19 #define HTTP_VER_MAJOR 1
20 #define HTTP_VER_MINOR 1
21 #define HTTP_VER_STR "1.1"
23 /* maximum request length: 64mb */
24 #define MAX_REQ_LENGTH (65536 * 1024)
26 struct client {
27 int s;
28 char *rcvbuf;
29 int bufsz;
30 struct client *next;
31 };
33 int start_server(void);
34 int accept_conn(int lis);
35 void close_conn(struct client *c);
36 int handle_client(struct client *c);
37 int do_get(struct client *c, const char *uri, int with_body);
38 void respond_error(struct client *c, int errcode);
39 void sighandler(int s);
40 int parse_args(int argc, char **argv);
42 static int lis;
43 static int port = 8080;
44 static struct client *clist;
46 static const char *indexfiles[] = {
47 "index.cgi",
48 "index.html",
49 "index.htm",
50 0
51 };
54 int main(int argc, char **argv)
55 {
56 if(parse_args(argc, argv) == -1) {
57 return 1;
58 }
60 signal(SIGINT, sighandler);
61 signal(SIGTERM, sighandler);
62 signal(SIGQUIT, sighandler);
64 if((lis = start_server()) == -1) {
65 return 1;
66 }
68 for(;;) {
69 struct client *c, dummy;
70 int maxfd = lis;
71 fd_set rdset;
73 FD_ZERO(&rdset);
74 FD_SET(lis, &rdset);
76 c = clist;
77 while(c) {
78 if(c->s > maxfd) {
79 maxfd = c->s;
80 }
81 FD_SET(c->s, &rdset);
82 c = c->next;
83 }
85 while(select(maxfd + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
87 c = clist;
88 while(c) {
89 if(FD_ISSET(c->s, &rdset)) {
90 handle_client(c);
91 }
92 c = c->next;
93 }
95 if(FD_ISSET(lis, &rdset)) {
96 accept_conn(lis);
97 }
99 dummy.next = clist;
100 c = &dummy;
102 while(c->next) {
103 struct client *n = c->next;
105 if(n->s == -1) {
106 /* marked for removal */
107 c->next = n->next;
108 free(n);
109 } else {
110 c = c->next;
111 }
112 }
113 clist = dummy.next;
114 }
116 return 0; /* unreachable */
117 }
119 int start_server(void)
120 {
121 int s;
122 struct sockaddr_in sa;
124 if((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
125 perror("failed to create listening socket");
126 return -1;
127 }
128 fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
130 memset(&sa, 0, sizeof sa);
131 sa.sin_family = AF_INET;
132 sa.sin_addr.s_addr = INADDR_ANY;
133 sa.sin_port = htons(port);
135 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
136 fprintf(stderr, "failed to bind socket to port %d: %s\n", port, strerror(errno));
137 return -1;
138 }
139 listen(s, 16);
141 return s;
142 }
144 int accept_conn(int lis)
145 {
146 int s;
147 struct client *c;
148 struct sockaddr_in addr;
149 socklen_t addr_sz = sizeof addr;
151 if((s = accept(lis, (struct sockaddr*)&addr, &addr_sz)) == -1) {
152 perror("failed to accept incoming connection");
153 return -1;
154 }
155 fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
157 if(!(c = malloc(sizeof *c))) {
158 perror("failed to allocate memory while accepting connection");
159 return -1;
160 }
161 c->s = s;
162 c->rcvbuf = 0;
163 c->bufsz = 0;
164 c->next = clist;
165 clist = c;
166 return 0;
167 }
169 void close_conn(struct client *c)
170 {
171 close(c->s);
172 c->s = -1; /* mark it for removal */
173 free(c->rcvbuf);
174 }
176 int handle_client(struct client *c)
177 {
178 struct http_req_header hdr;
179 static char buf[2048];
180 int rdsz, status;
182 while((rdsz = recv(c->s, buf, sizeof buf, 0)) > 0) {
183 char *newbuf;
184 int newsz = c->bufsz + rdsz;
185 if(newsz > MAX_REQ_LENGTH) {
186 respond_error(c, 413);
187 return -1;
188 }
190 if(!(newbuf = realloc(c->rcvbuf, newsz + 1))) {
191 fprintf(stderr, "failed to allocate %d byte buffer\n", newsz);
192 respond_error(c, 503);
193 return -1;
194 }
196 memcpy(newbuf + c->bufsz, buf, rdsz);
197 newbuf[newsz] = 0;
199 c->rcvbuf = newbuf;
200 c->bufsz = newsz;
201 }
203 if((status = http_parse_header(&hdr, c->rcvbuf, c->bufsz)) != HTTP_HDR_OK) {
204 http_print_header(&hdr);
205 switch(status) {
206 case HTTP_HDR_INVALID:
207 respond_error(c, 400);
208 return -1;
210 case HTTP_HDR_NOMEM:
211 respond_error(c, 503);
212 return -1;
214 case HTTP_HDR_PARTIAL:
215 return 0; /* partial header, continue reading */
216 }
217 }
218 http_print_header(&hdr);
220 /* we only support GET and HEAD at this point, so freak out on anything else */
221 switch(hdr.method) {
222 case HTTP_GET:
223 do_get(c, hdr.uri, 1);
224 break;
226 case HTTP_HEAD:
227 do_get(c, hdr.uri, 0);
228 break;
230 default:
231 respond_error(c, 501);
232 return -1;
233 }
235 close_conn(c);
236 return 0;
237 }
239 int do_get(struct client *c, const char *uri, int with_body)
240 {
241 const char *ptr;
242 struct http_resp_header resp;
244 if((ptr = strstr(uri, "://"))) {
245 /* skip the host part */
246 if(!(uri = strchr(ptr + 3, '/'))) {
247 respond_error(c, 404);
248 return -1;
249 }
250 ++uri;
251 }
253 if(*uri) {
254 struct stat st;
255 char *path = 0;
256 char *buf;
257 const char *type;
258 int fd, size;
260 if(stat(uri, &st) == -1) {
261 respond_error(c, 404);
262 return -1;
263 }
265 if(S_ISDIR(st.st_mode)) {
266 int i;
267 path = alloca(strlen(uri) + 64);
269 for(i=0; indexfiles[i]; i++) {
270 sprintf(path, "%s/%s", uri, indexfiles[i]);
271 if(stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
272 break;
273 }
274 }
276 if(indexfiles[i] == 0) {
277 respond_error(c, 404);
278 return -1;
279 }
280 } else {
281 path = (char*)uri;
282 }
284 if((fd = open(path, O_RDONLY)) == -1) {
285 respond_error(c, 403);
286 return -1;
287 }
289 /* construct response header */
290 http_init_resp(&resp);
291 http_add_resp_field(&resp, "Content-Length: %d", st.st_size);
292 if((type = mime_type(path))) {
293 http_add_resp_field(&resp, "Content-Type: %s", type);
294 }
296 size = http_serialize_resp(&resp, 0);
297 buf = alloca(size);
298 http_serialize_resp(&resp, buf);
299 send(c->s, buf, size, 0);
301 if(with_body) {
302 char *cont = mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
303 if(cont == (void*)-1) {
304 respond_error(c, 503);
305 close(fd);
306 return -1;
307 }
308 }
310 close(fd);
311 }
312 return 0;
313 }
315 void respond_error(struct client *c, int errcode)
316 {
317 char buf[512];
319 sprintf(buf, HTTP_VER_STR " %d %s\r\n\r\n", errcode, http_strmsg(errcode));
321 send(c->s, buf, strlen(buf), 0);
322 close_conn(c);
323 }
325 void sighandler(int s)
326 {
327 if(s == SIGINT || s == SIGTERM || s == SIGQUIT) {
328 close(lis);
329 while(clist) {
330 struct client *c = clist;
331 clist = clist->next;
332 close_conn(c);
333 free(c);
334 }
335 clist = 0;
337 printf("bye!\n");
338 exit(0);
339 }
340 }
343 static void print_help(const char *argv0)
344 {
345 printf("Usage: %s [options]\n", argv0);
346 printf("Options:\n");
347 printf(" -p <port> set the TCP/IP port number to use\n");
348 printf(" -h print usage help and exit\n");
349 }
351 int parse_args(int argc, char **argv)
352 {
353 int i;
355 for(i=1; i<argc; i++) {
356 if(argv[i][0] == '-' && argv[i][2] == 0) {
357 switch(argv[i][1]) {
358 case 'p':
359 if((port = atoi(argv[++i])) == 0) {
360 fprintf(stderr, "-p must be followed by a valid port number\n");
361 return -1;
362 }
363 break;
365 case 'h':
366 print_help(argv[0]);
367 exit(0);
369 default:
370 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
371 return -1;
372 }
373 } else {
374 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
375 return -1;
376 }
377 }
378 return 0;
379 }