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 replrest(struct hthead *head, char *rest)
96 /* Do not free the current rest string yet, so that the new one
97 * can a subpart of the old one. */
99 head->rest = sstrdup(rest);
103 void headpreheader(struct hthead *head, const char *name, const char *val)
105 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
106 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
108 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
109 head->headers[0][0] = sstrdup(name);
110 head->headers[0][1] = sstrdup(val);
113 void headappheader(struct hthead *head, const char *name, const char *val)
117 i = head->noheaders++;
118 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
119 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
120 head->headers[i][0] = sstrdup(name);
121 head->headers[i][1] = sstrdup(val);
124 int sendreq(int sock, struct hthead *req, int fd)
130 bufcatstr2(buf, req->method);
131 bufcatstr2(buf, req->url);
132 bufcatstr2(buf, req->ver);
133 bufcatstr2(buf, req->rest);
134 for(i = 0; i < req->noheaders; i++) {
135 bufcatstr2(buf, req->headers[i][0]);
136 bufcatstr2(buf, req->headers[i][1]);
139 ret = sendfd(sock, fd, buf.b, buf.d);
147 int recvreq(int sock, struct hthead **reqp)
156 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
163 *reqp = omalloc(req);
164 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
166 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
168 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
170 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
174 if(!*(name = decstr(&p, &l)))
176 val = decstr(&p, &l);
177 headappheader(req, name, val);