| 1 | package HTTP::AnyUA::Backend::Mojo::UserAgent; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A unified programming interface for Mojo::UserAgent | ||||||||||||||||||||||||||||
| 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 Scalar::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | ▶ | sub response_is_future { 1 } | ||||||||||||||||||||||||||
|
Subroutine
response_is_future: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub request { | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 0 | my ($method, $url, $args) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | my $future = Future->new; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | my $tx = $self->_munge_request(@_); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | $self->ua->start($tx => sub { | ||||||||||||||||||||||||||||
| 27 | 0 | ▶ | my $ua = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 28 | 0 | my $tx = shift; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my $resp = $self->_munge_response($tx, $args->{data_callback}); | |||||||||||||||||||||||||||
| 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=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 46 | 0 | my $method = shift; | |||||||||||||||||||||||||||
| 47 | 0 | my $url = shift; | |||||||||||||||||||||||||||
| 48 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | my $headers = $args->{headers} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'headers'} || {}
MC/DC0%0/1
$args->{'headers'} || {}
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
| |||||||||||||||||||||||||||||
| 51 | 0 | my $content = $args->{content}; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | my @content; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | my $content_length; | ||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ($content) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | for my $header (keys %$headers) { | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (lc($header) eq 'content-length') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $content_length = $headers->{$header}; | |||||||||||||||||||||||||||
| 60 | 0 | last; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # if we don't know the length we have to just read it all in | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | $content = HTTP::AnyUA::Util::coderef_content_to_string($content) if !$content_length; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | push @content, $content if ref($content) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | my $tx = $self->ua->build_tx($method => $url => $headers => @content); | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (ref($content) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $tx->req->headers->content_length($content_length); | |||||||||||||||||||||||||||
| 74 | # stream the request | ||||||||||||||||||||||||||||
| 75 | 0 | my $drain; | |||||||||||||||||||||||||||
| 76 | $drain = sub { | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $body = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | my $chunk = $content->() || ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
&$content() || ''
MC/DC0%0/1
&$content() || ''
&$content()
Truth table0%0/2
A
A= &$content()
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | undef $drain if !$chunk; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | $body->write($chunk, $drain); | |||||||||||||||||||||||||||
| 81 | 0 | }; | |||||||||||||||||||||||||||
| 82 | 0 | $tx->req->content->$drain; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if (my $data_cb = $args->{data_callback}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | # stream the response | ||||||||||||||||||||||||||||
| 87 | 0 | my $tx_copy = $tx; | |||||||||||||||||||||||||||
| 88 | 0 | Scalar::Util::weaken($tx_copy); | |||||||||||||||||||||||||||
| 89 | $tx->res->content->unsubscribe('read')->on(read => sub { | ||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my ($content, $bytes) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 91 | 0 | my $resp = $self->_munge_response($tx_copy, undef); | |||||||||||||||||||||||||||
| 92 | 0 | $data_cb->($bytes, $resp); | |||||||||||||||||||||||||||
| 93 | 0 | }); | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | return $tx; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _munge_response { | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_munge_response: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 101 | 0 | my $tx = shift; | |||||||||||||||||||||||||||
| 102 | 0 | my $data_cb = shift; | |||||||||||||||||||||||||||
| 103 | 0 | my $recurse = shift; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | my $resp = { | |||||||||||||||||||||||||||
| 106 | success => !!$tx->res->is_success, | ||||||||||||||||||||||||||||
| 107 | url => $tx->req->url->to_string, | ||||||||||||||||||||||||||||
| 108 | status => $tx->res->code, | ||||||||||||||||||||||||||||
| 109 | reason => $tx->res->message, | ||||||||||||||||||||||||||||
| 110 | headers => {}, | ||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # lowercase header keys | ||||||||||||||||||||||||||||
| 114 | 0 | my $headers = $tx->res->headers->to_hash; | |||||||||||||||||||||||||||
| 115 | 0 | for my $header (keys %$headers) { | |||||||||||||||||||||||||||
| 116 | 0 | $resp->{headers}{lc($header)} = delete $headers->{$header}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $version = $tx->res->version; | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | $resp->{protocol} = "HTTP/$version" if $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if (!$recurse) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | for my $redirect (@{$tx->redirects}) { | |||||||||||||||||||||||||||
| 124 | 0 | ▶ | push @{$resp->{redirects} ||= []}, $self->_munge_response($redirect, undef, 1); | ||||||||||||||||||||||||||
|
Condition0%0/2
$resp->{"redirects"} ||= []
MC/DC0%0/1
$resp->{"redirects"} ||= []
$resp->{"redirects"}
Truth table0%0/2
A
A= $resp->{"redirects"}
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | my $err = $tx->error; | |||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($err && !$err->{code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err and not $err->{"code"}
MC/DC0%0/2
$err and not $err->{"code"}
$err
not $err->{"code"}
Truth table0%0/3
A and not(B)
A= $errB= not $err->{"code"}
| |||||||||||||||||||||||||||||
| 130 | 0 | return HTTP::AnyUA::Util::internal_exception($err->{message}, $resp); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | my $body = $tx->res->body; | |||||||||||||||||||||||||||
| 134 | 0 | ▶ | $resp->{content} = $body if $body && !$data_cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$body and not $data_cb
MC/DC0%0/2
$body and not $data_cb
$body
not $data_cb
Truth table0%0/3
A and not(B)
A= $bodyB= not $data_cb
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return $resp; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | 1; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | __END__ | ||||||||||||||||||||||||||||