blib/lib/Langertha/Request/SyncHTTP.pm

filter: Statementstmt 100% 42 / 42 Branchbran 81.8% 18 / 22 Conditioncond 40.0% 4 / 10 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 83.3% 70 / 84 CC 16 SCAR 22.4
CC
16
Cov
86%
do_request
12.5
__ANON__
5.0
BEGIN
1.0
CRAP 9.4
« blib/lib/Langertha/HTTP/ConnectCheck.pm ↑ Summary blib/lib/Langertha/ServerToolCall.pm »
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
BranchTrueFalseTotal
if ($on_header) 28 117 145
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
BranchTrueFalseTotal
unless ($header_seen) 20 61 81
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
BranchTrueFalseTotal
if $chunk_handler 81 0 81
28 75 1;
29 };
30 81 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 75 6 81
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"
l!l
60
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
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
BranchTrueFalseTotal
if defined $callback_error 6 22 28
41 if defined $callback_error;
42 22 ▶ if ( defined( my $died = $response->header('X-Died') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $died = $response->header("X-Died"))) 1 21 22
43 1 ▶ $died .= "\n" unless $died =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $died =~ /\n\z/ 1 0 1
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
BranchTrueFalseTotal
unless ($header_seen) 8 13 21
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
BranchTrueFalseTotal
if $chunk_handler and defined $body and length $body 7 1 8
Condition50%3/6
$chunk_handler and defined $body and length $body
!ll&&!rl&&r
017
$chunk_handler and defined $body
!ll&&!rl&&r
008
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
ABCresult
0XX0
10X0
1100
1111
58 }
59 21 ▶ $chunk_handler->(undef) if $chunk_handler; # the async end-of-body signal
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_handler 21 0 21
60 21 1;
61 };
62 21 ▶ return Future->fail( $@ || "streaming callback died\n", http => $response, $request )
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 21 21
Condition0%0/2
$@ || "streaming callback died\n"
l!l
00
MC/DC0%0/1
$@ || "streaming callback died\n"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
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
« blib/lib/Langertha/HTTP/ConnectCheck.pm ↑ Summary blib/lib/Langertha/ServerToolCall.pm »