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;
118 } else if(c == '\n') {
120 } else if(c == EOF) {
126 } else if(state == 1) {
131 } else if(c == '\r') {
132 } else if(c == '\n') {
134 } else if(c == EOF) {
139 } else if(state == 2) {
141 } else if(c == '\n') {
144 headappheader(head, name.b, val.b);
148 } else if(c == EOF) {
163 void replrest(struct hthead *head, char *rest)
167 /* Do not free the current rest string yet, so that the new one
168 * can be taken from a subpart of the old one. */
170 head->rest = sstrdup(rest);
174 void headpreheader(struct hthead *head, const char *name, const char *val)
176 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
177 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * 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);
184 void headappheader(struct hthead *head, const char *name, const char *val)
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);
195 void headrmheader(struct hthead *head, const char *name)
199 for(i = 0; i < head->noheaders; i++) {
200 if(!strcasecmp(head->headers[i][0], name)) {
201 free(head->headers[i][0]);
202 free(head->headers[i][1]);
203 free(head->headers[i]);
204 memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
210 int writeresp(FILE *out, struct hthead *resp)
214 if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
216 for(i = 0; i < resp->noheaders; i++) {
217 if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
223 int sendreq(int sock, struct hthead *req, int fd)
229 bufcatstr2(buf, req->method);
230 bufcatstr2(buf, req->url);
231 bufcatstr2(buf, req->ver);
232 bufcatstr2(buf, req->rest);
233 for(i = 0; i < req->noheaders; i++) {
234 bufcatstr2(buf, req->headers[i][0]);
235 bufcatstr2(buf, req->headers[i][1]);
238 ret = sendfd(sock, fd, buf.b, buf.d);
246 int recvreq(int sock, struct hthead **reqp)
255 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
258 fcntl(fd, F_SETFD, FD_CLOEXEC);
263 *reqp = omalloc(req);
264 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
266 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
268 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
270 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
274 if(!*(name = decstr(&p, &l)))
276 val = decstr(&p, &l);
277 headappheader(req, name, val);
290 char *unquoteurl(char *in)
303 if((p[1] >= '0') && (p[1] <= '9')) c |= (p[1] - '0') << 4;
304 else if((p[1] >= 'a') && (p[1] <= 'f')) c |= (p[1] - 'a' + 10) << 4;
305 else if((p[1] >= 'A') && (p[1] <= 'F')) c |= (p[1] - 'A' + 10) << 4;
307 if((p[2] >= '0') && (p[2] <= '9')) c |= (p[2] - '0');
308 else if((p[2] >= 'a') && (p[2] <= 'f')) c |= (p[2] - 'a' + 10);
309 else if((p[2] >= 'A') && (p[2] <= 'F')) c |= (p[2] - 'A' + 10);