Mojolicious

 view release on metacpan or  search on metacpan

examples/proxy.pl  view on Meta::CPAN

#
# A simple HTTP proxy server for debugging
#
#   $ HTTP_PROXY=http://127.0.0.1:3000 mojo get http://mojolicious.org
#
use Mojolicious::Lite -signatures;

any '/*whatever' => {whatever => ''} => sub ($c) {
  my $req     = $c->req;
  my $method  = $req->method;
  my $url     = $req->url->to_abs;
  my $headers = $req->headers->clone->dehop->to_hash;
  $c->app->log->debug(qq{Forwarding "$method $url"});

lib/Mojo/UserAgent/Proxy.pm  view on Meta::CPAN

package Mojo::UserAgent::Proxy;
use Mojo::Base -base;

use Mojo::URL;

has [qw(http https not)];

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

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

sub prepare {
  my ($self, $tx) = @_;

lib/Mojo/UserAgent/Proxy.pm  view on Meta::CPAN

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

=head1 METHODS

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

=head2 detect

  $proxy = $proxy->detect;

Check environment variables C<HTTP_PROXY>, C<http_proxy>, C<HTTPS_PROXY>, C<https_proxy>, C<NO_PROXY> and C<no_proxy>
for proxy information. Automatic proxy detection can be enabled with the C<MOJO_PROXY> environment variable.

=head2 is_needed

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

Check if request for domain would use a proxy server.

=head2 prepare

t/mojo/proxy.t  view on Meta::CPAN

use Mojo::Base -strict;

use Test::More;
use Mojo::UserAgent::Proxy;

# Proxy detection
subtest 'Proxy detection with uppercase variable names' => sub {
  my $proxy = Mojo::UserAgent::Proxy->new;
  local $ENV{HTTP_PROXY}  = 'http://127.0.0.1';
  local $ENV{HTTPS_PROXY} = 'http://127.0.0.1:8080';
  local $ENV{NO_PROXY}    = 'mojolicious.org';
  $proxy->detect;
  is $proxy->http,  'http://127.0.0.1',      'right proxy';
  is $proxy->https, 'http://127.0.0.1:8080', 'right proxy';
  $proxy->http(undef);
  $proxy->https(undef);
  is $proxy->http,  undef, 'right proxy';
  is $proxy->https, undef, 'right proxy';
  ok !$proxy->is_needed('dummy.mojolicious.org'), 'no proxy needed';
  ok $proxy->is_needed('icious.org'),             'proxy needed';
  ok $proxy->is_needed('localhost'),              'proxy needed';
};

subtest 'Proxy detection with lowercase variable names' => sub {
  local $ENV{HTTP_PROXY};
  local $ENV{HTTPS_PROXY};
  local $ENV{NO_PROXY};

  local $ENV{http_proxy}  = 'proxy.example.com';
  local $ENV{https_proxy} = 'tunnel.example.com';
  local $ENV{no_proxy}    = 'localhost,localdomain,foo.com,example.com';

  my $proxy = Mojo::UserAgent::Proxy->new;
  $proxy->detect;
  is_deeply $proxy->not, ['localhost', 'localdomain', 'foo.com', 'example.com'], 'right list';



( run in 0.813 second using v1.01-cache-2.11-cpan-71847e10f99 )