晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
| DIR:/proc/thread-self/root/usr/local/share/perl5/LWP/UserAgent/DNS/ |
| Current File : //proc/thread-self/root/usr/local/share/perl5/LWP/UserAgent/DNS/Hosts.pm |
package LWP::UserAgent::DNS::Hosts;
use 5.008001;
use strict;
use warnings;
use Carp;
use LWP::Protocol;
use Scope::Guard qw(guard);
our $VERSION = '0.14';
$VERSION = eval $VERSION;
our @Protocols = qw(http https);
our %Implementors;
our %Hosts;
sub register_host {
my ($class, $host, $peer_addr) = @_;
$Hosts{$host} = $peer_addr;
}
sub register_hosts {
my ($class, %pairs) = @_;
while (my ($host, $peer_addr) = each %pairs) {
$class->register_host($host, $peer_addr);
}
}
sub clear_hosts {
%Hosts = ();
}
sub read_hosts {
my ($class, $source) = @_;
if (ref $source eq 'GLOB') {
$class->_read_hosts_from_handle($source);
}
elsif ($source !~ /[\x0D\x0A]/ && -f $source) {
$class->_read_hosts_from_file($source);
}
else {
$class->_read_hosts_from_string($source);
}
}
sub _read_hosts_from_handle {
my ($class, $handle) = @_;
while (<$handle>) {
chomp;
s/^\s+//g;
s/\s+$//g;
next if !$_ || /^#/;
my ($addr, @hosts) = split /\s+/;
for my $host (@hosts) {
$class->register_host($host, $addr);
}
}
}
sub _read_hosts_from_file {
my ($class, $file) = @_;
open my $fh, '<', $file or croak $!;
$class->_read_hosts_from_handle($fh);
close $fh;
}
sub _read_hosts_from_string {
my ($class, $string) = @_;
open my $fh, '<', \$string or croak $!;
$class->_read_hosts_from_handle($fh);
close $fh;
}
sub _registered_peer_addr {
my ($class, $host) = @_;
return $Hosts{$host};
}
sub _implementor {
my ($class, $proto) = @_;
return sprintf 'LWP::Protocol::%s::hosts' => $proto;
}
sub enable_override {
my $class = shift;
for my $proto (@Protocols) {
if (my $orig = LWP::Protocol::implementor($proto)) {
my $impl = $class->_implementor($proto);
if (eval "require $impl; 1") {
LWP::Protocol::implementor($proto => $impl);
$Implementors{$proto} = $orig;
}
}
else {
carp("LWP::Protocol::$proto is unavailable. Skip overriding it.");
}
}
if (defined wantarray) {
return guard { $class->disable_override };
}
}
sub disable_override {
my $class = shift;
for my $proto (@Protocols) {
if (my $impl = $Implementors{$proto}) {
LWP::Protocol::implementor($proto, $impl);
}
}
}
1;
=encoding utf-8
=for stopwords
=head1 NAME
LWP::UserAgent::DNS::Hosts - Override LWP HTTP/HTTPS request's host like /etc/hosts
=head1 SYNOPSIS
use LWP::UserAgent;
use LWP::UserAgent::DNS::Hosts;
# add entry
LWP::UserAgent::DNS::Hosts->register_host(
'www.cpan.org' => '127.0.0.1',
);
# add entries
LWP::UserAgent::DNS::Hosts->register_hosts(
'search.cpan.org' => '192.168.0.100',
'pause.perl.org' => '192.168.0.101',
);
# read hosts file
LWP::UserAgent::DNS::Hosts->read_hosts('/path/to/my/hosts');
LWP::UserAgent::DNS::Hosts->enable_override;
# override request hosts with peer addr defined above
my $ua = LWP::UserAgent->new;
my $res = $ua->get("http://www.cpan.org/");
print $res->content; # is same as "http://127.0.0.1/" content
=head1 DESCRIPTION
LWP::UserAgent::DNS::Hosts is a module to override HTTP/HTTPS request
peer addresses that uses LWP::UserAgent.
This module concept was got from L<LWP::Protocol::PSGI>.
=head1 METHODS
=over 4
=item register_host($host, $peer_addr)
LWP::UserAgent::DNS::Hosts->register_host($host, $peer_addr);
Registers a pair of hostname and peer ip address.
# /etc/hosts
127.0.0.1 example.com
equals to:
LWP::UserAgent::DNS::Hosts->register_hosts('example.com', '127.0.0.1');
=item register_hosts(%host_addr_pairs)
LWP::UserAgent::DNS::Hosts->register_hosts(
'example.com' => '192.168.0.1',
'example.org' => '192.168.0.2',
...
);
Registers pairs of hostname and peer ip address.
=item read_hosts($file_or_string)
LWP::UserAgent::DNS::Hosts->read_hosts('hosts.my');
LWP::UserAgent::DNS::Hosts->read_hosts(<<'__HOST__');
127.0.0.1 example.com
192.168.0.1 example.net example.org
__HOST__
Registers "/etc/hosts" syntax entries.
=item clear_hosts
Clears registered pairs.
=item enable_override
LWP::UserAgent::DNS::Hosts->enable_override;
my $guard = LWP::UserAgent::DNS::Hosts->enable_override;
Enables to override hook.
If called in a non-void context, returns a L<Guard> object that
automatically resets the override when it goes out of context.
=item disable_override
LWP::UserAgent::DNS::Hosts->disable_override;
Disables to override hook.
If you use the guard interface described above,
it will be automatically called for you.
=back
=head1 AUTHOR
NAKAGAWA Masaki E<lt>masaki@cpan.orgE<gt>
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<LWP::Protocol>, L<LWP::Protocol::http>, L<LWP::Protocol::https>
=cut
|