64944bf56b1c93a949526615faf01f4c14047144
[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
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <utils.h>
31 #include <log.h>
32 #include <req.h>
33 #include <proc.h>
34
35 struct hthead *mkreq(char *method, char *url, char *ver)
36 {
37     struct hthead *req;
38     
39     omalloc(req);
40     req->method = sstrdup(method);
41     req->url = sstrdup(url);
42     req->ver = sstrdup(ver);
43     req->rest = sstrdup(url);
44     return(req);
45 }
46
47 struct hthead *mkresp(int code, char *msg, char *ver)
48 {
49     struct hthead *resp;
50     
51     omalloc(resp);
52     resp->code = code;
53     resp->msg = sstrdup(msg);
54     resp->ver = sstrdup(ver);
55     return(resp);
56 }
57
58 void freehthead(struct hthead *head)
59 {
60     int i;
61     
62     if(head->method != NULL)
63         free(head->method);
64     if(head->url != NULL)
65         free(head->url);
66     if(head->msg != NULL)
67         free(head->msg);
68     if(head->ver != NULL)
69         free(head->ver);
70     if(head->rest != NULL)
71         free(head->rest);
72     if(head->headers) {
73         for(i = 0; i < head->noheaders; i++) {
74             free(head->headers[i][0]);
75             free(head->headers[i][1]);
76             free(head->headers[i]);
77         }
78         free(head->headers);
79     }
80     free(head);
81 }
82
83 char *getheader(struct hthead *head, char *name)
84 {
85     int i;
86     
87     for(i = 0; i < head->noheaders; i++) {
88         if(!strcasecmp(head->headers[i][0], name))
89             return(head->headers[i][1]);
90     }
91     return(NULL);
92 }
93
94 static void trim(struct charbuf *buf)
95 {
96     char *p;
97     
98     for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
99     memmove(buf->b, p, buf->d -= (p - buf->b));
100     if(buf->d > 0)
101         for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
102 }
103
104 int parseheaders(struct hthead *head, FILE *in)
105 {
106     int c, state;
107     struct charbuf name, val;
108     
109     bufinit(name);
110     bufinit(val);
111     state = 0;
112     while(1) {
113         c = fgetc(in);
114     again:
115         if(state == 0) {
116             if(c == '\r') {
117             } else if(c == '\n') {
118                 break;
119             } else if(c == EOF) {
120                 goto fail;
121             } else {
122                 state = 1;
123                 goto again;
124             }
125         } else if(state == 1) {
126             if(c == ':') {
127                 trim(&name);
128                 bufadd(name, 0);
129                 state = 2;
130             } else if(c == '\r') {
131             } else if(c == '\n') {
132                 goto fail;
133             } else if(c == EOF) {
134                 goto fail;
135             } else {
136                 bufadd(name, c);
137             }
138         } else if(state == 2) {
139             if(c == '\r') {
140             } else if(c == '\n') {
141                 trim(&val);
142                 bufadd(val, 0);
143                 headappheader(head, name.b, val.b);
144                 buffree(name);
145                 buffree(val);
146                 state = 0;
147             } else if(c == EOF) {
148                 goto fail;
149             } else {
150                 bufadd(val, c);
151             }
152         }
153     }
154     return(0);
155     
156 fail:
157     buffree(name);
158     buffree(val);
159     return(-1);
160 }
161
162 void replrest(struct hthead *head, char *rest)
163 {
164     char *tmp;
165     
166     /* Do not free the current rest string yet, so that the new one
167      * can be taken from a subpart of the old one. */
168     tmp = head->rest;
169     head->rest = sstrdup(rest);
170     free(tmp);
171 }
172
173 void headpreheader(struct hthead *head, const char *name, const char *val)
174 {
175     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
176     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
177     head->noheaders++;
178     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
179     head->headers[0][0] = sstrdup(name);
180     head->headers[0][1] = sstrdup(val);
181 }
182
183 void headappheader(struct hthead *head, const char *name, const char *val)
184 {
185     int i;
186
187     i = head->noheaders++;
188     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
189     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
190     head->headers[i][0] = sstrdup(name);
191     head->headers[i][1] = sstrdup(val);
192 }
193
194 void headrmheader(struct hthead *head, const char *name)
195 {
196     int i;
197     
198     for(i = 0; i < head->noheaders; i++) {
199         if(!strcasecmp(head->headers[i][0], name)) {
200             free(head->headers[i][0]);
201             free(head->headers[i][1]);
202             free(head->headers[i]);
203             memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
204             return;
205         }
206     }
207 }
208
209 int writeresp(FILE *out, struct hthead *resp)
210 {
211     int i;
212     
213     if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
214         return(-1);
215     for(i = 0; i < resp->noheaders; i++) {
216         if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
217             return(-1);
218     }
219     return(0);
220 }
221
222 int sendreq(int sock, struct hthead *req, int fd)
223 {
224     int ret, i;
225     struct charbuf buf;
226     
227     bufinit(buf);
228     bufcatstr2(buf, req->method);
229     bufcatstr2(buf, req->url);
230     bufcatstr2(buf, req->ver);
231     bufcatstr2(buf, req->rest);
232     for(i = 0; i < req->noheaders; i++) {
233         bufcatstr2(buf, req->headers[i][0]);
234         bufcatstr2(buf, req->headers[i][1]);
235     }
236     bufcatstr2(buf, "");
237     ret = sendfd(sock, fd, buf.b, buf.d);
238     buffree(buf);
239     if(ret < 0)
240         return(-1);
241     else
242         return(0);
243 }
244
245 int recvreq(int sock, struct hthead **reqp)
246 {
247     int fd;
248     struct charbuf buf;
249     char *p;
250     size_t l;
251     char *name, *val;
252     struct hthead *req;
253     
254     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
255         return(-1);
256     }
257     buf.s = buf.d;
258     p = buf.b;
259     l = buf.d;
260     
261     *reqp = omalloc(req);
262     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
263         goto fail;
264     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
265         goto fail;
266     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
267         goto fail;
268     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
269         goto fail;
270     
271     while(1) {
272         if(!*(name = decstr(&p, &l)))
273             break;
274         val = decstr(&p, &l);
275         headappheader(req, name, val);
276     }
277     
278     buffree(buf);
279     return(fd);
280     
281 fail:
282     close(fd);
283     freehthead(req);
284     errno = EPROTO;
285     return(-1);
286 }
287
288 char *unquoteurl(char *in)
289 {
290     struct charbuf buf;
291     char *p;
292     int c;
293     
294     bufinit(buf);
295     p = in;
296     while(*p) {
297         if(*p == '%') {
298             if(!p[1] || !p[2])
299                 goto fail;
300             c = 0;
301             if((p[1] >= '0') && (p[1] <= '9'))          c |= (p[1] - '0') << 4;
302             else if((p[1] >= 'a') && (p[1] <= 'f'))     c |= (p[1] - 'a' + 10) << 4;
303             else if((p[1] >= 'A') && (p[1] <= 'F'))     c |= (p[1] - 'A' + 10) << 4;
304             else                                        goto fail;
305             if((p[2] >= '0') && (p[2] <= '9'))          c |= (p[2] - '0');
306             else if((p[2] >= 'a') && (p[2] <= 'f'))     c |= (p[2] - 'a' + 10);
307             else if((p[2] >= 'A') && (p[2] <= 'F'))     c |= (p[2] - 'A' + 10);
308             else                                        goto fail;
309             bufadd(buf, c);
310             p += 3;
311         } else {
312             bufadd(buf, *(p++));
313         }
314     }
315     bufadd(buf, 0);
316     return(buf.b);
317 fail:
318     buffree(buf);
319     return(NULL);
320 }