-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRewrite.pm
101 lines (75 loc) · 2.3 KB
/
Rewrite.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Alien::Build::Plugin::Fetch::Rewrite;
use strict;
use warnings;
use 5.008001;
use URI;
use Alien::Build;
use Alien::Build::Plugin;
use Alien::Build::Plugin::Fetch::LWP;
use Alien::Build::Plugin::Decode::HTML;
# ABSTRACT: Alien::Build plugin to rewrite network requests to local resources
# VERSION
=head1 SYNOPSIS
In your ~/.alienbuild/rc.pl:
postload 'Fetch::Rewrite';
sub rewrite {
my($build, $uri) = @_;
# $build isa Alien::Build
# $uri isa URI
if($uri->host eq 'ftp.gnu.org')
{
# if we see a request to ftp.gnu.org (either ftp or http)
# we redirect it to the local mirror at
# http://mirror.example.com/ftp.gnu.org
$uri->scheme('http');
$uri->host('mirror.example.com');
$uri->host('/ftp.gnu.org' . $uri->path);
}
}
1;
=head1 DESCRIPTION
This plugin allows you to rewrite the URLs for remote networked resources
to local resources. This is useful if you are building CPAN modules that
rely on L<Alien> distributions where you do not have system packages. It
may also seem useful if you do not trust the remote resources, although
please keep in mind that like a C<Makefile.PL> or C<Build.PL>, an L<alienfile>
is arbitrary Perl code, and should be appropriately vetted before being
used in an environment with security requirements.
=head1 CAVEATS
This plugin is only able to rewrite URLs that are fetched through the standard
L<Alien::Build> URL fetching interface, and only URLs that are supported by
L<LWP::UserAgent> and L<URI>.
=cut
sub init
{
my($self, $meta) = @_;
unless($meta->prop->{start_url})
{
Alien::Build->log("sorry! this plugin requires a default url to function");
Alien::Build->log("no rewrites will be possible");
return;
}
Alien::Build::Plugin::Fetch::LWP->new->init($meta);
Alien::Build::Plugin::Decode::HTML->new->init($meta);
$meta->around_hook(fetch => sub {
my($f, $build, $url) = @_;
$url ||= $build->meta_prop->{start_url};
if(Alien::Build::rc->can('rewrite'))
{
my $orig = URI->new($url);
my $copy = $orig->clone;
Alien::Build::rc::rewrite($build, $copy);
if("$copy" ne "$orig")
{
$build->log("rewriting $orig as $copy");
$url = "$copy";
}
}
return $f->($build, $url);
});
}
1;
=head1 SEE ALSO
=over 4
=item L<Alien::Build>
=back