| 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
| |||||||||||||||||||||||||||||
| 26 | 12 | my $lwp = ${$authn}->{'LWP'}; | |||||||||||||||||||||||||||
| 27 | 12 | ▶ | if ( !defined $lwp ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 35 | 11 | my $request; | |||||||||||||||||||||||||||
| 36 | 11 | ▶ | if ( $action eq 'post' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 74 | 1 | $request = PUT "$target"; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 8 | ▶ | if ( $action eq 'delete' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | $request = DELETE "$target"; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 8 | ▶ | if ( !defined $request ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | if ( defined $target ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ( ${$authn}->{'Type'} eq 'basic' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | my $username = ${$authn}->{'Username'}; | |||||||||||||||||||||||||||
| 90 | 0 | my $password = ${$authn}->{'Password'}; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( defined $username && defined $password ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $username and defined $password
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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $verbose and $verbose >= 2
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | 1 | my $authn = ${$object}->{'Authn'}; | |||||||||||||||||||||||||||
| 121 | 1 | ▶ | if ( !defined $authn ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||