Install the library headers.
[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
3095582d
FT
33static 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,
43};
44
45char *urlquote(char *text)
46{
47 static char *ret = NULL;
48 struct charbuf buf;
49 unsigned char c;
50
51 if(ret != NULL)
52 free(ret);
53 bufinit(buf);
54 for(; *text; text++) {
55 c = *text;
56 if(!c < 128 && safechars[(int)c])
57 bufadd(buf, *text);
58 else
59 bprintf(&buf, "%%%02X", (int)c);
60 }
61 bufadd(buf, 0);
62 return(ret = buf.b);
63}
64
d422fdfd
FT
65char *htmlquote(char *text)
66{
3095582d 67 static char *ret = NULL;
d422fdfd
FT
68 struct charbuf buf;
69
3095582d
FT
70 if(ret != NULL)
71 free(ret);
d422fdfd
FT
72 bufinit(buf);
73 for(; *text; text++) {
74 if(*text == '<')
75 bufcatstr(buf, "&lt;");
76 else if(*text == '>')
77 bufcatstr(buf, "&gt;");
78 else if(*text == '&')
79 bufcatstr(buf, "&amp;");
3095582d
FT
80 else if(*text == '\"')
81 bufcatstr(buf, "&quot;");
d422fdfd
FT
82 else
83 bufadd(buf, *text);
84 }
85 bufadd(buf, 0);
3095582d 86 return(ret = buf.b);
d422fdfd
FT
87}
88
89void simpleerror(int fd, int code, char *msg, char *fmt, ...)
90{
91 struct charbuf buf;
3095582d 92 char *tmp;
d422fdfd
FT
93 va_list args;
94 FILE *out;
95
96 va_start(args, fmt);
3095582d 97 tmp = vsprintf2(fmt, args);
d422fdfd 98 va_end(args);
d422fdfd
FT
99 bufinit(buf);
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);
3095582d 108 bprintf(&buf, "<p>%s</p>\r\n", htmlquote(tmp));
d422fdfd
FT
109 bufcatstr(buf, "</body>\r\n");
110 bufcatstr(buf, "</html>\r\n");
ad1983c4 111 out = fdopen(dup(fd), "w");
81cfca6c
FT
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);
115 fprintf(out, "\n");
d422fdfd
FT
116 fwrite(buf.b, 1, buf.d, out);
117 fclose(out);
118 buffree(buf);
51a4b1ad 119 free(tmp);
d422fdfd 120}
b4164ce6 121
46e66302
FT
122void stdredir(struct hthead *req, int fd, int code, char *dst)
123{
124 FILE *out;
c35bb77a 125 char *sp, *cp, *ep, *qs, *path, *url, *adst, *proto, *host;
46e66302
FT
126
127 sp = strchr(dst, '/');
128 cp = strchr(dst, ':');
129 if(cp && (!sp || (cp < sp))) {
130 adst = sstrdup(dst);
131 } else {
132 proto = getheader(req, "X-Ash-Protocol");
133 host = getheader(req, "Host");
134 if((proto == NULL) || (host == NULL)) {
135 /* Not compliant, but there isn't a whole lot to be done
136 * about it. */
137 adst = sstrdup(dst);
138 } else {
139 if(*dst == '/') {
2f43c22d 140 path = sstrdup(dst + 1);
46e66302
FT
141 } else {
142 if((*(url = req->url)) == '/')
143 url++;
c35bb77a
FT
144 if((ep = strchr(url, '?')) == NULL) {
145 ep = url + strlen(url);
146 qs = "";
147 } else {
148 qs = ep;
149 }
150 for(; (ep > url) && (ep[-1] != '/'); ep--);
151 path = sprintf2("%.*s%s%s", ep - url, url, dst, qs);
46e66302
FT
152 }
153 adst = sprintf2("%s://%s/%s", proto, host, path);
154 free(path);
155 }
156 }
ad1983c4 157 out = fdopen(dup(fd), "w");
46e66302
FT
158 fprintf(out, "HTTP/1.1 %i Redirection\n", code);
159 fprintf(out, "Content-Length: 0\n");
160 fprintf(out, "Location: %s\n", adst);
161 fprintf(out, "\n");
162 fclose(out);
163 free(adst);
164}
165
b4164ce6
FT
166char *fmthttpdate(time_t time)
167{
168 /* I avoid using strftime, since it depends on locale settings. */
169 static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
170 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
171 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
172 struct tm *tm;
173
174 tm = gmtime(&time);
175 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));
176}
177
178time_t parsehttpdate(char *date)
179{
180 static regex_t *spec = NULL;
181 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
182 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
183 int i;
184 regmatch_t g[11];
185 struct tm tm;
186 int tz;
187
188 int gtoi(regmatch_t g)
189 {
190 int i, n;
191
192 for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
193 n = (n * 10) + (date[i] - '0');
194 return(n);
195 }
196
197 int gstrcmp(regmatch_t g, char *str) {
198 if(g.rm_eo - g.rm_so != strlen(str))
199 return(1);
200 return(strncasecmp(date + g.rm_so, str, g.rm_eo - g.rm_so));
201 }
202
203 if(spec == NULL) {
204 omalloc(spec);
205 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)) {
206 free(spec);
207 spec = NULL;
208 return(0);
209 }
210 }
211 if(regexec(spec, date, 11, g, 0))
212 return(0);
213 tm.tm_mday = gtoi(g[1]);
214 tm.tm_year = gtoi(g[3]) - 1900;
215 tm.tm_hour = gtoi(g[4]);
216 tm.tm_min = gtoi(g[5]);
217 tm.tm_sec = gtoi(g[6]);
218
219 tm.tm_mon = -1;
220 for(i = 0; i < 12; i++) {
221 if(!gstrcmp(g[2], months[i])) {
222 tm.tm_mon = i;
223 break;
224 }
225 }
226 if(tm.tm_mon < 0)
227 return(0);
228
229 if(g[8].rm_so > 0) {
230 if(!gstrcmp(g[8], "GMT"))
231 tz = 0;
232 else
233 return(0);
234 } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
235 tz = gtoi(g[9]) * 3600 + gtoi(g[10]) * 60;
236 if(date[g[7].rm_so] == '-')
237 tz = -tz;
238 } else {
239 return(0);
240 }
241
242 return(timegm(&tm) - tz);
243}