Redirect directory requests to dirplex to always include a trailing slash.
[ashd.git] / lib / resp.c
CommitLineData
d422fdfd
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
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.
9
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.
14
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/>.
17*/
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
22#include <stdarg.h>
b4164ce6
FT
23#include <time.h>
24#include <regex.h>
d422fdfd
FT
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29#include <utils.h>
30#include <resp.h>
31
32char *htmlquote(char *text)
33{
34 struct charbuf buf;
35
36 bufinit(buf);
37 for(; *text; text++) {
38 if(*text == '<')
39 bufcatstr(buf, "&lt;");
40 else if(*text == '>')
41 bufcatstr(buf, "&gt;");
42 else if(*text == '&')
43 bufcatstr(buf, "&amp;");
44 else
45 bufadd(buf, *text);
46 }
47 bufadd(buf, 0);
48 return(buf.b);
49}
50
51void simpleerror(int fd, int code, char *msg, char *fmt, ...)
52{
53 struct charbuf buf;
54 char *tmp1, *tmp2;
55 va_list args;
56 FILE *out;
57
58 va_start(args, fmt);
59 tmp1 = vsprintf2(fmt, args);
60 va_end(args);
61 tmp2 = htmlquote(tmp1);
62 free(tmp1);
63 bufinit(buf);
64 bufcatstr(buf, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n");
65 bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
66 bufcatstr(buf, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\" xml:lang=\"en-US\">\r\n");
67 bufcatstr(buf, "<head>\r\n");
68 bprintf(&buf, "<title>%s</title>\r\n", msg);
69 bufcatstr(buf, "</head>\r\n");
70 bufcatstr(buf, "<body>\r\n");
71 bprintf(&buf, "<h1>%s</h1>\r\n", msg);
72 bprintf(&buf, "<p>%s</p>\r\n", tmp2);
73 bufcatstr(buf, "</body>\r\n");
74 bufcatstr(buf, "</html>\r\n");
75 free(tmp2);
76 out = fdopen(fd, "w");
81cfca6c
FT
77 fprintf(out, "HTTP/1.1 %i %s\n", code, msg);
78 fprintf(out, "Content-Type: text/html\n");
79 fprintf(out, "Content-Length: %zi\n", buf.d);
80 fprintf(out, "\n");
d422fdfd
FT
81 fwrite(buf.b, 1, buf.d, out);
82 fclose(out);
83 buffree(buf);
84}
b4164ce6 85
46e66302
FT
86void stdredir(struct hthead *req, int fd, int code, char *dst)
87{
88 FILE *out;
89 char *sp, *cp, *ep, *path, *url, *adst, *proto, *host;
90
91 sp = strchr(dst, '/');
92 cp = strchr(dst, ':');
93 if(cp && (!sp || (cp < sp))) {
94 adst = sstrdup(dst);
95 } else {
96 proto = getheader(req, "X-Ash-Protocol");
97 host = getheader(req, "Host");
98 if((proto == NULL) || (host == NULL)) {
99 /* Not compliant, but there isn't a whole lot to be done
100 * about it. */
101 adst = sstrdup(dst);
102 } else {
103 if(*dst == '/') {
104 path = sstrdup(dst);
105 } else {
106 if((*(url = req->url)) == '/')
107 url++;
108 if((ep = strrchr(url, '/')) != NULL)
109 ep++;
110 else
111 ep = url;
112 path = sprintf2("%.*s%s", ep - url, url, dst);
113 }
114 adst = sprintf2("%s://%s/%s", proto, host, path);
115 free(path);
116 }
117 }
118 out = fdopen(fd, "w");
119 fprintf(out, "HTTP/1.1 %i Redirection\n", code);
120 fprintf(out, "Content-Length: 0\n");
121 fprintf(out, "Location: %s\n", adst);
122 fprintf(out, "\n");
123 fclose(out);
124 free(adst);
125}
126
b4164ce6
FT
127char *fmthttpdate(time_t time)
128{
129 /* I avoid using strftime, since it depends on locale settings. */
130 static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
131 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
132 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
133 struct tm *tm;
134
135 tm = gmtime(&time);
136 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));
137}
138
139time_t parsehttpdate(char *date)
140{
141 static regex_t *spec = NULL;
142 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
143 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
144 int i;
145 regmatch_t g[11];
146 struct tm tm;
147 int tz;
148
149 int gtoi(regmatch_t g)
150 {
151 int i, n;
152
153 for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
154 n = (n * 10) + (date[i] - '0');
155 return(n);
156 }
157
158 int gstrcmp(regmatch_t g, char *str) {
159 if(g.rm_eo - g.rm_so != strlen(str))
160 return(1);
161 return(strncasecmp(date + g.rm_so, str, g.rm_eo - g.rm_so));
162 }
163
164 if(spec == NULL) {
165 omalloc(spec);
166 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)) {
167 free(spec);
168 spec = NULL;
169 return(0);
170 }
171 }
172 if(regexec(spec, date, 11, g, 0))
173 return(0);
174 tm.tm_mday = gtoi(g[1]);
175 tm.tm_year = gtoi(g[3]) - 1900;
176 tm.tm_hour = gtoi(g[4]);
177 tm.tm_min = gtoi(g[5]);
178 tm.tm_sec = gtoi(g[6]);
179
180 tm.tm_mon = -1;
181 for(i = 0; i < 12; i++) {
182 if(!gstrcmp(g[2], months[i])) {
183 tm.tm_mon = i;
184 break;
185 }
186 }
187 if(tm.tm_mon < 0)
188 return(0);
189
190 if(g[8].rm_so > 0) {
191 if(!gstrcmp(g[8], "GMT"))
192 tz = 0;
193 else
194 return(0);
195 } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
196 tz = gtoi(g[9]) * 3600 + gtoi(g[10]) * 60;
197 if(date[g[7].rm_so] == '-')
198 tz = -tz;
199 } else {
200 return(0);
201 }
202
203 return(timegm(&tm) - tz);
204}