| 1 | package Net::Curl::Parallel::Response; | ||||||||||||
| 2 | |||||||||||||
| 3 | 1 | ▶ | use strictures 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use HTTP::Response; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use HTTP::Parser::XS qw(parse_http_response HEADERS_AS_HASHREF); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | has completed => (is => 'rw'); | |||||||||||
| 9 | 1 | has status => (is => 'ro'); | |||||||||||
| 10 | 4 | ▶ | has headers => (is => 'ro', default => sub{{}}); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 1 | has error => (is => 'ro'); | |||||||||||
| 12 | 1 | has raw_body => (is => 'ro'); | |||||||||||
| 13 | 1 | has raw_head => (is => 'ro'); | |||||||||||
| 14 | 1 | has fh_body => (is => 'ro'); | |||||||||||
| 15 | 1 | has fh_head => (is => 'ro'); | |||||||||||
| 16 | |||||||||||||
| 17 | 4 | ▶ | sub BUILD { | ||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 18 | 4 | my $self = shift; | |||||||||||
| 19 | 4 | $self->{completed} = 0; | |||||||||||
| 20 | 4 | $self->{raw_body} = ''; | |||||||||||
| 21 | 4 | $self->{raw_head} = ''; | |||||||||||
| 22 | 4 | open $self->{fh_body}, '>', \$self->{raw_body}; | |||||||||||
| 23 | 4 | open $self->{fh_head}, '>', \$self->{raw_head}; | |||||||||||
| 24 | } | ||||||||||||
| 25 | |||||||||||||
| 26 | 1 | ▶ | sub content { goto \&raw_body } # alias to mimic HTTP::Response | ||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 27 | 0 | ▶ | sub failed { !!$_[0]->error } | ||||||||||
|
Subroutine
failed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 28 | |||||||||||||
| 29 | 2 | ▶ | sub complete { | ||||||||||
|
Subroutine
complete: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 30 | 2 | my $self = shift; | |||||||||||
| 31 | |||||||||||||
| 32 | 2 | ▶ | return $self->fail('incomplete message') | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 33 | unless $self->raw_head; | ||||||||||||
| 34 | |||||||||||||
| 35 | 1 | my ($ret, $min, $status, $msg, $hdrs) | |||||||||||
| 36 | = parse_http_response($self->raw_head, HEADERS_AS_HASHREF); | ||||||||||||
| 37 | |||||||||||||
| 38 | 1 | $self->{status} = $status; | |||||||||||
| 39 | |||||||||||||
| 40 | # parse_http_response() will always return a hashref in the headers. | ||||||||||||
| 41 | # But, good coding says guard against randomness. | ||||||||||||
| 42 | # uncoverable condition right | ||||||||||||
| 43 | 1 | ▶ | $self->{headers} = $hdrs // {}; | ||||||||||
|
Condition100%1/2
$hdrs // {}
MC/DC100%0/1
$hdrs // {}
$hdrs
Truth table100%1/2
A
A= $hdrs
| |||||||||||||
| 44 | |||||||||||||
| 45 | 1 | $self->close_handles; | |||||||||||
| 46 | 1 | $self->completed(1); | |||||||||||
| 47 | |||||||||||||
| 48 | 1 | return 1; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | 3 | ▶ | sub fail { | ||||||||||
|
Subroutine
fail: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 52 | 3 | my ($self, $msg) = @_; | |||||||||||
| 53 | 3 | $self->{error} = $msg; | |||||||||||
| 54 | |||||||||||||
| 55 | 3 | ▶ | if ($self->{error} =~ /timeout|timed out/gi) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 56 | 1 | $self->{status} = 408; | |||||||||||
| 57 | } else { | ||||||||||||
| 58 | 2 | $self->{status} = 400; | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 3 | $self->close_handles; | |||||||||||
| 62 | 3 | $self->completed(1); | |||||||||||
| 63 | |||||||||||||
| 64 | 3 | return; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | 4 | ▶ | sub close_handles { | ||||||||||
|
Subroutine
close_handles: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 68 | 4 | my $self = shift; | |||||||||||
| 69 | |||||||||||||
| 70 | 4 | close $self->fh_head; | |||||||||||
| 71 | 4 | undef $self->{fh_head}; | |||||||||||
| 72 | |||||||||||||
| 73 | 4 | close $self->fh_body; | |||||||||||
| 74 | 4 | undef $self->{fh_body}; | |||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 2 | ▶ | sub as_http_response { | ||||||||||
|
Subroutine
as_http_response: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 78 | 2 | my $self = shift; | |||||||||||
| 79 | 2 | ▶ | return unless $self->completed; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 1 | my $res = HTTP::Response->new($self->status); | |||||||||||
| 81 | 1 | $res->header($_, $self->headers->{$_}) foreach keys %{$self->headers}; | |||||||||||
| 82 | 1 | $res->content($self->raw_body); | |||||||||||
| 83 | 1 | return $res; | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | 1 | 1; | |||||||||||
| 87 | |||||||||||||
| 88 | =head1 NAME | ||||||||||||
| 89 | |||||||||||||
| 90 | Net::Curl::Parallel::Response | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 DESCRIPTION | ||||||||||||
| 93 | |||||||||||||
| 94 | An HTTP response returned by L<Net::Curl::Parallel>. | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 METHODS | ||||||||||||
| 97 | |||||||||||||
| 98 | =head2 status | ||||||||||||
| 99 | |||||||||||||
| 100 | Returns the HTTP response status code. | ||||||||||||
| 101 | |||||||||||||
| 102 | =head2 failed | ||||||||||||
| 103 | |||||||||||||
| 104 | Returns true when the request generated an error. | ||||||||||||
| 105 | |||||||||||||
| 106 | =head2 error | ||||||||||||
| 107 | |||||||||||||
| 108 | Returns the error message string. | ||||||||||||
| 109 | |||||||||||||
| 110 | =head2 content | ||||||||||||
| 111 | |||||||||||||
| 112 | Returns the HTTP response body content string. | ||||||||||||
| 113 | |||||||||||||
| 114 | =head2 as_http_response | ||||||||||||
| 115 | |||||||||||||
| 116 | Returns the response as an L<HTTP::Response> instance. | ||||||||||||
| 117 | |||||||||||||
| 118 | =cut | ||||||||||||