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
|
package Net::FastCGI::IO;
use strict;
use warnings;
use warnings::register;
use Carp qw[];
use Errno qw[EBADF EINTR EPIPE];
use Net::FastCGI::Constant qw[FCGI_HEADER_LEN];
use Net::FastCGI::Protocol qw[build_header build_record build_stream
parse_header parse_record];
BEGIN {
our $VERSION = '0.14';
our @EXPORT_OK = qw[ can_read
can_write
read_header
read_record
write_header
write_record
write_stream ];
our %EXPORT_TAGS = ( all => \@EXPORT_OK );
require Exporter;
*import = \&Exporter::import;
eval q<use Time::HiRes 'time'>;
}
*throw = \&Carp::croak;
sub read_header {
@_ == 1 || throw(q/Usage: read_header(fh)/);
my ($fh) = @_;
my $len = FCGI_HEADER_LEN;
my $off = 0;
my $buf;
while ($len) {
my $r = sysread($fh, $buf, $len, $off);
if (defined $r) {
last unless $r;
$len -= $r;
$off += $r;
}
elsif ($! != EINTR) {
warnings::warn(qq<FastCGI: Could not read FCGI_Header: '$!'>)
if warnings::enabled;
return;
}
}
if ($len) {
$! = $off ? EPIPE : 0;
warnings::warn(q<FastCGI: Could not read FCGI_Header: Unexpected end of stream>)
if $off && warnings::enabled;
return;
}
return parse_header($buf);
}
sub write_header {
@_ == 5 || throw(q/Usage: write_header(fh, type, request_id, content_length, padding_length)/);
my $fh = shift;
my $buf = &build_header;
my $len = FCGI_HEADER_LEN;
my $off = 0;
while () {
my $r = syswrite($fh, $buf, $len, $off);
if (defined $r) {
$len -= $r;
$off += $r;
last unless $len;
}
elsif ($! != EINTR) {
warnings::warn(qq<FastCGI: Could not write FCGI_Header: '$!'>)
if warnings::enabled;
return undef;
}
}
return $off;
}
sub read_record {
@_ == 1 || throw(q/Usage: read_record(fh)/);
my ($fh) = @_;
my $len = FCGI_HEADER_LEN;
my $off = 0;
my $buf;
while ($len) {
my $r = sysread($fh, $buf, $len, $off);
if (defined $r) {
last unless $r;
$len -= $r;
$off += $r;
if (!$len && $off == FCGI_HEADER_LEN) {
$len = vec($buf, 2, 16) # Content Length
+ vec($buf, 6, 8); # Padding Length
}
}
elsif ($! != EINTR) {
warnings::warn(qq<FastCGI: Could not read FCGI_Record: '$!'>)
if warnings::enabled;
return;
}
}
if ($len) {
$! = $off ? EPIPE : 0;
warnings::warn(q<FastCGI: Could not read FCGI_Record: Unexpected end of stream>)
if $off && warnings::enabled;
return;
}
return parse_record($buf);
}
sub write_record {
@_ == 4 || @_ == 5 || throw(q/Usage: write_record(fh, type, request_id [, content])/);
my $fh = shift;
my $buf = &build_record;
my $len = length $buf;
my $off = 0;
while () {
my $r = syswrite($fh, $buf, $len, $off);
if (defined $r) {
$len -= $r;
$off += $r;
last unless $len;
}
elsif ($! != EINTR) {
warnings::warn(qq<FastCGI: Could not write FCGI_Record: '$!'>)
if warnings::enabled;
return undef;
}
}
return $off;
}
sub write_stream {
@_ == 4 || @_ == 5 || throw(q/Usage: write_stream(fh, type, request_id, content [, terminate])/);
my $fh = shift;
my $buf = &build_stream;
my $len = length $buf;
my $off = 0;
while () {
my $r = syswrite($fh, $buf, $len, $off);
if (defined $r) {
$len -= $r;
$off += $r;
last unless $len;
}
elsif ($! != EINTR) {
warnings::warn(qq<FastCGI: Could not write FCGI_Record stream: '$!'>)
if warnings::enabled;
return undef;
}
}
return $off;
}
sub can_read (*$) {
@_ == 2 || throw(q/Usage: can_read(fh, timeout)/);
my ($fh, $timeout) = @_;
my $fd = fileno($fh);
unless (defined $fd && $fd >= 0) {
$! = EBADF;
return undef;
}
my $initial = time;
my $pending = $timeout;
my $nfound;
vec(my $fdset = '', $fd, 1) = 1;
while () {
$nfound = select($fdset, undef, undef, $pending);
if ($nfound == -1) {
return undef unless $! == EINTR;
redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0;
$nfound = 0;
}
last;
}
$! = 0;
return $nfound;
}
sub can_write (*$) {
@_ == 2 || throw(q/Usage: can_write(fh, timeout)/);
my ($fh, $timeout) = @_;
my $fd = fileno($fh);
unless (defined $fd && $fd >= 0) {
$! = EBADF;
return undef;
}
my $initial = time;
my $pending = $timeout;
my $nfound;
vec(my $fdset = '', $fd, 1) = 1;
while () {
$nfound = select(undef, $fdset, undef, $pending);
if ($nfound == -1) {
return undef unless $! == EINTR;
redo if !$timeout || ($pending = $timeout - (time - $initial)) > 0;
$nfound = 0;
}
last;
}
$! = 0;
return $nfound;
}
1;
|