Rewrote htparser to handle both HTTP/1.0 and HTTP/1.1.
[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     for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
101 }
102
103 int parseheaders(struct hthead *head, FILE *in)
104 {
105     int c, state;
106     struct charbuf name, val;
107     
108     bufinit(name);
109     bufinit(val);
110     state = 0;
111     while(1) {
112         c = fgetc(in);
113     again:
114         if(state == 0) {
115             if(c == '\r') {
116             } else if(c == '\n') {
117                 break;
118             } else if(c == EOF) {
119                 goto fail;
120             } else {
121                 state = 1;
122                 goto again;
123             }
124         } else if(state == 1) {
125             if(c == ':') {
126                 trim(&name);
127                 bufadd(name, 0);
128                 state = 2;
129             } else if(c == '\r') {
130             } else if(c == '\n') {
131                 goto fail;
132             } else if(c == EOF) {
133                 goto fail;
134             } else {
135                 bufadd(name, c);
136             }
137         } else if(state == 2) {
138             if(c == '\r') {
139             } else if(c == '\n') {
140                 trim(&val);
141                 bufadd(val, 0);
142                 headappheader(head, name.b, val.b);
143                 buffree(name);
144                 buffree(val);
145                 state = 0;
146             } else if(c == EOF) {
147                 goto fail;
148             } else {
149                 bufadd(val, c);
150             }
151         }
152     }
153     return(0);
154     
155 fail:
156     buffree(name);
157     buffree(val);
158     return(-1);
159 }
160
161 void replrest(struct hthead *head, char *rest)
162 {
163     char *tmp;
164     
165     /* Do not free the current rest string yet, so that the new one
166      * can be taken from a subpart of the old one. */
167     tmp = head->rest;
168     head->rest = sstrdup(rest);
169     free(tmp);
170 }
171
172 void headpreheader(struct hthead *head, const char *name, const char *val)
173 {
174     head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
175     memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
176     head->noheaders++;
177     head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
178     head->headers[0][0] = sstrdup(name);
179     head->headers[0][1] = sstrdup(val);
180 }
181
182 void headappheader(struct hthead *head, const char *name, const char *val)
183 {
184     int i;
185
186     i = head->noheaders++;
187     head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
188     head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
189     head->headers[i][0] = sstrdup(name);
190     head->headers[i][1] = sstrdup(val);
191 }
192
193 int writeresp(FILE *out, struct hthead *resp)
194 {
195     int i;
196     
197     if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
198         return(-1);
199     for(i = 0; i < resp->noheaders; i++) {
200         if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
201             return(-1);
202     }
203     return(0);
204 }
205
206 int sendreq(int sock, struct hthead *req, int fd)
207 {
208     int ret, i;
209     struct charbuf buf;
210     
211     bufinit(buf);
212     bufcatstr2(buf, req->method);
213     bufcatstr2(buf, req->url);
214     bufcatstr2(buf, req->ver);
215     bufcatstr2(buf, req->rest);
216     for(i = 0; i < req->noheaders; i++) {
217         bufcatstr2(buf, req->headers[i][0]);
218         bufcatstr2(buf, req->headers[i][1]);
219     }
220     bufcatstr2(buf, "");
221     ret = sendfd(sock, fd, buf.b, buf.d);
222     buffree(buf);
223     if(ret < 0)
224         return(-1);
225     else
226         return(0);
227 }
228
229 int recvreq(int sock, struct hthead **reqp)
230 {
231     int fd;
232     struct charbuf buf;
233     char *p;
234     size_t l;
235     char *name, *val;
236     struct hthead *req;
237     
238     if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
239         return(-1);
240     }
241     buf.s = buf.d;
242     p = buf.b;
243     l = buf.d;
244     
245     *reqp = omalloc(req);
246     if((req->method = sstrdup(decstr(&p, &l))) == NULL)
247         goto fail;
248     if((req->url = sstrdup(decstr(&p, &l))) == NULL)
249         goto fail;
250     if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
251         goto fail;
252     if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
253         goto fail;
254     
255     while(1) {
256         if(!*(name = decstr(&p, &l)))
257             break;
258         val = decstr(&p, &l);
259         headappheader(req, name, val);
260     }
261     
262     buffree(buf);
263     return(fd);
264     
265 fail:
266     close(fd);
267     freehthead(req);
268     errno = EPROTO;
269     return(-1);
270 }