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   24944 use strict;
  1         2  
  1         26  
3 1     1   3 use warnings;
  1         2  
  1         39  
4             our $VERSION = '1.0.0'; # VERSION
5 1     1   561 use parent 'CGI::Emulate::PSGI';
  1         295  
  1         4  
6 1     1   36641 use CGI::Parse::PSGI::Streaming;
  1         2  
  1         25  
7 1     1   4 use SelectSaver;
  1         1  
  1         14  
8 1     1   3 use Carp qw(croak);
  1         1  
  1         35  
9 1     1   17 use 5.008001;
  1         2  
10              
11             # ABSTRACT: streaming PSGI adapter for CGI
12              
13              
14             sub handler {
15 1     1 1 49 my ($class, $code) = @_;
16              
17             # this closure is the PSGI application
18             return sub {
19 1     1   1317 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         1124 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         7 my $stdout = CGI::Parse::PSGI::Streaming::parse_cgi_output_streaming_fh($responder);
30              
31 1         8 my $saver = SelectSaver->new("::STDOUT");
32             # emulate_environment comes from CGI::Emulate::PSGI
33 1         41 local %ENV = (%ENV, $class->emulate_environment($env));
34              
35 1         107 local *STDIN = $env->{'psgi.input'};
36 1         2 local *STDOUT = $stdout;
37 1         2 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         9 close $stdout;
43 1         6 };
44 1         4 };
45             }
46              
47              
48             1;
49              
50             __END__