blib/lib/Pinto/Server/Responder/Action.pm

filter: Statementstmt 89.7% 123 / 137 Branchbran 83.3% 20 / 24 Conditioncond 54.5% 6 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 86.6% 26 / 30 Podpod 25.0% 1 / 4 totaltotal 83.4% 177 / 212 CC 18 SCAR 12.7
CC
18
Cov
87%
__ANON__
9.1
respond
6.6
check_protocol_version
5.0
CRAP 3.6
« blib/lib/Pinto/Schema.pm ↑ Summary blib/lib/Pinto/Action/Add.pm »
1 # ABSTRACT: Responder for action requests
2  
3 package Pinto::Server::Responder::Action;
4  
5 13 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use IO::Pipe;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 13 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Proc::Fork;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use Proc::Terminator;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use Plack::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 ▶use Pinto;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶use Pinto::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 13 ▶use Pinto::Chrome::Net;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 13 ▶use Pinto::Constants qw(:protocol);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #-------------------------------------------------------------------------------
26  
27 13 our $VERSION = '0.14'; # VERSION
28  
29 #-------------------------------------------------------------------------------
30  
31 13 extends qw(Pinto::Server::Responder);
32  
33 #-------------------------------------------------------------------------------
34  
35 59 ▶sub respond {
Subroutine respond: called CC=6 SCAR=18.8
Pod documented
36 59 my ($self) = @_;
37  
38 59 my $error_response = $self->check_protocol_version;
39 59 ▶ return $error_response if $error_response;
Branch: 1 branch
BranchTrueFalseTotal
if $error_response 3 56 59
40  
41 # path_info always has a leading slash, e.g. /action/list
42 56 my ( undef, undef, $action_name ) = split '/', $self->request->path_info;
43  
44 56 my %params = %{ $self->request->parameters }; # Copying
45 56 ▶ my $chrome_args = $params{chrome} ? decode_json( $params{chrome} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$params{'chrome'} ? : 0 56 56
46 56 ▶ my $pinto_args = $params{pinto} ? decode_json( $params{pinto} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$params{'pinto'} ? : 0 56 56
47 56 ▶ my $action_args = $params{action} ? decode_json( $params{action} ) : {};
Branch: 1 branch
BranchTrueFalseTotal
$params{'action'} ? : 47 9 56
48  
49 56 for my $upload_name ( $self->request->uploads->keys ) {
50 0 my $upload = $self->request->uploads->{$upload_name};
51 0 my $basename = $upload->filename;
52 0 my $localfile = file( $upload->path )->dir->file($basename);
53 0 File::Copy::move( $upload->path, $localfile ); #TODO: autodie
54 0 $action_args->{$upload_name} = $localfile;
55 }
56  
57 56 my $response;
58 56 my $pipe = IO::Pipe->new;
59  
60 56 ▶ run_fork {
Subroutine __ANON__: called CC=1 SCAR=0.1
61 10 ▶ child { $self->child_proc( $pipe, $chrome_args, $pinto_args, $action_name, $action_args ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
62 46 ▶ parent { my $child_pid = shift; $response = $self->parent_proc( $pipe, $child_pid ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
63 0 ▶ error { croak "Failed to fork: $!" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
64 56 };
65  
66 46 return $response;
67 }
68  
69 #-------------------------------------------------------------------------------
70  
71 59 ▶sub check_protocol_version {
Subroutine check_protocol_version: called CC=5 SCAR=16.1
Pod undocumented
72 59 my ($self) = @_;
73  
74 # NB: Format derived from GitHub: https://developer.github.com/v3/media
75 59 my $media_type_rx = qr{^ application / vnd [.] pinto [.] v(\d+) (?:[+] .+)? $}ix;
76  
77 59 ▶ my $accept = $self->request->header('Accept') || '';
Condition100%2/2
$self->request->header('Accept') || ''
l!l
572
MC/DC100%1/1
$self->request->header('Accept') || ''
$self->request->header('Accept')
Truth table100%2/2
A
A= $self->request->header('Accept')
Aresult
11
00
78 59 ▶ my $version = $accept =~ $media_type_rx ? $1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$accept =~ /$media_type_rx/ ? : 57 2 59
79  
80 59 ▶ return unless my $cmp = $version <=> $PINTO_PROTOCOL_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
unless my $cmp = $version <=> $Pinto::Server::Responder::Action::PINTO_PROTOCOL_VERSION 56 3 59
81  
82 3 my $fmt = 'Your client is too %s for this server. You must upgrade %s.';
83 3 ▶ my ($age, $component) = $cmp > 0 ? qw(new pintod) : qw(old pinto);
Branch: 1 branch
BranchTrueFalseTotal
$cmp > 0 ? : 1 2 3
84 3 my $msg = sprintf $fmt, $age, $component;
85  
86 3 return [ HTTP_UNSUPPORTED_MEDIA_TYPE, [], [$msg] ];
87 }
88  
89 #-------------------------------------------------------------------------------
90  
91 10 ▶sub child_proc {
Subroutine child_proc: called CC=2 SCAR=7.1
Pod undocumented
92 10 my ( $self, $pipe, $chrome_args, $pinto_args, $action_name, $action_args ) = @_;
93  
94 10 my $writer = $pipe->writer;
95 10 $writer->autoflush;
96  
97 # I'm not sure why, but cleanup isn't happening when we get
98 # a TERM signal from the parent process. I suspect it
99 # has something to do with File::NFSLock messing with %SIG
100 0 ▶ local $SIG{TERM} = sub { File::Temp::cleanup; die $@ };
Subroutine __ANON__: not called CC=1 SCAR=2.6
101  
102 ## no critic qw(PackageVar)
103 10 local $Pinto::Globals::current_username = delete $pinto_args->{username};
104 10 local $Pinto::Globals::current_time_offset = delete $pinto_args->{time_offset};
105 ## use critic;
106  
107 10 $chrome_args->{stdout} = $writer;
108 10 $chrome_args->{stderr} = $writer;
109  
110 10 my $chrome = Pinto::Chrome::Net->new($chrome_args);
111 10 my $pinto = Pinto->new( chrome => $chrome, root => $self->root );
112  
113 my $result =
114 10 ▶ try { $pinto->run( ucfirst $action_name => %{$action_args} ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
115 0 ▶ catch { print {$writer} $_; Pinto::Result->new->failed };
Subroutine __ANON__: not called CC=1 SCAR=3.5
116  
117 10 ▶ print {$writer} $PINTO_PROTOCOL_STATUS_OK . "\n" if $result->was_successful;
Branch: 1 branch
BranchTrueFalseTotal
if $result->was_successful 5 5 10
118  
119 10 exit $result->was_successful ? 0 : 1;
120 }
121  
122 #-------------------------------------------------------------------------------
123  
124 46 ▶sub parent_proc {
Subroutine parent_proc: called CC=1 SCAR=0.1
Pod undocumented
125 46 my ( $self, $pipe, $child_pid ) = @_;
126  
127 46 my $reader = $pipe->reader;
128 46 my $select = IO::Select->new($reader);
129 46 $reader->blocking(0);
130  
131 46 ▶ my $response = sub {
Subroutine __ANON__: called CC=8 SCAR=22.1
132 46 my $responder = shift;
133  
134 46 my $headers = ['Content-Type' => 'text/plain'];
135 46 my $writer = $responder->( [ HTTP_OK, $headers ] );
136 46 my $socket = $self->request->env->{'psgix.io'};
137 46 my $nullmsg = $PINTO_PROTOCOL_NULL_MESSAGE . "\n";
138  
139  
140 46 while (1) {
141  
142 230 my $input;
143 230 ▶ if ( $select->can_read(1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read(1)) 92 138 230
144 92 $input = <$reader>; # Will block until \n
145 92 ▶ last if not defined $input; # We reached eof
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 46 46 92
146 }
147  
148 184 my $ok = eval {
149 0 ▶ local $SIG{ALRM} = sub { die "Write timed out" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
150 184 alarm(3);
151  
152 184 ▶ $writer->write( $input || $nullmsg );
Condition66%2/3
$input || $nullmsg
l!l&&r!l&&!r
461380
MC/DC0%0/2
$input || $nullmsg
$input $nullmsg
Truth table66%2/3
A || B
A= $inputB= $nullmsg
ABresult
1X1
011
000
153 184 1; # Write succeeded
154 };
155  
156 184 alarm(0);
157 184 ▶ unless ( $ok && ( !$socket || getpeername($socket) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and !$socket || getpeername $socket) 0 184 184
Condition33%2/6
$ok and !$socket || getpeername $socket
!ll&&!rl&&r
00184
not $socket or getpeername $socket
l!l&&r!l&&!r
18400
MC/DC0%0/3
$ok and !$socket || getpeername $socket
$ok not $socket getpeername $socket
Truth table25%1/4
A and not(B) or C
A= $okB= not $socketC= getpeername $socket
ABCresult
0XX0
1000
11X1
1011
158 0 proc_terminate( $child_pid, max_wait => 10 );
159 0 last;
160 }
161 }
162  
163 46 ▶ $writer->close if not $socket; # Hangs otherwise!
Branch: 1 branch
BranchTrueFalseTotal
unless $socket 46 0 46
164 46 waitpid $child_pid, 0;
165 46 };
166  
167 46 return $response;
168 }
169  
170 #-------------------------------------------------------------------------------
171  
172 13 __PACKAGE__->meta->make_immutable;
173  
174 #-------------------------------------------------------------------------------
175  
176 13 1;
177  
178 __END__
« blib/lib/Pinto/Schema.pm ↑ Summary blib/lib/Pinto/Action/Add.pm »