| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | while(@argv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | my $a = shift @argv; | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | last if $a =~ /\.psgi \z/xms; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 43 | my @params; | |||||||||||||||||||||||||||
| 72 | 43 | ▶ | while(defined(my $s = shift @argv)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 8 | ▶ | if($s =~ s/\A -- //xms) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 4 | my($name, $value) = split /=/, $s, 2; | |||||||||||||||||||||||||||
| 75 | 4 | ▶ | if(not defined $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 3 | ▶ | $value = @argv | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
@argv and $argv[0] =~ /\Ahttp/
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/
| |||||||||||||||||||||||||||||
| 90 | 3 | $uri = URI->new(shift @argv); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 43 | ▶ | $uri->scheme('http') if not $uri->scheme; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 43 | ▶ | $uri->host('localhost') if not $uri->host; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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 || '/'
MC/DC100%1/1
$uri->path || '/'
$uri->path
Truth table100%2/2
A
A= $uri->path
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 43 | my $res = Plack::Util::run_app($app, \%env); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 43 | ▶ | if (ref $res eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||