| 1 | package HTTP::AnyUA::Backend::AnyEvent::HTTP; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A unified programming interface for AnyEvent::HTTP | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.903'; # VERSION | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use parent 'HTTP::AnyUA::Backend'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use HTTP::AnyUA::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 0 | ▶ | sub options { @_ == 2 ? $_[0]->{options} = pop : $_[0]->{options} } | ||||||||||||||||||||||||||
|
Subroutine
options: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub response_is_future { 1 } | ||||||||||||||||||||||||||
|
Subroutine
response_is_future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub request { | ||||||||||||||||||||||||||||
| 22 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 0 | my ($method, $url, $args) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | my %opts = $self->_munge_request($method, $url, $args); | |||||||||||||||||||||||||||
| 26 | 0 | my $future = Future->new; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | require AnyEvent::HTTP; | |||||||||||||||||||||||||||
| 29 | AnyEvent::HTTP::http_request($method => $url, %opts, sub { | ||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $resp = $self->_munge_response(@_, $args->{data_callback}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ($resp->{success}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | $future->done($resp); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | else { | ||||||||||||||||||||||||||||
| 36 | 0 | $future->fail($resp); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | }); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | return $future; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | sub _munge_request { | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_munge_request: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 46 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 47 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 48 | 0 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my %opts = %{$self->options || {}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if (my $headers = $args->{headers}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | # munge headers | ||||||||||||||||||||||||||||
| 54 | 0 | my %headers; | |||||||||||||||||||||||||||
| 55 | 0 | for my $header (keys %$headers) { | |||||||||||||||||||||||||||
| 56 | 0 | my $value = $headers->{$header}; | |||||||||||||||||||||||||||
| 57 | 0 | ▶ | $value = join(', ', @$value) if ref($value) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | $headers{$header} = $value; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 0 | $opts{headers} = \%headers; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | my @url_parts = HTTP::AnyUA::Util::split_url($url); | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | if (my $auth = $url_parts[4] and !$opts{headers}{'authorization'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
my $auth = $url_parts[4] and not $opts{"headers"}{"authorization"}
MC/DC0%0/2
my $auth = $url_parts[4] and not $opts{"headers"}{"authorization"}
my $auth = $url_parts[4]
not $opts{"headers"}{"authorization"}
Truth table0%0/3
A and not(B)
A= my $auth = $url_parts[4]B= not $opts{"headers"}{"authorization"}
| |||||||||||||||||||||||||||||
| 65 | # handle auth in the URL | ||||||||||||||||||||||||||||
| 66 | 0 | require MIME::Base64; | |||||||||||||||||||||||||||
| 67 | 0 | $opts{headers}{'authorization'} = 'Basic ' . MIME::Base64::encode_base64($auth, ''); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $content = HTTP::AnyUA::Util::coderef_content_to_string($args->{content}); | |||||||||||||||||||||||||||
| 71 | 0 | ▶ | $opts{body} = $content if $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (my $data_cb = $args->{data_callback}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | # stream the response | ||||||||||||||||||||||||||||
| 75 | $opts{on_body} = sub { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $data = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | $data_cb->($data, $self->_munge_response(undef, @_)); | |||||||||||||||||||||||||||
| 78 | 0 | 1; # continue | |||||||||||||||||||||||||||
| 79 | 0 | }; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | return %opts; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _munge_response { | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_munge_response: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 87 | 0 | my $data = shift; | |||||||||||||||||||||||||||
| 88 | 0 | my $headers = shift; | |||||||||||||||||||||||||||
| 89 | 0 | my $data_cb = shift; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # copy headers because http_request will continue to use the original | ||||||||||||||||||||||||||||
| 92 | 0 | my %headers = %$headers; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | my $code = delete $headers{Status}; | |||||||||||||||||||||||||||
| 95 | 0 | my $reason = delete $headers{Reason}; | |||||||||||||||||||||||||||
| 96 | 0 | my $url = delete $headers{URL}; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $resp = { | ||||||||||||||||||||||||||
|
Condition0%0/3
200 <= $code && $code <= 299
MC/DC0%0/2
200 <= $code && $code <= 299
200 <= $code
$code <= 299
Truth table0%0/3
A && B
A= 200 <= $codeB= $code <= 299
| |||||||||||||||||||||||||||||
| 99 | success => 200 <= $code && $code <= 299, | ||||||||||||||||||||||||||||
| 100 | url => $url, | ||||||||||||||||||||||||||||
| 101 | status => $code, | ||||||||||||||||||||||||||||
| 102 | reason => $reason, | ||||||||||||||||||||||||||||
| 103 | headers => \%headers, | ||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | my $version = delete $headers{HTTPVersion}; | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | $resp->{protocol} = "HTTP/$version" if $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $resp->{content} = $data if $data && !$data_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$data and not $data_cb
MC/DC0%0/2
$data and not $data_cb
$data
not $data_cb
Truth table0%0/3
A and not(B)
A= $dataB= not $data_cb
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my @redirects; | |||||||||||||||||||||||||||
| 112 | 0 | my $redirect = delete $headers{Redirect}; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | while ($redirect) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | # delete pseudo-header first so redirects aren't recursively munged | ||||||||||||||||||||||||||||
| 115 | 0 | my $next = delete $redirect->[1]{Redirect}; | |||||||||||||||||||||||||||
| 116 | 0 | unshift @redirects, $self->_munge_response(@$redirect); | |||||||||||||||||||||||||||
| 117 | 0 | $redirect = $next; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | 0 | ▶ | $resp->{redirects} = \@redirects if @redirects; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (590 <= $code && $code <= 599) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
590 <= $code and $code <= 599
MC/DC0%0/2
590 <= $code and $code <= 599
590 <= $code
$code <= 599
Truth table0%0/3
A and B
A= 590 <= $codeB= $code <= 599
| |||||||||||||||||||||||||||||
| 122 | 0 | HTTP::AnyUA::Util::internal_exception($reason, $resp); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | return $resp; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 1 | 1; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | __END__ | ||||||||||||||||||||||||||||