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>
37 struct hthead *mkreq(char *method, char *url, char *ver)
42 req->method = sstrdup(method);
43 req->url = sstrdup(url);
44 req->ver = sstrdup(ver);
45 req->rest = sstrdup(url);
49 struct hthead *mkresp(int code, char *msg, char *ver)
55 resp->msg = sstrdup(msg);
56 resp->ver = sstrdup(ver);
60 void freehthead(struct hthead *head)
64 if(head->method != NULL)
72 if(head->rest != NULL)
75 for(i = 0; i < head->noheaders; i++) {
76 free(head->headers[i][0]);
77 free(head->headers[i][1]);
78 free(head->headers[i]);
85 char *getheader(struct hthead *head, char *name)
89 for(i = 0; i < head->noheaders; i++) {
90 if(!strcasecmp(head->headers[i][0], name))
91 return(head->headers[i][1]);
96 static void trim(struct charbuf *buf)
100 for(p = buf->b; (p - buf->b < buf->d) && isspace(*p); p++);
101 memmove(buf->b, p, buf->d -= (p - buf->b));
103 for(p = buf->b + buf->d - 1; (p > buf->b) && isspace(*p); p--, buf->d--);
106 int parseheaders(struct hthead *head, FILE *in)
109 struct charbuf name, val;
123 } else if(c == '\n') {
125 } else if(c == EOF) {
131 } else if(state == 1) {
136 } else if(c == '\r') {
137 } else if(c == '\n') {
139 } else if(c == EOF) {
144 } else if(state == 2) {
146 } else if(c == '\n') {
149 headappheader(head, name.b, val.b);
153 } else if(c == EOF) {
168 int parseheadersb(struct hthead *head, struct bufio *in)
171 struct charbuf name, val;
185 } else if(c == '\n') {
187 } else if(c == EOF) {
193 } else if(state == 1) {
198 } else if(c == '\r') {
199 } else if(c == '\n') {
201 } else if(c == EOF) {
206 } else if(state == 2) {
208 } else if(c == '\n') {
211 headappheader(head, name.b, val.b);
215 } else if(c == EOF) {
230 struct hthead *parseresponse(FILE *in)
234 struct charbuf ver, msg;
245 } else if((c == EOF) || (c < 32) || (c >= 128)) {
257 } else if((c == EOF) || (c < '0') || (c > '9')) {
260 code = (code * 10) + (c - '0');
270 } else if((c == EOF) || (c < 32)) {
280 req = mkresp(code, msg.b, ver.b);
281 if(parseheaders(req, in))
296 struct hthead *parseresponseb(struct bufio *in)
300 struct charbuf ver, msg;
311 } else if((c == EOF) || (c < 32) || (c >= 128)) {
323 } else if((c == EOF) || (c < '0') || (c > '9')) {
326 code = (code * 10) + (c - '0');
336 } else if((c == EOF) || (c < 32)) {
346 req = mkresp(code, msg.b, ver.b);
347 if(parseheadersb(req, in))
362 void replrest(struct hthead *head, char *rest)
366 /* Do not free the current rest string yet, so that the new one
367 * can be taken from a subpart of the old one. */
369 head->rest = sstrdup(rest);
373 void headpreheader(struct hthead *head, const char *name, const char *val)
375 head->headers = srealloc(head->headers, sizeof(*head->headers) * (head->noheaders + 1));
376 memmove(head->headers + 1, head->headers, sizeof(*head->headers) * head->noheaders);
378 head->headers[0] = smalloc(sizeof(*head->headers[0]) * 2);
379 head->headers[0][0] = sstrdup(name);
380 head->headers[0][1] = sstrdup(val);
383 void headappheader(struct hthead *head, const char *name, const char *val)
387 i = head->noheaders++;
388 head->headers = srealloc(head->headers, sizeof(*head->headers) * head->noheaders);
389 head->headers[i] = smalloc(sizeof(*head->headers[i]) * 2);
390 head->headers[i][0] = sstrdup(name);
391 head->headers[i][1] = sstrdup(val);
394 void headrmheader(struct hthead *head, const char *name)
398 for(i = 0; i < head->noheaders; i++) {
399 if(!strcasecmp(head->headers[i][0], name)) {
400 free(head->headers[i][0]);
401 free(head->headers[i][1]);
402 free(head->headers[i]);
403 memmove(head->headers + i, head->headers + i + 1, sizeof(head->headers) * (--head->noheaders - i));
409 int writeresp(FILE *out, struct hthead *resp)
413 if(fprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
415 for(i = 0; i < resp->noheaders; i++) {
416 if(fprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
422 int writerespb(struct bufio *out, struct hthead *resp)
426 if(bioprintf(out, "%s %i %s\r\n", resp->ver, resp->code, resp->msg) < 0)
428 for(i = 0; i < resp->noheaders; i++) {
429 if(bioprintf(out, "%s: %s\r\n", resp->headers[i][0], resp->headers[i][1]) < 0)
435 int sendreq2(int sock, struct hthead *req, int fd, int flags)
441 bufcatstr2(buf, req->method);
442 bufcatstr2(buf, req->url);
443 bufcatstr2(buf, req->ver);
444 bufcatstr2(buf, req->rest);
445 for(i = 0; i < req->noheaders; i++) {
446 bufcatstr2(buf, req->headers[i][0]);
447 bufcatstr2(buf, req->headers[i][1]);
450 ret = sendfd2(sock, fd, buf.b, buf.d, flags);
458 int sendreq(int sock, struct hthead *req, int fd)
460 return(sendreq2(sock, req, fd, MSG_NOSIGNAL));
463 int recvreq(int sock, struct hthead **reqp)
472 if((fd = recvfd(sock, &buf.b, &buf.d)) < 0) {
475 fcntl(fd, F_SETFD, FD_CLOEXEC);
480 *reqp = omalloc(req);
481 if((req->method = sstrdup(decstr(&p, &l))) == NULL)
483 if((req->url = sstrdup(decstr(&p, &l))) == NULL)
485 if((req->ver = sstrdup(decstr(&p, &l))) == NULL)
487 if((req->rest = sstrdup(decstr(&p, &l))) == NULL)
491 if(!*(name = decstr(&p, &l)))
493 val = decstr(&p, &l);
494 headappheader(req, name, val);
507 char *unquoteurl(char *in)
520 if((p[1] >= '0') && (p[1] <= '9')) c |= (p[1] - '0') << 4;
521 else if((p[1] >= 'a') && (p[1] <= 'f')) c |= (p[1] - 'a' + 10) << 4;
522 else if((p[1] >= 'A') && (p[1] <= 'F')) c |= (p[1] - 'A' + 10) << 4;
524 if((p[2] >= '0') && (p[2] <= '9')) c |= (p[2] - '0');
525 else if((p[2] >= 'a') && (p[2] <= 'f')) c |= (p[2] - 'a' + 10);
526 else if((p[2] >= 'A') && (p[2] <= 'F')) c |= (p[2] - 'A' + 10);