File Coverage

blib/lib/CGI/Emulate/PSGI/Streaming.pm
Criterion Covered Total %
statement 33 33 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 43 43 100.0


line stmt bran cond sub pod time code
1             package CGI::Emulate::PSGI::Streaming;
2 1     1   35731 use strict;
  1         9  
  1         32  
3 1     1   6 use warnings;
  1         1  
  1         44  
4             our $VERSION = '1.0.1'; # VERSION
5 1     1   472 use parent 'CGI::Emulate::PSGI';
  1         308  
  1         5  
6 1     1   46941 use CGI::Parse::PSGI::Streaming;
  1         3  
  1         42  
7 1     1   7 use SelectSaver;
  1         2  
  1         22  
8 1     1   5 use Carp qw(croak);
  1         2  
  1         45  
9 1     1   19 use 5.008001;
  1         3  
10              
11             # ABSTRACT: streaming PSGI adapter for CGI
12              
13              
14             sub handler {
15 1     1 1 114 my ($class, $code) = @_;
16              
17             # this closure is the PSGI application
18             return sub {
19 1     1   759 my $env = shift;
20              
21             # this is the PSGI response, as a coderef because we want to
22             # have it streaming; as the PSGI spec says, it will be invoked
23             # by the backend, once per request, with a responder callback
24             return sub {
25 1         1413 my ($responder) = @_;
26              
27             # we have the responder (i.e. the thing we will send the
28             # response to), so we can now build the output filehandle
29 1         4 my $stdout = CGI::Parse::PSGI::Streaming::parse_cgi_output_streaming_fh($responder);
30              
31 1         9 my $saver = SelectSaver->new("::STDOUT");
32             # emulate_environment comes from CGI::Emulate::PSGI
33 1         48 local %ENV = (%ENV, $class->emulate_environment($env));
34              
35 1         166 local *STDIN = $env->{'psgi.input'};
36 1         3 local *STDOUT = $stdout;
37 1         3 local *STDERR = $env->{'psgi.errors'};
38              
39             # call the CGI code, and let it print to its heart's content
40 1         5 $code->();
41             # explicit close, to make sure the response is finalised
42 1         12 close $stdout;
43 1         8 };
44 1         5 };
45             }
46              
47              
48             1;
49              
50             __END__