blib/lib/Plack/Handler/Net/Async/HTTP/Server.pm

filter: Statementstmt 24.5% 14 / 57 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 57.1% 4 / 7 Podpod 100% 2 / 2 totaltotal 20.8% 20 / 96 SCAR 52.2
CC
17
Cov
17%
run
156.0
new
20.0
__ANON__
12.0
CRAP 184.2
↑ Summary blib/lib/Net/Async/HTTP/Server/PSGI.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-2014 -- leonerd@leonerd.org.uk
5  
6 package Plack::Handler::Net::Async::HTTP::Server;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Net::Async::HTTP::Server::PSGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.13';
15  
16 =head1 NAME
17  
18 C<Plack::Handler::Net::Async::HTTP::Server> - HTTP handler for Plack using L<Net::Async::HTTP::Server>
19  
20 =head1 SYNOPSIS
21  
22 use Plack::Handler::Net::Async::HTTP::Server;
23  
24 my $handler = Plack::Handler::Net::Async::HTTP::Server->new(
25 listen => [ ":8080" ],
26 );
27  
28 sub psgi_app { ... }
29  
30 $handler->run( \&psgi_app );
31  
32 =head1 DESCRIPTION
33  
34 This module allows L<Plack> to run a L<PSGI> application as a standalone
35 HTTP daemon under L<IO::Async>, by using L<Net::Async::HTTP::Server>.
36  
37 plackup -s Net::Async::HTTP::Server --listen ":8080" application.psgi
38  
39 This is internally implemented using L<Net::Async::HTTP::Server::PSGI>;
40 further information on environment etc.. is documented there.
41  
42 If L<IO::Async::SSL> is available, this handler supports accepting connections
43 via C<https>
44  
45 plackup -s Net::Async::HTTP::Server --ssl ...
46  
47 Or per-listen argument by appending C<:SSL>, as
48  
49 plackup -s Net::Async::HTTP::Server --listen ":8443:SSL" ...
50  
51 Any other options whose names start C<ssl_> will be passed on to the SSL
52 listen method.
53  
54 =cut
55  
56 =head1 METHODS
57  
58 =cut
59  
60 =head2 $handler = Plack::Handler::Net::Async::HTTP::Server->new( %args )
61  
62 Returns a new instance of a C<Plack::Handler::Net::Async::HTTP::Server>
63 object. Takes the following named arguments:
64  
65 =over 4
66  
67 =item listen => ARRAY of STRING
68  
69 Reference to an array containing listen string specifications. Each string
70 gives a port number and optional hostname, given as C<:port> or C<host:port>.
71  
72 =item server_ready => CODE
73  
74 Reference to code to invoke when the server is set up and listening, ready to
75 accept connections. It is invoked with a HASH reference containing the
76 following details:
77  
78 $server_ready->( {
79 host => HOST,
80 port => SERVICE,
81 server_software => NAME,
82 } )
83  
84 =item socket => STRING
85  
86 Gives a UNIX socket path to listen on, instead of a TCP socket.
87  
88 =item queuesize => INT
89  
90 Optional. If provided, sets the C<listen()> queue size for creating listening
91 sockets. If missing, a default of 10 is used.
92  
93 =back
94  
95 =cut
96  
97 sub new
98 {
99 0 ▶ my $class = shift;
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
100 0 my %opts = @_;
101  
102 0 delete $opts{host};
103 0 delete $opts{port};
104 0 delete $opts{socket};
105  
106 my $self = bless {
107 0 map { $_ => delete $opts{$_} } qw( listen server_ready queuesize ),
108 }, $class;
109  
110 # Grab all of the SSL options
111 0 ▶ $self->{ssl} = 1 if exists $opts{ssl}; delete $opts{ssl};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"ssl"} 0 0 0
112 0 $self->{$_} = delete $opts{$_} for grep m/^ssl_/, keys %opts;
113  
114 0 ▶ keys %opts and die "Unrecognised keys " . join( ", ", sort keys %opts );
Branch: 1 branch
BranchTrueFalseTotal
keys %opts and die 'Unrecognised keys ' . join(", ", sort(keys %opts)) 0 0 0
115  
116 0 return $self;
117 }
118  
119 =head2 $handler->run( $psgi_app )
120  
121 Creates the HTTP-listening socket or sockets, and runs the given PSGI
122 application for received requests.
123  
124 =cut
125  
126 sub run
127 {
128 0 ▶ my $self = shift;
Subroutine run: not called CC=12 SCAR=50.5
Pod documented
129 0 my ( $app ) = @_;
130  
131 0 my $loop = IO::Async::Loop->new;
132 0 ▶ my $queuesize = $self->{queuesize} || 10;
Condition0%0/2
$self->{'queuesize'} || 10
l!l
00
MC/DC0%0/1
$self->{'queuesize'} || 10
$self->{'queuesize'}
Truth table0%0/2
A
A= $self->{'queuesize'}
Aresult
11
00
133  
134 0 foreach my $listen ( @{ $self->{listen} } ) {
135 0 my $httpserver = Net::Async::HTTP::Server::PSGI->new(
136 app => $app,
137 );
138  
139 0 $loop->add( $httpserver );
140  
141 # IPv6 addresses contain colons. They'll be wrapped in [] brackets
142 0 my $host;
143 my $path;
144  
145 0 ▶ if( $listen =~ s/^\[([0-9a-f:]+)\]://i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($listen =~ s/^\[([0-9a-f:]+)\]://i) { } 0 0 0
elsif ($listen =~ s/^([^:]+?)://) { } 0 0 0
elsif ($listen =~ s/^://) { } 0 0 0
146 0 $host = $1;
147 }
148 elsif( $listen =~ s/^([^:]+?):// ) {
149 0 $host = $1;
150 }
151 elsif( $listen =~ s/^:// ) {
152 # OK
153 }
154 else {
155 0 $path = $listen;
156 }
157  
158 0 ▶ if( defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) { } 0 0 0
159 0 require IO::Socket::UNIX;
160  
161 0 ▶ unlink $path if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 0 0
162  
163 0 ▶ my $socket = IO::Socket::UNIX->new(
Branch: 1 branch
BranchTrueFalseTotal
my $socket = "IO::Socket::UNIX"->new("Local", $path, "Listen", $queuesize) or die "Cannot listen on $path - $!" 0 0 0
164 Local => $path,
165 Listen => $queuesize,
166 ) or die "Cannot listen on $path - $!";
167  
168 0 $httpserver->configure( handle => $socket );
169 }
170 else {
171 0 my ( $service, $ssl ) = split m/:/, $listen;
172 0 ▶ $ssl ||= $self->{ssl};
Condition0%0/3
$ssl ||= $self->{"ssl"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ssl ||= $self->{"ssl"}
$ssl $self->{"ssl"}
Truth table0%0/3
A ||= B
A= $sslB= $self->{"ssl"}
ABresult
1X1
011
000
173  
174 0 my %SSL_args;
175 0 ▶ if( $ssl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssl) 0 0 0
176 0 require IO::Async::SSL;
177 0 %SSL_args = (
178 extensions => [qw( SSL )],
179 );
180  
181 0 foreach my $key ( grep m/^ssl_/, keys %$self ) {
182 0 my $val = $self->{$key};
183 # IO::Async::Listener extension wants uppercase "SSL"
184 0 $key =~ s/^ssl/SSL/;
185  
186 0 $SSL_args{$key} = $val;
187 };
188 }
189  
190 $httpserver->listen(
191 host => $host,
192 service => $service,
193 socktype => "stream",
194 queuesize => $queuesize,
195  
196 %SSL_args,
197  
198 on_notifier => sub {
199 $self->{server_ready}->( {
200 host => $host,
201 port => $service,
202 proto => $ssl ? "https" : "http",
203 server_software => ref $self,
204 0 ▶ } ) if $self->{server_ready};
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
if $self->{"server_ready"} 0 0 0
$ssl ? : 0 0 0
205 },
206 0 )->get;
207 }
208 }
209  
210 0 $loop->run;
211 }
212  
213 =head1 SEE ALSO
214  
215 =over 4
216  
217 =item *
218  
219 L<Net::Async::HTTP::Server> - serve HTTP with L<IO::Async>
220  
221 =item *
222  
223 L<Plack> - Perl Superglue for Web frameworks and Web Servers (PSGI toolkit)
224  
225 =back
226  
227 =head1 AUTHOR
228  
229 Paul Evans <leonerd@leonerd.org.uk>
230  
231 =cut
232  
233 1 0x55AA;
↑ Summary blib/lib/Net/Async/HTTP/Server/PSGI.pm »