Sling/Request.pm

filter: Statementstmt 88.2% 90 / 102 Branchbran 82.5% 33 / 40 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 82.9% 136 / 164 CC 23 SCAR 27.6
CC
23
Cov
84%
string_to_request
24.1
request
4.0
BEGIN
1.0
CRAP 15.8
« Sling/Group.pm ↑ Summary Sling/Print.pm »
1 #!/usr/bin/perl -w
2  
3 package Apache::Sling::Request;
4  
5 11 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use HTTP::Request::Common qw(DELETE GET POST PUT);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Apache::Sling::Print;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 require Exporter;
14  
15 11 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 11 our @EXPORT_OK = ();
18  
19 11 our $VERSION = '0.27';
20  
21 #{{{sub string_to_request
22  
23 13 ▶sub string_to_request {
Subroutine string_to_request: called CC=20 SCAR=31.8
Pod documented
24 13 my ( $string, $authn, $verbose, $log ) = @_;
25 13 ▶ if ( !defined $string ) { croak 'No string defined to turn into request!'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 1 12 13
26 12 my $lwp = ${$authn}->{'LWP'};
27 12 ▶ if ( !defined $lwp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $lwp) 1 11 12
28 1 croak 'No reference to an lwp user agent supplied!';
29 }
30  
31 # Split based on the space character (\x20) only, such that
32 # newlines, tabs etc are maintained in the request variables:
33 11 my ( $action, $target, @req_variables ) = split /\x20/x, $string;
34 11 ▶ $action = ( defined $action ? $action : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $action ? : 9 2 11
35 11 my $request;
36 11 ▶ if ( $action eq 'post' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "post") 2 9 11
37 2 my $variables = join q{ }, @req_variables;
38 2 my $post_variables;
39 2 my $success = eval $variables;
40 2 ▶ if ( !defined $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 1 1 2
41 1 croak "Error parsing post variables: \"$variables\"";
42 }
43 1 $request = POST( "$target", $post_variables );
44 }
45 10 ▶ if ( $action eq 'data' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "data") 2 8 10
46  
47 # multi-part form upload
48 2 my $variables = join q{ }, @req_variables;
49 2 my $post_variables;
50 2 my $success = eval $variables;
51 2 ▶ if ( !defined $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 1 1 2
52 1 croak "Error parsing post variables: \"$variables\"";
53 }
54 $request =
55 1 POST( "$target", $post_variables, 'Content_Type' => 'form-data' );
56 }
57 9 ▶ if ( $action eq 'fileupload' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "fileupload") 2 7 9
58  
59 # multi-part form upload with the file name and file specified
60 2 my $filename = shift @req_variables;
61 2 my $file = shift @req_variables;
62 2 my $variables = join q{ }, @req_variables;
63 2 my $post_variables;
64 2 my $success = eval $variables;
65  
66 2 ▶ if ( !defined $success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $success) 1 1 2
67 1 croak "Error parsing post variables: \"$variables\"";
68 }
69 1 push @{$post_variables}, $filename => ["$file"];
70 1 $request =
71 POST( "$target", $post_variables, 'Content_Type' => 'form-data' );
72 }
73 8 ▶ if ( $action eq 'put' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "put") 1 7 8
74 1 $request = PUT "$target";
75 }
76 8 ▶ if ( $action eq 'delete' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "delete") 1 7 8
77 1 $request = DELETE "$target";
78 }
79 8 ▶ if ( !defined $request ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $request) 3 5 8
80 3 ▶ if ( defined $target ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target) { } 1 2 3
81 1 $request = GET "$target";
82 }
83 else {
84 2 croak 'Error generating request for blank target!';
85 }
86 }
87 6 ▶ if ( defined ${$authn}->{'Type'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${$authn;}->{"Type"}) 0 6 6
88 0 ▶ if ( ${$authn}->{'Type'} eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$authn;}->{"Type"} eq "basic") 0 0 0
89 0 my $username = ${$authn}->{'Username'};
90 0 my $password = ${$authn}->{'Password'};
91 0 ▶ if ( defined $username && defined $password ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
92  
93 # Always add an Authorization header to deal with application not
94 # properly requesting authentication to be sent:
95 0 my $encoded = 'Basic ' . encode_base64("$username:$password");
96 0 $request->header( 'Authorization' => $encoded );
97 }
98 }
99 }
100 6 ▶ if ( defined $verbose && $verbose >= 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose and $verbose >= 2) 0 6 6
Condition33%1/3
defined $verbose and $verbose >= 2
!ll&&!rl&&r
600
MC/DC0%0/2
defined $verbose and $verbose >= 2
defined $verbose $verbose >= 2
Truth table33%1/3
A and B
A= defined $verboseB= $verbose >= 2
ABresult
0X0
100
111
101 0 Apache::Sling::Print::print_with_lock(
102 "**** String representation of compiled request:\n"
103 . $request->as_string,
104 $log
105 );
106 }
107 6 return $request;
108 }
109  
110 #}}}
111  
112 #{{{sub request
113  
114 3 ▶sub request {
Subroutine request: called CC=4 SCAR=13.9
Pod documented
115 3 my ( $object, $string ) = @_;
116 3 ▶ if ( !defined $object ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $object) 1 2 3
117 1 croak 'No reference to a suitable object supplied!';
118 }
119 2 ▶ if ( !defined $string ) { croak 'No string defined to turn into request!'; }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $string) 1 1 2
120 1 my $authn = ${$object}->{'Authn'};
121 1 ▶ if ( !defined $authn ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authn) 0 1 1
122 0 croak 'Object does not reference a suitable auth object';
123 }
124 1 my $verbose = ${$object}->{'Verbose'};
125 1 my $log = ${$object}->{'Log'};
126 1 my $lwp = ${$authn}->{'LWP'};
127 my $res =
128 1 ${$lwp}->request( string_to_request( $string, $authn, $verbose, $log ) );
129 0 return \$res;
130 }
131  
132 #}}}
133  
134 11 1;
135  
136 __END__
« Sling/Group.pm ↑ Summary Sling/Print.pm »