Imposed some limits on request parts.
[ashd.git] / lib / req.c
CommitLineData
33733396
FT
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>
41213112
FT
21#include <unistd.h>
22#include <sys/socket.h>
23#include <errno.h>
5fc1bf9f
FT
24#include <ctype.h>
25#include <stdio.h>
470938bd 26#include <fcntl.h>
33733396
FT
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31#include <utils.h>
41213112 32#include <log.h>
33733396 33#include <req.h>
41213112 34#include <proc.h>
33733396 35
41213112 36struct hthead *mkreq(char *method, char *url, char *ver)
33733396 37{
41213112 38 struct hthead *req;
33733396
FT
39
40 omalloc(req);
41 req->method = sstrdup(method);
42 req->url = sstrdup(url);
43 req->ver = sstrdup(ver);
41213112 44 req->rest = sstrdup(url);
33733396
FT
45 return(req);
46}
47
41213112
FT
48struct 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
59void freehthead(struct hthead *head)
33733396
FT
60{
61 int i;
62
41213112
FT
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]);
33733396 78 }
41213112 79 free(head->headers);
33733396 80 }
41213112 81 free(head);
33733396
FT
82}
83
41213112 84char *getheader(struct hthead *head, char *name)
66987955
FT
85{
86 int i;
87
41213112
FT
88 for(i = 0; i < head->noheaders; i++) {
89 if(!strcasecmp(head->headers[i][0], name))
90 return(head->headers[i][1]);
66987955
FT
91 }
92 return(NULL);
93}
94
5fc1bf9f
FT
95static void trim(struct charbuf *buf)
96{
97 char *p;
98
a39d9e16
FT
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)
c7b1ef35 102 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
5fc1bf9f
FT
103}
104
105int parseheaders(struct hthead *head, FILE *in)
106{
107 int c, state;
108 struct charbuf name, val;
90b0ba0f 109 size_t tsz;
5fc1bf9f
FT
110
111 bufinit(name);
112 bufinit(val);
113 state = 0;
90b0ba0f 114 tsz = 0;
5fc1bf9f
FT
115 while(1) {
116 c = fgetc(in);
90b0ba0f
FT
117 if(++tsz >= 65536)
118 goto fail;
5fc1bf9f
FT
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
161fail:
162 buffree(name);
163 buffree(val);
164 return(-1);
165}
166
9e9eca79
FT
167void 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
5fc1bf9f 172 * can be taken from a subpart of the old one. */
9e9eca79
FT
173 tmp = head->rest;
174 head->rest = sstrdup(rest);
175 free(tmp);
176}
177
41213112 178void headpreheader(struct hthead *head, const char *name, const char *val)
33733396 179{
41213112
FT
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);
33733396
FT
186}
187
41213112 188void headappheader(struct hthead *head, const char *name, const char *val)
33733396
FT
189{
190 int i;
191
41213112
FT
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
f89ce57a
FT
199void 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]);
608f4ac7 208 memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
f89ce57a
FT
209 return;
210 }
211 }
212}
213
5fc1bf9f
FT
214int 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
af34331c 227int sendreq(int sock, struct hthead *req, int fd)
41213112
FT
228{
229 int ret, i;
41213112
FT
230 struct charbuf buf;
231
41213112
FT
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, "");
af34331c 242 ret = sendfd(sock, fd, buf.b, buf.d);
41213112 243 buffree(buf);
af34331c 244 if(ret < 0)
41213112 245 return(-1);
af34331c
FT
246 else
247 return(0);
41213112
FT
248}
249
250int 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 }
470938bd 262 fcntl(fd, F_SETFD, FD_CLOEXEC);
41213112
FT
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
287fail:
288 close(fd);
289 freehthead(req);
290 errno = EPROTO;
291 return(-1);
33733396 292}
1604c096
FT
293
294char *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);
323fail:
324 buffree(buf);
325 return(NULL);
326}