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

filter: Statementstmt 100% 95 / 95 Branchbran 90.0% 36 / 40 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 92.7% 154 / 166 SCAR 33.0
CC
27
Cov
94%
__ANON__
17.1
on_request
7.0
__ANON__
3.0
CRAP 27.1
« blib/lib/Plack/Handler/Net/Async/HTTP/Server.pm ↑ Summary blib/lib/Net/Async/HTTP/Server/Request.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 -- leonerd@leonerd.org.uk
5  
6 package Net::Async::HTTP::Server::PSGI;
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use base qw( Net::Async::HTTP::Server );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.13';
16  
17 3 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 my $CRLF = "\x0d\x0a";
20  
21 =head1 NAME
22  
23 C<Net::Async::HTTP::Server::PSGI> - use C<PSGI> applications with C<Net::Async::HTTP::Server>
24  
25 =head1 SYNOPSIS
26  
27 use Net::Async::HTTP::Server::PSGI;
28 use IO::Async::Loop;
29  
30 my $loop = IO::Async::Loop->new;
31  
32 my $httpserver = Net::Async::HTTP::Server::PSGI->new(
33 app => sub {
34 my $env = shift;
35  
36 return [
37 200,
38 [ "Content-Type" => "text/plain" ],
39 [ "Hello, world!" ],
40 ];
41 },
42 );
43  
44 $loop->add( $httpserver );
45  
46 $httpserver->listen(
47 addr => { family => "inet6", socktype => "stream", port => 8080 },
48 )->get;
49  
50 $loop->run;
51  
52 =head1 DESCRIPTION
53  
54 This subclass of L<Net::Async::HTTP::Server> allows an HTTP server to use a
55 L<PSGI> application to respond to requests. It acts as a gateway between the
56 HTTP connection from the web client, and the C<PSGI> application. Aside from
57 the use of C<PSGI> instead of the C<on_request> event, this class behaves
58 similarly to C<Net::Async::HTTP::Server>.
59  
60 To handle the content length when sending responses, the PSGI implementation
61 may add a header to the response. When sending a plain C<ARRAY> of strings, if
62 a C<Content-Length> header is absent, the length will be calculated by taking
63 the total of all the strings in the array, and setting the length header. When
64 sending content from an IO reference or using the streaming responder C<CODE>
65 reference, the C<Transfer-Encoding> header will be set to C<chunked>, and all
66 writes will be performed as C<HTTP/1.1> chunks.
67  
68 =cut
69  
70 =head1 PARAMETERS
71  
72 The following named parameters may be passed to C<new> or C<configure>:
73  
74 =over 8
75  
76 =item app => CODE
77  
78 Reference to the actual C<PSGI> application to use for responding to requests
79  
80 =back
81  
82 =cut
83  
84 sub configure
85 {
86 12 ▶ my $self = shift;
Subroutine configure: called CC=2 SCAR=6.9
Pod documented
87 12 my %args = @_;
88  
89 12 ▶ if( exists $args{app} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"app"}) 10 2 12
90 10 $self->{app} = delete $args{app};
91 }
92  
93 12 $self->SUPER::configure( %args );
94 }
95  
96 =head1 PSGI ENVIRONMENT
97  
98 The following extra keys are supplied to the environment of the C<PSGI> app:
99  
100 =over 8
101  
102 =item C<psgix.io>
103  
104 The actual L<IO::Socket> filehandle that the request was received on.
105  
106 If the server is running under SSL for HTTPS, this will be an
107 L<IO::Socket::SSL> instance, so reading from or writing to it will happen in
108 cleartext.
109  
110 =item C<net.async.http.server>
111  
112 The C<Net::Async::HTTP::Server::PSGI> object serving the request
113  
114 =item C<net.async.http.server.req>
115  
116 The L<Net::Async::HTTP::Server::Request> object representing this particular
117 request
118  
119 =item C<io.async.loop>
120  
121 The L<IO::Async::Loop> object that the C<Net::Async::HTTP::Server::PSGI>
122 object is a member of.
123  
124 =back
125  
126 =cut
127  
128 sub on_request
129 {
130 9 ▶ my $self = shift;
Subroutine on_request: called CC=7 SCAR=19.5
Pod documented
131 9 my ( $req ) = @_;
132  
133 # Much of this code stolen fro^W^Winspired by Plack::Handler::Net::FastCGI
134  
135 9 open my $stdin, "<", \$req->body;
136  
137 9 my $socket = $req->stream->read_handle;
138  
139 9 my $path_info = $req->path;
140 9 ▶ $path_info = "" if $path_info eq "/";
Branch: 1 branch
BranchTrueFalseTotal
if $path_info eq "/" 8 1 9
141  
142 9 ▶ my %env = (
Condition50%1/2
$req->query_string // ""
l!l
09
MC/DC0%0/1
$req->query_string // ""
$req->query_string
Truth table50%1/2
A
A= $req->query_string
Aresult
11
00
143 SERVER_PORT => $socket->sockport,
144 SERVER_NAME => $socket->sockhost,
145 SERVER_PROTOCOL => $req->protocol,
146 SCRIPT_NAME => '',
147 PATH_INFO => $path_info,
148 QUERY_STRING => $req->query_string // "",
149 REMOTE_ADDR => $socket->peerhost,
150 REMOTE_PORT => $socket->peerport,
151 REQUEST_METHOD => $req->method,
152 REQUEST_URI => $req->path,
153 'psgi.version' => [1,0],
154 'psgi.url_scheme' => "http",
155 'psgi.input' => $stdin,
156 'psgi.errors' => \*STDERR,
157 'psgi.multithread' => 0,
158 'psgi.multiprocess' => 0,
159 'psgi.run_once' => 0,
160 'psgi.nonblocking' => 1,
161 'psgi.streaming' => 1,
162  
163 # Extensions
164 'psgix.io' => $socket,
165 'psgix.input.buffered' => 1, # we're using a PerlIO scalar handle
166 'net.async.http.server' => $self,
167 'net.async.http.server.req' => $req,
168 'io.async.loop' => $self->get_loop,
169 );
170  
171 9 foreach ( $req->headers ) {
172 4 my ( $name, $value ) = @$_;
173 4 $name =~ s/-/_/g;
174 4 $name = uc $name;
175  
176 # Content-Length and Content-Type don't get HTTP_ prefix
177 4 ▶ $name = "HTTP_$name" unless $name =~ m/^CONTENT_(?:LENGTH|TYPE)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^CONTENT_(?:LENGTH|TYPE)$/ 2 2 4
178  
179 4 $env{$name} = $value;
180 }
181  
182 9 my $resp = $self->{app}->( \%env );
183  
184 my $responder = sub {
185 9 ▶ my ( $status, $headers, $body ) = @{ +shift };
Subroutine __ANON__: called CC=17 SCAR=28.4
186  
187 9 my $response = HTTP::Response->new( $status );
188 9 $response->protocol( $req->protocol );
189  
190 9 my $has_content_length = 0;
191 9 my $use_chunked_transfer;
192 9 ▶ while( my ( $key, $value ) = splice @$headers, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$headers, 0, 2)) 11 9 20
193 11 $response->header( $key, $value );
194  
195 11 ▶ $has_content_length = 1 if $key eq "Content-Length";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Content-Length" 2 9 11
196 11 ▶ $use_chunked_transfer++ if $key eq "Transfer-Encoding" and $value eq "chunked";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "Transfer-Encoding" and $value eq "chunked" 0 11 11
Condition33%1/3
$key eq "Transfer-Encoding" and $value eq "chunked"
!ll&&!rl&&r
1100
MC/DC0%0/2
$key eq "Transfer-Encoding" and $value eq "chunked"
$key eq "Transfer-Encoding" $value eq "chunked"
Truth table33%1/3
A and B
A= $key eq "Transfer-Encoding"B= $value eq "chunked"
ABresult
0X0
100
111
197 }
198  
199 9 ▶ if( !defined $body ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $body) { } 2 7 9
elsif (ref $body eq 'ARRAY') { } 5 2 7
200 2 ▶ croak "Responder given no body in void context" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 2 2
201  
202 2 ▶ unless( $has_content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_content_length) 1 1 2
203 1 $response->header( "Transfer-Encoding" => "chunked" );
204 1 $use_chunked_transfer++;
205 }
206  
207 2 $req->write( $response->as_string( $CRLF ) );
208  
209 2 ▶ return $use_chunked_transfer ?
Branch: 1 branch
BranchTrueFalseTotal
$use_chunked_transfer ? : 1 1 2
210 Net::Async::HTTP::Server::PSGI::ChunkWriterStream->new( $req ) :
211 Net::Async::HTTP::Server::PSGI::WriterStream->new( $req );
212 }
213 elsif( ref $body eq "ARRAY" ) {
214 5 ▶ unless( $has_content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_content_length) 5 0 5
215 5 my $len = 0;
216 5 my $found_undef;
217 5 ▶ $len += length( $_ // ( $found_undef++, "" ) ) for @$body;
Condition66%2/3
$_ // (++$found_undef, "")
l!l&&r!l&&!r
401
MC/DC50%1/2
$_ // (++$found_undef, "")
$_ (++$found_undef, "")
Truth table66%2/3
A // B
A= $_B= (++$found_undef, "")
ABresult
1X1
011
000
218 5 ▶ carp "Found undefined value in PSGI body" if $found_undef;
Branch: 1 branch
BranchTrueFalseTotal
if $found_undef 1 4 5
219  
220 5 $response->content_length( $len );
221 }
222  
223 5 $req->write( $response->as_string( $CRLF ) );
224  
225 5 $req->write( $_ ) for @$body;
226 5 $req->done;
227 }
228 else {
229 2 ▶ unless( $has_content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_content_length) 1 1 2
230 1 $response->header( "Transfer-Encoding" => "chunked" );
231 1 $use_chunked_transfer++;
232 }
233  
234 2 $req->write( $response->as_string( $CRLF ) );
235  
236 2 ▶ if( $use_chunked_transfer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_chunked_transfer) { } 1 1 2
237 $req->write( sub {
238 # We can't return the EOF chunk and set undef in one go
239 # What we'll have to do is send the EOF chunk then clear $body,
240 # which indicates end
241 3 ▶ return unless defined $body;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 1 2 3
242  
243 2 local $/ = \8192;
244 2 my $buffer = $body->getline;
245  
246 # Form HTTP chunks out of it
247 2 ▶ defined $buffer and
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer and return sprintf("%X$CRLF%s$CRLF", length $buffer, $buffer) 1 1 2
248 return sprintf( "%X$CRLF%s$CRLF", length $buffer, $buffer );
249  
250 1 $body->close;
251 1 undef $body;
252 1 return "0$CRLF$CRLF";
253 1 } );
254 }
255 else {
256 $req->write( sub {
257 2 ▶ local $/ = \8192;
Subroutine __ANON__: called CC=2 SCAR=6.9
258 2 my $buffer = $body->getline;
259  
260 2 ▶ defined $buffer and return $buffer;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer and return $buffer 1 1 2
261  
262 1 $body->close;
263 1 return undef;
264 1 } );
265 }
266  
267 2 $req->done;
268 }
269 9 };
270  
271 9 ▶ if( ref $resp eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $resp eq 'ARRAY') { } 6 3 9
elsif (ref $resp eq 'CODE') { } 3 0 3
272 6 $responder->( $resp );
273 }
274 elsif( ref $resp eq "CODE" ) {
275 3 $resp->( $responder );
276 }
277 }
278  
279 # Hide from indexer
280 package
281 Net::Async::HTTP::Server::PSGI::WriterStream;
282  
283 sub new
284 {
285 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
286 1 return bless [ @_ ], $class;
287 }
288  
289 2 ▶sub write { shift->[0]->write( $_[0] ) }
Subroutine write: called CC=1 SCAR=0.0
290 1 ▶sub close { shift->[0]->done }
Subroutine close: called CC=1 SCAR=0.0
291  
292 # Hide from indexer
293 package
294 Net::Async::HTTP::Server::PSGI::ChunkWriterStream;
295  
296 sub new
297 {
298 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
299 1 return bless [ @_ ], $class;
300 }
301  
302 2 ▶sub write { shift->[0]->write_chunk( $_[0] ) }
Subroutine write: called CC=1 SCAR=0.0
303 1 ▶sub close { shift->[0]->write_chunk_eof }
Subroutine close: called CC=1 SCAR=0.0
304  
305 =head1 SEE ALSO
306  
307 =over 4
308  
309 =item *
310  
311 L<PSGI> - Perl Web Server Gateway Interface Specification
312  
313 =item *
314  
315 L<Plack::Handler::Net::Async::HTTP::Server> - HTTP handler for Plack using
316 L<Net::Async::HTTP::Server>
317  
318 =back
319  
320 =head1 AUTHOR
321  
322 Paul Evans <leonerd@leonerd.org.uk>
323  
324 =cut
325  
326 3 0x55AA;
« blib/lib/Plack/Handler/Net/Async/HTTP/Server.pm ↑ Summary blib/lib/Net/Async/HTTP/Server/Request.pm »