Codebase list libhttp-async-perl / e08378f
Imported Upstream version 0.16 Florian Schlichting 10 years ago
17 changed file(s) with 201 addition(s) and 67 deletion(s). Raw diff Collapse all Expand all
00 CHANGES to HTTP::Async
1
2 0.16 - 2013/04/04
3
4 * Fixed CPAN Testers bug in bad-hosts.t
5
6 0.15 - 2013/04/04
7
8 * Two bug fixes provided by Josef Toman:
9 * Fixed header handling to use header_field_names()
10 * Replaced _make_url_absolute with URI::new_abs()
11
12 0.14 - 2013/04/01
13
14 * More diagnostics in bad-hosts.t on failure
15
16 0.13 - 2013/03/29
17
18 * Fixed t/real-servers.t to work whether or not Net::HTTPS::NB is available
19
20 0.12 - 2013/03/29
21
22 * New logic for making https requests through a proxy
23 * Made tests run ok in parallel by using different ports per test
24 * Set explicit SSL_verify_mode in real-servers.t
25 * Minor update to code comment about is_proxy mode
126
227 0.11 - 2012/11/13
328
7297
7398 0.01 - XXXXXXXXXX - svn r24
7499
75 * Initial release onto CPAN.
100 * Initial release onto CPAN.
2222 t/TestServer.pm
2323 t/timeout.t
2424 TODO
25 META.yml Module meta-data (added by MakeMaker)
25 META.yml Module YAML meta-data (added by MakeMaker)
26 META.json Module JSON meta-data (added by MakeMaker)
0 {
1 "abstract" : "unknown",
2 "author" : [
3 "unknown"
4 ],
5 "dynamic_config" : 1,
6 "generated_by" : "ExtUtils::MakeMaker version 6.64, CPAN::Meta::Converter version 2.120921",
7 "license" : [
8 "perl_5"
9 ],
10 "meta-spec" : {
11 "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec",
12 "version" : "2"
13 },
14 "name" : "HTTP-Async",
15 "no_index" : {
16 "directory" : [
17 "t",
18 "inc"
19 ]
20 },
21 "prereqs" : {
22 "build" : {
23 "requires" : {
24 "ExtUtils::MakeMaker" : "0"
25 }
26 },
27 "configure" : {
28 "requires" : {
29 "ExtUtils::MakeMaker" : "0"
30 }
31 },
32 "runtime" : {
33 "requires" : {
34 "Carp" : "0",
35 "Data::Dumper" : "0",
36 "HTTP::Request" : "0",
37 "HTTP::Response" : "0",
38 "HTTP::Server::Simple::CGI" : "0",
39 "HTTP::Status" : "0",
40 "IO::Select" : "0",
41 "LWP::UserAgent" : "0",
42 "Net::HTTP" : "0",
43 "Net::HTTP::NB" : "0",
44 "Test::HTTP::Server::Simple" : "0",
45 "Test::More" : "0",
46 "Time::HiRes" : "0",
47 "URI" : "0",
48 "URI::Escape" : "0"
49 }
50 }
51 },
52 "release_status" : "stable",
53 "version" : "0.16"
54 }
0 --- #YAML:1.0
1 name: HTTP-Async
2 version: 0.11
3 abstract: ~
4 author: []
5 license: perl
6 distribution_type: module
0 ---
1 abstract: unknown
2 author:
3 - unknown
4 build_requires:
5 ExtUtils::MakeMaker: 0
76 configure_requires:
8 ExtUtils::MakeMaker: 0
9 build_requires:
10 ExtUtils::MakeMaker: 0
7 ExtUtils::MakeMaker: 0
8 dynamic_config: 1
9 generated_by: 'ExtUtils::MakeMaker version 6.64, CPAN::Meta::Converter version 2.120921'
10 license: perl
11 meta-spec:
12 url: http://module-build.sourceforge.net/META-spec-v1.4.html
13 version: 1.4
14 name: HTTP-Async
15 no_index:
16 directory:
17 - t
18 - inc
1119 requires:
12 Carp: 0
13 Data::Dumper: 0
14 HTTP::Request: 0
15 HTTP::Response: 0
16 HTTP::Server::Simple::CGI: 0
17 HTTP::Status: 0
18 IO::Select: 0
19 LWP::UserAgent: 0
20 Net::HTTP: 0
21 Net::HTTP::NB: 0
22 Test::HTTP::Server::Simple: 0
23 Test::More: 0
24 Time::HiRes: 0
25 URI: 0
26 URI::Escape: 0
27 no_index:
28 directory:
29 - t
30 - inc
31 generated_by: ExtUtils::MakeMaker version 6.56
32 meta-spec:
33 url: http://module-build.sourceforge.net/META-spec-v1.4.html
34 version: 1.4
20 Carp: 0
21 Data::Dumper: 0
22 HTTP::Request: 0
23 HTTP::Response: 0
24 HTTP::Server::Simple::CGI: 0
25 HTTP::Status: 0
26 IO::Select: 0
27 LWP::UserAgent: 0
28 Net::HTTP: 0
29 Net::HTTP::NB: 0
30 Test::HTTP::Server::Simple: 0
31 Test::More: 0
32 Time::HiRes: 0
33 URI: 0
34 URI::Escape: 0
35 version: 0.16
2929 * Add 'info' sub that can be linked to SIGINFO to provide a summary of what is
3030 going on eg "print $async->info( 'terse' )".
3131
32 * Add logic to TestServer::new() to choose a different port when it finds the
33 one it wants is already in use. Related: make tests which refer to port
34 numbers outside of their call to TestServer::new() get them from the
35 TestServer object instead of hard-coding them.
22
33 package HTTP::Async;
44
5 our $VERSION = '0.11';
5 our $VERSION = '0.16';
66
77 use Carp;
88 use Data::Dumper;
649649 $args{PeerPort} ||= $uri->port;
650650
651651 my $net_http_class = 'Net::HTTP::NB';
652 if ($uri->scheme and $uri->scheme eq 'https') {
652 if ($uri->scheme and $uri->scheme eq 'https' and not $request_is_to_proxy) {
653653 $net_http_class = 'Net::HTTPS::NB';
654654 eval {
655655 require Net::HTTPS::NB;
656656 Net::HTTPS::NB->import();
657657 };
658658 die "$net_http_class must be installed for https support" if $@;
659 }
660 elsif($uri->scheme and $uri->scheme eq 'https' and $request_is_to_proxy) {
661 # We are making an HTTPS request through an HTTP proxy such as squid.
662 # The proxy will handle the HTTPS, we need to connect to it via HTTP
663 # and then make a request where the https is clear from the scheme...
664 $args{Host} = sprintf(
665 '%s:%s',
666 delete @args{'PeerAddr', 'PeerPort'}
667 );
659668 }
660669 my $s = eval { $net_http_class->new(%args) };
661670
674683 return 1;
675684 }
676685
677 my %headers = %{ $request->{_headers} };
686 my %headers;
687 for my $key ($request->{_headers}->header_field_names) {
688 $headers{$key} = $request->header($key);
689 }
678690
679691 # Decide what to use as the request_uri
680692 my $request_uri = $request_is_to_proxy # is this a proxy request....
731743 my $in = $args{url};
732744 my $ref = $args{ref};
733745
734 return $in if $in =~ m{ \A http:// }xms;
735
736 my $ret = $ref->scheme . '://' . $ref->authority;
737 return $ret . $in if $in =~ m{ \A / }xms;
738
739 $ret .= $ref->path;
740 return $ret . $in if $in =~ m{ \A [\?\#\;] }xms;
741
742 $ret =~ s{ [^/]+ \z }{}xms;
743 return $ret . $in;
746 return URI->new_abs($in, $ref)->as_string;
744747 }
745748
746749 sub _add_error_response_to_return {
782785 handle 304 responses.
783786
784787 Naveed Massjouni for adding the https handling code.
788
789 Alex Balhatchet for adding the https + proxy handling code, and for making the
790 tests run ok in parallel.
791
792 Josef Toman for fixing two bugs, one related to header handling and another
793 related to producing an absolute URL correctly.
785794
786795 =head1 BUGS AND REPO
787796
1111 sub new {
1212 my ($class, $port) = @_;
1313
14 $port ||= 10_249; # randomish port
14 # Require a port parameter to be passed in.
15 # Any default here would mean the tests don't run properly in parallel.
16 if (!$port) {
17 die "Missing positional parameter 'port' required";
18 }
1519
1620 return $class->SUPER::new($port);
1721 }
2024 my ( $self, $cgi ) = @_;
2125 my $params = $cgi->Vars;
2226
23 # If we are on port 8081 then we are a proxy - we should forward the
24 # requests.
27 # If we should act as a proxy then the handle_request() behaviour is
28 # handled by act_as_proxy.
2529 return act_as_proxy(@_) if $self->{is_proxy};
2630
2731 # We should act as a final destination server and so expect an absolute URL.
134138 # that this is the case.
135139 #
136140 # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.2
137 if ( $request_uri !~ m!^http://! ) {
141 if ( $request_uri !~ m!^https?://! ) {
138142 warn "ERROR - not fully qualified request_uri '$request_uri'";
139143 return;
140144 }
55 use HTTP::Request;
66
77 require 't/TestServer.pm';
8 my $s = TestServer->new;
8 my $s = TestServer->new(80900);
99 my $url_root = $s->started_ok("starting a test server");
1010
1111 use HTTP::Async;
55 use HTTP::Request;
66
77 require 't/TestServer.pm';
8 my $s = TestServer->new;
8 my $s = TestServer->new(80300);
99 my $url_root = $s->started_ok("starting a test server");
1010
1111 use HTTP::Async;
11 use strict;
22 use warnings;
33
4 use Test::More tests => 9;
4 use Test::More;
55
66 use HTTP::Request;
7 use LWP::UserAgent;
78
89 use HTTP::Async;
910 my $q = HTTP::Async->new;
1011
12 # Some weird ISPs or DNS providers take an address like http://i.dont.exist/
13 # and resolve it to something "useful" such as
14 # http://navigationshilfe1.t-online.de/dnserror?url=http://i.dont.exist/
15 #
16 # If that's happening then let's just give up on this test entirely.
17 {
18 my $ua = LWP::UserAgent->new;
19 if ($ua->get('http://i.dont.exist/foo/bar')->is_success) {
20 plan skip_all => 'http://i.dont.exist/foo/bar resolved to something!';
21 exit;
22 }
23 }
24
1125 # Try to add some requests for bad hosts. HTTP::Async should not fail
1226 # but should return HTTP::Responses with the correct status code etc.
27
28 plan tests => 9;
1329
1430 my @bad_requests =
1531 map { HTTP::Request->new( GET => $_ ) }
2036 while ( $q->not_empty ) {
2137 my $res = $q->next_response || next;
2238
23 isa_ok $res, 'HTTP::Response', "Got a proper response";
24 ok !$res->is_success, "Response was not a success";
25 ok $res->is_error, "Response was an error";
26 ok $res->request, "response has a request attached.";
39 my $request_uri = $res->request->uri;
40
41 isa_ok($res, 'HTTP::Response', "$request_uri - Got a proper response")
42 || diag sprintf("ref: %s", ref $res);
43
44 ok(!$res->is_success, "$request_uri - Response was not a success")
45 || diag sprintf("%s: %s", $res->code, $res->decoded_content);
46
47 ok($res->is_error, "$request_uri - Response was an error")
48 || diag sprintf("%s: %s", $res->code, $res->decoded_content);
49
50 ok $res->request, "$request_uri - Response has a request attached";
2751 }
66
77 require 't/TestServer.pm';
88
9 my $s = TestServer->new;
9 my $s = TestServer->new(81000);
1010 my $url_root = $s->started_ok("starting a test server");
1111
1212 my $q = HTTP::Async->new;
1010 }
1111
1212 require 't/TestServer.pm';
13 my $s = TestServer->new;
13 my $s = TestServer->new(80100);
1414 my $url_root = $s->started_ok("starting a test server");
1515
1616 use HTTP::Async;
66 use HTTP::Request;
77
88 require 't/TestServer.pm';
9 my $s1 = TestServer->new(10249);
9 my $s1 = TestServer->new(80500);
1010 my $s1_url_root = $s1->started_ok("starting a test server");
1111
12 my $s2 = TestServer->new(10250);
12 my $s2 = TestServer->new(80501);
1313 $s2->{is_proxy} = 1;
1414 my $s2_url_root = $s2->started_ok("starting a test server");
1515
2929
3030 my $req = HTTP::Request->new( 'GET', $url );
3131
32 my %opts = ( proxy_host => '127.0.0.1', proxy_port => 10250, );
32 my %opts = ( proxy_host => '127.0.0.1', proxy_port => 80501, );
3333
3434 my $id =
3535 $via_proxy
88 use HTTP::Request;
99 use Time::HiRes 'usleep';
1010
11 my $https_ok;
12 eval "use Net::HTTPS::NB";
13 if ($@) {
14 note "Install Net::HTTPS::NB to test https";
15 }
16 else {
17 $https_ok = 1;
18 IO::Socket::SSL::set_defaults(SSL_verify_mode => 0); # SSL_VERIFY_NONE
19 }
20
1121 # Create requests for a few well known sites.
1222 my @requests =
1323 map { HTTP::Request->new( GET => $_ ) }
24 grep { $https_ok || $_ !~ m{^https://} }
1425 sort qw( http://www.google.com http://www.yahoo.com https://www.gandi.net/ );
1526
1627 my $tests_per_request = 4;
55 use HTTP::Request;
66
77 require 't/TestServer.pm';
8 my $s = TestServer->new;
8 my $s = TestServer->new(80200);
99 my $url_root = $s->started_ok("starting a test server");
1010
1111 use HTTP::Async;
1313 # my $s = TestServer->new;
1414 # my $url_root = $s->started_ok("starting a test server");
1515
16 my @servers = map { TestServer->new($_) } 80800 .. 80804;
16 my @servers = map { TestServer->new($_) } 80600 .. 80604;
1717 foreach my $s (@servers) {
1818 my $url_root = $s->started_ok("starting a test server");
1919 }
55 use HTTP::Request;
66
77 require 't/TestServer.pm';
8 my $s = TestServer->new;
8 my $s = TestServer->new(80700);
99 my $url_root = $s->started_ok("starting a test server");
1010
1111 use HTTP::Async;