| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl -w | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Apache::Sling::Request; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 11 |  |  | 11 |  | 323 | use 5.008001; | 
|  | 11 |  |  |  |  | 44 |  | 
|  | 11 |  |  |  |  | 459 |  | 
| 6 | 11 |  |  | 11 |  | 70 | use strict; | 
|  | 11 |  |  |  |  | 23 |  | 
|  | 11 |  |  |  |  | 342 |  | 
| 7 | 11 |  |  | 11 |  | 59 | use warnings; | 
|  | 11 |  |  |  |  | 22 |  | 
|  | 11 |  |  |  |  | 509 |  | 
| 8 | 11 |  |  | 11 |  | 149 | use Carp; | 
|  | 11 |  |  |  |  | 19 |  | 
|  | 11 |  |  |  |  | 3314 |  | 
| 9 | 11 |  |  | 11 |  | 15238 | use HTTP::Request::Common qw(DELETE GET POST PUT); | 
|  | 11 |  |  |  |  | 26795 |  | 
|  | 11 |  |  |  |  | 932 |  | 
| 10 | 11 |  |  | 11 |  | 10266 | use MIME::Base64; | 
|  | 11 |  |  |  |  | 9894 |  | 
|  | 11 |  |  |  |  | 8339 |  | 
| 11 | 11 |  |  | 11 |  | 89 | use Apache::Sling::Print; | 
|  | 11 |  |  |  |  | 20 |  | 
|  | 11 |  |  |  |  | 1027 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | require Exporter; | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 11 |  |  | 11 |  | 64 | use base qw(Exporter); | 
|  | 11 |  |  |  |  | 23 |  | 
|  | 11 |  |  |  |  | 12331 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our @EXPORT_OK = (); | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | our $VERSION = '0.27'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | #{{{sub string_to_request | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub string_to_request { | 
| 24 | 13 |  |  | 13 | 1 | 2890 | my ( $string, $authn, $verbose, $log ) = @_; | 
| 25 | 13 | 100 |  |  |  | 36 | if ( !defined $string ) { croak 'No string defined to turn into request!'; } | 
|  | 1 |  |  |  |  | 29 |  | 
| 26 | 12 |  |  |  |  | 17 | my $lwp = ${$authn}->{'LWP'}; | 
|  | 12 |  |  |  |  | 23 |  | 
| 27 | 12 | 100 |  |  |  | 24 | if ( !defined $lwp ) { | 
| 28 | 1 |  |  |  |  | 10 | 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 |  |  |  |  | 43 | my ( $action, $target, @req_variables ) = split /\x20/x, $string; | 
| 34 | 11 | 100 |  |  |  | 29 | $action = ( defined $action ? $action : '' ); | 
| 35 | 11 |  |  |  |  | 16 | my $request; | 
| 36 | 11 | 100 |  |  |  | 25 | if ( $action eq 'post' ) { | 
| 37 | 2 |  |  |  |  | 6 | my $variables = join q{ }, @req_variables; | 
| 38 | 2 |  |  |  |  | 5 | my $post_variables; | 
| 39 | 2 |  |  |  |  | 138 | my $success = eval $variables; | 
| 40 | 2 | 100 |  |  |  | 9 | if ( !defined $success ) { | 
| 41 | 1 |  |  |  |  | 16 | croak "Error parsing post variables: \"$variables\""; | 
| 42 |  |  |  |  |  |  | } | 
| 43 | 1 |  |  |  |  | 7 | $request = POST( "$target", $post_variables ); | 
| 44 |  |  |  |  |  |  | } | 
| 45 | 10 | 100 |  |  |  | 8850 | if ( $action eq 'data' ) { | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | # multi-part form upload | 
| 48 | 2 |  |  |  |  | 6 | my $variables = join q{ }, @req_variables; | 
| 49 | 2 |  |  |  |  | 5 | my $post_variables; | 
| 50 | 2 |  |  |  |  | 97 | my $success = eval $variables; | 
| 51 | 2 | 100 |  |  |  | 10 | if ( !defined $success ) { | 
| 52 | 1 |  |  |  |  | 10 | croak "Error parsing post variables: \"$variables\""; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  | $request = | 
| 55 | 1 |  |  |  |  | 5 | POST( "$target", $post_variables, 'Content_Type' => 'form-data' ); | 
| 56 |  |  |  |  |  |  | } | 
| 57 | 9 | 100 |  |  |  | 423 | if ( $action eq 'fileupload' ) { | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | # multi-part form upload with the file name and file specified | 
| 60 | 2 |  |  |  |  | 5 | my $filename  = shift @req_variables; | 
| 61 | 2 |  |  |  |  | 4 | my $file      = shift @req_variables; | 
| 62 | 2 |  |  |  |  | 6 | my $variables = join q{ }, @req_variables; | 
| 63 | 2 |  |  |  |  | 3 | my $post_variables; | 
| 64 | 2 |  |  |  |  | 125 | my $success = eval $variables; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 2 | 100 |  |  |  | 11 | if ( !defined $success ) { | 
| 67 | 1 |  |  |  |  | 21 | croak "Error parsing post variables: \"$variables\""; | 
| 68 |  |  |  |  |  |  | } | 
| 69 | 1 |  |  |  |  | 3 | push @{$post_variables}, $filename => ["$file"]; | 
|  | 1 |  |  |  |  | 3 |  | 
| 70 | 1 |  |  |  |  | 4 | $request = | 
| 71 |  |  |  |  |  |  | POST( "$target", $post_variables, 'Content_Type' => 'form-data' ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 8 | 100 |  |  |  | 14049 | if ( $action eq 'put' ) { | 
| 74 | 1 |  |  |  |  | 5 | $request = PUT "$target"; | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 8 | 100 |  |  |  | 178 | if ( $action eq 'delete' ) { | 
| 77 | 1 |  |  |  |  | 5 | $request = DELETE "$target"; | 
| 78 |  |  |  |  |  |  | } | 
| 79 | 8 | 100 |  |  |  | 112 | if ( !defined $request ) { | 
| 80 | 3 | 100 |  |  |  | 8 | if ( defined $target ) { | 
| 81 | 1 |  |  |  |  | 5 | $request = GET "$target"; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  | else { | 
| 84 | 2 |  |  |  |  | 20 | croak 'Error generating request for blank target!'; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  | } | 
| 87 | 6 | 50 |  |  |  | 91 | if ( defined ${$authn}->{'Type'} ) { | 
|  | 6 |  |  |  |  | 26 |  | 
| 88 | 0 | 0 |  |  |  | 0 | if ( ${$authn}->{'Type'} eq 'basic' ) { | 
|  | 0 |  |  |  |  | 0 |  | 
| 89 | 0 |  |  |  |  | 0 | my $username = ${$authn}->{'Username'}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 90 | 0 |  |  |  |  | 0 | my $password = ${$authn}->{'Password'}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 91 | 0 | 0 | 0 |  |  | 0 | if ( defined $username && defined $password ) { | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Always add an Authorization header to deal with application not | 
| 94 |  |  |  |  |  |  | # properly requesting authentication to be sent: | 
| 95 | 0 |  |  |  |  | 0 | my $encoded = 'Basic ' . encode_base64("$username:$password"); | 
| 96 | 0 |  |  |  |  | 0 | $request->header( 'Authorization' => $encoded ); | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  | } | 
| 100 | 6 | 50 | 33 |  |  | 18 | if ( defined $verbose && $verbose >= 2 ) { | 
| 101 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_with_lock( | 
| 102 |  |  |  |  |  |  | "**** String representation of compiled request:\n" | 
| 103 |  |  |  |  |  |  | . $request->as_string, | 
| 104 |  |  |  |  |  |  | $log | 
| 105 |  |  |  |  |  |  | ); | 
| 106 |  |  |  |  |  |  | } | 
| 107 | 6 |  |  |  |  | 36 | return $request; | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | #}}} | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | #{{{sub request | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | sub request { | 
| 115 | 3 |  |  | 3 | 1 | 65 | my ( $object, $string ) = @_; | 
| 116 | 3 | 100 |  |  |  | 8 | if ( !defined $object ) { | 
| 117 | 1 |  |  |  |  | 9 | croak 'No reference to a suitable object supplied!'; | 
| 118 |  |  |  |  |  |  | } | 
| 119 | 2 | 100 |  |  |  | 5 | if ( !defined $string ) { croak 'No string defined to turn into request!'; } | 
|  | 1 |  |  |  |  | 11 |  | 
| 120 | 1 |  |  |  |  | 1 | my $authn = ${$object}->{'Authn'}; | 
|  | 1 |  |  |  |  | 10 |  | 
| 121 | 1 | 50 |  |  |  | 7 | if ( !defined $authn ) { | 
| 122 | 0 |  |  |  |  | 0 | croak 'Object does not reference a suitable auth object'; | 
| 123 |  |  |  |  |  |  | } | 
| 124 | 1 |  |  |  |  | 3 | my $verbose = ${$object}->{'Verbose'}; | 
|  | 1 |  |  |  |  | 3 |  | 
| 125 | 1 |  |  |  |  | 2 | my $log     = ${$object}->{'Log'}; | 
|  | 1 |  |  |  |  | 2 |  | 
| 126 | 1 |  |  |  |  | 2 | my $lwp     = ${$authn}->{'LWP'}; | 
|  | 1 |  |  |  |  | 2 |  | 
| 127 | 1 |  |  |  |  | 4 | my $res = | 
| 128 | 1 |  |  |  |  | 3 | ${$lwp}->request( string_to_request( $string, $authn, $verbose, $log ) ); | 
| 129 | 0 |  |  |  |  |  | return \$res; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | #}}} | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | 1; | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | __END__ |