use strict;
use warnings;
# ABSTRACT: A small, simple, correct HTTP/1.1 client
-our $VERSION = '0.031'; # VERSION
+our $VERSION = '0.033'; # VERSION
use Carp ();
my @attributes;
BEGIN {
- @attributes = qw(agent cookie_jar default_headers local_address max_redirect max_size proxy timeout SSL_options verify_SSL);
+ @attributes = qw(cookie_jar default_headers local_address max_redirect max_size proxy no_proxy timeout SSL_options verify_SSL);
no strict 'refs';
for my $accessor ( @attributes ) {
*{$accessor} = sub {
}
}
+sub agent {
+ my($self, $agent) = @_;
+ if( @_ > 1 ){
+ $self->{agent} =
+ (defined $agent && $agent =~ / $/) ? $agent . $self->_agent : $agent;
+ }
+ return $self->{agent};
+}
+
sub new {
my($class, %args) = @_;
- (my $default_agent = $class) =~ s{::}{-}g;
- $default_agent .= "/" . ($class->VERSION || 0);
-
my $self = {
- agent => $default_agent,
max_redirect => 5,
timeout => 60,
verify_SSL => $args{verify_SSL} || $args{verify_ssl} || 0, # no verification by default
+ no_proxy => $ENV{no_proxy},
};
- $args{agent} .= $default_agent
- if defined $args{agent} && $args{agent} =~ / $/;
+ bless $self, $class;
$class->_validate_cookie_jar( $args{cookie_jar} ) if $args{cookie_jar};
$self->{$key} = $args{$key} if exists $args{$key}
}
+ $self->agent( exists $args{agent} ? $args{agent} : $class->_agent );
+
# Never override proxy argument as this breaks backwards compat.
if (!exists $self->{proxy} && (my $http_proxy = $ENV{http_proxy})) {
if ($http_proxy =~ m{\Ahttp://[^/?#:@]+:\d+/?\z}) {
}
}
- return bless $self, $class;
+ # Split no_proxy to array reference if not provided as such
+ unless ( ref $self->{no_proxy} eq 'ARRAY' ) {
+ $self->{no_proxy} =
+ (defined $self->{no_proxy}) ? [ split /\s*,\s*/, $self->{no_proxy} ] : [];
+ }
+
+ return $self;
}
https => 443,
);
+sub _agent {
+ my $class = ref($_[0]) || $_[0];
+ (my $default_agent = $class) =~ s{::}{-}g;
+ return $default_agent . "/" . ($class->VERSION || 0);
+}
+
sub _request {
my ($self, $method, $url, $args) = @_;
local_address => $self->{local_address},
);
- if ($self->{proxy}) {
+ if ($self->{proxy} && ! grep { $host =~ /\Q$_\E$/ } @{$self->{no_proxy}}) {
$request->{uri} = "$scheme://$request->{host_port}$path_query";
die(qq/HTTPS via proxy is not supported\n/)
if $request->{scheme} eq 'https';
=head1 VERSION
-version 0.031
+version 0.033
=head1 SYNOPSIS
=item *
+C<no_proxy>
+
+List of domain suffixes that should not be proxied. Must be a comma-separated string or an array reference. (default is C<$ENV{no_proxy}>)
+
+=item *
+
C<timeout>
Request timeout in seconds (default is 60)
max_redirect
max_size
proxy
+no_proxy
timeout
verify_SSL
SSL_options
=item *
+C<no_proxy> environment variable is supported in the format comma-separated
+list of domain extensions proxy should not be used for. If a C<no_proxy>
+argument is passed to C<new>, then the C<no_proxy> environment variable is
+ignored.
+
+=item *
+
There is no provision for delaying a request body using an C<Expect> header.
Unexpected C<1XX> responses are silently ignored as per the specification.
=item *
-L<LWP::UserAgent>
+L<HTTP::Thin> - HTTP::Tiny wrapper with L<HTTP::Request>/L<HTTP::Response> compatibility
+
+=item *
+
+L<HTTP::Tiny::Mech> - Wrap L<WWW::Mechanize> instance in HTTP::Tiny compatible interface
=item *
-L<IO::Socket::SSL>
+L<IO::Socket::SSL> - Required for SSL support
=item *
-L<Mozilla::CA>
+L<LWP::UserAgent> - If HTTP::Tiny isn't enough for you, this is the "standard" way to do things
=item *
-L<Net::SSLeay>
+L<Mozilla::CA> - Required if you want to validate SSL certificates
+
+=item *
+
+L<Net::SSLeay> - Required for SSL support
=back
=item *
+Brad Gilbert <bgills@cpan.org>
+
+=item *
+
Chris Nehren <apeiron@cpan.org>
=item *
=item *
+Syohei YOSHIDA <syohex@gmail.com>
+
+=item *
+
Tony Cook <tony@develop-help.com>
=back
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 8;
use HTTP::Tiny;
# a couple tests to ensure that we get the default agent expected, the coorect
'agent string is as properly appended to',
;
}
+
+{
+ my $ua = HTTP::Tiny->new();
+
+ is( HTTP::Tiny->_agent(), $default, 'check _agent on class' );
+ is $ua->_agent(), $default, 'check _agent on object';
+
+ $ua->agent(undef);
+ is $ua->agent, undef, 'agent string is empty';
+
+ $ua->agent('something else');
+ is $ua->agent, 'something else', 'agent string is as expected';
+
+ $ua->agent('something else ');
+ is
+ $ua->agent,
+ "something else $default",
+ 'agent string is as properly appended to',
+ ;
+}
--- /dev/null
+#!perl
+use strict;
+use warnings;
+
+use Test::More 0.88;
+
+use HTTP::Tiny;
+
+# blank slate for testing
+delete $ENV{no_proxy};
+
+{
+ my $c = HTTP::Tiny->new();
+ is_deeply( $c->no_proxy, [], "no no_proxy given" );
+}
+
+my @cases = (
+ #<<< No perltidy
+ {
+ no_proxy => [
+ undef,
+ [],
+ ],
+ expect => [],
+ },
+ {
+ no_proxy => [
+ "localhost",
+ ["localhost"],
+ ],
+ expect => ["localhost"],
+ },
+ {
+ no_proxy => [
+ "localhost,example.com",
+ "localhost, example.com",
+ [qw/localhost example.com/]
+ ],
+ expect => [ "localhost", "example.com" ],
+ },
+ #>>>
+);
+
+for my $c (@cases) {
+ for my $no_proxy ( @{ $c->{no_proxy} } ) {
+ my $label =
+ !defined $no_proxy ? 'undef'
+ : ref $no_proxy ? "[qw/@$no_proxy/]"
+ : "'$no_proxy'";
+
+ # Can't test environment with array ref (ENV stringifies in new perls)
+ if ( ref $no_proxy ) {
+ my $ht = HTTP::Tiny->new( no_proxy => $no_proxy );
+ is_deeply( $ht->no_proxy, $c->{expect}, "new(no_proxy => $label)" );
+ }
+ else {
+ {
+ local $ENV{no_proxy} = $no_proxy;
+ my $ht = HTTP::Tiny->new();
+ is_deeply( $ht->no_proxy, $c->{expect}, "\$ENV{no_proxy} = $label" );
+ }
+ {
+ local $ENV{no_proxy} = "Shouldnt,see,this";
+ my $ht = HTTP::Tiny->new( no_proxy => $no_proxy );
+ is_deeply( $ht->no_proxy, $c->{expect},
+ "new(no_proxy => $label) versus other \$ENV{no_proxy}" );
+ }
+ }
+ }
+}
+
+done_testing();