blib/lib/Net/Async/HTTP/Server/Request.pm

filter: Statementstmt 95.2% 101 / 106 Branchbran 71.8% 23 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 97.0% 33 / 34 Podpod 91.3% 21 / 23 totaltotal 91.2% 178 / 195 SCAR 28.5
CC
17
Cov
90%
respond
6.0
respond_chunk_header
4.0
write
4.0
CRAP 17.3
« blib/lib/Net/Async/HTTP/Server/PSGI.pm ↑ Summary blib/lib/Net/Async/HTTP/Server/Protocol.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2013-2023 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::HTTP::Server::Request 0.14;
7  
8 13 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 13 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 13 my $CRLF = "\x0d\x0a";
17  
18 =head1 NAME
19  
20 C<Net::Async::HTTP::Server::Request> - represents a single outstanding request
21  
22 =head1 DESCRIPTION
23  
24 Objects in this class represent a single outstanding request received by a
25 L<Net::Async::HTTP::Server> instance. It allows access to the data received
26 from the web client and allows responding to it.
27  
28 =cut
29  
30 sub new
31 {
32 25 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
33 25 my ( $conn, $request ) = @_;
34  
35 25 return bless {
36 conn => $conn,
37 req => $request,
38  
39 pending => [],
40 bytes_written => 0,
41 is_done => 0,
42 is_closed => 0,
43 }, $class;
44 }
45  
46 =head1 METHODS
47  
48 =cut
49  
50 =head2 is_closed
51  
52 $is_closed = $request->is_closed
53  
54 Returns true if the underlying network connection for this request has already
55 been closed. If this is the case, the application is free to drop the request
56 object and perform no further processing on it.
57  
58 =cut
59  
60 sub _close
61 {
62 1 ▶ my $self = shift;
Subroutine _close: called CC=1 SCAR=0.0
63 1 $self->{is_closed} = 1;
64 }
65  
66 sub is_closed
67 {
68 3 ▶ my $self = shift;
Subroutine is_closed: called CC=1 SCAR=0.0
Pod documented
69 3 return $self->{is_closed};
70 }
71  
72 =head2 method
73  
74 $method = $request->method
75  
76 Return the method name from the request header.
77  
78 =cut
79  
80 sub method
81 {
82 27 ▶ my $self = shift;
Subroutine method: called CC=1 SCAR=0.0
Pod documented
83 27 return $self->{req}->method;
84 }
85  
86 =head2 path
87  
88 $path = $request->path
89  
90 Return the path name from the request header.
91  
92 =cut
93  
94 sub path
95 {
96 26 ▶ my $self = shift;
Subroutine path: called CC=1 SCAR=0.0
Pod documented
97 26 return $self->{req}->uri->path;
98 }
99  
100 =head2 query_string
101  
102 $query_string = $request->query_string
103  
104 Return the query string from the request header.
105  
106 =cut
107  
108 sub query_string
109 {
110 11 ▶ my $self = shift;
Subroutine query_string: called CC=1 SCAR=0.0
Pod documented
111 11 return $self->{req}->uri->query;
112 }
113  
114 =head2 query_form
115  
116 %params = $request->query_form
117  
118 I<Since version 0.09.>
119  
120 Return an even-sized list of name and value pairs that gives the decoded data
121 in the query string. This is the same format as the same-named method on
122 L<URI>.
123  
124 =cut
125  
126 sub query_form
127 {
128 1 ▶ my $self = shift;
Subroutine query_form: called CC=1 SCAR=0.0
Pod documented
129 1 return $self->{req}->uri->query_form;
130 }
131  
132 =head2 query_param_names
133  
134 @names = $request->query_param_names
135  
136 I<Since version 0.09.>
137  
138 Return a list of the names of all the query parameters.
139  
140 =cut
141  
142 sub query_param_names
143 {
144 1 ▶ my $self = shift;
Subroutine query_param_names: called CC=1 SCAR=0.0
Pod documented
145 1 return $self->{req}->uri->query_param;
146 }
147  
148 =head2 query_param
149  
150 $value = $request->query_param( $name )
151  
152 @values = $request->query_param( $name )
153  
154 I<Since version 0.09.>
155  
156 Return the value or values of a single decoded query parameter.
157  
158 =cut
159  
160 sub query_param
161 {
162 1 ▶ my $self = shift;
Subroutine query_param: called CC=1 SCAR=0.0
Pod documented
163 1 return $self->{req}->uri->query_param( @_ );
164 }
165  
166 =head2 protocol
167  
168 $protocol = $request->protocol
169  
170 Return the protocol version from the request header. This will be the full
171 string, such as C<HTTP/1.1>.
172  
173 =cut
174  
175 sub protocol
176 {
177 62 ▶ my $self = shift;
Subroutine protocol: called CC=1 SCAR=0.0
Pod documented
178 62 return $self->{req}->protocol;
179 }
180  
181 =head2 header
182  
183 $value = $request->header( $key )
184  
185 Return the value of a request header.
186  
187 =cut
188  
189 sub header
190 {
191 1 ▶ my $self = shift;
Subroutine header: called CC=1 SCAR=0.0
Pod documented
192 1 my ( $key ) = @_;
193 1 return $self->{req}->header( $key );
194 }
195  
196 =head2 headers
197  
198 @headers = $request->headers
199  
200 Returns a list of 2-element C<ARRAY> refs containing all the request headers.
201 Each referenced array contains, in order, the name and the value.
202  
203 =cut
204  
205 sub headers
206 {
207 11 ▶ my $self = shift;
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
208 11 my @headers;
209  
210 $self->{req}->scan( sub {
211 5 ▶ my ( $name, $value ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
212 5 push @headers, [ $name, $value ];
213 11 } );
214  
215 11 return @headers;
216 }
217  
218 =head2 body
219  
220 $body = $request->body
221  
222 Return the body content from the request as a string of bytes.
223  
224 =cut
225  
226 sub body
227 {
228 15 ▶ my $self = shift;
Subroutine body: called CC=1 SCAR=0.0
Pod documented
229 15 return $self->{req}->content;
230 }
231  
232 # Called by NaHTTP::Server::Protocol
233 sub _write_to_stream
234 {
235 62 ▶ my $self = shift;
Subroutine _write_to_stream: called CC=4 SCAR=13.9
236 62 my ( $stream ) = @_;
237  
238 62 ▶ while( defined( my $next = shift @{ $self->{pending} } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $next = shift @{$$self{"pending"};})) 39 62 101
239 $stream->write( $next,
240 on_write => sub {
241 42 ▶ $self->{bytes_written} += $_[1];
Subroutine __ANON__: called CC=1 SCAR=0.0
242 },
243 $self->protocol eq "HTTP/1.0" ?
244 39 ▶ ( on_flush => sub { $stream->close } ) :
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->protocol eq 'HTTP/1.0' ? : 1 38 39
245 (),
246 );
247 }
248  
249 # An empty ->write to ensure we capture the written byte count correctly
250 $stream->write( "",
251 on_write => sub {
252 19 ▶ $self->{conn}->parent->_done_request( $self );
Subroutine __ANON__: called CC=1 SCAR=0.0
253 }
254 62 ▶ ) if $self->{is_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_done"} 21 41 62
255  
256 62 return $self->{is_done};
257 }
258  
259 =head2 write
260  
261 $request->write( $data )
262  
263 Append more data to the response to be written to the client. C<$data> can
264 either be a plain string, or a C<CODE> reference to be used in the underlying
265 L<IO::Async::Stream>'s C<write> method.
266  
267 =cut
268  
269 sub write
270 {
271 42 ▶ my $self = shift;
Subroutine write: called CC=4 SCAR=13.9
Pod documented
272 42 my ( $data ) = @_;
273  
274 42 ▶ unless( defined $self->{response_status_line} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"response_status_line"}) 22 20 42
275 22 ( $self->{response_status_line} ) = split m/$CRLF/, $data;
276 }
277  
278 42 ▶ return if $self->{is_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_closed"} 2 40 42
279  
280 40 ▶ $self->{is_done} and croak "This request has already been completed";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"is_done"} and croak("This request has already been completed") 0 40 40
281  
282 40 push @{ $self->{pending} }, $data;
283 40 $self->{conn}->_flush_requests;
284 }
285  
286 =head2 write_chunk
287  
288 $request->write_chunk( $data )
289  
290 Append more data to the response in the form of an HTTP chunked-transfer
291 chunk. This convenience is a shortcut wrapper for prepending the chunk header.
292  
293 =cut
294  
295 sub write_chunk
296 {
297 6 ▶ my $self = shift;
Subroutine write_chunk: called CC=3 SCAR=11.0
Pod documented
298 6 my ( $data ) = @_;
299  
300 6 ▶ return if $self->{is_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_closed"} 0 6 6
301 6 ▶ return unless my $len = length $data; # Must not write zero-byte chunks
Branch: 1 branch
BranchTrueFalseTotal
unless my $len = length $data 1 5 6
302  
303 5 $self->write( sprintf "%X$CRLF%s$CRLF", $len, $data );
304 }
305  
306 =head2 done
307  
308 $request->done
309  
310 Marks this response as completed.
311  
312 =cut
313  
314 sub done
315 {
316 23 ▶ my $self = shift;
Subroutine done: called CC=3 SCAR=11.0
Pod documented
317  
318 23 ▶ return if $self->{is_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_closed"} 2 21 23
319  
320 21 ▶ $self->{is_done} and croak "This request has already been completed";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"is_done"} and croak("This request has already been completed") 0 21 21
321  
322 21 $self->{is_done} = 1;
323 21 $self->{conn}->_flush_requests;
324 }
325  
326 =head2 write_chunk_eof
327  
328 $request->write_chunk_eof
329  
330 Sends the final EOF chunk and marks this response as completed.
331  
332 =cut
333  
334 sub write_chunk_eof
335 {
336 3 ▶ my $self = shift;
Subroutine write_chunk_eof: called CC=2 SCAR=7.0
Pod documented
337  
338 3 ▶ return if $self->{is_closed};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_closed"} 0 3 3
339  
340 3 $self->write( "0$CRLF$CRLF" );
341 3 $self->done;
342 }
343  
344 =head2 as_http_request
345  
346 $req = $request->as_http_request
347  
348 Returns the data of the request as an L<HTTP::Request> object.
349  
350 =cut
351  
352 sub as_http_request
353 {
354 1 ▶ my $self = shift;
Subroutine as_http_request: called CC=1 SCAR=0.0
Pod documented
355 1 return $self->{req};
356 }
357  
358 =head2 respond
359  
360 $request->respond( $response )
361  
362 Respond to the request using the given L<HTTP::Response> object.
363  
364 =cut
365  
366 sub respond
367 {
368 0 ▶ my $self = shift;
Subroutine respond: not called CC=2 SCAR=17.9
Pod documented
369 0 my ( $response ) = @_;
370  
371 0 ▶ defined $response->protocol or
Branch: 1 branch
BranchTrueFalseTotal
defined $response->protocol or $response->protocol($self->protocol) 0 0 0
372 $response->protocol( $self->protocol );
373  
374 0 $self->write( $response->as_string( $CRLF ) );
375 0 $self->done;
376 }
377  
378 =head2 respond_chunk_header
379  
380 $request->respond_chunk_header( $response )
381  
382 Respond to the request using the given L<HTTP::Response> object to send in
383 HTTP/1.1 chunked encoding mode.
384  
385 The headers in the C<$response> will be sent (which will be modified to set
386 the C<Transfer-Encoding> header). Each call to C<write_chunk> will send
387 another chunk of data. C<write_chunk_eof> will send the final EOF chunk and
388 mark the request as complete.
389  
390 If the C<$response> already contained content, that will be sent as one chunk
391 immediately after the header is sent.
392  
393 =cut
394  
395 sub respond_chunk_header
396 {
397 2 ▶ my $self = shift;
Subroutine respond_chunk_header: called CC=4 SCAR=14.0
Pod documented
398 2 my ( $response ) = @_;
399  
400 2 ▶ defined $response->protocol or
Branch: 1 branch
BranchTrueFalseTotal
defined $response->protocol or $response->protocol($self->protocol) 2 0 2
401 $response->protocol( $self->protocol );
402 2 ▶ defined $response->header( "Transfer-Encoding" ) or
Branch: 1 branch
BranchTrueFalseTotal
defined $response->header("Transfer-Encoding") or $response->header("Transfer-Encoding", "chunked") 2 0 2
403 $response->header( "Transfer-Encoding" => "chunked" );
404  
405 2 my $content = $response->content;
406  
407 2 my $header = $response->as_string( $CRLF );
408 # Trim any content from the header as it would need to be chunked
409 2 $header =~ s/$CRLF$CRLF.*$/$CRLF$CRLF/s;
410  
411 2 $self->write( $header );
412  
413 2 ▶ $self->write_chunk( $response->content ) if length $response->content;
Branch: 1 branch
BranchTrueFalseTotal
if length $response->content 1 1 2
414 }
415  
416 =head2 stream
417  
418 $stream = $request->stream
419  
420 Returns the L<IO::Async::Stream> object representing this connection. Usually
421 this would be used for such things as inspecting the client's connection
422 address on the C<read_handle> of the stream. It should not be necessary to
423 directly perform IO operations on this stream itself.
424  
425 =cut
426  
427 sub stream
428 {
429 10 ▶ my $self = shift;
Subroutine stream: called CC=1 SCAR=0.0
Pod documented
430 10 return $self->{conn};
431 }
432  
433 =head2 response_status_line
434  
435 $status = $request->response_status_line
436  
437 If a response header has been written by calling the C<write> method, returns
438 the first line of it.
439  
440 =cut
441  
442 sub response_status_line
443 {
444 1 ▶ my $self = shift;
Subroutine response_status_line: called CC=1 SCAR=0.0
Pod documented
445 1 return $self->{response_status_line};
446 }
447  
448 =head2 response_status_code
449  
450 $code = $request->response_status_code
451  
452 If a response header has been written by calling the C<write> method, returns
453 the status code from it.
454  
455 =cut
456  
457 sub response_status_code
458 {
459 2 ▶ my $self = shift;
Subroutine response_status_code: called CC=2 SCAR=7.1
Pod documented
460 2 ▶ my $line = $self->{response_status_line} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $line = $self->{"response_status_line"} or return undef 0 2 2
461 2 return +( split m/ /, $line )[1];
462 }
463  
464 # For metrics
465 sub bytes_written
466 {
467 1 ▶ my $self = shift;
Subroutine bytes_written: called CC=1 SCAR=0.0
Pod undocumented
468 1 return $self->{bytes_written};
469 }
470  
471 =head1 AUTHOR
472  
473 Paul Evans <leonerd@leonerd.org.uk>
474  
475 =cut
476  
477 13 0x55AA;
« blib/lib/Net/Async/HTTP/Server/PSGI.pm ↑ Summary blib/lib/Net/Async/HTTP/Server/Protocol.pm »