tinywebd

view src/main.c @ 3:852a745503cf

http header parsing, not tested
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Apr 2015 15:20:16 +0300
parents f425a9805d17
children 9e054c002489
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/select.h>
10 #include <sys/types.h>
11 #include <sys/socket.h>
12 #include <arpa/inet.h>
13 #include "http.h"
15 /* HTTP version */
16 #define HTTP_VER_MAJOR 1
17 #define HTTP_VER_MINOR 1
18 #define HTTP_VER_STR "1.1"
20 /* maximum request length: 64mb */
21 #define MAX_REQ_LENGTH (65536 * 1024)
23 struct client {
24 int s;
25 char *rcvbuf;
26 int bufsz;
27 struct client *next;
28 };
30 int start_server(void);
31 int accept_conn(int lis);
32 void close_conn(struct client *c);
33 int handle_client(struct client *c);
34 void do_get_head(struct client *c);
35 void respond_error(struct client *c, int errcode);
36 void sighandler(int s);
37 int parse_args(int argc, char **argv);
39 static int lis;
40 static int port = 8080;
41 static struct client *clist;
43 int main(int argc, char **argv)
44 {
45 if(parse_args(argc, argv) == -1) {
46 return 1;
47 }
49 signal(SIGINT, sighandler);
50 signal(SIGTERM, sighandler);
51 signal(SIGQUIT, sighandler);
53 if((lis = start_server()) == -1) {
54 return 1;
55 }
57 for(;;) {
58 struct client *c, dummy;
59 int maxfd = lis;
60 fd_set rdset;
62 FD_ZERO(&rdset);
63 FD_SET(lis, &rdset);
65 c = clist;
66 while(c) {
67 if(c->s > maxfd) {
68 maxfd = c->s;
69 }
70 FD_SET(c->s, &rdset);
71 c = c->next;
72 }
74 while(select(maxfd + 1, &rdset, 0, 0, 0) == -1 && errno == EINTR);
76 c = clist;
77 while(c) {
78 if(FD_ISSET(c->s, &rdset)) {
79 handle_client(c);
80 }
81 c = c->next;
82 }
84 if(FD_ISSET(lis, &rdset)) {
85 accept_conn(lis);
86 }
88 dummy.next = clist;
89 c = &dummy;
91 while(c->next) {
92 struct client *n = c->next;
94 if(n->s == -1) {
95 /* marked for removal */
96 c->next = n->next;
97 free(n);
98 } else {
99 c = c->next;
100 }
101 }
102 }
104 return 0; /* unreachable */
105 }
107 int start_server(void)
108 {
109 int s;
110 struct sockaddr_in sa;
112 if((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
113 perror("failed to create listening socket");
114 return -1;
115 }
116 fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
118 memset(&sa, 0, sizeof sa);
119 sa.sin_family = AF_INET;
120 sa.sin_addr.s_addr = INADDR_ANY;
121 sa.sin_port = htons(port);
123 if(bind(s, (struct sockaddr*)&sa, sizeof sa) == -1) {
124 fprintf(stderr, "failed to bind socket to port %d: %s\n", port, strerror(errno));
125 return -1;
126 }
127 listen(s, 16);
129 return s;
130 }
132 int accept_conn(int lis)
133 {
134 int s;
135 struct client *c;
136 struct sockaddr_in addr;
137 socklen_t addr_sz = sizeof addr;
139 if((s = accept(lis, (struct sockaddr*)&addr, &addr_sz)) == -1) {
140 perror("failed to accept incoming connection");
141 return -1;
142 }
143 fcntl(s, F_SETFL, fcntl(s, F_GETFL) | O_NONBLOCK);
145 if(!(c = malloc(sizeof *c))) {
146 perror("failed to allocate memory while accepting connection");
147 return -1;
148 }
149 c->s = s;
150 c->rcvbuf = 0;
151 c->bufsz = 0;
152 c->next = clist;
153 clist = c;
154 return 0;
155 }
157 void close_conn(struct client *c)
158 {
159 close(c->s);
160 c->s = -1; /* mark it for removal */
161 free(c->rcvbuf);
162 }
164 int handle_client(struct client *c)
165 {
166 struct http_req_header hdr;
167 static char buf[2048];
168 int rdsz, status;
170 while((rdsz = recv(c->s, buf, sizeof buf, 0)) > 0) {
171 if(c->rcvbuf) {
172 char *newbuf;
173 int newsz = c->bufsz + rdsz;
174 if(newsz > MAX_REQ_LENGTH) {
175 respond_error(c, 413);
176 return -1;
177 }
179 if(!(newbuf = realloc(buf, newsz + 1))) {
180 fprintf(stderr, "failed to allocate %d byte buffer\n", newsz);
181 respond_error(c, 503);
182 return -1;
183 }
185 memcpy(newbuf + c->bufsz, buf, rdsz);
186 newbuf[newsz] = 0;
188 c->rcvbuf = newbuf;
189 c->bufsz = newsz;
190 }
191 }
193 if((status = http_parse_header(&hdr, c->rcvbuf, c->bufsz)) != HTTP_HDR_OK) {
194 http_print_header(&hdr);
195 switch(status) {
196 case HTTP_HDR_INVALID:
197 respond_error(c, 400);
198 return -1;
200 case HTTP_HDR_NOMEM:
201 respond_error(c, 503);
202 return -1;
204 case HTTP_HDR_PARTIAL:
205 return 0; /* partial header, continue reading */
206 }
207 }
208 http_print_header(&hdr);
210 /* we only support GET and HEAD at this point, so freak out on anything else */
211 switch(hdr.method) {
212 case HTTP_GET:
213 case HTTP_HEAD:
214 do_get_head(c);
215 break;
217 default:
218 respond_error(c, 501);
219 return -1;
220 }
222 close_conn(c);
223 return 0;
224 }
226 void do_get_head(struct client *c)
227 {
228 }
230 void respond_error(struct client *c, int errcode)
231 {
232 char buf[512];
234 sprintf(buf, HTTP_VER_STR " %d %s\r\n\r\n", errcode, http_strmsg(errcode));
236 send(c->s, buf, strlen(buf), 0);
237 close_conn(c);
238 }
240 void sighandler(int s)
241 {
242 if(s == SIGINT || s == SIGTERM || s == SIGQUIT) {
243 close(lis);
244 while(clist) {
245 struct client *c = clist;
246 clist = clist->next;
247 close_conn(c);
248 free(c);
249 }
250 clist = 0;
252 printf("bye!\n");
253 exit(0);
254 }
255 }
258 static void print_help(const char *argv0)
259 {
260 printf("Usage: %s [options]\n", argv0);
261 printf("Options:\n");
262 printf(" -p <port> set the TCP/IP port number to use\n");
263 printf(" -h print usage help and exit\n");
264 }
266 int parse_args(int argc, char **argv)
267 {
268 int i;
270 for(i=1; i<argc; i++) {
271 if(argv[i][0] == '-' && argv[i][2] == 0) {
272 switch(argv[i][1]) {
273 case 'p':
274 if((port = atoi(argv[++i])) == 0) {
275 fprintf(stderr, "-p must be followed by a valid port number\n");
276 return -1;
277 }
278 break;
280 case 'h':
281 print_help(argv[0]);
282 exit(0);
284 default:
285 fprintf(stderr, "unrecognized option: %s\n", argv[i]);
286 return -1;
287 }
288 } else {
289 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
290 return -1;
291 }
292 }
293 return 0;
294 }