1 |
/* |
2 |
* mod_czs -- nuke iso8859-2 characters |
3 |
* sets `Contnet-type: text/html; charset=foobar' based on user agent string |
4 |
* from client browser |
5 |
* |
6 |
* Dobrica Pavlinusic <dpavlin@rot13.org> |
7 |
* Robert Avilov <ravilov@linux.hr> (some fixes of ua_charset part) |
8 |
* |
9 |
* based on mod_format by Stephen F. Booth and |
10 |
* mod_mocrify by Peter Triller, Ernst Bachmann |
11 |
* |
12 |
* Usage: httpd.conf |
13 |
* AddHandler czs .html |
14 |
* |
15 |
* This program is free software; you can redistribute it and/or modify |
16 |
* it under the terms of the GNU General Public License as published by |
17 |
* the Free Software Foundation; either version 2 of the License, or |
18 |
* (at your option) any later version. |
19 |
* |
20 |
* This program is distributed in the hope that it will be useful, |
21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 |
* GNU General Public License for more details. |
24 |
* |
25 |
* You should have received a copy of the GNU General Public License |
26 |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
*/ |
29 |
|
30 |
#include "httpd.h" |
31 |
#include "http_main.h" |
32 |
#include "http_config.h" |
33 |
#include "http_log.h" |
34 |
#include "http_protocol.h" |
35 |
|
36 |
module MODULE_VAR_EXPORT mod_czs; |
37 |
|
38 |
FILE *in; |
39 |
|
40 |
static int czs_handler(request_rec *r) { |
41 |
|
42 |
char buffer[MAX_STRING_LEN] = "undefined buffer content"; |
43 |
char *b,*do_czs,*do_mac; |
44 |
char *rexp = "< *META *HTTP-EQUIV=\"(Content-Type)\" *CONTENT=\"(text/html[^\"]+)\" *>"; |
45 |
regex_t *regexp; |
46 |
int nsub = 10; |
47 |
regmatch_t regm[10]; |
48 |
int i,f_ch=MAX_STRING_LEN,l_ch=0; |
49 |
|
50 |
if(r->method_number != M_GET) |
51 |
return DECLINED; |
52 |
if(r->finfo.st_mode == 0) |
53 |
return NOT_FOUND; |
54 |
|
55 |
do_czs = (char *)ap_table_get(r->notes,"do_czs"); |
56 |
do_mac = (char *)ap_table_get(r->notes,"do_mac"); |
57 |
|
58 |
in = ap_pfopen(r->pool, r->filename, "r"); |
59 |
|
60 |
if(in == NULL) { |
61 |
ap_log_reason("file permissions deny server access", r->filename, r); |
62 |
return FORBIDDEN; |
63 |
} |
64 |
#ifdef DEBUG |
65 |
ap_table_setn(r->headers_out, "X-czs_filename", r->filename); |
66 |
if (r->args != 0) { |
67 |
ap_table_setn(r->headers_out, "X-czs_args", r->args); |
68 |
} |
69 |
ap_table_setn(r->headers_out, "X-do_czs", do_czs); |
70 |
ap_table_setn(r->headers_out, "X-do_mac", do_mac); |
71 |
ap_table_setn(r->headers_out, "X-content-type", r->content_type); |
72 |
#endif |
73 |
|
74 |
#ifdef TEST_QUERYSTRINGV |
75 |
if (r->args == 0 && do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) { |
76 |
#else |
77 |
if (do_czs == NULL && do_mac == NULL || ap_strcasestr(r->content_type,"cgi") != NULL) { |
78 |
#endif |
79 |
return DECLINED; |
80 |
/* ap_send_fd(in, r); */ |
81 |
} else { |
82 |
|
83 |
regexp = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_ICASE ); |
84 |
|
85 |
if (regexp == NULL) { |
86 |
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, |
87 |
"unable to compile pattern \"%s\"", rexp); |
88 |
return DECLINED; |
89 |
} |
90 |
|
91 |
ap_soft_timeout("send", r); |
92 |
ap_send_http_header(r); |
93 |
|
94 |
if(r->header_only) { |
95 |
ap_kill_timeout(r); |
96 |
ap_pfclose(r->pool, in); |
97 |
return OK; |
98 |
} |
99 |
|
100 |
|
101 |
while(fgets(buffer,MAX_STRING_LEN,in)) { |
102 |
if (do_czs != NULL) { |
103 |
for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) { |
104 |
switch ( buffer[i] ) { |
105 |
case '¹': buffer[i]='s'; break; |
106 |
case 'ð': buffer[i]='d'; break; |
107 |
case 'è': buffer[i]='c'; break; |
108 |
case 'æ': buffer[i]='c'; break; |
109 |
case '¾': buffer[i]='z'; break; |
110 |
case '©': buffer[i]='S'; break; |
111 |
case 'Ð': buffer[i]='D'; break; |
112 |
case 'È': buffer[i]='C'; break; |
113 |
case 'Æ': buffer[i]='C'; break; |
114 |
case '®': buffer[i]='Z'; break; |
115 |
} |
116 |
} |
117 |
} |
118 |
|
119 |
|
120 |
if (ap_regexec(regexp, buffer, nsub, regm, 0) == 0) { |
121 |
#if 0 |
122 |
ap_rprintf(r,"<!-- num replacements: %02x buffer:\n%s\n",nsub,buffer); |
123 |
ap_rprintf(r,"\n-->\n",i,regm[i].rm_so,regm[i].rm_eo); |
124 |
#endif |
125 |
for (i=0; i<10; i++) { |
126 |
if (regm[i].rm_so != -1) { |
127 |
if (regm[i].rm_so < f_ch) |
128 |
f_ch = regm[i].rm_so; |
129 |
if (regm[i].rm_eo > l_ch) |
130 |
l_ch = regm[i].rm_eo; |
131 |
} |
132 |
#if 0 |
133 |
ap_rprintf(r,"<!-- %02x: %02x-%02x -->\n",i,regm[i].rm_so,regm[i].rm_eo); |
134 |
#endif |
135 |
} |
136 |
buffer[f_ch]=0; |
137 |
ap_rprintf(r,"%s", ap_pstrcat(r->pool, buffer,"<!-- removed charset -->", buffer+l_ch, NULL)); |
138 |
} else { |
139 |
ap_rprintf(r,"%s",buffer); |
140 |
} |
141 |
} |
142 |
/* ap_pregfree(r->pool, regexp); */ |
143 |
} |
144 |
|
145 |
ap_kill_timeout(r); |
146 |
ap_pfclose(r->pool, in); |
147 |
return OK; |
148 |
} |
149 |
|
150 |
int translate_path(request_rec *r) { |
151 |
char *uri = r->uri; |
152 |
request_rec *subr; |
153 |
char *ct; |
154 |
char *ua; |
155 |
|
156 |
#ifdef DEBUG |
157 |
ap_table_setn(r->headers_out, "X-translate-uri", r->uri); |
158 |
#endif |
159 |
|
160 |
if (uri[0]=='/' && (uri[1] | 0x20) =='c' && (uri[2] | 0x20)=='z' && (uri[3] | 0x20) =='s' && uri[4]=='/') { |
161 |
subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r); |
162 |
r->filename=ap_pstrdup(r->pool, subr->filename); |
163 |
ct = (char *)subr->content_type; |
164 |
if (ct == NULL) return DECLINED; /* hm? */ |
165 |
#ifdef DEBUG |
166 |
ap_table_setn(r->headers_out, "X-translate-sub-ct", ct); |
167 |
#endif |
168 |
ap_table_setn(r->notes,"do_czs",ap_pstrdup(r->pool,"1")); |
169 |
ap_destroy_sub_req(subr); |
170 |
return OK; |
171 |
} |
172 |
|
173 |
ua = (char *)ap_table_get(r->headers_in, "User-Agent"); |
174 |
if (ua == NULL) ua = "unknown"; |
175 |
|
176 |
#ifdef DEBUG |
177 |
ap_table_setn(r->headers_out, "X-translate-UA", ua); |
178 |
#endif |
179 |
|
180 |
if (uri[0]=='/' && (uri[1] | 0x20) =='m' && (uri[2] | 0x20)=='a' && (uri[3] | 0x20) =='c' && uri[4]=='/') { |
181 |
subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r); |
182 |
r->filename=ap_pstrdup(r->pool, subr->filename); |
183 |
ct = (char *)subr->content_type; |
184 |
if (ct == NULL) return DECLINED; /* hm? */ |
185 |
#ifdef DEBUG |
186 |
ap_table_setn(r->headers_out, "X-translate-sub-ct", ct); |
187 |
#endif |
188 |
/* will store User-Agent in do_mac as true value */ |
189 |
if (ap_strcasestr(ua, "Mac")) |
190 |
ap_table_setn(r->notes,"do_mac",ua); |
191 |
else { |
192 |
ap_table_setn(r->headers_out, "Location", r->uri+4); |
193 |
return REDIRECT; |
194 |
} |
195 |
ap_destroy_sub_req(subr); |
196 |
return OK; |
197 |
} |
198 |
|
199 |
#if 0 |
200 |
if (ap_strcasestr(ua, "Mac")) { |
201 |
ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, "/mac/", r->uri, NULL)); |
202 |
return REDIRECT; |
203 |
} |
204 |
#endif |
205 |
|
206 |
return DECLINED; |
207 |
} |
208 |
|
209 |
static int add_charset_header(request_rec * r) |
210 |
{ |
211 |
char *ua, *ct; |
212 |
|
213 |
ua = (char *)ap_table_get(r->notes, "do_mac"); |
214 |
ct = (char *)r->content_type; |
215 |
|
216 |
if (ct != NULL && !ap_table_get(r->notes,"do_czs")) { |
217 |
|
218 |
if (ap_strcasestr(ct, "text") == NULL) |
219 |
return DECLINED; /* Don't mess with other types */ |
220 |
|
221 |
if (ua) { /* do_mac */ |
222 |
if (ap_strcasestr(ua, "MSIE") != NULL) |
223 |
r->content_type = ap_pstrcat(r->pool, ct,"; charset=x-mac-roman", NULL); |
224 |
else |
225 |
r->content_type = ap_pstrcat(r->pool, ct,"; charset=MacCE", NULL); |
226 |
} else |
227 |
r->content_type = ap_pstrcat(r->pool, ct,"; charset=iso-8859-2", NULL); |
228 |
} |
229 |
|
230 |
#ifdef DEBUG |
231 |
ap_table_setn(r->headers_out, "X-Content-Type", ct); |
232 |
ap_table_setn(r->headers_out, "X-new-Content-Type", r->content_type); |
233 |
ap_table_setn(r->headers_out, "X-User-Agent", ua); |
234 |
#endif |
235 |
return DECLINED; |
236 |
} |
237 |
|
238 |
|
239 |
/* Dispatch list of content handlers */ |
240 |
static const handler_rec czs_handlers[] = { |
241 |
{ "czs", czs_handler }, |
242 |
{ NULL, NULL } |
243 |
}; |
244 |
|
245 |
/* Dispatch list for API hooks */ |
246 |
module MODULE_VAR_EXPORT czs_module = { |
247 |
STANDARD_MODULE_STUFF, |
248 |
NULL, /* module initializer */ |
249 |
NULL, /* create per-dir config structures */ |
250 |
NULL, /* merge per-dir config structures */ |
251 |
NULL, /* create per-server config structures */ |
252 |
NULL, /* merge per-server config structures */ |
253 |
NULL, /* table of config file commands */ |
254 |
czs_handlers, /* [#8] MIME-typed-dispatched handlers */ |
255 |
translate_path, /* [#1] URI to filename translation */ |
256 |
NULL, /* [#4] validate user id from request */ |
257 |
NULL, /* [#5] check if the user is ok _here_ */ |
258 |
NULL, /* [#2] check access by host address */ |
259 |
NULL, /* [#6] determine MIME type */ |
260 |
add_charset_header, /* [#7] pre-run fixups */ |
261 |
NULL, /* [#9] log a transaction */ |
262 |
NULL, /* [#3] header parser */ |
263 |
NULL, /* child_init */ |
264 |
NULL, /* child_exit */ |
265 |
NULL /* [#0] post read-request */ |
266 |
#ifdef EAPI |
267 |
,NULL, /* EAPI: add_module */ |
268 |
NULL, /* EAPI: remove_module */ |
269 |
NULL, /* EAPI: rewrite_command */ |
270 |
NULL /* EAPI: new_connection */ |
271 |
#endif |
272 |
}; |
273 |
|