File Coverage

blib/lib/FCGI/EV/Std.pm
Criterion Covered Total %
statement 26 59 44.0
branch 0 16 0.0
condition 0 7 0.0
subroutine 9 13 69.2
pod 0 2 0.0
total 35 97 36.0


line stmt bran cond sub pod time code
1             package FCGI::EV::Std;
2 1     1   59268 use 5.010001;
  1         5  
3 1     1   5 use warnings;
  1         1  
  1         19  
4 1     1   4 use strict;
  1         2  
  1         13  
5 1     1   472 use utf8;
  1         11  
  1         4  
6 1     1   24 use Carp;
  1         1  
  1         56  
7              
8             our $VERSION = 'v2.0.1';
9              
10 1     1   5 use Scalar::Util qw( weaken );
  1         1  
  1         53  
11              
12 1     1   580 use if $INC{'CGI.pm'}, 'CGI::Stateless';
  1         10  
  1         7  
13              
14              
15 1         95 use constant HTTP_417_EXPECTATION_FAILED =>
16             "Status: 417 Expectation Failed\r\n"
17             . "Content-Type: text/html\r\n"
18             . "\r\n"
19             . '417 Expectation Failed'
20             . '

Expectation Failed

'
21             . '

Request entity too large or incomplete.

'
22 1     1   57 ;
  1         2  
23              
24 1     1   5 use constant MiB => 1*1024*1024; ## no critic (Capitalization)
  1         1  
  1         494  
25              
26             our $BLOCKING = 1;
27             our $MAX_STDIN = MiB;
28             our $MAIN = \&main::main;
29             our $HUP = undef;
30              
31              
32             sub new {
33 0     0 0   my ($class, $server, $env) = @_;
34 0   0       my $len = $env->{CONTENT_LENGTH} || 0;
35 0           my $self = bless {
36             server => $server,
37             env => $env,
38             stdin => q{},
39             allow => $len <= $MAX_STDIN,
40             }, $class;
41 0           weaken($self->{server});
42 0           return $self;
43             }
44              
45             # No protection against reading STDIN larger than CONTENT_LENGTH - because
46             # FastCGI protocol FORBID this so web server shouldn't send so much data
47             # ( http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S6.2 ).
48             #
49             # If huge files will be uploaded it MAY have sense to save STDIN to
50             # secure tempfile - but code which parse POST data from STDIN in CGI also
51             # shouldn't read all STDIN into memory, or this feature will not help.
52             sub stdin {
53 0     0 0   my ($self, $stdin, $is_eof) = @_;
54 0 0         if ($self->{allow}) {
55 0           $self->{stdin} .= $stdin;
56             }
57 0 0         if ($is_eof) {
58 0 0 0       if (length $self->{stdin} != ($self->{env}{CONTENT_LENGTH} || 0)) {
59 0           $self->{server}->stdout(HTTP_417_EXPECTATION_FAILED, 1);
60             }
61             else {
62 0           local *STDIN;
63 0 0         open STDIN, '<', \$self->{stdin} or die "open STDIN: $!\n";
64 0           local %ENV = %{ $self->{env} };
  0            
65 0 0         if ($INC{'CGI/Stateless.pm'}) {
66 0           local $CGI::Q = CGI::Stateless->new();
67 0           $self->_run();
68             }
69             else {
70 0           $self->_run();
71             }
72             }
73             }
74 0           return;
75             }
76              
77             sub _run {
78 0     0     my ($self) = @_;
79 0 0         if ($BLOCKING) {
80 0           local *STDOUT;
81 0           my $reply = q{};
82 0 0         open STDOUT, '>', \$reply or die "open STDOUT: $!\n";
83 0           $MAIN->();
84 0           $self->{server}->stdout($reply, 1);
85             }
86             else {
87 0           $MAIN->($self->{server});
88             }
89 0           return;
90             }
91              
92             sub DESTROY {
93 0     0     my ($self) = @_;
94 0 0 0       if (!$BLOCKING && $HUP) {
95 0           $HUP->($self->{server});
96             }
97 0           return;
98             }
99              
100              
101             1; # Magic true value required at end of module
102             __END__