| 1 | package Langertha::Request::SyncHTTP; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Synchronous LWP-backed HTTP client satisfying the async do_request contract | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 27 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | 27 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 27 | ▶ | use Future; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 27 | has user_agent => ( | |||||||||||||||||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 9 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 10 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 145 | ▶ | sub do_request { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
do_request: called CC=12 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 145 | my ( $self, %args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 15 | 145 | my $request = $args{request}; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 145 | my $on_header = $args{on_header}; | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 145 | ▶ | if ($on_header) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 28 | 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 | 28 | }); | |||||||||||||||||||||||||||||||||||||||||
| 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 | 28 | ▶ | return Future->fail( $callback_error, http => $response, $request ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | if defined $callback_error; | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 22 | ▶ | 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 | 21 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 53 | 21 | ▶ | unless ($header_seen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | $header_seen = 1; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 8 | $chunk_handler = $on_header->($response); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | my $body = $response->content; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 8 | ▶ | $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 | 21 | ▶ | $chunk_handler->(undef) if $chunk_handler; # the async end-of-body signal | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | 21 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 21 | ▶ | 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 | 21 | return Future->done($response); | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 117 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||
| 68 | 117 | return Future->done($response); | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 27 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 27 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | __END__ | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | Langertha::Request::SyncHTTP - Synchronous LWP-backed HTTP client satisfying the async do_request contract | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | version 0.503 | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =head2 user_agent | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | The synchronous HTTP client used to run the request, normally an | ||||||||||||||||||||||||||||||||||||||||||
| 94 | L<LWP::UserAgent>. Any object with a | ||||||||||||||||||||||||||||||||||||||||||
| 95 | C<< ->request($http_request [, $content_cb]) >> method that returns an | ||||||||||||||||||||||||||||||||||||||||||
| 96 | L<HTTP::Response> satisfies the contract. Required. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =head2 do_request | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | my $future = $client->do_request( request => $http_request ); | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | my $future = $client->do_request( | ||||||||||||||||||||||||||||||||||||||||||
| 103 | request => $http_request, | ||||||||||||||||||||||||||||||||||||||||||
| 104 | on_header => sub { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | my ($response) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 106 | return sub { my ($data) = @_; ... }; # per-chunk, undef at end | ||||||||||||||||||||||||||||||||||||||||||
| 107 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 108 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | Runs C<$http_request> synchronously through L</user_agent> and returns an | ||||||||||||||||||||||||||||||||||||||||||
| 111 | already-complete L<Future> resolving to the L<HTTP::Response>. No event loop | ||||||||||||||||||||||||||||||||||||||||||
| 112 | is involved: because the future is already ready, any C<await> on it (or | ||||||||||||||||||||||||||||||||||||||||||
| 113 | C<< ->get >>) resolves immediately, so the whole C<_f> chain runs | ||||||||||||||||||||||||||||||||||||||||||
| 114 | synchronously and sequentially. | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | When an C<on_header> callback is given the request streams: L<LWP::UserAgent>'s | ||||||||||||||||||||||||||||||||||||||||||
| 117 | per-chunk content callback is bridged to the contract — C<on_header> is called | ||||||||||||||||||||||||||||||||||||||||||
| 118 | once with the L<HTTP::Response> (headers) and returns a chunk-sub, which then | ||||||||||||||||||||||||||||||||||||||||||
| 119 | receives each body chunk as LWP reads it from the socket and finally C<undef> | ||||||||||||||||||||||||||||||||||||||||||
| 120 | once to signal end-of-body. Delivery is incremental (the first chunk reaches | ||||||||||||||||||||||||||||||||||||||||||
| 121 | the caller before the body has finished arriving, so time-to-first-token is | ||||||||||||||||||||||||||||||||||||||||||
| 122 | real) but blocking: the calling thread waits inside the request until the | ||||||||||||||||||||||||||||||||||||||||||
| 123 | stream ends. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | LWP runs the content callback only for a successful response. For a | ||||||||||||||||||||||||||||||||||||||||||
| 126 | non-success status, and for an LWP-internal error response (connection | ||||||||||||||||||||||||||||||||||||||||||
| 127 | refused, DNS failure, timeout), the shim still calls C<on_header> once with | ||||||||||||||||||||||||||||||||||||||||||
| 128 | the response and hands the chunk-sub the accumulated body, then C<undef> — | ||||||||||||||||||||||||||||||||||||||||||
| 129 | the same sequence L<Net::Async::HTTP> produces, so the caller's | ||||||||||||||||||||||||||||||||||||||||||
| 130 | C<< $response->is_success >> check fires the same way on both backends. | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | If the chunk-sub (or C<on_header>) dies, or LWP aborts reading the body | ||||||||||||||||||||||||||||||||||||||||||
| 133 | mid-stream (recorded in its C<X-Died> header), the returned future B<fails> | ||||||||||||||||||||||||||||||||||||||||||
| 134 | with that exception and no C<undef> end signal is sent: a truncated stream is | ||||||||||||||||||||||||||||||||||||||||||
| 135 | never resolved as a success. | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | This is the drop-in fallback backend for the async C<do_request> contract | ||||||||||||||||||||||||||||||||||||||||||
| 138 | (L<Langertha::Role::AsyncHTTP>): HTTP error statuses (4xx/5xx) B<resolve> | ||||||||||||||||||||||||||||||||||||||||||
| 139 | with the response — they do not fail the future — so the caller checks | ||||||||||||||||||||||||||||||||||||||||||
| 140 | C<< $response->is_success >>, exactly as with L<Net::Async::HTTP>. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | =item * L<Langertha::Role::AsyncHTTP> - Backend selection that falls back to this shim | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =item * L<Langertha::Role::HTTP> - Provides the C<user_agent> this shim runs over | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||
| 157 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||
| 173 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 176 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =cut | ||||||||||||||||||||||||||||||||||||||||||