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