File Coverage

blib/lib/App/Charon/Web.pm
Criterion Covered Total %
statement 32 68 47.0
branch 0 14 0.0
condition 0 3 0.0
subroutine 11 19 57.8
pod 0 2 0.0
total 43 106 40.5


line stmt bran cond sub pod time code
1             package App::Charon::Web;
2             $App::Charon::Web::VERSION = '0.001003';
3             # ABSTRACT: Internal (for now) Plack app behind charon
4              
5 1     1   15683 use utf8;
  1         8  
  1         4  
6 1     1   469 use Web::Simple;
  1         36313  
  1         5  
7 1     1   2502 use warnings NONFATAL => 'all';
  1         1  
  1         23  
8              
9 1     1   364 use Plack::App::Directory;
  1         60761  
  1         27  
10 1     1   6 use Plack::App::File;
  1         1  
  1         407  
11              
12             has _quit => (
13             is => 'ro',
14             required => 1,
15             init_arg => 'quit',
16             );
17              
18             has _root => (
19             is => 'ro',
20             default => '.',
21             init_arg => 'root',
22             );
23              
24             has _query_param_auth => (
25             is => 'ro',
26             default => sub {
27             require App::Genpass;
28             [auth => scalar App::Genpass->new->generate]
29             },
30             init_arg => 'query_param_auth',
31             );
32              
33             has _show_index => (
34             is => 'ro',
35             default => 1,
36             init_arg => 'show_index',
37             );
38              
39             sub BUILDARGS {
40 0     0 0   my ($self, %params) = @_;
41              
42 0 0         $params{query_param_auth} = [split m/=/, $params{query_param_auth}, 2]
43             if exists $params{query_param_auth};
44              
45 0           \%params
46             }
47              
48             has _root_server => (
49             is => 'ro',
50             lazy => 1,
51             builder => sub {
52 0     0     my $self = shift;
53              
54 0 0         if ($self->_show_index) {
55 0           my $ret = Plack::App::Directory->new( root => $self->_root );
56 0           $ret = Plack::Middleware::FixLinks->new(
57 0           suffix => '?' . join('=', @{$self->_query_param_auth}),
58 0 0         )->wrap($ret) if @{$self->_query_param_auth};
59 0           return $ret
60             } else {
61 0           return Plack::App::File->new( root => $self->_root )
62             }
63             },
64             );
65              
66             sub dispatch_request {
67 0     0 0   my $self = shift;
68              
69 0           my @qpa = @{$self->_query_param_auth};
  0            
70             (@qpa ? ( "?$qpa[0]~" => sub {
71 0     0     my ($self, $value) = @_;
72              
73 0 0 0       return [403, ['Content-Type' => 'text/plain'], ['nice try']]
74             if !$value || $value ne $qpa[1];
75 0           return;
76             }) : ()),
77 0     0     '/quit' => sub { shift->_quit->done(1); [200, [], ''] },
  0            
78 0     0     '/_/...' => sub { $self->_root_server },
79 0 0         }
80              
81             1;
82              
83             BEGIN {
84             package Plack::Middleware::FixLinks;
85 1     1   1 $Plack::Middleware::FixLinks::VERSION = '0.001003';
86 1     1   5 use parent qw(Plack::Middleware);
  1         1  
  1         5  
87              
88 1     1   53 use Plack::Util;
  1         1  
  1         14  
89 1     1   426 use HTML::Zoom;
  1         7005  
  1         35  
90 1     1   6 use Plack::Util::Accessor qw(suffix);
  1         2  
  1         8  
91 1     1   549 use Plack::Response;
  1         1157  
  1         157  
92              
93             sub call {
94 0     0     my($self, $env) = @_;
95              
96 0           my $res = $self->app->($env);
97              
98 0           my $response = Plack::Response->new(@$res);
99 0 0         return $res unless $response->content_type eq 'text/html';
100             return $self->response_cb($res, sub {
101 0     0     my $res = shift;
102              
103 0           my $hz;
104              
105 0 0         if (ref $res->[2] eq 'ARRAY') {
106 0           $hz = HTML::Zoom->from_html($res->[2][0])
107             } else {
108 0           local $/ = undef;
109 0           my $fh = $res->[2];
110 0           $hz = HTML::Zoom->from_html(<$fh>)
111             }
112              
113             $res->[2] = $hz
114             ->select('a')
115 0           ->transform_attribute(href => sub { $_[0] . $self->suffix })
116 0           ->to_fh;
117              
118 0           return;
119 0           });
120             }
121              
122 1         18 1
123             }
124              
125             __END__