File Coverage

blib/lib/Plack/Handler/AnyEvent/ReverseHTTP.pm
Criterion Covered Total %
statement 18 52 34.6
branch 0 8 0.0
condition n/a
subroutine 6 10 60.0
pod 0 3 0.0
total 24 73 32.8


line stmt bran cond sub pod time code
1             package Plack::Handler::AnyEvent::ReverseHTTP;
2 1     1   6 use strict;
  1         1  
  1         40  
3 1     1   32 use 5.008_001;
  1         11  
  1         48  
4             our $VERSION = '0.04';
5              
6 1     1   1001 use AnyEvent::ReverseHTTP;
  1         114352  
  1         71  
7 1     1   859 use HTTP::Message::PSGI;
  1         23464  
  1         56  
8 1     1   9 use HTTP::Response;
  1         2  
  1         21  
9 1     1   4 use Plack::Util;
  1         2  
  1         495  
10              
11             sub new {
12 0     0 0   my($class, %args) = @_;
13 0           bless \%args, $class;
14             }
15              
16             sub register_service {
17 0     0 0   my($self, $app) = @_;
18             $self->{guard} = reverse_http $self->{host}, $self->{token}, sub {
19 0     0     my $req = shift;
20 0           my $env = $req->to_psgi;
21              
22 0 0         if (my $client = delete $env->{HTTP_REQUESTING_CLIENT}) {
23 0           @{$env}{qw( REMOTE_ADDR REMOTE_PORT )} = split /:/, $client, 2;
  0            
24             }
25              
26 0           $env->{'psgi.nonblocking'} = Plack::Util::TRUE;
27 0           $env->{'psgi.streaming'} = Plack::Util::TRUE;
28 0           $env->{'psgi.multithread'} = Plack::Util::FALSE;
29 0           $env->{'psgi.multiprocess'} = Plack::Util::FALSE;
30 0           $env->{'psgi.run_once'} = Plack::Util::FALSE;
31              
32 0           my $r = $app->($env);
33 0 0         if (ref $r eq 'ARRAY') {
    0          
34 0           return HTTP::Response->from_psgi($r);
35             } elsif (ref $r eq 'CODE') {
36 0           my $cv = AE::cv;
37             $r->(sub {
38 0           my $r = shift;
39              
40 0 0         if (defined $r->[2]) {
41 0           my $res = HTTP::Response->from_psgi($r);
42 0           $cv->send($res);
43             } else {
44 0           my $res = HTTP::Response->from_psgi([ $r->[0], $r->[1], [] ]); # dummy
45 0           my @body;
46             return Plack::Util::inline_object
47 0           write => sub { push @body, $_[0] },
48 0           close => sub { $res->content(join '', @body); $cv->send($res) };
  0            
  0            
49             }
50 0           });
51 0           return $cv;
52             } else {
53 0           die "Bad response: $r";
54             }
55 0           };
56             }
57              
58             sub run {
59 0     0 0   my $self = shift;
60 0           $self->register_service(@_);
61 0           AE::cv->recv;
62             }
63              
64             1;
65              
66             __END__