blib/lib/Plack/Handler/CLI.pm

filter: Statementstmt 89.5% 77 / 86 Branchbran 70.5% 24 / 34 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 86.1% 124 / 144 CC 20 SCAR 24.1
CC
20
Cov
85%
run
20.8
_handle_response
3.0
BEGIN
2.3
CRAP 11.2
↑ Summary
1 package Plack::Handler::CLI;
2 3 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.05';
6  
7 3 ▶use IO::Handle (); # autoflush
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Plack::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 _RES_STATUS => 0,
13 _RES_HEADERS => 1,
14 _RES_BODY => 2,
15 3 };
16  
17 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=8.4
18 3 ▶ if(eval { require URI::Escape::XS }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require URI::Escape::XS } }) { } 3 0 3
19 3 *_uri_escape = \&URI::Escape::XS::encodeURIComponent;
20 }
21 else {
22 0 require URI::Escape;
23 0 *_uri_escape = \&URI::Escape::uri_escape_utf8;
24 }
25 }
26  
27 3 my $CRLF = "\015\012";
28  
29 3 has need_headers => (
30 is => 'ro',
31 isa => 'Bool',
32 default => 1,
33 );
34  
35 has stdin => (
36 is => 'ro',
37 isa => 'FileHandle',
38 39 ▶ default => sub { \*STDIN },
Subroutine __ANON__: called CC=1 SCAR=0.0
39 3 );
40  
41 has stdout => (
42 is => 'ro',
43 isa => 'FileHandle',
44 37 ▶ default => sub { \*STDOUT },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 3 );
46  
47 has stderr => (
48 is => 'ro',
49 isa => 'FileHandle',
50 39 ▶ default => sub { \*STDERR },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 3 );
52  
53 43 ▶sub run {
Subroutine run: called CC=17 SCAR=30.3
Pod documented
54 43 my($self, $app, $argv_ref) = @_;
55  
56 43 my @argv;
57 43 ▶ if($argv_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argv_ref) { } 6 37 43
58 6 @argv = @{$argv_ref};
59 }
60 else {
61 # skip after *.psgi
62 37 @argv = @ARGV;
63 0 ▶ if(grep { /\.psgi \z/xms } @argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/\.psgi \z/msx;} @argv) 0 37 37
64 0 ▶ while(@argv) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
65 0 my $a = shift @argv;
66 0 ▶ last if $a =~ /\.psgi \z/xms;
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ /\.psgi \z/msx 0 0 0
67 }
68 }
69 }
70  
71 43 my @params;
72 43 ▶ while(defined(my $s = shift @argv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $s = shift @argv)) 8 39 47
73 8 ▶ if($s =~ s/\A -- //xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ s/\A -- //msx) { } 4 4 8
74 4 my($name, $value) = split /=/, $s, 2;
75 4 ▶ if(not defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 3 1 4
76 3 ▶ $value = @argv
Branch: 1 branch
BranchTrueFalseTotal
@argv ? : 3 0 3
77 ? shift(@argv)
78 : Plack::Util::TRUE;
79 }
80 4 push @params, $name, $value;
81 }
82 else {
83 4 unshift @argv, $s; # push back
84 4 last;
85 }
86 }
87  
88 43 my $uri = URI->new();
89 43 ▶ if ( @argv && $argv[0] =~ m{\Ahttp} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@argv and $argv[0] =~ /\Ahttp/) 3 40 43
Condition100%3/3
@argv and $argv[0] =~ /\Ahttp/
!ll&&!rl&&r
3913
MC/DC100%2/2
@argv and $argv[0] =~ /\Ahttp/
@argv $argv[0] =~ /\Ahttp/
Truth table100%3/3
A and B
A= @argvB= $argv[0] =~ /\Ahttp/
ABresult
0X0
100
111
90 3 $uri = URI->new(shift @argv);
91 }
92  
93 43 ▶ $uri->scheme('http') if not $uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->scheme 40 3 43
94 43 ▶ $uri->host('localhost') if not $uri->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri->host 40 3 43
95 43 $uri->path_segments($uri->path_segments, @argv);
96 43 $uri->query_form($uri->query_form, @params);
97  
98 my %env = (
99 # HTTP
100 HTTP_USER_AGENT => sprintf('%s/%s', ref($self), $self->VERSION),
101  
102 HTTP_COOKIE => '', # TODO?
103 HTTP_HOST => $uri->host,
104  
105 # Client
106 REQUEST_METHOD => 'GET',
107 REQUEST_URI => $uri,
108 QUERY_STRING => $uri->query,
109 PATH_INFO => $uri->path || '/',
110 SCRIPT_NAME => '',
111 REMOTE_ADDR => '0.0.0.0',
112 REMOTE_USER => $ENV{USER},
113  
114 # Server
115 43 ▶ SERVER_PROTOCOL => 'HTTP/1.0',
Condition100%2/2
$uri->path || '/'
l!l
340
MC/DC100%1/1
$uri->path || '/'
$uri->path
Truth table100%2/2
A
A= $uri->path
Aresult
11
00
116 SERVER_PORT => 0,
117 SERVER_NAME => 'localhost',
118 SERVER_SOFTWARE => ref($self),
119  
120 # PSGI
121 'psgi.version' => [1,1],
122 'psgi.url_scheme' => $uri->scheme,
123 'psgi.input' => $self->stdin,
124 'psgi.errors' => $self->stderr,
125 'psgi.multithread' => Plack::Util::FALSE,
126 'psgi.multiprocess' => Plack::Util::TRUE,
127 'psgi.run_once' => Plack::Util::TRUE,
128 'psgi.streaming' => Plack::Util::FALSE,
129 'psgi.nonblocking' => Plack::Util::FALSE,
130  
131 %ENV, # override
132 );
133 43 ▶ $env{SCRIPT_NAME} = '' if $env{SCRIPT_NAME} eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $env{"SCRIPT_NAME"} eq "/" 37 6 43
134  
135 43 my $res = Plack::Util::run_app($app, \%env);
136  
137 43 ▶ if (ref $res eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 43 0 43
elsif (ref $res eq 'CODE') { } 0 0 0
138 43 $self->_handle_response($res);
139 }
140 elsif (ref $res eq 'CODE') {
141 0 ▶ $res->(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
142 0 $self->_handle_response($_[0]);
143 0 });
144 }
145 else {
146 0 die "Bad response $res";
147 }
148 }
149  
150 43 ▶sub _handle_response {
Subroutine _handle_response: called CC=3 SCAR=11.0
151 43 my ($self, $res) = @_;
152  
153 43 my $stdout = $self->stdout;
154  
155 43 $stdout->autoflush(1);
156  
157 43 ▶ if($self->need_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->need_headers) 40 3 43
158 40 my $hdrs = "Status: $res->[_RES_STATUS]" . $CRLF;
159  
160 40 $hdrs .= "Server: " . ref($self) . $CRLF;
161  
162 40 my $headers = $res->[_RES_HEADERS];
163 40 ▶ while (my ($k, $v) = splice @$headers, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$headers, 0, 2)) 52 40 92
164 52 $hdrs .= "$k: $v" . $CRLF;
165 }
166 40 $hdrs .= $CRLF;
167  
168 40 print $stdout $hdrs;
169 }
170  
171 43 my $body = $res->[_RES_BODY];
172 43 ▶ my $cb = sub { print $stdout @_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
173 43 Plack::Util::foreach($body, $cb);
174 43 return;
175 }
176  
177 3 ▶no Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
178 3 __PACKAGE__->meta->make_immutable();
179 __END__
↑ Summary