Newer Older
109 lines | 2.347kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::UserAgent::Proxy;
2
use Mojo::Base -base;
3

            
4
has [qw(http https not)];
5

            
6
sub detect {
7
  my $self = shift;
8
  $self->http($ENV{HTTP_PROXY}   || $ENV{http_proxy});
9
  $self->https($ENV{HTTPS_PROXY} || $ENV{https_proxy});
10
  return $self->not([split /,/, $ENV{NO_PROXY} || $ENV{no_proxy} || '']);
11
}
12

            
13
sub inject {
14
  my ($self, $tx) = @_;
15

            
16
  $self->detect if $ENV{MOJO_PROXY};
17
  my $req = $tx->req;
18
  my $url = $req->url;
19
  return if !$self->is_needed($url->host) || defined $req->proxy;
20

            
21
  # HTTP proxy
22
  my $proto = $url->protocol;
23
  my $http  = $self->http;
24
  $req->proxy($http) if $http && $proto eq 'http';
25

            
26
  # HTTPS proxy
27
  my $https = $self->https;
28
  $req->proxy($https) if $https && $proto eq 'https';
29
}
30

            
31
sub is_needed {
32
  !grep { $_[1] =~ /\Q$_\E$/ } @{$_[0]->not || []};
33
}
34

            
35
1;
36

            
37
=encoding utf8
38

            
39
=head1 NAME
40

            
41
Mojo::UserAgent::Proxy - User agent proxy manager
42

            
43
=head1 SYNOPSIS
44

            
45
  use Mojo::UserAgent::Proxy;
46

            
47
  my $proxy = Mojo::UserAgent::Proxy->new;
48
  $proxy->detect;
49
  say $proxy->http;
50

            
51
=head1 DESCRIPTION
52

            
53
L<Mojo::UserAgent::Proxy> manages proxy servers for L<Mojo::UserAgent>.
54

            
55
=head1 ATTRIBUTES
56

            
57
L<Mojo::UserAgent::Proxy> implements the following attributes.
58

            
59
=head2 http
60

            
61
  my $http = $ua->http;
62
  $ua      = $ua->http('http://sri:secret@127.0.0.1:8080');
63

            
64
Proxy server to use for HTTP and WebSocket requests.
65

            
66
=head2 https
67

            
68
  my $https = $ua->https;
69
  $ua       = $ua->https('http://sri:secret@127.0.0.1:8080');
70

            
71
Proxy server to use for HTTPS and WebSocket requests.
72

            
73
=head2 not
74

            
75
  my $not = $proxy->not;
76
  $ua     = $proxy->not([qw(localhost intranet.mojolicio.us)]);
77

            
78
Domains that don't require a proxy server to be used.
79

            
80
=head1 METHODS
81

            
82
L<Mojo::UserAgent::Proxy> inherits all methods from L<Mojo::Base> and
83
implements the following new ones.
84

            
85
=head2 detect
86

            
87
  $proxy = $proxy->detect;
88

            
89
Check environment variables HTTP_PROXY, http_proxy, HTTPS_PROXY, https_proxy,
90
NO_PROXY and no_proxy for proxy information. Automatic proxy detection can be
91
enabled with the MOJO_PROXY environment variable.
92

            
93
=head2 inject
94

            
95
  $proxy->inject(Mojo::Transaction::HTTP->new);
96

            
97
Inject proxy server information into transaction.
98

            
99
=head2 is_needed
100

            
101
  my $bool = $proxy->is_needed('intranet.example.com');
102

            
103
Check if request for domain would use a proxy server.
104

            
105
=head1 SEE ALSO
106

            
107
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
108

            
109
=cut