| 1 | package Langertha::Request::SyncHTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Synchronous LWP-backed HTTP client satisfying the async do_request contract | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 36 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 36 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 36 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 36 | has user_agent => ( | |||||||||||||||||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 194 | ▶ | sub do_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_request: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 194 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 194 | my $request = $args{request}; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 194 | my $on_header = $args{on_header}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 194 | ▶ | if ($on_header) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 41 | my ( $chunk_handler, $header_seen, $callback_error ); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 81 | ▶ | my $response = $self->user_agent->request($request, sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 81 | my ( $data, $header_response ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 81 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 23 | 81 | ▶ | unless ($header_seen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 20 | $header_seen = 1; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 20 | $chunk_handler = $on_header->($header_response); | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 81 | ▶ | $chunk_handler->($data) if $chunk_handler; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 75 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | 81 | ▶ | return if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | # Remember the original exception (objects survive), then die again so | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # LWP stops reading the body. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | ▶ | $callback_error = $@ || "streaming callback died\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || "streaming callback died\n"
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 6 | die $callback_error; | |||||||||||||||||||||||||||||||||||||||||
| 35 | 41 | }); | |||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | # LWP catches a die in the content callback (and a mid-body read failure) | ||||||||||||||||||||||||||||||||||||||||||
| 38 | # and records it as X-Died on an otherwise successful-looking response. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | # Fail like Net::Async::HTTP does instead of resolving a truncated stream. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 41 | ▶ | return Future->fail( $callback_error, http => $response, $request ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | if defined $callback_error; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 35 | ▶ | if ( defined( my $died = $response->header('X-Died') ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | ▶ | $died .= "\n" unless $died =~ /\n\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | return Future->fail( $died, http => $response, $request ); | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | # LWP never runs the content callback for a non-success response (the | ||||||||||||||||||||||||||||||||||||||||||
| 48 | # body is accumulated on the response instead), nor for an internal error | ||||||||||||||||||||||||||||||||||||||||||
| 49 | # (connection refused, DNS, timeout) or an empty body. Net::Async::HTTP | ||||||||||||||||||||||||||||||||||||||||||
| 50 | # calls on_header for every response and hands it the body, so do the | ||||||||||||||||||||||||||||||||||||||||||
| 51 | # same here once the request is done. | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 34 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 53 | 34 | ▶ | unless ($header_seen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 21 | $header_seen = 1; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 21 | $chunk_handler = $on_header->($response); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 21 | my $body = $response->content; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 21 | ▶ | $chunk_handler->($body) if $chunk_handler && defined $body && length $body; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$chunk_handler and defined $body and length $body
$chunk_handler and defined $body
MC/DC33%1/3
$chunk_handler and defined $body and length $body
$chunk_handler
defined $body
length $body
Truth table50%2/4
A and B and C
A= $chunk_handlerB= defined $bodyC= length $body
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 34 | ▶ | $chunk_handler->(undef) if $chunk_handler; # the async end-of-body signal | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 34 | ▶ | return Future->fail( $@ || "streaming callback died\n", http => $response, $request ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$@ || "streaming callback died\n"
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table0%0/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | unless $ok; | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 34 | return Future->done($response); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 153 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 153 | return Future->done($response); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 36 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 36 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||||||||||||||||