2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
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.
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.
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/>.
22 #include <sys/socket.h>
36 struct hthead *mkreq(char *method, char *url, char *ver)
41 req->method = sstrdup(method);
42 req->url = sstrdup(url);
43 req->ver = sstrdup(ver);
44 req->rest = sstrdup(url);
48 struct hthead *mkresp(int code, char *msg, char *ver)
54 resp->msg = sstrdup(msg);
55 resp->ver = sstrdup(ver);
59 void freehthead(struct hthead *head)
63 if(head->method != NULL)
71 if(head->rest != NULL)
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]);
84 char *getheader(struct hthead *head, char *name)
88 for(i = 0; i < head->noheaders; i++) {
89 if(!strcasecmp(head->headers[i][0], name))
90 return(head->headers[i][1]);
95 static void trim(struct charbuf *buf)
99 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
100 memmove(buf->b, p, buf->d -= (p - buf->b));
102 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
105 int parseheaders(struct hthead *head, FILE *in)
108 struct charbuf name, val;
122 } else if(c == '\n') {
124 } else if(c == EOF) {
130 } else if(state == 1) {
135 } else if(c == '\r') {
136 } else if(c == '\n') {
138 } else if(c == EOF) {
143 } else if(state == 2) {
145 } else if(c == '\n') {
148 headappheader(head, name.b, val.b);
152 } else if(c == EOF) {
167 struct hthead *parseresponse(FILE *in)
171 struct charbuf ver, msg;
182 } else if((c == EOF) || (c < 32) || (c >= 128)) {
194 } else if((c == EOF) || (c < '0') || (c > '9')) {
197 code = (code * 10) + (c - '0');
207 } else if((c == EOF) || (c < 32)) {
217 req = mkresp(code, msg.b, ver.b);
218 if(parseheaders(req, in))
233 void replrest(struct hthead *head, char *rest)
237 /* Do not free the current rest string yet, so that the new one
238 * can be taken from a subpart of the old one. */
240 head->rest = sstrdup(rest);
244 void headpreheader(struct hthead *head, const char *name, const char *val)
246 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
247 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
249 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
250 head->headers[0][0] = sstrdup(name);
251 head->headers[0][1] = sstrdup(val);
254 void headappheader(struct hthead *head, const char *name, const char *val)
258 i = head->noheaders++;
259 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
260 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
261 head->headers[i][0] = sstrdup(name);
262 head->headers[i][1] = sstrdup(val);
265 void headrmheader(struct hthead *head, const char *name)
269 for(i = 0; i < head->noheaders; i++) {
270 if(!strcasecmp(head->headers[i][0], name)) {
271 free(head->headers[i][0]);
272 free(head->headers[i][1]);
273 free(head->headers[i]);
274 memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
280 int writeresp(FILE *out, struct hthead *resp)
284 if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
286 for(i = 0; i < resp->noheaders; i++) {
287 if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
293 int sendreq(int sock, struct hthead *req, int fd)
299 bufcatstr2(buf, req->method);
300 bufcatstr2(buf, req->url);
301 bufcatstr2(buf, req->ver);
302 bufcatstr2(buf, req->rest);
303 for(i = 0; i < req->noheaders; i++) {
304 bufcatstr2(buf, req->headers[i][0]);
305 bufcatstr2(buf, req->headers[i][1]);
308 ret = sendfd(sock, fd, buf.b, buf.d);
316 int recvreq(int sock, struct hthead **reqp)
325 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
328 fcntl(fd, F_SETFD, FD_CLOEXEC);
333 *reqp = omalloc(req);
334 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
336 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
338 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
340 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
344 if(!*(name = decstr(&p, &l)))
346 val = decstr(&p, &l);
347 headappheader(req, name, val);
360 char *unquoteurl(char *in)
373 if((p[1] >= '0') && (p[1] <= '9')) c |= (p[1] - '0') << 4;
374 else if((p[1] >= 'a') && (p[1] <= 'f')) c |= (p[1] - 'a' + 10) << 4;
375 else if((p[1] >= 'A') && (p[1] <= 'F')) c |= (p[1] - 'A' + 10) << 4;
377 if((p[2] >= '0') && (p[2] <= '9')) c |= (p[2] - '0');
378 else if((p[2] >= 'a') && (p[2] <= 'f')) c |= (p[2] - 'a' + 10);
379 else if((p[2] >= 'A') && (p[2] <= 'F')) c |= (p[2] - 'A' + 10);