1 |
wakaba |
1.1 |
#!/usr/bin/perl |
2 |
|
|
use strict; |
3 |
|
|
|
4 |
|
|
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
wakaba |
1.16 |
/home/wakaba/work/manakai2/lib]; |
6 |
wakaba |
1.1 |
use CGI::Carp qw[fatalsToBrowser]; |
7 |
|
|
|
8 |
wakaba |
1.53 |
require WebHACC::Input; |
9 |
wakaba |
1.2 |
|
10 |
wakaba |
1.35 |
{ |
11 |
wakaba |
1.58 |
require Message::CGI::HTTP; |
12 |
wakaba |
1.16 |
my $http = Message::CGI::HTTP->new; |
13 |
wakaba |
1.1 |
|
14 |
wakaba |
1.58 |
require WebHACC::Output; |
15 |
|
|
my $out = WebHACC::Output->new; |
16 |
|
|
$out->handle (*STDOUT); |
17 |
|
|
$out->set_utf8; |
18 |
|
|
|
19 |
wakaba |
1.16 |
if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
20 |
wakaba |
1.58 |
$out->http_error (404); |
21 |
wakaba |
1.8 |
exit; |
22 |
|
|
} |
23 |
wakaba |
1.59 |
|
24 |
|
|
## TODO: We need real conneg support... |
25 |
|
|
my $primary_language = 'en'; |
26 |
|
|
if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) { |
27 |
|
|
$primary_language = 'ja'; |
28 |
|
|
} |
29 |
|
|
$out->load_text_catalog ($primary_language); |
30 |
wakaba |
1.53 |
|
31 |
|
|
$out->set_flush; |
32 |
wakaba |
1.58 |
$out->http_header; |
33 |
|
|
$out->html_header; |
34 |
|
|
$out->unset_flush; |
35 |
wakaba |
1.55 |
|
36 |
wakaba |
1.58 |
my $input = get_input_document ($http); |
37 |
wakaba |
1.53 |
$out->input ($input); |
38 |
|
|
|
39 |
wakaba |
1.58 |
require WebHACC::Result; |
40 |
wakaba |
1.55 |
my $result = WebHACC::Result->new; |
41 |
|
|
$result->output ($out); |
42 |
|
|
$result->{conforming_min} = 1; |
43 |
|
|
$result->{conforming_max} = 1; |
44 |
wakaba |
1.14 |
|
45 |
wakaba |
1.55 |
$out->html ('<script src="../cc-script.js"></script>'); |
46 |
wakaba |
1.54 |
|
47 |
wakaba |
1.55 |
check_and_print ($input => $result => $out); |
48 |
|
|
|
49 |
|
|
$result->generate_result_section; |
50 |
wakaba |
1.1 |
|
51 |
wakaba |
1.53 |
$out->nav_list; |
52 |
wakaba |
1.16 |
|
53 |
wakaba |
1.53 |
exit; |
54 |
wakaba |
1.35 |
} |
55 |
wakaba |
1.1 |
|
56 |
wakaba |
1.53 |
sub check_and_print ($$$) { |
57 |
|
|
my ($input, $result, $out) = @_; |
58 |
|
|
my $original_input = $out->input; |
59 |
|
|
$out->input ($input); |
60 |
wakaba |
1.31 |
|
61 |
wakaba |
1.55 |
$input->generate_info_section ($result); |
62 |
|
|
|
63 |
wakaba |
1.54 |
$input->generate_transfer_sections ($result); |
64 |
wakaba |
1.31 |
|
65 |
wakaba |
1.55 |
unless (defined $input->{s}) { |
66 |
|
|
$result->{conforming_min} = 0; |
67 |
|
|
return; |
68 |
|
|
} |
69 |
wakaba |
1.31 |
|
70 |
wakaba |
1.53 |
my $checker_class = { |
71 |
|
|
'text/cache-manifest' => 'WebHACC::Language::CacheManifest', |
72 |
|
|
'text/css' => 'WebHACC::Language::CSS', |
73 |
|
|
'text/html' => 'WebHACC::Language::HTML', |
74 |
|
|
'text/x-webidl' => 'WebHACC::Language::WebIDL', |
75 |
|
|
|
76 |
|
|
'text/xml' => 'WebHACC::Language::XML', |
77 |
|
|
'application/atom+xml' => 'WebHACC::Language::XML', |
78 |
|
|
'application/rss+xml' => 'WebHACC::Language::XML', |
79 |
|
|
'image/svg+xml' => 'WebHACC::Language::XML', |
80 |
|
|
'application/xhtml+xml' => 'WebHACC::Language::XML', |
81 |
|
|
'application/xml' => 'WebHACC::Language::XML', |
82 |
|
|
## TODO: Should we make all XML MIME Types fall |
83 |
|
|
## into this category? |
84 |
|
|
|
85 |
|
|
## NOTE: This type has different model from normal XML types. |
86 |
|
|
'application/rdf+xml' => 'WebHACC::Language::XML', |
87 |
|
|
}->{$input->{media_type}} || 'WebHACC::Language::Default'; |
88 |
|
|
|
89 |
|
|
eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@"; |
90 |
|
|
my $checker = $checker_class->new; |
91 |
|
|
$checker->input ($input); |
92 |
|
|
$checker->output ($out); |
93 |
|
|
$checker->result ($result); |
94 |
|
|
|
95 |
|
|
## TODO: A cache manifest MUST be text/cache-manifest |
96 |
|
|
## TODO: WebIDL media type "text/x-webidl" |
97 |
|
|
|
98 |
|
|
$checker->generate_syntax_error_section; |
99 |
|
|
$checker->generate_source_string_section; |
100 |
|
|
|
101 |
wakaba |
1.55 |
my @subdoc; |
102 |
wakaba |
1.53 |
$checker->onsubdoc (sub { |
103 |
|
|
push @subdoc, shift; |
104 |
|
|
}); |
105 |
|
|
|
106 |
|
|
$checker->generate_structure_dump_section; |
107 |
|
|
$checker->generate_structure_error_section; |
108 |
|
|
$checker->generate_additional_sections; |
109 |
|
|
|
110 |
wakaba |
1.34 |
my $id_prefix = 0; |
111 |
wakaba |
1.53 |
for my $_subinput (@subdoc) { |
112 |
wakaba |
1.55 |
my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix); |
113 |
wakaba |
1.53 |
$subinput->{$_} = $_subinput->{$_} for keys %$_subinput; |
114 |
wakaba |
1.34 |
$subinput->{base_uri} = $subinput->{container_node}->base_uri |
115 |
|
|
unless defined $subinput->{base_uri}; |
116 |
wakaba |
1.55 |
$subinput->{parent_input} = $input; |
117 |
wakaba |
1.34 |
|
118 |
wakaba |
1.55 |
$subinput->start_section ($result); |
119 |
wakaba |
1.53 |
check_and_print ($subinput => $result => $out); |
120 |
wakaba |
1.55 |
$subinput->end_section ($result); |
121 |
wakaba |
1.34 |
} |
122 |
wakaba |
1.53 |
|
123 |
|
|
$out->input ($original_input); |
124 |
wakaba |
1.31 |
} # check_and_print |
125 |
|
|
|
126 |
wakaba |
1.58 |
sub get_input_document ($) { |
127 |
|
|
my $http = shift; |
128 |
|
|
|
129 |
|
|
require Message::DOM::DOMImplementation; |
130 |
|
|
my $dom = Message::DOM::DOMImplementation->new; |
131 |
wakaba |
1.9 |
|
132 |
wakaba |
1.54 |
require Encode; |
133 |
|
|
my $request_uri = Encode::decode ('utf-8', $http->get_parameter ('uri')); |
134 |
wakaba |
1.53 |
my $r = WebHACC::Input->new; |
135 |
wakaba |
1.9 |
if (defined $request_uri and length $request_uri) { |
136 |
|
|
my $uri = $dom->create_uri_reference ($request_uri); |
137 |
|
|
unless ({ |
138 |
|
|
http => 1, |
139 |
|
|
}->{lc $uri->uri_scheme}) { |
140 |
wakaba |
1.54 |
$r = WebHACC::Input::Error->new; |
141 |
|
|
$r->{uri} = $request_uri; |
142 |
|
|
$r->{request_uri} = $request_uri; |
143 |
|
|
$r->{error_status_text} = 'URL scheme not allowed'; |
144 |
wakaba |
1.9 |
} |
145 |
|
|
|
146 |
|
|
require Message::Util::HostPermit; |
147 |
|
|
my $host_permit = new Message::Util::HostPermit; |
148 |
|
|
$host_permit->add_rule (<<EOH); |
149 |
|
|
Allow host=suika port=80 |
150 |
|
|
Deny host=suika |
151 |
|
|
Allow host=suika.fam.cx port=80 |
152 |
|
|
Deny host=suika.fam.cx |
153 |
|
|
Deny host=localhost |
154 |
|
|
Deny host=*.localdomain |
155 |
|
|
Deny ipv4=0.0.0.0/8 |
156 |
|
|
Deny ipv4=10.0.0.0/8 |
157 |
|
|
Deny ipv4=127.0.0.0/8 |
158 |
|
|
Deny ipv4=169.254.0.0/16 |
159 |
|
|
Deny ipv4=172.0.0.0/11 |
160 |
|
|
Deny ipv4=192.0.2.0/24 |
161 |
|
|
Deny ipv4=192.88.99.0/24 |
162 |
|
|
Deny ipv4=192.168.0.0/16 |
163 |
|
|
Deny ipv4=198.18.0.0/15 |
164 |
|
|
Deny ipv4=224.0.0.0/4 |
165 |
|
|
Deny ipv4=255.255.255.255/32 |
166 |
|
|
Deny ipv6=0::0/0 |
167 |
|
|
Allow host=* |
168 |
|
|
EOH |
169 |
|
|
unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) { |
170 |
wakaba |
1.54 |
my $r = WebHACC::Input::Error->new; |
171 |
|
|
$r->{uri} = $request_uri; |
172 |
|
|
$r->{request_uri} = $request_uri; |
173 |
|
|
$r->{error_status_text} = 'Connection to the host is forbidden'; |
174 |
|
|
return $r; |
175 |
wakaba |
1.9 |
} |
176 |
|
|
|
177 |
|
|
require LWP::UserAgent; |
178 |
|
|
my $ua = WDCC::LWPUA->new; |
179 |
|
|
$ua->{wdcc_dom} = $dom; |
180 |
|
|
$ua->{wdcc_host_permit} = $host_permit; |
181 |
|
|
$ua->agent ('Mozilla'); ## TODO: for now. |
182 |
|
|
$ua->parse_head (0); |
183 |
|
|
$ua->protocols_allowed ([qw/http/]); |
184 |
|
|
$ua->max_size (1000_000); |
185 |
|
|
my $req = HTTP::Request->new (GET => $request_uri); |
186 |
wakaba |
1.28 |
$req->header ('Accept-Encoding' => 'identity, *; q=0'); |
187 |
wakaba |
1.9 |
my $res = $ua->request ($req); |
188 |
wakaba |
1.16 |
## TODO: 401 sets |is_success| true. |
189 |
|
|
if ($res->is_success or $http->get_parameter ('error-page')) { |
190 |
wakaba |
1.9 |
$r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! |
191 |
|
|
$r->{uri} = $res->request->uri; |
192 |
|
|
$r->{request_uri} = $request_uri; |
193 |
|
|
|
194 |
|
|
## TODO: More strict parsing... |
195 |
|
|
my $ct = $res->header ('Content-Type'); |
196 |
wakaba |
1.22 |
if (defined $ct and $ct =~ /;\s*charset\s*=\s*"?([^\s;"]+)"?/i) { |
197 |
wakaba |
1.9 |
$r->{charset} = lc $1; |
198 |
|
|
$r->{charset} =~ tr/\\//d; |
199 |
wakaba |
1.26 |
$r->{official_charset} = $r->{charset}; |
200 |
wakaba |
1.9 |
} |
201 |
|
|
|
202 |
wakaba |
1.16 |
my $input_charset = $http->get_parameter ('charset'); |
203 |
wakaba |
1.9 |
if (defined $input_charset and length $input_charset) { |
204 |
|
|
$r->{charset_overridden} |
205 |
|
|
= (not defined $r->{charset} or $r->{charset} ne $input_charset); |
206 |
|
|
$r->{charset} = $input_charset; |
207 |
wakaba |
1.25 |
} |
208 |
|
|
|
209 |
|
|
## TODO: Support for HTTP Content-Encoding |
210 |
wakaba |
1.9 |
|
211 |
|
|
$r->{s} = ''.$res->content; |
212 |
wakaba |
1.25 |
|
213 |
|
|
require Whatpm::ContentType; |
214 |
|
|
($r->{official_type}, $r->{media_type}) |
215 |
|
|
= Whatpm::ContentType->get_sniffed_type |
216 |
|
|
(get_file_head => sub { |
217 |
|
|
return substr $r->{s}, 0, shift; |
218 |
|
|
}, |
219 |
|
|
http_content_type_byte => $ct, |
220 |
|
|
has_http_content_encoding => |
221 |
|
|
defined $res->header ('Content-Encoding'), |
222 |
|
|
supported_image_types => {}); |
223 |
wakaba |
1.9 |
} else { |
224 |
|
|
$r->{uri} = $res->request->uri; |
225 |
|
|
$r->{request_uri} = $request_uri; |
226 |
|
|
$r->{error_status_text} = $res->status_line; |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
$r->{header_field} = []; |
230 |
|
|
$res->scan (sub { |
231 |
|
|
push @{$r->{header_field}}, [$_[0], $_[1]]; |
232 |
|
|
}); |
233 |
|
|
$r->{header_status_code} = $res->code; |
234 |
|
|
$r->{header_status_text} = $res->message; |
235 |
|
|
} else { |
236 |
wakaba |
1.16 |
$r->{s} = ''.$http->get_parameter ('s'); |
237 |
wakaba |
1.9 |
$r->{uri} = q<thismessage:/>; |
238 |
|
|
$r->{request_uri} = q<thismessage:/>; |
239 |
|
|
$r->{base_uri} = q<thismessage:/>; |
240 |
wakaba |
1.16 |
$r->{charset} = ''.$http->get_parameter ('_charset_'); |
241 |
wakaba |
1.9 |
$r->{charset} =~ s/\s+//g; |
242 |
|
|
$r->{charset} = 'utf-8' if $r->{charset} eq ''; |
243 |
wakaba |
1.26 |
$r->{official_charset} = $r->{charset}; |
244 |
wakaba |
1.9 |
$r->{header_field} = []; |
245 |
wakaba |
1.25 |
|
246 |
|
|
require Whatpm::ContentType; |
247 |
|
|
($r->{official_type}, $r->{media_type}) |
248 |
|
|
= Whatpm::ContentType->get_sniffed_type |
249 |
|
|
(get_file_head => sub { |
250 |
|
|
return substr $r->{s}, 0, shift; |
251 |
|
|
}, |
252 |
|
|
http_content_type_byte => undef, |
253 |
|
|
has_http_content_encoding => 0, |
254 |
|
|
supported_image_types => {}); |
255 |
wakaba |
1.9 |
} |
256 |
|
|
|
257 |
wakaba |
1.16 |
my $input_format = $http->get_parameter ('i'); |
258 |
wakaba |
1.9 |
if (defined $input_format and length $input_format) { |
259 |
|
|
$r->{media_type_overridden} |
260 |
|
|
= (not defined $r->{media_type} or $input_format ne $r->{media_type}); |
261 |
|
|
$r->{media_type} = $input_format; |
262 |
|
|
} |
263 |
|
|
if (defined $r->{s} and not defined $r->{media_type}) { |
264 |
|
|
$r->{media_type} = 'text/html'; |
265 |
|
|
$r->{media_type_overridden} = 1; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
if ($r->{media_type} eq 'text/xml') { |
269 |
|
|
unless (defined $r->{charset}) { |
270 |
|
|
$r->{charset} = 'us-ascii'; |
271 |
wakaba |
1.26 |
$r->{official_charset} = $r->{charset}; |
272 |
wakaba |
1.9 |
} elsif ($r->{charset_overridden} and $r->{charset} eq 'us-ascii') { |
273 |
|
|
$r->{charset_overridden} = 0; |
274 |
|
|
} |
275 |
|
|
} |
276 |
|
|
|
277 |
|
|
if (length $r->{s} > 1000_000) { |
278 |
|
|
$r->{error_status_text} = 'Entity-body too large'; |
279 |
|
|
delete $r->{s}; |
280 |
|
|
return $r; |
281 |
|
|
} |
282 |
|
|
|
283 |
wakaba |
1.35 |
$r->{inner_html_element} = $http->get_parameter ('e'); |
284 |
|
|
|
285 |
wakaba |
1.9 |
return $r; |
286 |
|
|
} # get_input_document |
287 |
|
|
|
288 |
|
|
package WDCC::LWPUA; |
289 |
|
|
BEGIN { push our @ISA, 'LWP::UserAgent'; } |
290 |
|
|
|
291 |
|
|
sub redirect_ok { |
292 |
|
|
my $ua = shift; |
293 |
|
|
unless ($ua->SUPER::redirect_ok (@_)) { |
294 |
|
|
return 0; |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
my $uris = $_[1]->header ('Location'); |
298 |
|
|
return 0 unless $uris; |
299 |
|
|
my $uri = $ua->{wdcc_dom}->create_uri_reference ($uris); |
300 |
|
|
unless ({ |
301 |
|
|
http => 1, |
302 |
|
|
}->{lc $uri->uri_scheme}) { |
303 |
|
|
return 0; |
304 |
|
|
} |
305 |
|
|
unless ($ua->{wdcc_host_permit}->check ($uri->uri_host, $uri->uri_port || 80)) { |
306 |
|
|
return 0; |
307 |
|
|
} |
308 |
|
|
return 1; |
309 |
|
|
} # redirect_ok |
310 |
|
|
|
311 |
wakaba |
1.1 |
=head1 AUTHOR |
312 |
|
|
|
313 |
|
|
Wakaba <w@suika.fam.cx>. |
314 |
|
|
|
315 |
|
|
=head1 LICENSE |
316 |
|
|
|
317 |
wakaba |
1.35 |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
318 |
wakaba |
1.1 |
|
319 |
|
|
This library is free software; you can redistribute it |
320 |
|
|
and/or modify it under the same terms as Perl itself. |
321 |
|
|
|
322 |
|
|
=cut |
323 |
|
|
|
324 |
wakaba |
1.59 |
## $Date: 2008/07/21 09:40:59 $ |