callfcgi: Improved recvrec implementation.
[ashd.git] / lib / resp.c
... / ...
CommitLineData
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 <unistd.h>
21#include <string.h>
22#include <stdio.h>
23#include <stdarg.h>
24#include <time.h>
25#include <regex.h>
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <utils.h>
31#include <resp.h>
32
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
65char *htmlquote(char *text)
66{
67 static char *ret = NULL;
68 struct charbuf buf;
69
70 if(ret != NULL)
71 free(ret);
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;");
80 else if(*text == '\"')
81 bufcatstr(buf, "&quot;");
82 else
83 bufadd(buf, *text);
84 }
85 bufadd(buf, 0);
86 return(ret = buf.b);
87}
88
89static void simpleerror2v(FILE *out, int code, char *msg, char *fmt, va_list args)
90{
91 struct charbuf buf;
92 char *tmp;
93
94 tmp = vsprintf2(fmt, args);
95 bufinit(buf);
96 bufcatstr(buf, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n");
97 bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
98 bufcatstr(buf, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\" xml:lang=\"en-US\">\r\n");
99 bufcatstr(buf, "<head>\r\n");
100 bprintf(&buf, "<title>%s</title>\r\n", msg);
101 bufcatstr(buf, "</head>\r\n");
102 bufcatstr(buf, "<body>\r\n");
103 bprintf(&buf, "<h1>%s</h1>\r\n", msg);
104 bprintf(&buf, "<p>%s</p>\r\n", htmlquote(tmp));
105 bufcatstr(buf, "</body>\r\n");
106 bufcatstr(buf, "</html>\r\n");
107 fprintf(out, "HTTP/1.1 %i %s\n", code, msg);
108 fprintf(out, "Content-Type: text/html\n");
109 fprintf(out, "Content-Length: %zi\n", buf.d);
110 fprintf(out, "\n");
111 fwrite(buf.b, 1, buf.d, out);
112 buffree(buf);
113 free(tmp);
114}
115
116void simpleerror2(FILE *out, int code, char *msg, char *fmt, ...)
117{
118 va_list args;
119
120 va_start(args, fmt);
121 simpleerror2v(out, code, msg, fmt, args);
122 va_end(args);
123}
124
125void simpleerror(int fd, int code, char *msg, char *fmt, ...)
126{
127 va_list args;
128 FILE *out;
129
130 va_start(args, fmt);
131 out = fdopen(dup(fd), "w");
132 simpleerror2v(out, code, msg, fmt, args);
133 fclose(out);
134 va_end(args);
135}
136
137void stdredir(struct hthead *req, int fd, int code, char *dst)
138{
139 FILE *out;
140 char *sp, *cp, *ep, *qs, *path, *url, *adst, *proto, *host;
141
142 sp = strchr(dst, '/');
143 cp = strchr(dst, ':');
144 if(cp && (!sp || (cp < sp))) {
145 adst = sstrdup(dst);
146 } else {
147 proto = getheader(req, "X-Ash-Protocol");
148 host = getheader(req, "Host");
149 if((proto == NULL) || (host == NULL)) {
150 /* Not compliant, but there isn't a whole lot to be done
151 * about it. */
152 adst = sstrdup(dst);
153 } else {
154 if(*dst == '/') {
155 path = sstrdup(dst + 1);
156 } else {
157 if((*(url = req->url)) == '/')
158 url++;
159 if((ep = strchr(url, '?')) == NULL) {
160 ep = url + strlen(url);
161 qs = "";
162 } else {
163 qs = ep;
164 }
165 for(; (ep > url) && (ep[-1] != '/'); ep--);
166 path = sprintf2("%.*s%s%s", ep - url, url, dst, qs);
167 }
168 adst = sprintf2("%s://%s/%s", proto, host, path);
169 free(path);
170 }
171 }
172 out = fdopen(dup(fd), "w");
173 fprintf(out, "HTTP/1.1 %i Redirection\n", code);
174 fprintf(out, "Content-Length: 0\n");
175 fprintf(out, "Location: %s\n", adst);
176 fprintf(out, "\n");
177 fclose(out);
178 free(adst);
179}
180
181char *fmthttpdate(time_t time)
182{
183 /* I avoid using strftime, since it depends on locale settings. */
184 static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
185 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
186 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
187 struct tm *tm;
188
189 tm = gmtime(&time);
190 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));
191}
192
193static int gtoi(char *bstr, regmatch_t g)
194{
195 int i, n;
196
197 for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
198 n = (n * 10) + (bstr[i] - '0');
199 return(n);
200}
201
202static int gstrcmp(char *bstr, regmatch_t g, char *str)
203{
204 if(g.rm_eo - g.rm_so != strlen(str))
205 return(1);
206 return(strncasecmp(bstr + g.rm_so, str, g.rm_eo - g.rm_so));
207}
208
209time_t parsehttpdate(char *date)
210{
211 static regex_t *spec = NULL;
212 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
213 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
214 int i;
215 regmatch_t g[11];
216 struct tm tm;
217 int tz;
218
219 if(spec == NULL) {
220 omalloc(spec);
221 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)) {
222 free(spec);
223 spec = NULL;
224 return(0);
225 }
226 }
227 if(regexec(spec, date, 11, g, 0))
228 return(0);
229 tm.tm_mday = gtoi(date, g[1]);
230 tm.tm_year = gtoi(date, g[3]) - 1900;
231 tm.tm_hour = gtoi(date, g[4]);
232 tm.tm_min = gtoi(date, g[5]);
233 tm.tm_sec = gtoi(date, g[6]);
234
235 tm.tm_mon = -1;
236 for(i = 0; i < 12; i++) {
237 if(!gstrcmp(date, g[2], months[i])) {
238 tm.tm_mon = i;
239 break;
240 }
241 }
242 if(tm.tm_mon < 0)
243 return(0);
244
245 if(g[8].rm_so > 0) {
246 if(!gstrcmp(date, g[8], "GMT"))
247 tz = 0;
248 else
249 return(0);
250 } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
251 tz = gtoi(date, g[9]) * 3600 + gtoi(date, g[10]) * 60;
252 if(date[g[7].rm_so] == '-')
253 tz = -tz;
254 } else {
255 return(0);
256 }
257
258 return(timegm(&tm) - tz);
259}
260
261char *httpdefstatus(int code)
262{
263 switch(code) {
264 case 200:
265 return("OK");
266 case 201:
267 return("Created");
268 case 202:
269 return("Accepted");
270 case 204:
271 return("No Content");
272 case 300:
273 return("Multiple Choices");
274 case 301:
275 return("Moved Permanently");
276 case 302:
277 return("Found");
278 case 303:
279 return("See Other");
280 case 304:
281 return("Not Modified");
282 case 307:
283 return("Moved Temporarily");
284 case 400:
285 return("Bad Request");
286 case 401:
287 return("Unauthorized");
288 case 403:
289 return("Forbidden");
290 case 404:
291 return("Not Found");
292 case 500:
293 return("Internal Server Error");
294 case 501:
295 return("Not Implemented");
296 case 503:
297 return("Service Unavailable");
298 default:
299 return("Unknown status");
300 }
301}