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