a3e727390948601f8300e63cda1e7a20c7e4e346
[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     
110     bufinit(name);
111     bufinit(val);
112     state = 0;
113     while(1) {
114         c = fgetc(in);
115     again:
116         if(state == 0) {
117             if(c == '\r') {
118             } else if(c == '\n') {
119                 break;
120             } else if(c == EOF) {
121                 goto fail;
122             } else {
123                 state = 1;
124                 goto again;
125             }
126         } else if(state == 1) {
127             if(c == ':') {
128                 trim(&name);
129                 bufadd(name, 0);
130                 state = 2;
131             } else if(c == '\r') {
132             } else if(c == '\n') {
133                 goto fail;
134             } else if(c == EOF) {
135                 goto fail;
136             } else {
137                 bufadd(name, c);
138             }
139         } else if(state == 2) {
140             if(c == '\r') {
141             } else if(c == '\n') {
142                 trim(&val);
143                 bufadd(val, 0);
144                 headappheader(head, name.b, val.b);
145                 buffree(name);
146                 buffree(val);
147                 state = 0;
148             } else if(c == EOF) {
149                 goto fail;
150             } else {
151                 bufadd(val, c);
152             }
153         }
154     }
155     return(0);
156     
157 fail:
158     buffree(name);
159     buffree(val);
160     return(-1);
161 }
162
163 void replrest(struct hthead *head, char *rest)
164 {
165     char *tmp;
166     
167     /* Do not free the current rest string yet, so that the new one
168      * can be taken from a subpart of the old one. */
169     tmp = head->rest;
170     head->rest = sstrdup(rest);
171     free(tmp);
172 }
173
174 void headpreheader(struct hthead *head, const char *name, const char *val)
175 {
176     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
177     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
178     head->noheaders++;
179     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
180     head->headers[0][0] = sstrdup(name);
181     head->headers[0][1] = sstrdup(val);
182 }
183
184 void headappheader(struct hthead *head, const char *name, const char *val)
185 {
186     int i;
187
188     i = head->noheaders++;
189     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
190     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
191     head->headers[i][0] = sstrdup(name);
192     head->headers[i][1] = sstrdup(val);
193 }
194
195 void headrmheader(struct hthead *head, const char *name)
196 {
197     int i;
198     
199     for(i = 0; i < head->noheaders; i++) {
200         if(!strcasecmp(head->headers[i][0], name)) {
201             free(head->headers[i][0]);
202             free(head->headers[i][1]);
203             free(head->headers[i]);
204             memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
205             return;
206         }
207     }
208 }
209
210 int writeresp(FILE *out, struct hthead *resp)
211 {
212     int i;
213     
214     if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
215         return(-1);
216     for(i = 0; i < resp->noheaders; i++) {
217         if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
218             return(-1);
219     }
220     return(0);
221 }
222
223 int sendreq(int sock, struct hthead *req, int fd)
224 {
225     int ret, i;
226     struct charbuf buf;
227     
228     bufinit(buf);
229     bufcatstr2(buf, req->method);
230     bufcatstr2(buf, req->url);
231     bufcatstr2(buf, req->ver);
232     bufcatstr2(buf, req->rest);
233     for(i = 0; i < req->noheaders; i++) {
234         bufcatstr2(buf, req->headers[i][0]);
235         bufcatstr2(buf, req->headers[i][1]);
236     }
237     bufcatstr2(buf, "");
238     ret = sendfd(sock, fd, buf.b, buf.d);
239     buffree(buf);
240     if(ret < 0)
241         return(-1);
242     else
243         return(0);
244 }
245
246 int recvreq(int sock, struct hthead **reqp)
247 {
248     int fd;
249     struct charbuf buf;
250     char *p;
251     size_t l;
252     char *name, *val;
253     struct hthead *req;
254     
255     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
256         return(-1);
257     }
258     fcntl(fd, F_SETFD, FD_CLOEXEC);
259     buf.s = buf.d;
260     p = buf.b;
261     l = buf.d;
262     
263     *reqp = omalloc(req);
264     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
265         goto fail;
266     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
267         goto fail;
268     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
269         goto fail;
270     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
271         goto fail;
272     
273     while(1) {
274         if(!*(name = decstr(&p, &l)))
275             break;
276         val = decstr(&p, &l);
277         headappheader(req, name, val);
278     }
279     
280     buffree(buf);
281     return(fd);
282     
283 fail:
284     close(fd);
285     freehthead(req);
286     errno = EPROTO;
287     return(-1);
288 }
289
290 char *unquoteurl(char *in)
291 {
292     struct charbuf buf;
293     char *p;
294     int c;
295     
296     bufinit(buf);
297     p = in;
298     while(*p) {
299         if(*p == '%') {
300             if(!p[1] || !p[2])
301                 goto fail;
302             c = 0;
303             if((p[1] >= '0') && (p[1] <= '9'))          c |= (p[1] - '0') << 4;
304             else if((p[1] >= 'a') && (p[1] <= 'f'))     c |= (p[1] - 'a' + 10) << 4;
305             else if((p[1] >= 'A') && (p[1] <= 'F'))     c |= (p[1] - 'A' + 10) << 4;
306             else                                        goto fail;
307             if((p[2] >= '0') && (p[2] <= '9'))          c |= (p[2] - '0');
308             else if((p[2] >= 'a') && (p[2] <= 'f'))     c |= (p[2] - 'a' + 10);
309             else if((p[2] >= 'A') && (p[2] <= 'F'))     c |= (p[2] - 'A' + 10);
310             else                                        goto fail;
311             bufadd(buf, c);
312             p += 3;
313         } else {
314             bufadd(buf, *(p++));
315         }
316     }
317     bufadd(buf, 0);
318     return(buf.b);
319 fail:
320     buffree(buf);
321     return(NULL);
322 }