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>
33 struct hthead *mkreq(char *method, char *url, char *ver)
38 req->method = sstrdup(method);
39 req->url = sstrdup(url);
40 req->ver = sstrdup(ver);
41 req->rest = sstrdup(url);
45 struct hthead *mkresp(int code, char *msg, char *ver)
51 resp->msg = sstrdup(msg);
52 resp->ver = sstrdup(ver);
56 void freehthead(struct hthead *head)
60 if(head->method != NULL)
68 if(head->rest != NULL)
71 for(i = 0; i < head->noheaders; i++) {
72 free(head->headers[i][0]);
73 free(head->headers[i][1]);
74 free(head->headers[i]);
81 char *getheader(struct hthead *head, char *name)
85 for(i = 0; i < head->noheaders; i++) {
86 if(!strcasecmp(head->headers[i][0], name))
87 return(head->headers[i][1]);
92 void headpreheader(struct hthead *head, const char *name, const char *val)
94 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
95 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
97 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
98 head->headers[0][0] = sstrdup(name);
99 head->headers[0][1] = sstrdup(val);
102 void headappheader(struct hthead *head, const char *name, const char *val)
106 i = head->noheaders++;
107 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
108 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
109 head->headers[i][0] = sstrdup(name);
110 head->headers[i][1] = sstrdup(val);
113 int sendreq(int sock, struct hthead *req)
119 if(socketpair(PF_UNIX, SOCK_STREAM, 0, pfds))
122 bufcatstr2(buf, req->method);
123 bufcatstr2(buf, req->url);
124 bufcatstr2(buf, req->ver);
125 bufcatstr2(buf, req->rest);
126 for(i = 0; i < req->noheaders; i++) {
127 bufcatstr2(buf, req->headers[i][0]);
128 bufcatstr2(buf, req->headers[i][1]);
131 ret = sendfd(sock, pfds[0], buf.b, buf.d);
142 int recvreq(int sock, struct hthead **reqp)
151 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
158 *reqp = omalloc(req);
159 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
161 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
163 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
165 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
169 if(!*(name = decstr(&p, &l)))
171 val = decstr(&p, &l);
172 headappheader(req, name, val);