File Coverage

blib/lib/Plack/Handler/AnyEvent/SCGI.pm
Criterion Covered Total %
statement 15 57 26.3
branch 0 10 0.0
condition 0 6 0.0
subroutine 5 14 35.7
pod 0 5 0.0
total 20 92 21.7


line stmt bran cond sub pod time code
1             package Plack::Handler::AnyEvent::SCGI;
2              
3 2     2   30646 use strict;
  2         4  
  2         57  
4 2     2   36 use 5.008_001;
  2         6  
  2         87  
5             our $VERSION = '0.03';
6              
7 2     2   831 use AnyEvent::SCGI;
  2         71283  
  2         158  
8 2     2   1055 use URI::Escape;
  2         2159  
  2         107  
9 2     2   987 use Plack::Util;
  2         20417  
  2         1064  
10              
11             sub new {
12 0     0 0   my($class, %args) = @_;
13 0           bless { port => 9999, %args }, $class;
14             }
15              
16             sub run {
17 0     0 0   my $self = shift;
18 0           $self->register_service(@_);
19 0           $self->{_cv}->recv;
20             }
21              
22             sub register_service {
23 0     0 0   my($self, $app) = @_;
24              
25             $self->{_server} = scgi_server $self->{host} || '127.0.0.1', $self->{port}, sub {
26 0     0     my($handle, $env, $content_r, $fatal, $error) = @_;
27 0 0         $self->handle_request($app, $handle, $env, $content_r) unless $fatal;
28 0   0       };
29              
30 0           $self->{_cv} = AE::cv;
31             }
32              
33             sub handle_request {
34 0     0 0   my($self, $app, $handle, $env, $content_r) = @_;
35              
36 0           delete $env->{HTTP_CONTENT_TYPE};
37 0           delete $env->{HTTP_CONTENT_LENGTH};
38 0           ($env->{PATH_INFO}, $env->{QUERY_STRING}) = split /\?/, $env->{REQUEST_URI};
39 0           $env->{PATH_INFO} = URI::Escape::uri_unescape $env->{PATH_INFO};
40 0           $env->{SCRIPT_NAME} = '';
41 0           $env->{SERVER_NAME} =~ s/:\d+$//; # lighttpd bug?
42              
43             $env = {
44             %$env,
45             'psgi.version' => [1,1],
46             'psgi.url_scheme' => ($env->{HTTPS}||'off') =~ /^(?:on|1)$/i ? 'https' : 'http',
47 0 0 0       'psgi.input' => do { open my $io, "<", $content_r || \''; $io },
  0   0        
  0            
48             'psgi.errors' => *STDERR,
49             'psgi.multithread' => Plack::Util::FALSE,
50             'psgi.multiprocess' => Plack::Util::FALSE,
51             'psgi.run_once' => Plack::Util::FALSE,
52             'psgi.streaming' => Plack::Util::TRUE,
53             'psgi.nonblocking' => Plack::Util::TRUE,
54             'psgix.input.buffered' => Plack::Util::TRUE,
55             };
56              
57 0           my $res = Plack::Util::run_app $app, $env;
58              
59 0 0         if (ref $res eq 'ARRAY') {
    0          
60 0           $self->handle_response($res, $handle);
61             } elsif (ref $res eq 'CODE') {
62 0     0     $res->(sub { $self->handle_response($_[0], $handle) });
  0            
63             } else {
64 0           die "Bad response $res";
65             }
66             }
67              
68             sub handle_response {
69 0     0 0   my($self, $res, $handle) = @_;
70              
71 0           my $hdrs;
72 0           $hdrs = "Status: $res->[0]\015\012";
73              
74 0           my $headers = $res->[1];
75 0           while (my ($k, $v) = splice @$headers, 0, 2) {
76 0           $hdrs .= "$k: $v\015\012";
77             }
78 0           $hdrs .= "\015\012";
79              
80 0           $handle->push_write($hdrs);
81              
82 0     0     my $cb = sub { $handle->push_write($_[0]) };
  0            
83 0           my $body = $res->[2];
84 0 0         if (defined $body) {
85 0           Plack::Util::foreach($body, $cb);
86 0           $handle->push_shutdown;
87             } else {
88             return Plack::Util::inline_object
89             write => $cb,
90 0     0     close => sub { $handle->push_shutdown };
  0            
91             }
92             }
93              
94             1;
95             __END__