/usr/share/perl5/AnyEvent/FCGI/Request.pm is in libanyevent-fcgi-perl 0.04-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | package AnyEvent::FCGI::Request;
=head1 NAME
AnyEvent::FCGI::Request - a single FastCGI request handle for L<AnyEvent::FCGI>
=head1 SYNOPSIS
use AnyEvent;
use AnyEvent::FCGI;
use CGI::Stateless;
my $fcgi = new AnyEvent::FCGI(
port => 9000,
on_request => sub {
my $request = shift;
local *STDIN; open STDIN, '<', \$request->read_stdin;
local %ENV = %{$request->params};
local $CGI::Q = new CGI::Stateless;
$request->respond(
'Hello, ' . (CGI::param('name') || 'anonymous'),
'Content-Type' => 'text/html; charset=utf8',
'Set-Cookie' => 'cookie_a=1; path=/',
'Set-Cookie' => 'cookie_b=2; path=/',
);
}
);
AnyEvent->loop;
=head1 DESCRIPTION
This is the request object as generated by L<AnyEvent::FCGI>.
When given to the controlling program, each request will already have its parameters and STDIN data.
The program can then write response data to the STDOUT stream,
messages to the STDERR stream, and eventually finish it.
This module would not be used directly by a program using C<AnyEvent::FCGI>, but
rather, objects in this class are passed into the C<on_request> callback of
the containing C<AnyEvent::FCGI> object.
=cut
use strict;
use warnings;
use Scalar::Util qw/weaken/;
sub new {
my ($class, %params) = @_;
my $self = bless {
id => $params{id},
fcgi => $params{fcgi},
connection => $params{connection},
flags => $params{flags},
stdin => '',
stdin_done => 0,
params => {},
params_string => '',
params_done => 0,
used_stderr => 0,
}, $class;
weaken($self->{fcgi});
weaken($self->{connection});
return $self;
}
sub _ready_check {
my $self = shift;
if ($self->{stdin_done} && $self->{params_done} && $self->{fcgi}) {
$self->{fcgi}->_request_ready($self);
}
}
sub _process_stdin_record {
my ($self, $record) = @_;
if ($record->{length}) {
$self->{stdin} .= $record->{content};
} else {
$self->{stdin_done} = 1;
}
$self->_ready_check;
}
sub _process_params_record {
my ($self, $record) = @_;
if ($record->{length}) {
$self->{params_string} .= $record->{content};
} else {
$self->{params_done} = 1;
my $data = $self->{params_string};
while (length $data) {
my ($name_length, $value_length);
foreach my $length ($name_length, $value_length) {
($length) = unpack('C', $data);
if ($length & 0x80) {
($length) = unpack('N', $data);
$length &= 0x7FFFFFFF;
substr($data, 0, 4, '');
} else {
substr($data, 0, 1, '');
}
}
my $name = substr($data, 0, $name_length, '');
my $value = substr($data, 0, $value_length, '');
$self->{params}->{$name} = $value;
}
$self->{params_string} = '';
}
$self->_ready_check;
}
sub _send_record {
my ($self, $record) = @_;
if ($self->is_active) {
$self->{connection}->send_record($record);
} else {
warn 'Cannot respond to inactive request';
}
}
sub _print_stream {
my ($self, $data, $stream) = @_;
while (length $data) {
my $chunk = substr($data, 0, AnyEvent::FCGI::Connection->MAX_DATA_SIZE, '');
$self->_send_record({
request_id => $self->{id},
type => $stream,
content => $chunk,
});
}
}
=head1 METHODS
=head2 is_active
Returns false if the webserver has already closed the control connection.
No further work on this request is necessary, as it will be discarded.
This method can be used if response will not be sent immediately from C<on_request> callback.
=cut
sub is_active {
return defined shift->{connection};
}
=head2 param($key)
This method returns the value of a single request parameter, or C<undef> if no such key exists.
=cut
sub param {
my ($self, $key) = @_;
return $self->{params}->{$key};
}
=head2 params
This method returns a reference to a hash containing a copy of the request
parameters that had been sent by the webserver as part of the request.
=cut
sub params {
my ($self) = @_;
return {%{$self->{params}}};
}
=head2 read_stdin($size)
This method works similarly to the C<read(HANDLE)> function. It returns the
next block of up to $size bytes from the STDIN buffer. If no data is available
any more, then C<undef> is returned instead.
=cut
sub read_stdin {
my ($self, $size) = @_;
$size ||= length $self->{stdin};
return length $self->{stdin} ? substr($self->{stdin}, 0, $size, '') : undef;
}
=head2 print_stdout($data)
This method appends the given data to the STDOUT stream of the FastCGI
request, sending it to the webserver to be sent to the client.
=cut
sub print_stdout {
my ($self, $data) = @_;
$self->_print_stream($data, AnyEvent::FCGI->FCGI_STDOUT);
}
=head2 print_stderr($data)
This method appends the given data to the STDERR stream of the FastCGI
request, sending it to the webserver.
=cut
sub print_stderr {
my ($self, $data) = @_;
$self->{used_stderr} = 1;
$self->_print_stream($data, AnyEvent::FCGI->FCGI_STDERR);
}
=head2 finish
When the request has been dealt with, this method should be called to indicate
to the webserver that it is finished. After calling this method, no more data
may be appended to the STDOUT stream.
=cut
sub finish {
my ($self) = @_;
if ($self->is_active) {
$self->_send_record({
request_id => $self->{id},
type => AnyEvent::FCGI->FCGI_STDOUT,
content => '',
});
$self->_send_record({
request_id => $self->{id},
type => AnyEvent::FCGI->FCGI_STDERR,
content => '',
}) if $self->{used_stderr};
$self->_send_record({
request_id => $self->{id},
type => AnyEvent::FCGI->FCGI_END_REQUEST,
content => pack('Ncccc', 0, AnyEvent::FCGI->FCGI_REQUEST_COMPLETE, 0, 0, 0),
});
$self->{connection}->{io}->push_shutdown unless $self->{flags} & AnyEvent::FCGI->FCGI_KEEP_CONN;
delete $self->{connection}->{requests}->{$self->{id}};
delete $self->{connection};
} else {
warn 'Cannot finish inactive request';
}
}
=head2 respond($content, @headers)
This method sends the response to the webserver and finishes the request.
HTTP reply code can be specified in C<Status> header (200 by default).
This method can be used instead of C<print_stdout> and C<finish>.
=cut
sub respond {
my ($self, $content, @headers) = @_;
if ($self->is_active) {
my $has_status;
my $output = '';
while (scalar @headers) {
my $header = shift @headers;
$has_status = 1 if $header eq 'Status';
$output .= $header . ': ' . shift(@headers) . "\n";
}
$output .= "Status: 200 OK\n" unless $has_status;
$output .= "\n$content";
$self->print_stdout($output);
$self->finish;
} else {
warn 'Cannot respond to inactive request';
}
}
1;
|