| 1 | package LWP::UserAgent::DNS::Hosts; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use LWP::Protocol; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use Scope::Guard qw(guard); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | our $VERSION = '0.14'; | |||||||||||||||||||||||||||
| 11 | 5 | $VERSION = eval $VERSION; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | our @Protocols = qw(http https); | |||||||||||||||||||||||||||
| 14 | 5 | our %Implementors; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | our %Hosts; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 15 | ▶ | sub register_host { | ||||||||||||||||||||||||||
|
Subroutine
register_host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 15 | my ($class, $host, $peer_addr) = @_; | |||||||||||||||||||||||||||
| 20 | 15 | $Hosts{$host} = $peer_addr; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | sub register_hosts { | ||||||||||||||||||||||||||
|
Subroutine
register_hosts: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 2 | my ($class, %pairs) = @_; | |||||||||||||||||||||||||||
| 25 | 2 | ▶ | while (my ($host, $peer_addr) = each %pairs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 4 | $class->register_host($host, $peer_addr); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 6 | ▶ | sub clear_hosts { | ||||||||||||||||||||||||||
|
Subroutine
clear_hosts: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 6 | %Hosts = (); | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 3 | ▶ | sub read_hosts { | ||||||||||||||||||||||||||
|
Subroutine
read_hosts: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 35 | 3 | my ($class, $source) = @_; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3 | ▶ | if (ref $source eq 'GLOB') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 38 | 1 | $class->_read_hosts_from_handle($source); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | ▶ | elsif ($source !~ /[\x0D\x0A]/ && -f $source) { | |||||||||||||||||||||||||||
|
Condition66%2/3
not $source =~ /[\x0D\x0A]/ and -f $source
MC/DC50%1/2
not $source =~ /[\x0D\x0A]/ and -f $source
not $source =~ /[\x0D\x0A]/
-f $source
Truth table66%2/3
not(A) and B
A= not $source =~ /[\x0D\x0A]/B= -f $source
| |||||||||||||||||||||||||||||
| 41 | 1 | $class->_read_hosts_from_file($source); | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | else { | ||||||||||||||||||||||||||||
| 44 | 1 | $class->_read_hosts_from_string($source); | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | ▶ | sub _read_hosts_from_handle { | ||||||||||||||||||||||||||
|
Subroutine
_read_hosts_from_handle: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 49 | 3 | my ($class, $handle) = @_; | |||||||||||||||||||||||||||
| 50 | 3 | ▶ | while (<$handle>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 15 | chomp; | |||||||||||||||||||||||||||
| 52 | 15 | s/^\s+//g; | |||||||||||||||||||||||||||
| 53 | 15 | s/\s+$//g; | |||||||||||||||||||||||||||
| 54 | 15 | ▶ | next if !$_ || /^#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $_ or /^#/
MC/DC100%2/2
not $_ or /^#/
not $_
/^#/
Truth table100%3/3
not(A) or B
A= not $_B= /^#/
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | my ($addr, @hosts) = split /\s+/; | |||||||||||||||||||||||||||
| 57 | 6 | for my $host (@hosts) { | |||||||||||||||||||||||||||
| 58 | 9 | $class->register_host($host, $addr); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | sub _read_hosts_from_file { | ||||||||||||||||||||||||||
|
Subroutine
_read_hosts_from_file: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 64 | 1 | my ($class, $file) = @_; | |||||||||||||||||||||||||||
| 65 | 1 | ▶ | open my $fh, '<', $file or croak $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 1 | $class->_read_hosts_from_handle($fh); | |||||||||||||||||||||||||||
| 67 | 1 | close $fh; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | sub _read_hosts_from_string { | ||||||||||||||||||||||||||
|
Subroutine
_read_hosts_from_string: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 71 | 1 | my ($class, $string) = @_; | |||||||||||||||||||||||||||
| 72 | 1 | ▶ | open my $fh, '<', \$string or croak $!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | $class->_read_hosts_from_handle($fh); | |||||||||||||||||||||||||||
| 74 | 1 | close $fh; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 4 | ▶ | sub _registered_peer_addr { | ||||||||||||||||||||||||||
|
Subroutine
_registered_peer_addr: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 4 | my ($class, $host) = @_; | |||||||||||||||||||||||||||
| 79 | 4 | return $Hosts{$host}; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub _implementor { | ||||||||||||||||||||||||||
|
Subroutine
_implementor: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 83 | 2 | my ($class, $proto) = @_; | |||||||||||||||||||||||||||
| 84 | 2 | return sprintf 'LWP::Protocol::%s::hosts' => $proto; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 1 | ▶ | sub enable_override { | ||||||||||||||||||||||||||
|
Subroutine
enable_override: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | for my $proto (@Protocols) { | |||||||||||||||||||||||||||
| 91 | 2 | ▶ | if (my $orig = LWP::Protocol::implementor($proto)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 2 | my $impl = $class->_implementor($proto); | |||||||||||||||||||||||||||
| 93 | 2 | ▶ | if (eval "require $impl; 1") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | LWP::Protocol::implementor($proto => $impl); | |||||||||||||||||||||||||||
| 95 | 2 | $Implementors{$proto} = $orig; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||
| 99 | 0 | carp("LWP::Protocol::$proto is unavailable. Skip overriding it."); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 1 | ▶ | return guard { $class->disable_override }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 1 | ▶ | sub disable_override { | ||||||||||||||||||||||||||
|
Subroutine
disable_override: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 110 | 1 | for my $proto (@Protocols) { | |||||||||||||||||||||||||||
| 111 | 2 | ▶ | if (my $impl = $Implementors{$proto}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 2 | LWP::Protocol::implementor($proto, $impl); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =for stopwords | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =head1 NAME | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | LWP::UserAgent::DNS::Hosts - Override LWP HTTP/HTTPS request's host like /etc/hosts | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | use LWP::UserAgent; | ||||||||||||||||||||||||||||
| 130 | use LWP::UserAgent::DNS::Hosts; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # add entry | ||||||||||||||||||||||||||||
| 133 | LWP::UserAgent::DNS::Hosts->register_host( | ||||||||||||||||||||||||||||
| 134 | 'www.cpan.org' => '127.0.0.1', | ||||||||||||||||||||||||||||
| 135 | ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # add entries | ||||||||||||||||||||||||||||
| 138 | LWP::UserAgent::DNS::Hosts->register_hosts( | ||||||||||||||||||||||||||||
| 139 | 'search.cpan.org' => '192.168.0.100', | ||||||||||||||||||||||||||||
| 140 | 'pause.perl.org' => '192.168.0.101', | ||||||||||||||||||||||||||||
| 141 | ); | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # read hosts file | ||||||||||||||||||||||||||||
| 144 | LWP::UserAgent::DNS::Hosts->read_hosts('/path/to/my/hosts'); | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | LWP::UserAgent::DNS::Hosts->enable_override; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | # override request hosts with peer addr defined above | ||||||||||||||||||||||||||||
| 149 | my $ua = LWP::UserAgent->new; | ||||||||||||||||||||||||||||
| 150 | my $res = $ua->get("http://www.cpan.org/"); | ||||||||||||||||||||||||||||
| 151 | print $res->content; # is same as "http://127.0.0.1/" content | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | LWP::UserAgent::DNS::Hosts is a module to override HTTP/HTTPS request | ||||||||||||||||||||||||||||
| 156 | peer addresses that uses LWP::UserAgent. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | This module concept was got from L<LWP::Protocol::PSGI>. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head1 METHODS | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =over 4 | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | =item register_host($host, $peer_addr) | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | LWP::UserAgent::DNS::Hosts->register_host($host, $peer_addr); | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Registers a pair of hostname and peer ip address. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # /etc/hosts | ||||||||||||||||||||||||||||
| 171 | 127.0.0.1 example.com | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | equals to: | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | LWP::UserAgent::DNS::Hosts->register_hosts('example.com', '127.0.0.1'); | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item register_hosts(%host_addr_pairs) | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | LWP::UserAgent::DNS::Hosts->register_hosts( | ||||||||||||||||||||||||||||
| 180 | 'example.com' => '192.168.0.1', | ||||||||||||||||||||||||||||
| 181 | 'example.org' => '192.168.0.2', | ||||||||||||||||||||||||||||
| 182 | ... | ||||||||||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Registers pairs of hostname and peer ip address. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =item read_hosts($file_or_string) | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | LWP::UserAgent::DNS::Hosts->read_hosts('hosts.my'); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | LWP::UserAgent::DNS::Hosts->read_hosts(<<'__HOST__'); | ||||||||||||||||||||||||||||
| 192 | 127.0.0.1 example.com | ||||||||||||||||||||||||||||
| 193 | 192.168.0.1 example.net example.org | ||||||||||||||||||||||||||||
| 194 | __HOST__ | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | Registers "/etc/hosts" syntax entries. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =item clear_hosts | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Clears registered pairs. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item enable_override | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | LWP::UserAgent::DNS::Hosts->enable_override; | ||||||||||||||||||||||||||||
| 205 | my $guard = LWP::UserAgent::DNS::Hosts->enable_override; | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Enables to override hook. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | If called in a non-void context, returns a L<Guard> object that | ||||||||||||||||||||||||||||
| 210 | automatically resets the override when it goes out of context. | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =item disable_override | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | LWP::UserAgent::DNS::Hosts->disable_override; | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Disables to override hook. | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | If you use the guard interface described above, | ||||||||||||||||||||||||||||
| 219 | it will be automatically called for you. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =back | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 230 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | L<LWP::Protocol>, L<LWP::Protocol::http>, L<LWP::Protocol::https> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||