Commit | Line | Data |
---|---|---|
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> | |
23 | ||
24 | #ifdef HAVE_CONFIG_H | |
25 | #include <config.h> | |
26 | #endif | |
27 | #include <utils.h> | |
28 | #include <resp.h> | |
29 | ||
30 | char *htmlquote(char *text) | |
31 | { | |
32 | struct charbuf buf; | |
33 | ||
34 | bufinit(buf); | |
35 | for(; *text; text++) { | |
36 | if(*text == '<') | |
37 | bufcatstr(buf, "<"); | |
38 | else if(*text == '>') | |
39 | bufcatstr(buf, ">"); | |
40 | else if(*text == '&') | |
41 | bufcatstr(buf, "&"); | |
42 | else | |
43 | bufadd(buf, *text); | |
44 | } | |
45 | bufadd(buf, 0); | |
46 | return(buf.b); | |
47 | } | |
48 | ||
49 | void simpleerror(int fd, int code, char *msg, char *fmt, ...) | |
50 | { | |
51 | struct charbuf buf; | |
52 | char *tmp1, *tmp2; | |
53 | va_list args; | |
54 | FILE *out; | |
55 | ||
56 | va_start(args, fmt); | |
57 | tmp1 = vsprintf2(fmt, args); | |
58 | va_end(args); | |
59 | tmp2 = htmlquote(tmp1); | |
60 | free(tmp1); | |
61 | bufinit(buf); | |
62 | bufcatstr(buf, "<?xml version=\"1.0\" encoding=\"US-ASCII\"?>\r\n"); | |
63 | bufcatstr(buf, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n"); | |
64 | bufcatstr(buf, "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en-US\" xml:lang=\"en-US\">\r\n"); | |
65 | bufcatstr(buf, "<head>\r\n"); | |
66 | bprintf(&buf, "<title>%s</title>\r\n", msg); | |
67 | bufcatstr(buf, "</head>\r\n"); | |
68 | bufcatstr(buf, "<body>\r\n"); | |
69 | bprintf(&buf, "<h1>%s</h1>\r\n", msg); | |
70 | bprintf(&buf, "<p>%s</p>\r\n", tmp2); | |
71 | bufcatstr(buf, "</body>\r\n"); | |
72 | bufcatstr(buf, "</html>\r\n"); | |
73 | free(tmp2); | |
74 | out = fdopen(fd, "w"); | |
75 | fprintf(out, "HTTP/1.1 %i %s\r\n", code, msg); | |
76 | fprintf(out, "Content-Type: text/html\r\n"); | |
77 | fprintf(out, "Content-Length: %i\r\n", buf.d); | |
78 | fprintf(out, "\r\n"); | |
79 | fwrite(buf.b, 1, buf.d, out); | |
80 | fclose(out); | |
81 | buffree(buf); | |
82 | } |