1 | package Mojo::UserAgent::Proxy;
|
---|
2 | use Mojo::Base -base;
|
---|
3 |
|
---|
4 | use Mojo::URL;
|
---|
5 |
|
---|
6 | has [qw(http https not)];
|
---|
7 |
|
---|
8 | sub detect {
|
---|
9 | my $self = shift;
|
---|
10 | $self->http($ENV{HTTP_PROXY} || $ENV{http_proxy});
|
---|
11 | $self->https($ENV{HTTPS_PROXY} || $ENV{https_proxy});
|
---|
12 | return $self->not([split ',', $ENV{NO_PROXY} || $ENV{no_proxy} || '']);
|
---|
13 | }
|
---|
14 |
|
---|
15 | sub is_needed {
|
---|
16 | !grep { $_[1] =~ /\Q$_\E$/ } @{$_[0]->not || []};
|
---|
17 | }
|
---|
18 |
|
---|
19 | sub prepare {
|
---|
20 | my ($self, $tx) = @_;
|
---|
21 |
|
---|
22 | $self->detect if $ENV{MOJO_PROXY};
|
---|
23 | my $req = $tx->req;
|
---|
24 | my $url = $req->url;
|
---|
25 | return unless $self->is_needed($url->host);
|
---|
26 |
|
---|
27 | # HTTP proxy
|
---|
28 | my $proto = $url->protocol;
|
---|
29 | my $http = $self->http;
|
---|
30 | $req->proxy(Mojo::URL->new($http)) if $http && $proto eq 'http';
|
---|
31 |
|
---|
32 | # HTTPS proxy
|
---|
33 | my $https = $self->https;
|
---|
34 | $req->proxy(Mojo::URL->new($https)) if $https && $proto eq 'https';
|
---|
35 | }
|
---|
36 |
|
---|
37 | 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:[email protected]: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:[email protected]: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
|
---|
85 | implements the following new ones.
|
---|
86 |
|
---|
87 | =head2 detect
|
---|
88 |
|
---|
89 | $proxy = $proxy->detect;
|
---|
90 |
|
---|
91 | Check environment variables C<HTTP_PROXY>, C<http_proxy>, C<HTTPS_PROXY>,
|
---|
92 | C<https_proxy>, C<NO_PROXY> and C<no_proxy> for proxy information. Automatic
|
---|
93 | proxy detection can be enabled with the C<MOJO_PROXY> environment variable.
|
---|
94 |
|
---|
95 | =head2 is_needed
|
---|
96 |
|
---|
97 | my $bool = $proxy->is_needed('intranet.example.com');
|
---|
98 |
|
---|
99 | Check if request for domain would use a proxy server.
|
---|
100 |
|
---|
101 | =head2 prepare
|
---|
102 |
|
---|
103 | $proxy->prepare(Mojo::Transaction::HTTP->new);
|
---|
104 |
|
---|
105 | Prepare proxy server information for transaction.
|
---|
106 |
|
---|
107 | =head1 SEE ALSO
|
---|
108 |
|
---|
109 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
110 |
|
---|
111 | =cut
|
---|