Imposed some limits on request parts.
[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 void replrest(struct hthead *head, char *rest)
168 {
169     char *tmp;
170     
171     /* Do not free the current rest string yet, so that the new one
172      * can be taken from a subpart of the old one. */
173     tmp = head->rest;
174     head->rest = sstrdup(rest);
175     free(tmp);
176 }
177
178 void headpreheader(struct hthead *head, const char *name, const char *val)
179 {
180     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
181     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
182     head->noheaders++;
183     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
184     head->headers[0][0] = sstrdup(name);
185     head->headers[0][1] = sstrdup(val);
186 }
187
188 void headappheader(struct hthead *head, const char *name, const char *val)
189 {
190     int i;
191
192     i = head->noheaders++;
193     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
194     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
195     head->headers[i][0] = sstrdup(name);
196     head->headers[i][1] = sstrdup(val);
197 }
198
199 void headrmheader(struct hthead *head, const char *name)
200 {
201     int i;
202     
203     for(i = 0; i < head->noheaders; i++) {
204         if(!strcasecmp(head->headers[i][0], name)) {
205             free(head->headers[i][0]);
206             free(head->headers[i][1]);
207             free(head->headers[i]);
208             memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
209             return;
210         }
211     }
212 }
213
214 int writeresp(FILE *out, struct hthead *resp)
215 {
216     int i;
217     
218     if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
219         return(-1);
220     for(i = 0; i < resp->noheaders; i++) {
221         if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
222             return(-1);
223     }
224     return(0);
225 }
226
227 int sendreq(int sock, struct hthead *req, int fd)
228 {
229     int ret, i;
230     struct charbuf buf;
231     
232     bufinit(buf);
233     bufcatstr2(buf, req->method);
234     bufcatstr2(buf, req->url);
235     bufcatstr2(buf, req->ver);
236     bufcatstr2(buf, req->rest);
237     for(i = 0; i < req->noheaders; i++) {
238         bufcatstr2(buf, req->headers[i][0]);
239         bufcatstr2(buf, req->headers[i][1]);
240     }
241     bufcatstr2(buf, "");
242     ret = sendfd(sock, fd, buf.b, buf.d);
243     buffree(buf);
244     if(ret < 0)
245         return(-1);
246     else
247         return(0);
248 }
249
250 int recvreq(int sock, struct hthead **reqp)
251 {
252     int fd;
253     struct charbuf buf;
254     char *p;
255     size_t l;
256     char *name, *val;
257     struct hthead *req;
258     
259     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
260         return(-1);
261     }
262     fcntl(fd, F_SETFD, FD_CLOEXEC);
263     buf.s = buf.d;
264     p = buf.b;
265     l = buf.d;
266     
267     *reqp = omalloc(req);
268     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
269         goto fail;
270     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
271         goto fail;
272     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
273         goto fail;
274     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
275         goto fail;
276     
277     while(1) {
278         if(!*(name = decstr(&p, &l)))
279             break;
280         val = decstr(&p, &l);
281         headappheader(req, name, val);
282     }
283     
284     buffree(buf);
285     return(fd);
286     
287 fail:
288     close(fd);
289     freehthead(req);
290     errno = EPROTO;
291     return(-1);
292 }
293
294 char *unquoteurl(char *in)
295 {
296     struct charbuf buf;
297     char *p;
298     int c;
299     
300     bufinit(buf);
301     p = in;
302     while(*p) {
303         if(*p == '%') {
304             if(!p[1] || !p[2])
305                 goto fail;
306             c = 0;
307             if((p[1] >= '0') && (p[1] <= '9'))          c |= (p[1] - '0') << 4;
308             else if((p[1] >= 'a') && (p[1] <= 'f'))     c |= (p[1] - 'a' + 10) << 4;
309             else if((p[1] >= 'A') && (p[1] <= 'F'))     c |= (p[1] - 'A' + 10) << 4;
310             else                                        goto fail;
311             if((p[2] >= '0') && (p[2] <= '9'))          c |= (p[2] - '0');
312             else if((p[2] >= 'a') && (p[2] <= 'f'))     c |= (p[2] - 'a' + 10);
313             else if((p[2] >= 'A') && (p[2] <= 'F'))     c |= (p[2] - 'A' + 10);
314             else                                        goto fail;
315             bufadd(buf, c);
316             p += 3;
317         } else {
318             bufadd(buf, *(p++));
319         }
320     }
321     bufadd(buf, 0);
322     return(buf.b);
323 fail:
324     buffree(buf);
325     return(NULL);
326 }