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/>.
33 static char safechars[128] = {
34 /* x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 xa xb xc xd xe xf */
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1,
38 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
39 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
40 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
41 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
42 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0,
45 char *urlquote(char *text)
47 static char *ret = NULL;
54 for(; *text; text++) {
56 if(!c < 128 && safechars[(int)c])
59 bprintf(&buf, "%%%02X", (int)c);
65 char *htmlquote(char *text)
67 static char *ret = NULL;
73 for(; *text; text++) {
75 bufcatstr(buf, "<");
77 bufcatstr(buf, ">");
79 bufcatstr(buf, "&");
80 else if(*text == '\"')
81 bufcatstr(buf, """);
89 void simpleerror(int fd, int code, char *msg, char *fmt, ...)
97 tmp = vsprintf2(fmt, args);
100 bufcatstr(buf, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n");
101 bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
102 bufcatstr(buf, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\" xml:lang=\"en-US\">\r\n");
103 bufcatstr(buf, "<head>\r\n");
104 bprintf(&buf, "<title>%s</title>\r\n", msg);
105 bufcatstr(buf, "</head>\r\n");
106 bufcatstr(buf, "<body>\r\n");
107 bprintf(&buf, "<h1>%s</h1>\r\n", msg);
108 bprintf(&buf, "<p>%s</p>\r\n", htmlquote(tmp));
109 bufcatstr(buf, "</body>\r\n");
110 bufcatstr(buf, "</html>\r\n");
111 out = fdopen(dup(fd), "w");
112 fprintf(out, "HTTP/1.1 %i %s\n", code, msg);
113 fprintf(out, "Content-Type: text/html\n");
114 fprintf(out, "Content-Length: %zi\n", buf.d);
116 fwrite(buf.b, 1, buf.d, out);
121 void stdredir(struct hthead *req, int fd, int code, char *dst)
124 char *sp, *cp, *ep, *qs, *path, *url, *adst, *proto, *host;
126 sp = strchr(dst, '/');
127 cp = strchr(dst, ':');
128 if(cp && (!sp || (cp < sp))) {
131 proto = getheader(req, "X-Ash-Protocol");
132 host = getheader(req, "Host");
133 if((proto == NULL) || (host == NULL)) {
134 /* Not compliant, but there isn't a whole lot to be done
139 path = sstrdup(dst + 1);
141 if((*(url = req->url)) == '/')
143 if((ep = strchr(url, '?')) == NULL) {
144 ep = url + strlen(url);
149 for(; (ep > url) && (ep[-1] != '/'); ep--);
150 path = sprintf2("%.*s%s%s", ep - url, url, dst, qs);
152 adst = sprintf2("%s://%s/%s", proto, host, path);
156 out = fdopen(dup(fd), "w");
157 fprintf(out, "HTTP/1.1 %i Redirection\n", code);
158 fprintf(out, "Content-Length: 0\n");
159 fprintf(out, "Location: %s\n", adst);
165 char *fmthttpdate(time_t time)
167 /* I avoid using strftime, since it depends on locale settings. */
168 static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
169 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
170 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
174 return(sprintf3("%s, %i %s %i %02i:%02i:%02i GMT", days[(tm->tm_wday + 6) % 7], tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec));
177 time_t parsehttpdate(char *date)
179 static regex_t *spec = NULL;
180 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
181 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
187 int gtoi(regmatch_t g)
191 for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
192 n = (n * 10) + (date[i] - '0');
196 int gstrcmp(regmatch_t g, char *str) {
197 if(g.rm_eo - g.rm_so != strlen(str))
199 return(strncasecmp(date + g.rm_so, str, g.rm_eo - g.rm_so));
204 if(regcomp(spec, "^[A-Z]{3}, +([0-9]+) +([A-Z]{3}) +([0-9]+) +([0-9]{2}):([0-9]{2}):([0-9]{2}) +(([A-Z]+)|[+-]([0-9]{2})([0-9]{2}))$", REG_EXTENDED | REG_ICASE)) {
210 if(regexec(spec, date, 11, g, 0))
212 tm.tm_mday = gtoi(g[1]);
213 tm.tm_year = gtoi(g[3]) - 1900;
214 tm.tm_hour = gtoi(g[4]);
215 tm.tm_min = gtoi(g[5]);
216 tm.tm_sec = gtoi(g[6]);
219 for(i = 0; i < 12; i++) {
220 if(!gstrcmp(g[2], months[i])) {
229 if(!gstrcmp(g[8], "GMT"))
233 } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
234 tz = gtoi(g[9]) * 3600 + gtoi(g[10]) * 60;
235 if(date[g[7].rm_so] == '-')
241 return(timegm(&tm) - tz);