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; |
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 |
in = ap_pfopen(r->pool, r->filename, "r"); |
56 |
|
57 |
if(in == NULL) { |
58 |
ap_log_reason("file permissions deny server access", r->filename, r); |
59 |
return FORBIDDEN; |
60 |
} |
61 |
#ifdef DEBUG |
62 |
if(r->args != 0 || ap_table_get(r->notes,"do_czs")) { |
63 |
ap_table_setn(r->headers_out, "X-czs_filename", r->filename); |
64 |
ap_table_setn(r->headers_out, "X-czs_content-type", r->content_type); |
65 |
if (r->args != 0) { |
66 |
ap_table_setn(r->headers_out, "X-czs_args", r->args); |
67 |
} |
68 |
} |
69 |
#endif |
70 |
|
71 |
#ifdef TEST_QUERYSTRINGV |
72 |
if(r->args == 0 && !ap_table_get(r->notes,"do_czs") || |
73 |
strstr(r->content_type,"cgi") != NULL) { |
74 |
#else |
75 |
if(!ap_table_get(r->notes,"do_czs") || |
76 |
strstr(r->content_type,"cgi") != NULL) { |
77 |
#endif |
78 |
return DECLINED; |
79 |
/* ap_send_fd(in, r); */ |
80 |
} else { |
81 |
|
82 |
regexp = ap_pregcomp(r->pool, rexp, REG_EXTENDED | REG_ICASE ); |
83 |
|
84 |
if (regexp == NULL) { |
85 |
ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r, |
86 |
"unable to compile pattern \"%s\"", rexp); |
87 |
return DECLINED; |
88 |
} |
89 |
|
90 |
ap_soft_timeout("send", r); |
91 |
ap_send_http_header(r); |
92 |
|
93 |
if(r->header_only) { |
94 |
ap_kill_timeout(r); |
95 |
ap_pfclose(r->pool, in); |
96 |
return OK; |
97 |
} |
98 |
|
99 |
|
100 |
while(fgets(buffer,MAX_STRING_LEN,in)) { |
101 |
for(i=0; i<MAX_STRING_LEN && buffer[i]; i++) { |
102 |
switch ( buffer[i] ) { |
103 |
case '¹': buffer[i]='s'; break; |
104 |
case 'ð': buffer[i]='d'; break; |
105 |
case 'è': buffer[i]='c'; break; |
106 |
case 'æ': buffer[i]='c'; break; |
107 |
case '¾': buffer[i]='z'; break; |
108 |
case '©': buffer[i]='S'; break; |
109 |
case 'Ð': buffer[i]='D'; break; |
110 |
case 'È': buffer[i]='C'; break; |
111 |
case 'Æ': buffer[i]='C'; break; |
112 |
case '®': buffer[i]='Z'; break; |
113 |
} |
114 |
} |
115 |
|
116 |
|
117 |
if (ap_regexec(regexp, buffer, nsub, regm, 0) == 0) { |
118 |
#if 0 |
119 |
ap_rprintf(r,"<!-- num replacements: %02x buffer:\n%s\n",nsub,buffer); |
120 |
ap_rprintf(r,"\n-->\n",i,regm[i].rm_so,regm[i].rm_eo); |
121 |
#endif |
122 |
for (i=0; i<10; i++) { |
123 |
if (regm[i].rm_so != -1) { |
124 |
if (regm[i].rm_so < f_ch) |
125 |
f_ch = regm[i].rm_so; |
126 |
if (regm[i].rm_eo > l_ch) |
127 |
l_ch = regm[i].rm_eo; |
128 |
} |
129 |
#if 0 |
130 |
ap_rprintf(r,"<!-- %02x: %02x-%02x -->\n",i,regm[i].rm_so,regm[i].rm_eo); |
131 |
#endif |
132 |
} |
133 |
buffer[f_ch]=0; |
134 |
ap_rprintf(r,"%s", ap_pstrcat(r->pool, buffer,"<!-- removed charset -->", buffer+l_ch, NULL)); |
135 |
} else { |
136 |
ap_rprintf(r,"%s",buffer); |
137 |
} |
138 |
} |
139 |
/* ap_pregfree(r->pool, regexp); */ |
140 |
} |
141 |
|
142 |
ap_kill_timeout(r); |
143 |
ap_pfclose(r->pool, in); |
144 |
return OK; |
145 |
} |
146 |
|
147 |
int translate_path(request_rec *r) { |
148 |
char *uri = r->uri; |
149 |
request_rec *subr; |
150 |
char *type = NULL; |
151 |
|
152 |
#ifdef DEBUG |
153 |
ap_table_setn(r->headers_out, "X-translate", r->uri); |
154 |
#endif |
155 |
if (uri[0]=='/' && (uri[1] | 0x20) =='c' && (uri[2] | 0x20)=='z' && (uri[3] | 0x20) =='s') { |
156 |
#if 0 |
157 |
ap_table_setn(r->headers_out, "Location", ap_pstrcat(r->pool, uri+2, "?czs", NULL)); |
158 |
return REDIRECT; |
159 |
#endif |
160 |
subr = (request_rec *) ap_sub_req_lookup_uri(r->uri+4, r); |
161 |
r->filename=ap_pstrdup(r->pool, subr->filename); |
162 |
type = subr->content_type; |
163 |
#ifdef DEBUG |
164 |
ap_table_setn(r->headers_out, "X-translate-content-type", type); |
165 |
#endif |
166 |
if (type == NULL) { |
167 |
return DECLINED; /* hm? */ |
168 |
} |
169 |
ap_table_setn(r->notes,"do_czs",1); |
170 |
ap_destroy_sub_req(subr); |
171 |
return OK; |
172 |
} |
173 |
|
174 |
return DECLINED; |
175 |
} |
176 |
|
177 |
static int add_charset_header(request_rec * r) |
178 |
{ |
179 |
const char *ua, *ct; |
180 |
|
181 |
ua = ap_table_get(r->headers_in, "User-Agent"); |
182 |
if (ua == NULL) |
183 |
ua = "<unknown>"; |
184 |
|
185 |
ct = r->content_type; |
186 |
|
187 |
if (ct != NULL && !ap_table_get(r->notes,"do_czs")) { |
188 |
|
189 |
if (strstr(ct, "text/html") == NULL) |
190 |
return DECLINED; /* Don't mess with other types */ |
191 |
|
192 |
if (strstr(ua, "Mac") != NULL) { |
193 |
if (strstr(ua, "MSIE") != NULL) |
194 |
r->content_type = "text/html; charset=x-mac-roman"; |
195 |
else |
196 |
r->content_type = "text/html; charset=MacCE"; |
197 |
} else |
198 |
r->content_type = "text/html; charset=iso-8859-2"; |
199 |
} |
200 |
|
201 |
#ifdef DEBUG |
202 |
ap_table_setn(r->headers_out, "X-Content-Type", ct); |
203 |
ap_table_setn(r->headers_out, "X-new-Content-Type", r->content_type); |
204 |
ap_table_setn(r->headers_out, "X-User-Agent", ua); |
205 |
#endif |
206 |
return DECLINED; |
207 |
} |
208 |
|
209 |
|
210 |
/* Dispatch list of content handlers */ |
211 |
static const handler_rec czs_handlers[] = { |
212 |
{ "czs", czs_handler }, |
213 |
{ NULL, NULL } |
214 |
}; |
215 |
|
216 |
/* Dispatch list for API hooks */ |
217 |
module MODULE_VAR_EXPORT czs_module = { |
218 |
STANDARD_MODULE_STUFF, |
219 |
NULL, /* module initializer */ |
220 |
NULL, /* create per-dir config structures */ |
221 |
NULL, /* merge per-dir config structures */ |
222 |
NULL, /* create per-server config structures */ |
223 |
NULL, /* merge per-server config structures */ |
224 |
NULL, /* table of config file commands */ |
225 |
czs_handlers, /* [#8] MIME-typed-dispatched handlers */ |
226 |
translate_path, /* [#1] URI to filename translation */ |
227 |
NULL, /* [#4] validate user id from request */ |
228 |
NULL, /* [#5] check if the user is ok _here_ */ |
229 |
NULL, /* [#2] check access by host address */ |
230 |
NULL, /* [#6] determine MIME type */ |
231 |
add_charset_header, /* [#7] pre-run fixups */ |
232 |
NULL, /* [#9] log a transaction */ |
233 |
NULL, /* [#3] header parser */ |
234 |
NULL, /* child_init */ |
235 |
NULL, /* child_exit */ |
236 |
NULL /* [#0] post read-request */ |
237 |
#ifdef EAPI |
238 |
,NULL, /* EAPI: add_module */ |
239 |
NULL, /* EAPI: remove_module */ |
240 |
NULL, /* EAPI: rewrite_command */ |
241 |
NULL /* EAPI: new_connection */ |
242 |
#endif |
243 |
}; |
244 |
|