1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
|
4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
/home/wakaba/work/manakai2/lib |
6 |
../lib |
7 |
]; |
8 |
|
9 |
my $log_uri; |
10 |
|
11 |
use Getopt::Long; |
12 |
GetOptions ( |
13 |
'log-uri=s' => \$log_uri, |
14 |
); |
15 |
die unless defined $log_uri; |
16 |
|
17 |
my $diff_command = 'diff'; |
18 |
my $diff_options = ['--rcs', '-a']; |
19 |
|
20 |
require Message::DOM::DOMImplementation; |
21 |
my $dom = 'Message::DOM::DOMImplementation'; |
22 |
|
23 |
my $ent = get_remote_entity ($log_uri); |
24 |
unless (defined $ent->{s}) { |
25 |
die "<$log_uri>: $ent->{error_status_text}"; |
26 |
} |
27 |
|
28 |
require RCSFormat; |
29 |
|
30 |
my $rcs = new RCSFormat; |
31 |
$rcs->{admin}->{strict} = 1; |
32 |
$rcs->{admin}->{comment} = '# '; |
33 |
$rcs->{admin}->{expand} = 'b'; |
34 |
|
35 |
my $text = {}; |
36 |
my $text_from = {}; |
37 |
|
38 |
#if ($ent->{s} =~ m!<title>CVS log for ([^<>]+)</title>!gc) { |
39 |
# $info->{file_path} = htunescape ($1); |
40 |
#} |
41 |
|
42 |
my $branches = {}; |
43 |
my $branch_rev = {}; |
44 |
if ($ent->{s} =~ m[View only Branch]gc) { |
45 |
while ($ent->{s} =~ m[<option value="([^"]+)"|</select]gc) { |
46 |
last unless defined $1; |
47 |
$branches->{$1} = 1; |
48 |
} |
49 |
} |
50 |
|
51 |
pos ($ent->{s}) = 0; |
52 |
|
53 |
REV: while ($ent->{s} =~ m!<hr size=1 noshade>!gc) { |
54 |
my $rev = ''; |
55 |
if ($ent->{s} =~ m!Revision!gc and |
56 |
$ent->{s} =~ m!<a href="([^"]+)"[^<>]*><b>([0-9.]+)</b></a>!gc) { |
57 |
my $uri = $dom->create_uri_reference ($1) |
58 |
->get_absolute_reference ($log_uri)->uri_reference; |
59 |
$rev = $2; |
60 |
|
61 |
my $revent = get_remote_entity ($uri); |
62 |
if (defined $revent->{s}) { |
63 |
$text->{$rev} = $revent->{s}; |
64 |
} |
65 |
|
66 |
$rcs->{admin}->{head} ||= $rev; |
67 |
} |
68 |
|
69 |
if ($ent->{s} =~ m!<i>\w+ (\w+)\s*(\d+)\s*(\d+):(\d+):(\d+) (\d+) UTC</i> \([^()]+\) by <i>([^<>]+)</i>!gc) { |
70 |
$rcs->{delta}->{$rev}->{date} = sprintf '%02d.%02d.%02d.%02d.%02d.%02d', |
71 |
$6, { |
72 |
Jan => '01', Feb => '02', Mar => '03', Apr => '04', |
73 |
May => '05', Jun => '06', Jul => '07', Aug => '08', |
74 |
Sep => '09', Oct => '10', Nov => '11', Dec => '12', |
75 |
}->{$1}, $2, $3, $4, $5; |
76 |
$rcs->{delta}->{$rev}->{author} = $7; |
77 |
} |
78 |
|
79 |
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
80 |
|
81 |
if ($ent->{s} =~ m[\G<br>Branch:((?>(?!<(?>[bh]r|pre)).)+)]gcs) { |
82 |
my $b = $1; |
83 |
while ($b =~ m!><b>([^<>]+)</b></a>!gc) { |
84 |
my $branch = $1; |
85 |
if ($branch ne 'MAIN') { |
86 |
my $rev = $rev; |
87 |
$rev =~ s/\.(\d+)\.\d+\z//; |
88 |
push @{$rcs->{admin}->{symbols} ||= []}, [$branch => "$rev.0.$1"]; |
89 |
} |
90 |
} |
91 |
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
92 |
} |
93 |
|
94 |
if ($ent->{s} =~ m[\G<br>Branch point]gcs) { |
95 |
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
96 |
} |
97 |
|
98 |
if ($ent->{s} =~ m[\G<br>CVS Tags:((?>(?!<(?>[bh]r|pre)).)+)]gcs) { |
99 |
my $b = $1; |
100 |
while ($b =~ m!><b>([^<>]+)</b></a>!gc) { |
101 |
my $rev = $branches->{$1} ? "$rev.0.".((++$branch_rev->{$rev})*2) : $rev; |
102 |
if ($1 eq 'HEAD') { |
103 |
$rcs->{admin}->{head} = $rev; |
104 |
$rcs->{deltatext}->{$rev}->{text} = $text->{$rev}; |
105 |
} else { |
106 |
push @{$rcs->{admin}->{symbols} ||= []}, [$1 => $rev]; |
107 |
} |
108 |
} |
109 |
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
110 |
} |
111 |
|
112 |
if ($ent->{s} =~ m[\G<br>Branch point]gcs) { |
113 |
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
114 |
} |
115 |
|
116 |
if ($ent->{s} =~ m!\G<br>Changes since <b>([0-9.]+):!gc) { |
117 |
my $from_rev = $1; |
118 |
if ($rev =~ /\A[0-9]+\.[0-9]+\z/) { |
119 |
$rcs->{delta}->{$rev}->{next} = $from_rev; |
120 |
$text_from->{$from_rev} = $rev; |
121 |
} elsif ($from_rev =~ /\A[0-9]+\.[0-9]+\z/) { |
122 |
push @{$rcs->{delta}->{$from_rev}->{branches} ||= []}, $rev; |
123 |
$text_from->{$rev} = $from_rev; |
124 |
} else { |
125 |
$rcs->{delta}->{$from_rev}->{next} = $rev; |
126 |
$text_from->{$rev} = $from_rev; |
127 |
} |
128 |
} |
129 |
|
130 |
if ($ent->{s} =~ m!<pre>(.*?)</pre>!gcs) { |
131 |
$rcs->{deltatext}->{$rev}->{log} = htunescape ($1); |
132 |
} |
133 |
|
134 |
$rcs->{delta}->{$rev}->{state} = 'Exp'; |
135 |
} # REV |
136 |
|
137 |
delete $text_from->{$rcs->{admin}->{head}}; |
138 |
|
139 |
my %symbol; |
140 |
for (@{$rcs->{admin}->{symbols} || []}) { |
141 |
$symbol{$_->[0]} = $_->[1]; |
142 |
} |
143 |
$rcs->{admin}->{symbols} = [map {[$_ => $symbol{$_}]} keys %symbol]; |
144 |
|
145 |
|
146 |
require File::Temp; |
147 |
require IPC::Open2; |
148 |
while (keys %$text_from) { |
149 |
my $rev = each %$text_from; |
150 |
my $any; |
151 |
if (defined $text->{$text_from->{$rev}}) { |
152 |
$any = 1; |
153 |
my $from_file = new File::Temp; |
154 |
binmode $from_file; |
155 |
$from_file->print ($text->{$text_from->{$rev}}); |
156 |
my $to_file = new File::Temp; |
157 |
binmode $to_file; |
158 |
$to_file->print ($text->{$rev}); |
159 |
my (undef, $diff_filemame) = File::Temp::tempfile |
160 |
('DIFFXXXX', DIR => File::Temp::tempdir (), OPEN => 0); |
161 |
IPC::Open2::open2 (my $diffin, my $diffout, |
162 |
$diff_command, @{$diff_options}, |
163 |
$from_file->filename => $to_file->filename) |
164 |
or die "$0: $diff_command: $!"; |
165 |
binmode $diffin; |
166 |
local $/ = undef; |
167 |
$rcs->{deltatext}->{$rev}->{text} = <$diffin>; |
168 |
delete $text_from->{$rev}; |
169 |
} |
170 |
last unless $any; |
171 |
} |
172 |
|
173 |
print $rcs->stringify; |
174 |
|
175 |
sub htunescape ($) { |
176 |
my $s = shift; |
177 |
$s =~ s!<[^<>]+>!!g; |
178 |
$s =~ s/</</g; |
179 |
$s =~ s/>/>/g; |
180 |
$s =~ s/&/&/g; |
181 |
return $s; |
182 |
} # htunescape |
183 |
|
184 |
sub get_remote_entity ($) { |
185 |
my $request_uri = $_[0]; |
186 |
my $r = {}; |
187 |
|
188 |
my $uri = $dom->create_uri_reference ($request_uri); |
189 |
unless ({ |
190 |
http => 1, |
191 |
}->{lc $uri->uri_scheme}) { |
192 |
return {uri => $request_uri, request_uri => $request_uri, |
193 |
error_status_text => 'URI scheme not allowed'}; |
194 |
} |
195 |
|
196 |
require Message::Util::HostPermit; |
197 |
my $host_permit = new Message::Util::HostPermit; |
198 |
$host_permit->add_rule (<<EOH); |
199 |
Allow host=suika port=80 |
200 |
Deny host=suika |
201 |
Allow host=suika.fam.cx port=80 |
202 |
Deny host=suika.fam.cx |
203 |
Deny host=localhost |
204 |
Deny host=*.localdomain |
205 |
Deny ipv4=0.0.0.0/8 |
206 |
Deny ipv4=10.0.0.0/8 |
207 |
Deny ipv4=127.0.0.0/8 |
208 |
Deny ipv4=169.254.0.0/16 |
209 |
Deny ipv4=172.0.0.0/11 |
210 |
Deny ipv4=192.0.2.0/24 |
211 |
Deny ipv4=192.88.99.0/24 |
212 |
Deny ipv4=192.168.0.0/16 |
213 |
Deny ipv4=198.18.0.0/15 |
214 |
Deny ipv4=224.0.0.0/4 |
215 |
Deny ipv4=255.255.255.255/32 |
216 |
Deny ipv6=0::0/0 |
217 |
Allow host=* |
218 |
EOH |
219 |
unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) { |
220 |
return {uri => $request_uri, request_uri => $request_uri, |
221 |
error_status_text => 'Connection to the host is forbidden'}; |
222 |
} |
223 |
|
224 |
require LWP::UserAgent; |
225 |
my $ua = WDCC::LWPUA->new; |
226 |
$ua->{wdcc_dom} = $dom; |
227 |
$ua->{wdcc_host_permit} = $host_permit; |
228 |
$ua->agent ('Mozilla'); ## TODO: for now. |
229 |
$ua->parse_head (0); |
230 |
$ua->protocols_allowed ([qw/http/]); |
231 |
$ua->max_size (1000_000); |
232 |
my $req = HTTP::Request->new (GET => $request_uri); |
233 |
my $res = $ua->request ($req); |
234 |
## TODO: 401 sets |is_success| true. |
235 |
if ($res->is_success) { |
236 |
$r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! |
237 |
$r->{uri} = $res->request->uri; |
238 |
$r->{request_uri} = $request_uri; |
239 |
|
240 |
## TODO: More strict parsing... |
241 |
my $ct = $res->header ('Content-Type'); |
242 |
if (defined $ct and $ct =~ m#^([0-9A-Za-z._+-]+/[0-9A-Za-z._+-]+)#) { |
243 |
$r->{media_type} = lc $1; |
244 |
} |
245 |
if (defined $ct and $ct =~ /;\s*charset\s*=\s*"?(\S+)"?/i) { |
246 |
$r->{charset} = lc $1; |
247 |
$r->{charset} =~ tr/\\//d; |
248 |
} |
249 |
|
250 |
$r->{s} = ''.$res->content; |
251 |
} else { |
252 |
$r->{uri} = $res->request->uri; |
253 |
$r->{request_uri} = $request_uri; |
254 |
$r->{error_status_text} = $res->status_line; |
255 |
} |
256 |
|
257 |
$r->{header_field} = []; |
258 |
$res->scan (sub { |
259 |
push @{$r->{header_field}}, [$_[0], $_[1]]; |
260 |
}); |
261 |
$r->{header_status_code} = $res->code; |
262 |
$r->{header_status_text} = $res->message; |
263 |
|
264 |
return $r; |
265 |
} # get_remote_entity |
266 |
|
267 |
package WDCC::LWPUA; |
268 |
BEGIN { push our @ISA, 'LWP::UserAgent'; } |
269 |
|
270 |
sub redirect_ok { |
271 |
my $ua = shift; |
272 |
unless ($ua->SUPER::redirect_ok (@_)) { |
273 |
return 0; |
274 |
} |
275 |
|
276 |
my $uris = $_[1]->header ('Location'); |
277 |
return 0 unless $uris; |
278 |
my $uri = $ua->{wdcc_dom}->create_uri_reference ($uris); |
279 |
unless ({ |
280 |
http => 1, |
281 |
}->{lc $uri->uri_scheme}) { |
282 |
return 0; |
283 |
} |
284 |
unless ($ua->{wdcc_host_permit}->check ($uri->uri_host, $uri->uri_port || 80)) { |
285 |
return 0; |
286 |
} |
287 |
return 1; |
288 |
} # redirect_ok |