| 1 | package Mojo::UserAgent::Proxy; | ||||||||||||||||||||||||||||
| 2 | 24 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 24 | ▶ | use Mojo::URL; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 24 | has [qw(http https not)]; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | sub detect { | ||||||||||||||||||||||||||
|
Subroutine
detect: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 9 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 10 | 4 | ▶ | $self->http($ENV{HTTP_PROXY} || $ENV{http_proxy}); | ||||||||||||||||||||||||||
|
Condition100%3/3
$ENV{'HTTP_PROXY'} || $ENV{'http_proxy'}
MC/DC100%2/2
$ENV{'HTTP_PROXY'} || $ENV{'http_proxy'}
$ENV{'HTTP_PROXY'}
$ENV{'http_proxy'}
Truth table100%3/3
A || B
A= $ENV{'HTTP_PROXY'}B= $ENV{'http_proxy'}
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | $self->https($ENV{HTTPS_PROXY} || $ENV{https_proxy}); | ||||||||||||||||||||||||||
|
Condition100%3/3
$ENV{'HTTPS_PROXY'} || $ENV{'https_proxy'}
MC/DC100%2/2
$ENV{'HTTPS_PROXY'} || $ENV{'https_proxy'}
$ENV{'HTTPS_PROXY'}
$ENV{'https_proxy'}
Truth table100%3/3
A || B
A= $ENV{'HTTPS_PROXY'}B= $ENV{'https_proxy'}
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | return $self->not([split /,/, $ENV{NO_PROXY} || $ENV{no_proxy} || '']); | ||||||||||||||||||||||||||
|
Condition100%5/5
$ENV{'NO_PROXY'} || $ENV{'no_proxy'} || ''
$ENV{"NO_PROXY"} or $ENV{"no_proxy"}
MC/DC100%2/2
$ENV{'NO_PROXY'} || $ENV{'no_proxy'} || ''
$ENV{"NO_PROXY"}
$ENV{"no_proxy"}
Truth table100%3/3
A or B
A= $ENV{"NO_PROXY"}B= $ENV{"no_proxy"}
| |||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 171 | ▶ | sub is_needed { | ||||||||||||||||||||||||||
|
Subroutine
is_needed: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 171 | ▶ | !grep { $_[1] =~ /\Q$_\E$/ } @{$_[0]->not // []}; | ||||||||||||||||||||||||||
|
Condition100%2/2
$_[0]->not // []
MC/DC100%1/1
$_[0]->not // []
$_[0]->not
Truth table100%2/2
A
A= $_[0]->not
| |||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 160 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=7 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 160 | my ($self, $tx) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 160 | ▶ | $self->detect if $ENV{MOJO_PROXY}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 160 | my $req = $tx->req; | |||||||||||||||||||||||||||
| 24 | 160 | my $url = $req->url; | |||||||||||||||||||||||||||
| 25 | 160 | ▶ | return unless $self->is_needed($url->host); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # HTTP proxy | ||||||||||||||||||||||||||||
| 28 | 160 | my $proto = $url->protocol; | |||||||||||||||||||||||||||
| 29 | 160 | my $http = $self->http; | |||||||||||||||||||||||||||
| 30 | 160 | ▶ | $req->proxy(Mojo::URL->new($http)) if $http && $proto eq 'http'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$http and $proto eq "http"
MC/DC0%0/2
$http and $proto eq "http"
$http
$proto eq "http"
Truth table33%1/3
A and B
A= $httpB= $proto eq "http"
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # HTTPS proxy | ||||||||||||||||||||||||||||
| 33 | 160 | my $https = $self->https; | |||||||||||||||||||||||||||
| 34 | 160 | ▶ | $req->proxy(Mojo::URL->new($https)) if $https && $proto eq 'https'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$https and $proto eq "https"
MC/DC0%0/2
$https and $proto eq "https"
$https
$proto eq "https"
Truth table33%1/3
A and B
A= $httpsB= $proto eq "https"
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 24 | 1; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =encoding utf8 | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =head1 NAME | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | Mojo::UserAgent::Proxy - User agent proxy manager | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use Mojo::UserAgent::Proxy; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | my $proxy = Mojo::UserAgent::Proxy->new; | ||||||||||||||||||||||||||||
| 50 | $proxy->detect; | ||||||||||||||||||||||||||||
| 51 | say $proxy->http; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | L<Mojo::UserAgent::Proxy> manages proxy servers for L<Mojo::UserAgent>. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | L<Mojo::UserAgent::Proxy> implements the following attributes. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head2 http | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | my $http = $proxy->http; | ||||||||||||||||||||||||||||
| 64 | $proxy = $proxy->http('socks://sri:secret@127.0.0.1:8080'); | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Proxy server to use for HTTP and WebSocket requests. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 https | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | my $https = $proxy->https; | ||||||||||||||||||||||||||||
| 71 | $proxy = $proxy->https('http://sri:secret@127.0.0.1:8080'); | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Proxy server to use for HTTPS and WebSocket requests. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 not | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | my $not = $proxy->not; | ||||||||||||||||||||||||||||
| 78 | $proxy = $proxy->not(['localhost', 'intranet.mojolicious.org']); | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | Domains that don't require a proxy server to be used. | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 METHODS | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | L<Mojo::UserAgent::Proxy> inherits all methods from L<Mojo::Base> and implements the following new ones. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 detect | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | $proxy = $proxy->detect; | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Check environment variables C<HTTP_PROXY>, C<http_proxy>, C<HTTPS_PROXY>, C<https_proxy>, C<NO_PROXY> and C<no_proxy> | ||||||||||||||||||||||||||||
| 91 | for proxy information. Automatic proxy detection can be enabled with the C<MOJO_PROXY> environment variable. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =head2 is_needed | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | my $bool = $proxy->is_needed('intranet.example.com'); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Check if request for domain would use a proxy server. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 prepare | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | $proxy->prepare(Mojo::Transaction::HTTP->new); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | Prepare proxy server information for transaction. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||