1 |
wakaba |
1.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="([^"]+)"\s*><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 |
|
|
unless (defined $rcs->{admin}->{head}) { |
67 |
|
|
$rcs->{admin}->{head} = $rev; |
68 |
|
|
$rcs->{deltatext}->{$rev}->{text} = $text->{$rev}; |
69 |
|
|
} |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
if ($ent->{s} =~ m!<i>\w+ (\w+)\s*(\d+)\s*(\d+):(\d+):(\d+) (\d+) UTC</i> \([^()]+\) by <i>([^<>]+)</i>!gc) { |
73 |
|
|
$rcs->{delta}->{$rev}->{date} = sprintf '%02d.%02d.%02d.%02d.%02d.%02d', |
74 |
|
|
$6, { |
75 |
|
|
Jan => '01', Feb => '02', Mar => '03', Apr => '04', |
76 |
|
|
May => '05', Jun => '06', Jul => '07', Aug => '08', |
77 |
|
|
Sep => '09', Oct => '10', Nov => '11', Dec => '12', |
78 |
|
|
}->{$1}, $2, $3, $4, $5; |
79 |
|
|
$rcs->{delta}->{$rev}->{author} = $7; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
83 |
|
|
|
84 |
|
|
if ($ent->{s} =~ m[\G<br>Branch:((?>(?!<(?>[bh]r|pre)).)+)]gcs) { |
85 |
|
|
my $b = $1; |
86 |
|
|
while ($b =~ m!><b>([^<>]+)</b></a>!gc) { |
87 |
|
|
push @{$rcs->{delta}->{$rev}->{branches} ||= []}, $1 |
88 |
|
|
unless $1 eq 'MAIN'; |
89 |
|
|
} |
90 |
|
|
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
91 |
|
|
} |
92 |
|
|
|
93 |
|
|
if ($ent->{s} =~ m[\G<br>CVS Tags:((?>(?!<(?>[bh]r|pre)).)+)]gcs) { |
94 |
|
|
my $b = $1; |
95 |
|
|
while ($b =~ m!><b>([^<>]+)</b></a>!gc) { |
96 |
|
|
my $rev = $branches->{$1} ? "$rev.0.".((++$branch_rev->{$rev})*2) : $rev; |
97 |
|
|
push @{$rcs->{admin}->{symbols} ||= []}, [$1 => $rev] |
98 |
|
|
unless $1 eq 'HEAD'; ## TODO |
99 |
|
|
} |
100 |
|
|
$ent->{s} =~ m[(?=<(?>br|pre)>)]gc; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
if ($ent->{s} =~ m!\G<br>Changes since <b>([0-9.]+):!gc) { |
104 |
|
|
$rcs->{delta}->{$rev}->{next} = $1; |
105 |
|
|
$text_from->{$1} = $rev; |
106 |
|
|
} |
107 |
|
|
|
108 |
|
|
if ($ent->{s} =~ m!<pre>(.*?)</pre>!gcs) { |
109 |
|
|
$rcs->{deltatext}->{$rev}->{log} = htunescape ($1); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
$rcs->{delta}->{$rev}->{state} = 'Exp'; |
113 |
|
|
} # REV |
114 |
|
|
|
115 |
|
|
require File::Temp; |
116 |
|
|
require IPC::Open2; |
117 |
|
|
while (keys %$text_from) { |
118 |
|
|
my $rev = each %$text_from; |
119 |
|
|
if (defined $text->{$text_from->{$rev}}) { |
120 |
|
|
my $from_file = new File::Temp; |
121 |
|
|
binmode $from_file; |
122 |
|
|
$from_file->print ($text->{$text_from->{$rev}}); |
123 |
|
|
my $to_file = new File::Temp; |
124 |
|
|
binmode $to_file; |
125 |
|
|
$to_file->print ($text->{$rev}); |
126 |
|
|
my (undef, $diff_filemame) = File::Temp::tempfile |
127 |
|
|
('DIFFXXXX', DIR => File::Temp::tempdir (), OPEN => 0); |
128 |
|
|
IPC::Open2::open2 (my $diffin, my $diffout, |
129 |
|
|
$diff_command, @{$diff_options}, |
130 |
|
|
$from_file->filename => $to_file->filename) |
131 |
|
|
or die "$0: $diff_command: $!"; |
132 |
|
|
binmode $diffin; |
133 |
|
|
local $/ = undef; |
134 |
|
|
$rcs->{deltatext}->{$rev}->{text} = <$diffin>; |
135 |
|
|
delete $text_from->{$rev}; |
136 |
|
|
} |
137 |
|
|
} |
138 |
|
|
|
139 |
|
|
print $rcs->stringify; |
140 |
|
|
|
141 |
|
|
sub htunescape ($) { |
142 |
|
|
my $s = shift; |
143 |
|
|
$s =~ s!<[^<>]+>!!g; |
144 |
|
|
$s =~ s/</</g; |
145 |
|
|
$s =~ s/>/>/g; |
146 |
|
|
$s =~ s/&/&/g; |
147 |
|
|
return $s; |
148 |
|
|
} # htunescape |
149 |
|
|
|
150 |
|
|
sub get_remote_entity ($) { |
151 |
|
|
my $request_uri = $_[0]; |
152 |
|
|
my $r = {}; |
153 |
|
|
|
154 |
|
|
my $uri = $dom->create_uri_reference ($request_uri); |
155 |
|
|
unless ({ |
156 |
|
|
http => 1, |
157 |
|
|
}->{lc $uri->uri_scheme}) { |
158 |
|
|
return {uri => $request_uri, request_uri => $request_uri, |
159 |
|
|
error_status_text => 'URI scheme not allowed'}; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
require Message::Util::HostPermit; |
163 |
|
|
my $host_permit = new Message::Util::HostPermit; |
164 |
|
|
$host_permit->add_rule (<<EOH); |
165 |
|
|
Allow host=suika port=80 |
166 |
|
|
Deny host=suika |
167 |
|
|
Allow host=suika.fam.cx port=80 |
168 |
|
|
Deny host=suika.fam.cx |
169 |
|
|
Deny host=localhost |
170 |
|
|
Deny host=*.localdomain |
171 |
|
|
Deny ipv4=0.0.0.0/8 |
172 |
|
|
Deny ipv4=10.0.0.0/8 |
173 |
|
|
Deny ipv4=127.0.0.0/8 |
174 |
|
|
Deny ipv4=169.254.0.0/16 |
175 |
|
|
Deny ipv4=172.0.0.0/11 |
176 |
|
|
Deny ipv4=192.0.2.0/24 |
177 |
|
|
Deny ipv4=192.88.99.0/24 |
178 |
|
|
Deny ipv4=192.168.0.0/16 |
179 |
|
|
Deny ipv4=198.18.0.0/15 |
180 |
|
|
Deny ipv4=224.0.0.0/4 |
181 |
|
|
Deny ipv4=255.255.255.255/32 |
182 |
|
|
Deny ipv6=0::0/0 |
183 |
|
|
Allow host=* |
184 |
|
|
EOH |
185 |
|
|
unless ($host_permit->check ($uri->uri_host, $uri->uri_port || 80)) { |
186 |
|
|
return {uri => $request_uri, request_uri => $request_uri, |
187 |
|
|
error_status_text => 'Connection to the host is forbidden'}; |
188 |
|
|
} |
189 |
|
|
|
190 |
|
|
require LWP::UserAgent; |
191 |
|
|
my $ua = WDCC::LWPUA->new; |
192 |
|
|
$ua->{wdcc_dom} = $dom; |
193 |
|
|
$ua->{wdcc_host_permit} = $host_permit; |
194 |
|
|
$ua->agent ('Mozilla'); ## TODO: for now. |
195 |
|
|
$ua->parse_head (0); |
196 |
|
|
$ua->protocols_allowed ([qw/http/]); |
197 |
|
|
$ua->max_size (1000_000); |
198 |
|
|
my $req = HTTP::Request->new (GET => $request_uri); |
199 |
|
|
my $res = $ua->request ($req); |
200 |
|
|
## TODO: 401 sets |is_success| true. |
201 |
|
|
if ($res->is_success) { |
202 |
|
|
$r->{base_uri} = $res->base; ## NOTE: It does check |Content-Base|, |Content-Location|, and <base>. ## TODO: Use our own code! |
203 |
|
|
$r->{uri} = $res->request->uri; |
204 |
|
|
$r->{request_uri} = $request_uri; |
205 |
|
|
|
206 |
|
|
## TODO: More strict parsing... |
207 |
|
|
my $ct = $res->header ('Content-Type'); |
208 |
|
|
if (defined $ct and $ct =~ m#^([0-9A-Za-z._+-]+/[0-9A-Za-z._+-]+)#) { |
209 |
|
|
$r->{media_type} = lc $1; |
210 |
|
|
} |
211 |
|
|
if (defined $ct and $ct =~ /;\s*charset\s*=\s*"?(\S+)"?/i) { |
212 |
|
|
$r->{charset} = lc $1; |
213 |
|
|
$r->{charset} =~ tr/\\//d; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
$r->{s} = ''.$res->content; |
217 |
|
|
} else { |
218 |
|
|
$r->{uri} = $res->request->uri; |
219 |
|
|
$r->{request_uri} = $request_uri; |
220 |
|
|
$r->{error_status_text} = $res->status_line; |
221 |
|
|
} |
222 |
|
|
|
223 |
|
|
$r->{header_field} = []; |
224 |
|
|
$res->scan (sub { |
225 |
|
|
push @{$r->{header_field}}, [$_[0], $_[1]]; |
226 |
|
|
}); |
227 |
|
|
$r->{header_status_code} = $res->code; |
228 |
|
|
$r->{header_status_text} = $res->message; |
229 |
|
|
|
230 |
|
|
return $r; |
231 |
|
|
} # get_remote_entity |
232 |
|
|
|
233 |
|
|
package WDCC::LWPUA; |
234 |
|
|
BEGIN { push our @ISA, 'LWP::UserAgent'; } |
235 |
|
|
|
236 |
|
|
sub redirect_ok { |
237 |
|
|
my $ua = shift; |
238 |
|
|
unless ($ua->SUPER::redirect_ok (@_)) { |
239 |
|
|
return 0; |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
my $uris = $_[1]->header ('Location'); |
243 |
|
|
return 0 unless $uris; |
244 |
|
|
my $uri = $ua->{wdcc_dom}->create_uri_reference ($uris); |
245 |
|
|
unless ({ |
246 |
|
|
http => 1, |
247 |
|
|
}->{lc $uri->uri_scheme}) { |
248 |
|
|
return 0; |
249 |
|
|
} |
250 |
|
|
unless ($ua->{wdcc_host_permit}->check ($uri->uri_host, $uri->uri_port || 80)) { |
251 |
|
|
return 0; |
252 |
|
|
} |
253 |
|
|
return 1; |
254 |
|
|
} # redirect_ok |