line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package AnyEvent::FCGI; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
AnyEvent::FCGI - non-blocking FastCGI server |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use AnyEvent; |
10
|
|
|
|
|
|
|
use AnyEvent::FCGI; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my $fcgi = new AnyEvent::FCGI( |
13
|
|
|
|
|
|
|
port => 9000, |
14
|
|
|
|
|
|
|
on_request => sub { |
15
|
|
|
|
|
|
|
my $request = shift; |
16
|
|
|
|
|
|
|
$request->respond( |
17
|
|
|
|
|
|
|
'OH HAI! QUERY_STRING is ' . $request->param('QUERY_STRING'), |
18
|
|
|
|
|
|
|
'Content-Type' => 'text/plain', |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $timer = AnyEvent->timer( |
24
|
|
|
|
|
|
|
after => 10, |
25
|
|
|
|
|
|
|
interval => 0, |
26
|
|
|
|
|
|
|
cb => sub { |
27
|
|
|
|
|
|
|
# shut down server after 10 seconds |
28
|
|
|
|
|
|
|
$fcgi = undef; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
AnyEvent->loop; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 DESCRIPTION |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
This module implements non-blocking FastCGI server for event based applications. |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=cut |
39
|
|
|
|
|
|
|
|
40
|
1
|
|
|
1
|
|
42477
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
50
|
|
41
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
135
|
|
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw/weaken refaddr/; |
|
1
|
|
|
|
|
6
|
|
|
1
|
|
|
|
|
255
|
|
46
|
|
|
|
|
|
|
|
47
|
1
|
|
|
1
|
|
744
|
use AnyEvent; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
use AnyEvent::Socket; |
49
|
|
|
|
|
|
|
use AnyEvent::Handle; |
50
|
|
|
|
|
|
|
use AnyEvent::FCGI::Connection; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
use constant FCGI_VERSION_1 => 1; |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
use constant FCGI_BEGIN_REQUEST => 1; |
55
|
|
|
|
|
|
|
use constant FCGI_ABORT_REQUEST => 2; |
56
|
|
|
|
|
|
|
use constant FCGI_END_REQUEST => 3; |
57
|
|
|
|
|
|
|
use constant FCGI_PARAMS => 4; |
58
|
|
|
|
|
|
|
use constant FCGI_STDIN => 5; |
59
|
|
|
|
|
|
|
use constant FCGI_STDOUT => 6; |
60
|
|
|
|
|
|
|
use constant FCGI_STDERR => 7; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
use constant FCGI_RESPONDER => 1; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
use constant FCGI_KEEP_CONN => 1; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
use constant FCGI_REQUEST_COMPLETE => 0; |
67
|
|
|
|
|
|
|
use constant FCGI_OVERLOADED => 2; |
68
|
|
|
|
|
|
|
use constant FCGI_UNKNOWN_ROLE => 3; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=head1 METHODS |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 new |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
This function creates a new FastCGI server and returns a new instance of a C object. |
75
|
|
|
|
|
|
|
To shut down the server just remove all references to this object. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head3 PARAMETERS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=over 4 |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item port => $port |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
The TCP port the FastCGI server will listen on. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item host => $host |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The TCP address of the FastCGI server will listen on. |
88
|
|
|
|
|
|
|
If undefined 0.0.0.0 will be used. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item socket => $path |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Path to UNIX domain socket to listen. If specified, C and C parameters ignored. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item on_request => sub { } |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
Reference to a handler to call when a new FastCGI request is received. |
97
|
|
|
|
|
|
|
It will be invoked as |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
$on_request->($request) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
where C<$request> will be a new L object. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=item backlog => $backlog |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Optional. Integer number of socket backlog (listen queue) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=back |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub new { |
112
|
|
|
|
|
|
|
my ($class, %params) = @_; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
my $self = bless { |
115
|
|
|
|
|
|
|
connections => {}, |
116
|
|
|
|
|
|
|
on_request_cb => $params{on_request}, |
117
|
|
|
|
|
|
|
}, $class; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
my $fcgi = $self; |
120
|
|
|
|
|
|
|
weaken($fcgi); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$params{socket} ||= $params{unix}; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
$self->{server} = tcp_server( |
125
|
|
|
|
|
|
|
$params{socket} ? 'unix/' : $params{host}, |
126
|
|
|
|
|
|
|
$params{socket} || $params{port}, |
127
|
|
|
|
|
|
|
sub {$fcgi->_on_accept(shift)}, |
128
|
|
|
|
|
|
|
$params{backlog} ? sub {$params{backlog}} : undef |
129
|
|
|
|
|
|
|
); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
return $self; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _on_accept { |
135
|
|
|
|
|
|
|
my ($self, $fh) = @_; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
if ($fh) { |
138
|
|
|
|
|
|
|
my $connection = new AnyEvent::FCGI::Connection(fcgi => $self, fh => $fh); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
$self->{connections}->{refaddr($connection)} = $connection; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub _request_ready { |
145
|
|
|
|
|
|
|
my ($self, $request) = @_; |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$self->{on_request_cb}->($request); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
sub DESTROY { |
151
|
|
|
|
|
|
|
my ($self) = @_; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
if ($self) { |
154
|
|
|
|
|
|
|
$self->{connections} = {}; |
155
|
|
|
|
|
|
|
} |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 SEE ALSO |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L, L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
This module based on L and L. |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 LICENSE |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
This module is free software; you can redistribute it and/or |
167
|
|
|
|
|
|
|
modify it under the same terms as Perl itself. See L. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 AUTHOR |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Vitaly Kramskikh, Evkramskih@cpan.orgE |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
1; |