File Coverage

blib/lib/FCGI/Async.pm
Criterion Covered Total %
statement 23 28 82.1
branch 3 10 30.0
condition 1 3 33.3
subroutine 5 7 71.4
pod 1 1 100.0
total 33 49 67.3


line stmt bran cond sub pod time code
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, 2011 -- leonerd@leonerd.org.uk
5              
6             package FCGI::Async;
7              
8 4     4   304414 use strict;
  4         8  
  4         131  
9 4     4   25 use warnings;
  4         44  
  4         154  
10              
11 4     4   32 use base qw( Net::Async::FastCGI );
  4         7  
  4         2598  
12              
13             our $VERSION = '0.22';
14              
15 4     4   210023 use Carp;
  4         11  
  4         1750  
16              
17             # Back compat
18             *MAX_CONNS = \$Net::Async::FastCGI::MAX_CONNS;
19             *MAX_REQS = \$Net::Async::FastCGI::MAX_REQS;
20              
21             =head1 NAME
22              
23             C - use FastCGI with L
24              
25             =head1 SYNOPSIS
26              
27             use FCGI::Async;
28             use IO::Async::Loop;
29              
30             my $loop = IO::Async::Loop->new();
31              
32             my $fcgi = FCGI::Async->new(
33             loop => $loop
34             service => 1234,
35              
36             on_request => sub {
37             my ( $fcgi, $req ) = @_;
38              
39             # Handle the request here
40             }
41             );
42              
43             $loop->loop_forever;
44              
45             =head1 DESCRIPTION
46              
47             This subclass of L provides a slightly different API;
48             where it can take an argument containing the L object, rather
49             than be added as C object within one. It is provided mostly as a
50             backward-compatibility wrapper for older code using this interface; newer
51             code ought to use the C interface directly.
52              
53             =cut
54              
55             =head1 CONSTRUCTOR
56              
57             =cut
58              
59             =head2 $fcgi = FCGI::Async->new( %args )
60              
61             Returns a new instance of a C object.
62              
63             If either a C or C argument are passed to the constructor,
64             then the newly-created object is added to the given C, then
65             the C method is invoked, passing the entire C<%args> hash to it.
66              
67             If either of the above arguments are given, then a C must
68             also be provided:
69              
70             =over 4
71              
72             =item loop => IO::Async::Loop
73              
74             A reference to the C which will contain the listening
75             sockets.
76              
77             =back
78              
79             =cut
80              
81             sub new
82             {
83 3     3 1 5279 my $class = shift;
84 3         13 my %args = @_;
85              
86 3         10 my $loop = delete $args{loop};
87              
88 3         58 my $self = $class->SUPER::new( %args );
89              
90 3 50       767 if( defined $args{handle} ) {
    0          
91 3 50       12 $loop or croak "Require a 'loop' argument";
92              
93 3         23 $loop->add( $self );
94              
95 3         604 my $handle = delete $args{handle};
96              
97             # IO::Async version 0.27 requires this to support ->sockname method
98 3 50 33     22 bless $handle, "IO::Socket" if ref($handle) eq "GLOB" and defined getsockname($handle);
99              
100 3         13 $self->configure( handle => $handle );
101             }
102             elsif( defined $args{service} ) {
103 0 0       0 $loop or croak "Require a 'loop' argument";
104              
105 0         0 $loop->add( $self );
106              
107             $self->listen(
108             %args,
109              
110             # listen wants some error handling callbacks. Since this is a
111             # constructor it's reasonable to provide default 'croak' ones if
112             # they're not supplied
113 0     0   0 on_resolve_error => sub { croak "Resolve error $_[0] while constructing a " . __PACKAGE__ },
114 0     0   0 on_listen_error => sub { croak "Cannot listen while constructing a " . __PACKAGE__ },
115 0         0 );
116             }
117              
118 3         291 return $self;
119             }
120              
121             # Keep perl happy; keep Britain tidy
122             1;
123              
124             __END__