Use libmagic for sendfile.
[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");
77 fprintf(out, "HTTP/1.1 %i %s\r\n", code, msg);
78 fprintf(out, "Content-Type: text/html\r\n");
f9255ddd 79 fprintf(out, "Content-Length: %zi\r\n", buf.d);
d422fdfd
FT
80 fprintf(out, "\r\n");
81 fwrite(buf.b, 1, buf.d, out);
82 fclose(out);
83 buffree(buf);
84}
b4164ce6
FT
85
86char *fmthttpdate(time_t time)
87{
88 /* I avoid using strftime, since it depends on locale settings. */
89 static char *days[] = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
90 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
91 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
92 struct tm *tm;
93
94 tm = gmtime(&time);
95 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));
96}
97
98time_t parsehttpdate(char *date)
99{
100 static regex_t *spec = NULL;
101 static char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
102 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
103 int i;
104 regmatch_t g[11];
105 struct tm tm;
106 int tz;
107
108 int gtoi(regmatch_t g)
109 {
110 int i, n;
111
112 for(i = g.rm_so, n = 0; i < g.rm_eo; i++)
113 n = (n * 10) + (date[i] - '0');
114 return(n);
115 }
116
117 int gstrcmp(regmatch_t g, char *str) {
118 if(g.rm_eo - g.rm_so != strlen(str))
119 return(1);
120 return(strncasecmp(date + g.rm_so, str, g.rm_eo - g.rm_so));
121 }
122
123 if(spec == NULL) {
124 omalloc(spec);
125 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)) {
126 free(spec);
127 spec = NULL;
128 return(0);
129 }
130 }
131 if(regexec(spec, date, 11, g, 0))
132 return(0);
133 tm.tm_mday = gtoi(g[1]);
134 tm.tm_year = gtoi(g[3]) - 1900;
135 tm.tm_hour = gtoi(g[4]);
136 tm.tm_min = gtoi(g[5]);
137 tm.tm_sec = gtoi(g[6]);
138
139 tm.tm_mon = -1;
140 for(i = 0; i < 12; i++) {
141 if(!gstrcmp(g[2], months[i])) {
142 tm.tm_mon = i;
143 break;
144 }
145 }
146 if(tm.tm_mon < 0)
147 return(0);
148
149 if(g[8].rm_so > 0) {
150 if(!gstrcmp(g[8], "GMT"))
151 tz = 0;
152 else
153 return(0);
154 } else if((g[9].rm_so > 0) && (g[10].rm_so > 0)) {
155 tz = gtoi(g[9]) * 3600 + gtoi(g[10]) * 60;
156 if(date[g[7].rm_so] == '-')
157 tz = -tz;
158 } else {
159 return(0);
160 }
161
162 return(timegm(&tm) - tz);
163}