lib: Added variants of sendfd and sendreq that take sendmsg flags.
[ashd.git] / lib / req.c
1 /*
2     ashd - A Sane HTTP Daemon
3     Copyright (C) 2008  Fredrik Tolf <fredrik@dolda2000.com>
4
5     This program is free software: you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation, either version 3 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/socket.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <fcntl.h>
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <utils.h>
32 #include <log.h>
33 #include <req.h>
34 #include <proc.h>
35
36 struct hthead *mkreq(char *method, char *url, char *ver)
37 {
38     struct hthead *req;
39     
40     omalloc(req);
41     req->method = sstrdup(method);
42     req->url = sstrdup(url);
43     req->ver = sstrdup(ver);
44     req->rest = sstrdup(url);
45     return(req);
46 }
47
48 struct hthead *mkresp(int code, char *msg, char *ver)
49 {
50     struct hthead *resp;
51     
52     omalloc(resp);
53     resp->code = code;
54     resp->msg = sstrdup(msg);
55     resp->ver = sstrdup(ver);
56     return(resp);
57 }
58
59 void freehthead(struct hthead *head)
60 {
61     int i;
62     
63     if(head->method != NULL)
64         free(head->method);
65     if(head->url != NULL)
66         free(head->url);
67     if(head->msg != NULL)
68         free(head->msg);
69     if(head->ver != NULL)
70         free(head->ver);
71     if(head->rest != NULL)
72         free(head->rest);
73     if(head->headers) {
74         for(i = 0; i < head->noheaders; i++) {
75             free(head->headers[i][0]);
76             free(head->headers[i][1]);
77             free(head->headers[i]);
78         }
79         free(head->headers);
80     }
81     free(head);
82 }
83
84 char *getheader(struct hthead *head, char *name)
85 {
86     int i;
87     
88     for(i = 0; i < head->noheaders; i++) {
89         if(!strcasecmp(head->headers[i][0], name))
90             return(head->headers[i][1]);
91     }
92     return(NULL);
93 }
94
95 static void trim(struct charbuf *buf)
96 {
97     char *p;
98     
99     for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
100     memmove(buf->b, p, buf->d -= (p - buf->b));
101     if(buf->d > 0)
102         for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
103 }
104
105 int parseheaders(struct hthead *head, FILE *in)
106 {
107     int c, state;
108     struct charbuf name, val;
109     size_t tsz;
110     
111     bufinit(name);
112     bufinit(val);
113     state = 0;
114     tsz = 0;
115     while(1) {
116         c = fgetc(in);
117         if(++tsz >= 65536)
118             goto fail;
119     again:
120         if(state == 0) {
121             if(c == '\r') {
122             } else if(c == '\n') {
123                 break;
124             } else if(c == EOF) {
125                 goto fail;
126             } else {
127                 state = 1;
128                 goto again;
129             }
130         } else if(state == 1) {
131             if(c == ':') {
132                 trim(&name);
133                 bufadd(name, 0);
134                 state = 2;
135             } else if(c == '\r') {
136             } else if(c == '\n') {
137                 goto fail;
138             } else if(c == EOF) {
139                 goto fail;
140             } else {
141                 bufadd(name, c);
142             }
143         } else if(state == 2) {
144             if(c == '\r') {
145             } else if(c == '\n') {
146                 trim(&val);
147                 bufadd(val, 0);
148                 headappheader(head, name.b, val.b);
149                 buffree(name);
150                 buffree(val);
151                 state = 0;
152             } else if(c == EOF) {
153                 goto fail;
154             } else {
155                 bufadd(val, c);
156             }
157         }
158     }
159     return(0);
160     
161 fail:
162     buffree(name);
163     buffree(val);
164     return(-1);
165 }
166
167 struct hthead *parseresponse(FILE *in)
168 {
169     struct hthead *req;
170     int code;
171     struct charbuf ver, msg;
172     int c;
173     
174     req = NULL;
175     bufinit(ver);
176     bufinit(msg);
177     code = 0;
178     while(1) {
179         c = getc(in);
180         if(c == ' ') {
181             break;
182         } else if((c == EOF) || (c < 32) || (c >= 128)) {
183             goto fail;
184         } else {
185             bufadd(ver, c);
186             if(ver.d >= 128)
187                 goto fail;
188         }
189     }
190     while(1) {
191         c = getc(in);
192         if(c == ' ') {
193             break;
194         } else if((c == EOF) || (c < '0') || (c > '9')) {
195             goto fail;
196         } else {
197             code = (code * 10) + (c - '0');
198             if(code >= 10000)
199                 goto fail;
200         }
201     }
202     while(1) {
203         c = getc(in);
204         if(c == 10) {
205             break;
206         } else if(c == 13) {
207         } else if((c == EOF) || (c < 32)) {
208             goto fail;
209         } else {
210             bufadd(msg, c);
211             if(msg.d >= 512)
212                 goto fail;
213         }
214     }
215     bufadd(msg, 0);
216     bufadd(ver, 0);
217     req = mkresp(code, msg.b, ver.b);
218     if(parseheaders(req, in))
219         goto fail;
220     goto out;
221     
222 fail:
223     if(req != NULL) {
224         freehthead(req);
225         req = NULL;
226     }
227 out:
228     buffree(msg);
229     buffree(ver);
230     return(req);
231 }
232
233 void replrest(struct hthead *head, char *rest)
234 {
235     char *tmp;
236     
237     /* Do not free the current rest string yet, so that the new one
238      * can be taken from a subpart of the old one. */
239     tmp = head->rest;
240     head->rest = sstrdup(rest);
241     free(tmp);
242 }
243
244 void headpreheader(struct hthead *head, const char *name, const char *val)
245 {
246     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
247     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
248     head->noheaders++;
249     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
250     head->headers[0][0] = sstrdup(name);
251     head->headers[0][1] = sstrdup(val);
252 }
253
254 void headappheader(struct hthead *head, const char *name, const char *val)
255 {
256     int i;
257
258     i = head->noheaders++;
259     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
260     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
261     head->headers[i][0] = sstrdup(name);
262     head->headers[i][1] = sstrdup(val);
263 }
264
265 void headrmheader(struct hthead *head, const char *name)
266 {
267     int i;
268     
269     for(i = 0; i < head->noheaders; i++) {
270         if(!strcasecmp(head->headers[i][0], name)) {
271             free(head->headers[i][0]);
272             free(head->headers[i][1]);
273             free(head->headers[i]);
274             memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
275             return;
276         }
277     }
278 }
279
280 int writeresp(FILE *out, struct hthead *resp)
281 {
282     int i;
283     
284     if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
285         return(-1);
286     for(i = 0; i < resp->noheaders; i++) {
287         if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
288             return(-1);
289     }
290     return(0);
291 }
292
293 int sendreq2(int sock, struct hthead *req, int fd, int flags)
294 {
295     int ret, i;
296     struct charbuf buf;
297     
298     bufinit(buf);
299     bufcatstr2(buf, req->method);
300     bufcatstr2(buf, req->url);
301     bufcatstr2(buf, req->ver);
302     bufcatstr2(buf, req->rest);
303     for(i = 0; i < req->noheaders; i++) {
304         bufcatstr2(buf, req->headers[i][0]);
305         bufcatstr2(buf, req->headers[i][1]);
306     }
307     bufcatstr2(buf, "");
308     ret = sendfd2(sock, fd, buf.b, buf.d, flags);
309     buffree(buf);
310     if(ret < 0)
311         return(-1);
312     else
313         return(0);
314 }
315
316 int sendreq(int sock, struct hthead *req, int fd)
317 {
318     return(sendreq2(sock, req, fd, MSG_NOSIGNAL | MSG_DONTWAIT));
319 }
320
321 int recvreq(int sock, struct hthead **reqp)
322 {
323     int fd;
324     struct charbuf buf;
325     char *p;
326     size_t l;
327     char *name, *val;
328     struct hthead *req;
329     
330     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
331         return(-1);
332     }
333     fcntl(fd, F_SETFD, FD_CLOEXEC);
334     buf.s = buf.d;
335     p = buf.b;
336     l = buf.d;
337     
338     *reqp = omalloc(req);
339     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
340         goto fail;
341     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
342         goto fail;
343     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
344         goto fail;
345     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
346         goto fail;
347     
348     while(1) {
349         if(!*(name = decstr(&p, &l)))
350             break;
351         val = decstr(&p, &l);
352         headappheader(req, name, val);
353     }
354     
355     buffree(buf);
356     return(fd);
357     
358 fail:
359     close(fd);
360     freehthead(req);
361     errno = EPROTO;
362     return(-1);
363 }
364
365 char *unquoteurl(char *in)
366 {
367     struct charbuf buf;
368     char *p;
369     int c;
370     
371     bufinit(buf);
372     p = in;
373     while(*p) {
374         if(*p == '%') {
375             if(!p[1] || !p[2])
376                 goto fail;
377             c = 0;
378             if((p[1] >= '0') && (p[1] <= '9'))          c |= (p[1] - '0') << 4;
379             else if((p[1] >= 'a') && (p[1] <= 'f'))     c |= (p[1] - 'a' + 10) << 4;
380             else if((p[1] >= 'A') && (p[1] <= 'F'))     c |= (p[1] - 'A' + 10) << 4;
381             else                                        goto fail;
382             if((p[2] >= '0') && (p[2] <= '9'))          c |= (p[2] - '0');
383             else if((p[2] >= 'a') && (p[2] <= 'f'))     c |= (p[2] - 'a' + 10);
384             else if((p[2] >= 'A') && (p[2] <= 'F'))     c |= (p[2] - 'A' + 10);
385             else                                        goto fail;
386             bufadd(buf, c);
387             p += 3;
388         } else {
389             bufadd(buf, *(p++));
390         }
391     }
392     bufadd(buf, 0);
393     return(buf.b);
394 fail:
395     buffree(buf);
396     return(NULL);
397 }