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>
35 struct hthead *mkreq(char *method, char *url, char *ver)
40 req->method = sstrdup(method);
41 req->url = sstrdup(url);
42 req->ver = sstrdup(ver);
43 req->rest = sstrdup(url);
47 struct hthead *mkresp(int code, char *msg, char *ver)
53 resp->msg = sstrdup(msg);
54 resp->ver = sstrdup(ver);
58 void freehthead(struct hthead *head)
62 if(head->method != NULL)
70 if(head->rest != NULL)
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]);
83 char *getheader(struct hthead *head, char *name)
87 for(i = 0; i < head->noheaders; i++) {
88 if(!strcasecmp(head->headers[i][0], name))
89 return(head->headers[i][1]);
94 static void trim(struct charbuf *buf)
98 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
99 memmove(buf->b, p, buf->d -= (p - buf->b));
100 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
103 int parseheaders(struct hthead *head, FILE *in)
106 struct charbuf name, val;
116 } else if(c == '\n') {
118 } else if(c == EOF) {
124 } else if(state == 1) {
129 } else if(c == '\r') {
130 } else if(c == '\n') {
132 } else if(c == EOF) {
137 } else if(state == 2) {
139 } else if(c == '\n') {
142 headappheader(head, name.b, val.b);
146 } else if(c == EOF) {
161 void replrest(struct hthead *head, char *rest)
165 /* Do not free the current rest string yet, so that the new one
166 * can be taken from a subpart of the old one. */
168 head->rest = sstrdup(rest);
172 void headpreheader(struct hthead *head, const char *name, const char *val)
174 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
175 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
177 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
178 head->headers[0][0] = sstrdup(name);
179 head->headers[0][1] = sstrdup(val);
182 void headappheader(struct hthead *head, const char *name, const char *val)
186 i = head->noheaders++;
187 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
188 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
189 head->headers[i][0] = sstrdup(name);
190 head->headers[i][1] = sstrdup(val);
193 int writeresp(FILE *out, struct hthead *resp)
197 if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
199 for(i = 0; i < resp->noheaders; i++) {
200 if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
206 int sendreq(int sock, struct hthead *req, int fd)
212 bufcatstr2(buf, req->method);
213 bufcatstr2(buf, req->url);
214 bufcatstr2(buf, req->ver);
215 bufcatstr2(buf, req->rest);
216 for(i = 0; i < req->noheaders; i++) {
217 bufcatstr2(buf, req->headers[i][0]);
218 bufcatstr2(buf, req->headers[i][1]);
221 ret = sendfd(sock, fd, buf.b, buf.d);
229 int recvreq(int sock, struct hthead **reqp)
238 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
245 *reqp = omalloc(req);
246 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
248 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
250 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
252 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
256 if(!*(name = decstr(&p, &l)))
258 val = decstr(&p, &l);
259 headappheader(req, name, val);
272 char *unquoteurl(char *in)
285 if((p[1] >= '0') && (p[1] <= '9')) c |= (p[1] - '0') << 4;
286 else if((p[1] >= 'a') && (p[1] <= 'f')) c |= (p[1] - 'a' + 10) << 4;
287 else if((p[1] >= 'A') && (p[1] <= 'F')) c |= (p[1] - 'A' + 10) << 4;
289 if((p[2] >= '0') && (p[2] <= '9')) c |= (p[2] - '0');
290 else if((p[2] >= 'a') && (p[2] <= 'f')) c |= (p[2] - 'a' + 10);
291 else if((p[2] >= 'A') && (p[2] <= 'F')) c |= (p[2] - 'A' + 10);