libht: Fixed a bug in trim.
[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>
33733396
FT
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <utils.h>
41213112 31#include <log.h>
33733396 32#include <req.h>
41213112 33#include <proc.h>
33733396 34
41213112 35struct hthead *mkreq(char *method, char *url, char *ver)
33733396 36{
41213112 37 struct hthead *req;
33733396
FT
38
39 omalloc(req);
40 req->method = sstrdup(method);
41 req->url = sstrdup(url);
42 req->ver = sstrdup(ver);
41213112 43 req->rest = sstrdup(url);
33733396
FT
44 return(req);
45}
46
41213112
FT
47struct 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
58void freehthead(struct hthead *head)
33733396
FT
59{
60 int i;
61
41213112
FT
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]);
33733396 77 }
41213112 78 free(head->headers);
33733396 79 }
41213112 80 free(head);
33733396
FT
81}
82
41213112 83char *getheader(struct hthead *head, char *name)
66987955
FT
84{
85 int i;
86
41213112
FT
87 for(i = 0; i < head->noheaders; i++) {
88 if(!strcasecmp(head->headers[i][0], name))
89 return(head->headers[i][1]);
66987955
FT
90 }
91 return(NULL);
92}
93
5fc1bf9f
FT
94static void trim(struct charbuf *buf)
95{
96 char *p;
97
c7b1ef35
FT
98 if(buf->d > 0) {
99 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
100 memmove(buf->b, p, buf->d -= (p - buf->b));
101 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
102 }
5fc1bf9f
FT
103}
104
105int 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
157fail:
158 buffree(name);
159 buffree(val);
160 return(-1);
161}
162
9e9eca79
FT
163void 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
5fc1bf9f 168 * can be taken from a subpart of the old one. */
9e9eca79
FT
169 tmp = head->rest;
170 head->rest = sstrdup(rest);
171 free(tmp);
172}
173
41213112 174void headpreheader(struct hthead *head, const char *name, const char *val)
33733396 175{
41213112
FT
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);
33733396
FT
182}
183
41213112 184void headappheader(struct hthead *head, const char *name, const char *val)
33733396
FT
185{
186 int i;
187
41213112
FT
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
5fc1bf9f
FT
195int writeresp(FILE *out, struct hthead *resp)
196{
197 int i;
198
199 if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
200 return(-1);
201 for(i = 0; i < resp->noheaders; i++) {
202 if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
203 return(-1);
204 }
205 return(0);
206}
207
af34331c 208int sendreq(int sock, struct hthead *req, int fd)
41213112
FT
209{
210 int ret, i;
41213112
FT
211 struct charbuf buf;
212
41213112
FT
213 bufinit(buf);
214 bufcatstr2(buf, req->method);
215 bufcatstr2(buf, req->url);
216 bufcatstr2(buf, req->ver);
217 bufcatstr2(buf, req->rest);
218 for(i = 0; i < req->noheaders; i++) {
219 bufcatstr2(buf, req->headers[i][0]);
220 bufcatstr2(buf, req->headers[i][1]);
221 }
222 bufcatstr2(buf, "");
af34331c 223 ret = sendfd(sock, fd, buf.b, buf.d);
41213112 224 buffree(buf);
af34331c 225 if(ret < 0)
41213112 226 return(-1);
af34331c
FT
227 else
228 return(0);
41213112
FT
229}
230
231int recvreq(int sock, struct hthead **reqp)
232{
233 int fd;
234 struct charbuf buf;
235 char *p;
236 size_t l;
237 char *name, *val;
238 struct hthead *req;
239
240 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
241 return(-1);
242 }
243 buf.s = buf.d;
244 p = buf.b;
245 l = buf.d;
246
247 *reqp = omalloc(req);
248 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
249 goto fail;
250 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
251 goto fail;
252 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
253 goto fail;
254 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
255 goto fail;
256
257 while(1) {
258 if(!*(name = decstr(&p, &l)))
259 break;
260 val = decstr(&p, &l);
261 headappheader(req, name, val);
262 }
263
264 buffree(buf);
265 return(fd);
266
267fail:
268 close(fd);
269 freehthead(req);
270 errno = EPROTO;
271 return(-1);
33733396 272}
1604c096
FT
273
274char *unquoteurl(char *in)
275{
276 struct charbuf buf;
277 char *p;
278 int c;
279
280 bufinit(buf);
281 p = in;
282 while(*p) {
283 if(*p == '%') {
284 if(!p[1] || !p[2])
285 goto fail;
286 c = 0;
287 if((p[1] >= '0') && (p[1] <= '9')) c |= (p[1] - '0') << 4;
288 else if((p[1] >= 'a') && (p[1] <= 'f')) c |= (p[1] - 'a' + 10) << 4;
289 else if((p[1] >= 'A') && (p[1] <= 'F')) c |= (p[1] - 'A' + 10) << 4;
290 else goto fail;
291 if((p[2] >= '0') && (p[2] <= '9')) c |= (p[2] - '0');
292 else if((p[2] >= 'a') && (p[2] <= 'f')) c |= (p[2] - 'a' + 10);
293 else if((p[2] >= 'A') && (p[2] <= 'F')) c |= (p[2] - 'A' + 10);
294 else goto fail;
295 bufadd(buf, c);
296 p += 3;
297 } else {
298 bufadd(buf, *(p++));
299 }
300 }
301 bufadd(buf, 0);
302 return(buf.b);
303fail:
304 buffree(buf);
305 return(NULL);
306}