blib/lib/Plack/App/GitSmartHttp.pm

filter: Statementstmt 95.6% 220 / 230 Branchbran 81.4% 57 / 70 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 40 / 40 Podpod 8.7% 2 / 23 totaltotal 87.0% 329 / 378 CC 42 SCAR 13.7
CC
42
Cov
92%
get_info_refs
8.2
__ANON__
7.3
service_rpc
7.1
CRAP 3.9
↑ Summary
1 package Plack::App::GitSmartHttp;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use parent qw/Plack::Component/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use Plack::Util::Accessor qw( root git_path upload_pack received_pack );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 21 ▶use File::chdir;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 21 ▶use File::Which qw(which);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 21 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 21 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 21 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 21 ▶use IO::Uncompress::Gunzip qw($GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 21 ▶use constant BUFFER_SIZE => 8192;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 21 our $VERSION = '0.07';
23 21 my @SERVICES = (
24 [ 'POST', 'service_rpc', qr{(.*?)/git-upload-pack$}, 'upload-pack' ],
25 [ 'POST', 'service_rpc', qr{(.*?)/git-receive-pack$}, 'receive-pack' ],
26  
27 [ 'GET', 'get_info_refs', qr{(.*?)/info/refs$} ],
28 [ 'GET', 'get_text_file', qr{(.*?)/HEAD$} ],
29 [ 'GET', 'get_text_file', qr{(.*?)/objects/info/alternates$} ],
30 [ 'GET', 'get_text_file', qr{(.*?)/objects/info/http-alternates$} ],
31 [ 'GET', 'get_info_packs', qr{(.*?)/objects/info/packs$} ],
32 [ 'GET', 'get_loose_object', qr{(.*?)/objects/[0-9a-f]{2}/[0-9a-f]{38}$} ],
33 [
34 'GET', 'get_pack_file', qr{(.*?)/objects/pack/pack-[0-9a-f]{40}\.pack$}
35 ],
36 [ 'GET', 'get_idx_file', qr{(.*?)/objects/pack/pack-[0-9a-f]{40}\.idx$} ],
37 );
38  
39 20 ▶sub prepare_app {
Subroutine prepare_app: called CC=4 SCAR=14.8
Pod documented
40 20 my $self = shift;
41 20 ▶ unless ( $self->git_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->git_path) 16 4 20
42 16 my $git_path = which('git');
43 16 ▶ unless ($git_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($git_path) 0 16 16
44 0 die 'could not find git';
45 }
46 16 $self->git_path($git_path);
47 }
48 20 ▶ unless ( -x $self->git_path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-x $self->git_path) 0 20 20
49 0 die 'git_path is not executable [' . $self->git_path . ']';
50 }
51 }
52  
53 20 ▶sub call {
Subroutine call: called CC=4 SCAR=13.9
Pod documented
54 20 my $self = shift;
55 20 my $env = shift;
56 20 my $req = Plack::Request->new($env);
57  
58 20 my ( $cmd, $path, $reqfile, $rpc ) = $self->match_routing($req);
59  
60 20 ▶ return $self->return_404 unless $cmd;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmd 1 19 20
61 19 ▶ return $self->return_not_allowed($env) if $cmd eq 'not_allowed';
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "not_allowed" 2 17 19
62  
63 17 my $dir = $self->get_git_repo_dir($path);
64 17 ▶ return $self->return_404 unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 1 16 17
65  
66 {
67 16 local $CWD = $dir;
68 16 $self->$cmd(
69 {
70 req => $req,
71 path => $path,
72 reqfile => $reqfile,
73 rpc => $rpc
74 }
75 );
76 }
77 }
78  
79 7 ▶sub get_service {
Subroutine get_service: called CC=3 SCAR=11.0
Pod undocumented
80 7 my $self = shift;
81 7 my $req = shift;
82  
83 7 my $service = $req->param('service');
84 7 ▶ return unless $service;
Branch: 1 branch
BranchTrueFalseTotal
unless $service 1 6 7
85 6 ▶ return unless substr( $service, 0, 4 ) eq 'git-';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($service, 0, 4) eq "git-" 2 4 6
86 4 $service =~ s/git-//g;
87 4 return $service;
88 }
89  
90 30 ▶sub match_routing {
Subroutine match_routing: called CC=4 SCAR=13.9
Pod undocumented
91 30 my $self = shift;
92 30 my $req = shift;
93  
94 30 my ( $cmd, $path, $file, $rpc );
95 30 for my $s (@SERVICES) {
96 124 my $match = $s->[2];
97 124 ▶ if ( $req->path_info =~ /$match/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->path_info =~ /$match/) 29 95 124
98 29 ▶ return ('not_allowed') if $s->[0] ne uc( $req->method );
Branch: 1 branch
BranchTrueFalseTotal
if $s->[0] ne uc $req->method 3 26 29
99 26 $cmd = $s->[1];
100 26 $path = $1;
101 26 $file = $req->path_info;
102 26 $file =~ s|\Q$path/\E||;
103 26 $rpc = $s->[3];
104 26 return ( $cmd, $path, $file, $rpc );
105 }
106 }
107 1 return ();
108 }
109  
110 20 ▶sub get_git_repo_dir {
Subroutine get_git_repo_dir: called CC=3 SCAR=11.0
Pod undocumented
111 20 my $self = shift;
112 20 my $path = shift;
113  
114 20 ▶ my $root = $self->root || `pwd`;
Condition66%2/3
$self->root || `pwd`
l!l&&r!l&&!r
1910
MC/DC0%0/2
$self->root || `pwd`
$self->root `pwd`
Truth table66%2/3
A || B
A= $self->rootB= `pwd`
ABresult
1X1
011
000
115 20 chomp $root;
116 20 $path = catdir( $root, $path );
117 20 ▶ return $path if ( -d $path );
Branch: 1 branch
BranchTrueFalseTotal
if -d $path 18 2 20
118 2 return;
119 }
120  
121 8 ▶sub service_rpc {
Subroutine service_rpc: called CC=7 SCAR=19.6
Pod undocumented
122 8 my $self = shift;
123 8 my $args = shift;
124  
125 8 my $req = $args->{req};
126 8 my $rpc = $args->{rpc};
127  
128 8 ▶ return $self->return_403
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_access($req, $rpc, 1) 4 4 8
129 unless $self->has_access( $req, $rpc, 1 );
130  
131 4 my @cmd = $self->git_command( $rpc, '--stateless-rpc', '.' );
132  
133 4 my $input = $req->input;
134 4 ▶ if ( exists $req->env->{HTTP_CONTENT_ENCODING}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $req->env->{"HTTP_CONTENT_ENCODING"} and $req->env->{"HTTP_CONTENT_ENCODING"} =~ /^(?:x-)?gzip$/) 2 2 4
Condition66%2/3
exists $req->env->{"HTTP_CONTENT_ENCODING"} and $req->env->{"HTTP_CONTENT_ENCODING"} =~ /^(?:x-)?gzip$/
!ll&&!rl&&r
202
MC/DC50%1/2
exists $req->env->{"HTTP_CONTENT_ENCODING"} and $req->env->{"HTTP_CONTENT_ENCODING"} =~ /^(?:x-)?gzip$/
exists $req->env->{"HTTP_CONTENT_ENCODING"} $req->env->{"HTTP_CONTENT_ENCODING"} =~ /^(?:x-)?gzip$/
Truth table66%2/3
A and B
A= exists $req->env->{"HTTP_CONTENT_ENCODING"}B= $req->env->{"HTTP_CONTENT_ENCODING"} =~ /^(?:x-)?gzip$/
ABresult
0X0
100
111
135 && $req->env->{HTTP_CONTENT_ENCODING} =~ /^(?:x-)?gzip$/ )
136 {
137 2 $input = IO::Uncompress::Gunzip->new($input);
138 2 ▶ unless ($input) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input) 0 2 2
139 0 $req->env->{'psgi.errors'}->print("gunzip failed: $GunzipError");
140 0 return $self->return_400;
141 }
142 }
143 4 my ( $cout, $cerr ) = ( gensym, gensym );
144 4 my $pid = open3( my $cin, $cout, $cerr, @cmd );
145 4 my $input_len = 0;
146 4 ▶ while ( my $len = $input->read( my $buf, BUFFER_SIZE ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $len = $input->read(my $buf, 8192) > 0) 3 4 7
147 3 print $cin $buf;
148 3 $input_len += $len;
149 }
150 4 close $cin;
151 4 ▶ if ( $input_len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input_len == 0) 1 3 4
152 1 close $cout;
153 1 close $cerr;
154 1 waitpid( $pid, 0 );
155 1 return $self->return_400;
156 }
157  
158 3 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
159 3 my $respond = shift;
160 3 my $writer = $respond->(
161 [
162 200,
163 [
164 'Content-Type' =>
165 sprintf( 'application/x-git-%s-result', $rpc ),
166 ]
167 ]
168 );
169  
170 3 my ( $out, $err, $buf ) = ( '', '', '' );
171 3 my $s = IO::Select->new( $cout, $cerr );
172 3 ▶ while ( my @ready = $s->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $s->can_read) 6 3 9
173 6 for my $handle (@ready) {
174 6 ▶ while ( sysread( $handle, $buf, BUFFER_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $handle, $buf, 8192) 24 6 30
175 24 ▶ if ( $handle == $cerr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle == $cerr) { } 0 24 24
176 0 $err .= $buf;
177 }
178 else {
179 24 $writer->write($buf);
180 }
181 }
182 6 ▶ $s->remove($handle) if eof($handle);
Branch: 1 branch
BranchTrueFalseTotal
if eof $handle 6 0 6
183 }
184 }
185 3 close $cout;
186 3 close $cerr;
187 3 waitpid( $pid, 0 );
188  
189 3 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 3 3
190 0 $req->env->{'psgi.errors'}->print("git command failed: $err");
191 }
192 3 $writer->close();
193 }
194 3 }
195  
196 3 ▶sub get_info_refs {
Subroutine get_info_refs: called CC=8 SCAR=21.0
Pod undocumented
197 3 my $self = shift;
198 3 my $args = shift;
199  
200 3 my $req = $args->{req};
201 3 my $service = $self->get_service($req);
202 3 ▶ if ( $self->has_access( $args->{req}, $service ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_access($args->{'req'}, $service)) { } 2 1 3
203 2 my @cmd =
204 $self->git_command( $service, '--stateless-rpc', '--advertise-refs',
205 '.' );
206  
207 2 my ( $cout, $cerr ) = ( gensym, gensym );
208 2 my $pid = open3( my $cin, $cout, $cerr, @cmd );
209 2 close $cin;
210 2 my ( $refs, $err, $buf ) = ( '', '', '' );
211 2 my $s = IO::Select->new( $cout, $cerr );
212 2 ▶ while ( my @ready = $s->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $s->can_read) 4 2 6
213 4 for my $handle (@ready) {
214 4 ▶ while ( sysread( $handle, $buf, BUFFER_SIZE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread $handle, $buf, 8192) 2 4 6
215 2 ▶ if ( $handle == $cerr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($handle == $cerr) { } 0 2 2
216 0 $err .= $buf;
217 }
218 else {
219 2 $refs .= $buf;
220 }
221 }
222 4 ▶ $s->remove($handle) if eof($handle);
Branch: 1 branch
BranchTrueFalseTotal
if eof $handle 4 0 4
223 }
224 }
225 2 close $cout;
226 2 close $cerr;
227 2 waitpid( $pid, 0 );
228  
229 2 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 2 2
230 0 $req->env->{'psgi.errors'}->print("git command failed: $err");
231 0 return $self->return_400;
232 }
233  
234 2 my $res = $req->new_response(200);
235 2 $res->headers(
236 [
237 'Content-Type' =>
238 sprintf( 'application/x-git-%s-advertisement', $service ),
239 ]
240 );
241 2 my $body =
242 pkt_write("# service=git-${service}\n") . pkt_flush() . $refs;
243 2 $res->body($body);
244 2 return $res->finalize;
245 }
246 else {
247 1 return $self->dumb_info_refs($args);
248 }
249 }
250  
251 1 ▶sub dumb_info_refs {
Subroutine dumb_info_refs: called CC=1 SCAR=0.0
Pod undocumented
252 1 my $self = shift;
253 1 my $args = shift;
254 1 $self->update_server_info;
255 1 $self->send_file( $args, "text/plain; charset=utf-8" );
256 }
257  
258 1 ▶sub get_info_packs {
Subroutine get_info_packs: called CC=1 SCAR=0.0
Pod undocumented
259 1 my $self = shift;
260 1 my $args = shift;
261 1 $self->send_file( $args, "text/plain; charset=utf-8" );
262 }
263  
264 1 ▶sub get_loose_object {
Subroutine get_loose_object: called CC=1 SCAR=0.0
Pod undocumented
265 1 my $self = shift;
266 1 my $args = shift;
267 1 $self->send_file( $args, "application/x-git-loose-object" );
268 }
269  
270 1 ▶sub get_pack_file {
Subroutine get_pack_file: called CC=1 SCAR=0.0
Pod undocumented
271 1 my $self = shift;
272 1 my $args = shift;
273 1 $self->send_file( $args, "application/x-git-packed-objects" );
274 }
275  
276 1 ▶sub get_idx_file {
Subroutine get_idx_file: called CC=1 SCAR=0.0
Pod undocumented
277 1 my $self = shift;
278 1 my $args = shift;
279 1 $self->send_file( $args, "application/x-git-packed-objects-toc" );
280 }
281  
282 1 ▶sub get_text_file {
Subroutine get_text_file: called CC=1 SCAR=0.0
Pod undocumented
283 1 my $self = shift;
284 1 my $args = shift;
285 1 $self->send_file( $args, "text/plain" );
286 }
287  
288 1 ▶sub update_server_info {
Subroutine update_server_info: called CC=1 SCAR=0.0
Pod undocumented
289 1 my $self = shift;
290 1 system( $self->git_command('update-server-info') );
291 }
292  
293 7 ▶sub git_command {
Subroutine git_command: called CC=1 SCAR=0.0
Pod undocumented
294 7 my $self = shift;
295 7 my @commands = @_;
296 7 my $git_bin = $self->git_path;
297 7 return ( $git_bin, @commands );
298 }
299  
300 11 ▶sub has_access {
Subroutine has_access: called CC=6 SCAR=18.0
Pod undocumented
301 11 my $self = shift;
302 11 my ( $req, $rpc, $check_content_type ) = @_;
303  
304 11 ▶ if ( $check_content_type
Branch: 1 branch
BranchTrueFalseTotal
if ($check_content_type and $req->content_type ne sprintf('application/x-git-%s-request', $rpc)) 4 7 11
Condition100%3/3
$check_content_type and $req->content_type ne sprintf('application/x-git-%s-request', $rpc)
!ll&&!rl&&r
344
MC/DC100%2/2
$check_content_type and $req->content_type ne sprintf('application/x-git-%s-request', $rpc)
$check_content_type $req->content_type ne sprintf('application/x-git-%s-request', $rpc)
Truth table100%3/3
A and B
A= $check_content_typeB= $req->content_type ne sprintf('application/x-git-%s-request', $rpc)
ABresult
0X0
100
111
305 && $req->content_type ne
306 sprintf( "application/x-git-%s-request", $rpc ) )
307 {
308 4 return;
309 }
310  
311 7 ▶ return if !$rpc;
Branch: 1 branch
BranchTrueFalseTotal
unless $rpc 1 6 7
312 6 ▶ return $self->received_pack if $rpc eq 'receive-pack';
Branch: 1 branch
BranchTrueFalseTotal
if $rpc eq "receive-pack" 1 5 6
313 5 ▶ return $self->upload_pack if $rpc eq 'upload-pack';
Branch: 1 branch
BranchTrueFalseTotal
if $rpc eq "upload-pack" 5 0 5
314 0 return;
315 }
316  
317 6 ▶sub send_file {
Subroutine send_file: called CC=4 SCAR=14.0
Pod undocumented
318 6 my $self = shift;
319 6 my ( $args, $content_type ) = @_;
320  
321 6 my $file = $args->{reqfile};
322 6 ▶ return $self->return_404 unless -e $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 6 6
323  
324 6 my @stat = stat $file;
325 6 my $res = $args->{req}->new_response(200);
326 6 $res->headers(
327 [
328 'Content-Type' => $content_type,
329 'Last-Modified' => HTTP::Date::time2str( $stat[9] ),
330 'Expires' => 'Fri, 01 Jan 1980 00:00:00 GMT',
331 'Pragma' => 'no-cache',
332 'Cache-Control' => 'no-cache, max-age=0, must-revalidate',
333 ]
334 );
335  
336 6 ▶ if ( $stat[7] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stat[7]) 6 0 6
337 6 $res->header( 'Content-Length' => $stat[7] );
338 }
339 6 ▶ open my $fh, "<:raw", $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or return $self->return_403 6 0 6
340 or return $self->return_403;
341  
342 6 Plack::Util::set_io_path( $fh, Cwd::realpath($file) );
343 6 $res->body($fh);
344 6 $res->finalize;
345 }
346  
347 2 ▶sub pkt_flush {
Subroutine pkt_flush: called CC=1 SCAR=0.0
Pod undocumented
348 2 return '0000';
349 }
350  
351 2 ▶sub pkt_write {
Subroutine pkt_write: called CC=1 SCAR=0.0
Pod undocumented
352 2 my $str = shift;
353 2 return sprintf( '%04x', length($str) + 4 ) . $str;
354 }
355  
356 2 ▶sub return_not_allowed {
Subroutine return_not_allowed: called CC=2 SCAR=6.9
Pod undocumented
357 2 my $self = shift;
358 2 my $env = shift;
359 2 ▶ if ( $env->{SERVER_PROTOCOL} eq 'HTTP/1.1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'SERVER_PROTOCOL'} eq 'HTTP/1.1') { } 1 1 2
360 return [
361 1 405, [ 'Content-Type' => 'text/plain', 'Content-Length' => 18 ],
362 ['Method Not Allowed']
363 ];
364 }
365 else {
366 return [
367 1 400, [ 'Content-Type' => 'text/plain', 'Content-Length' => 11 ],
368 ['Bad Request']
369 ];
370 }
371 }
372  
373 4 ▶sub return_403 {
Subroutine return_403: called CC=1 SCAR=0.0
Pod undocumented
374 4 my $self = shift;
375 return [
376 4 403, [ 'Content-Type' => 'text/plain', 'Content-Length' => 9 ],
377 ['Forbidden']
378 ];
379 }
380  
381 1 ▶sub return_400 {
Subroutine return_400: called CC=1 SCAR=0.0
Pod undocumented
382 1 my $self = shift;
383 return [
384 1 400, [ 'Content-Type' => 'text/plain', 'Content-Length' => 11 ],
385 ['Bad Request']
386 ];
387 }
388  
389 2 ▶sub return_404 {
Subroutine return_404: called CC=1 SCAR=0.0
Pod undocumented
390 2 my $self = shift;
391 return [
392 2 404, [ 'Content-Type' => 'text/plain', 'Content-Length' => 9 ],
393 ['Not Found']
394 ];
395 }
396  
397 21 1;
398 __END__
↑ Summary