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

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