| 1 | package AnyEvent::Connector; | ||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use AnyEvent::Socket (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use AnyEvent::Connector::Proxy::http; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $VERSION = "0.04"; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 19 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 19 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 15 | 19 | my $self = bless { | |||||||||||||||||||||||||||
| 16 | proxy_obj => undef, | ||||||||||||||||||||||||||||
| 17 | no_proxy => [] | ||||||||||||||||||||||||||||
| 18 | }, $class; | ||||||||||||||||||||||||||||
| 19 | 19 | $self->_env_proxy_for($args{env_proxy}); | |||||||||||||||||||||||||||
| 20 | 19 | my $proxy = $args{proxy}; | |||||||||||||||||||||||||||
| 21 | 19 | ▶ | if(defined($proxy)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 14 | $self->_set_proxy($proxy); | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | 19 | my $no_proxy = $args{no_proxy}; | |||||||||||||||||||||||||||
| 25 | 19 | ▶ | if(defined($no_proxy)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | 8 | $self->_set_no_proxy($no_proxy); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | 19 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 20 | ▶ | sub _set_proxy { | ||||||||||||||||||||||||||
|
Subroutine
_set_proxy: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||
| 32 | 20 | my ($self, $proxy) = @_; | |||||||||||||||||||||||||||
| 33 | 20 | ▶ | if($proxy eq "") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | 1 | $self->{proxy_obj} = undef; | |||||||||||||||||||||||||||
| 35 | 1 | return; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | 19 | my $proxy_uri = URI->new($proxy); | |||||||||||||||||||||||||||
| 38 | 19 | my $scheme = $proxy_uri->scheme; | |||||||||||||||||||||||||||
| 39 | 19 | ▶ | if(!defined($scheme) || $scheme ne "http") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $scheme or $scheme ne "http"
MC/DC0%0/2
not defined $scheme or $scheme ne "http"
not defined $scheme
$scheme ne "http"
Truth table33%1/3
not(A) or B
A= not defined $schemeB= $scheme ne "http"
| |||||||||||||||||||||||||||||
| 40 | 0 | croak "Only http proxy is supported: $proxy"; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 19 | $self->{proxy_obj} = AnyEvent::Connector::Proxy::http->new($proxy_uri); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 14 | ▶ | sub _set_no_proxy { | ||||||||||||||||||||||||||
|
Subroutine
_set_no_proxy: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 46 | 14 | my ($self, $no_proxy) = @_; | |||||||||||||||||||||||||||
| 47 | 14 | my $ref = ref($no_proxy); | |||||||||||||||||||||||||||
| 48 | 14 | ▶ | if($ref eq "ARRAY") { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 49 | ; | ||||||||||||||||||||||||||||
| 50 | }elsif(!$ref) { | ||||||||||||||||||||||||||||
| 51 | 6 | $no_proxy = [$no_proxy]; | |||||||||||||||||||||||||||
| 52 | }else { | ||||||||||||||||||||||||||||
| 53 | 0 | croak "no_proxy expects STRING or ARRAYREF, but it was $ref"; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 14 | $self->{no_proxy} = [grep {$_ ne ""} @$no_proxy]; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 19 | ▶ | sub _env_proxy_for { | ||||||||||||||||||||||||||
|
Subroutine
_env_proxy_for: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 59 | 19 | my ($self, $protocol) = @_; | |||||||||||||||||||||||||||
| 60 | 19 | ▶ | return if !defined($protocol); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 7 | $self->_env_no_proxy(); | |||||||||||||||||||||||||||
| 62 | 7 | my @keys = (lc($protocol) . "_proxy", uc($protocol) . "_PROXY"); | |||||||||||||||||||||||||||
| 63 | 7 | foreach my $key (@keys) { | |||||||||||||||||||||||||||
| 64 | 8 | my $p = $ENV{$key}; | |||||||||||||||||||||||||||
| 65 | 8 | ▶ | if(defined($p)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 6 | $self->_set_proxy($p); | |||||||||||||||||||||||||||
| 67 | 6 | return; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | ▶ | sub _env_no_proxy { | ||||||||||||||||||||||||||
|
Subroutine
_env_no_proxy: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 73 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 74 | 7 | foreach my $key (qw(no_proxy NO_PROXY)) { | |||||||||||||||||||||||||||
| 75 | 8 | my $no_proxy = $ENV{$key}; | |||||||||||||||||||||||||||
| 76 | 8 | ▶ | if(defined($no_proxy)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 6 | $self->_set_no_proxy([split /\s*,\s*/, $no_proxy]); | |||||||||||||||||||||||||||
| 78 | 6 | return; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 55 | ▶ | sub _proxy_uri_for { | ||||||||||||||||||||||||||
|
Subroutine
_proxy_uri_for: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 84 | 55 | my ($self, $host, $port) = @_; | |||||||||||||||||||||||||||
| 85 | 55 | foreach my $no_domain (@{$self->{no_proxy}}) { | |||||||||||||||||||||||||||
| 86 | 33 | ▶ | if($host =~ /\Q$no_domain\E$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 12 | return undef; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 43 | return $self->{proxy_obj}; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 51 | ▶ | sub proxy_for { | ||||||||||||||||||||||||||
|
Subroutine
proxy_for: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 51 | my ($self, $host, $port) = @_; | |||||||||||||||||||||||||||
| 95 | 51 | my $p = $self->_proxy_uri_for($host, $port); | |||||||||||||||||||||||||||
| 96 | 51 | ▶ | return defined($p) ? $p->uri_string : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 4 | ▶ | sub tcp_connect { | ||||||||||||||||||||||||||
|
Subroutine
tcp_connect: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | 4 | my ($self, $host, $port, $connect_cb, $prepare_cb) = @_; | |||||||||||||||||||||||||||
| 101 | 4 | my $proxy = $self->_proxy_uri_for($host, $port); | |||||||||||||||||||||||||||
| 102 | 4 | ▶ | if(!defined($proxy)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return AnyEvent::Socket::tcp_connect $host, $port, $connect_cb, $prepare_cb; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 4 | ▶ | return AnyEvent::Socket::tcp_connect $proxy->host, $proxy->port, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 106 | 4 | my ($fh, $conn_host, $conn_port, $retry) = @_; | |||||||||||||||||||||||||||
| 107 | 4 | ▶ | if(!defined($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | $connect_cb->(); | |||||||||||||||||||||||||||
| 109 | 1 | return; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | $proxy->establish_proxy($fh, $host, $port, sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 112 | 3 | my ($success) = @_; | |||||||||||||||||||||||||||
| 113 | 3 | ▶ | $connect_cb->($success ? ($fh, $conn_host, $conn_port, $retry) : ()); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 3 | }); | |||||||||||||||||||||||||||
| 115 | 4 | }, $prepare_cb; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 4 | 1; | |||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||