OpenInteract2/URL.pm

filter: Statementstmt 20.3% 24 / 118 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 31.8% 7 / 22 Podpod 0.0% 0 / 12 totaltotal 13.8% 31 / 224 CC 32 SCAR 35.3
CC
32
Cov
13%
create
72.0
_create_deployment
72.0
_parse
42.0
CRAP 34.3
« OpenInteract2/File.pm ↑ Summary OpenInteract2/ActionResolver.pm »
1 package OpenInteract2::URL;
2  
3 # $Id: URL.pm,v 1.31 2005/03/18 04:09:48 lachoy Exp $
4  
5 87 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 87 ▶use Log::Log4perl qw( get_logger );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 87 ▶use OpenInteract2::Constants qw( :log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 87 ▶use OpenInteract2::Context qw( CTX DEPLOY_URL DEPLOY_IMAGE_URL DEPLOY_STATIC_URL );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 87 ▶use OpenInteract2::Log qw( uchk );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 87 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 87 $OpenInteract2::URL::VERSION = sprintf("%d.%02d", q$Revision: 1.31 $ =~ /(\d+)\.(\d+)/);
13  
14 87 ▶use constant QUERY_ARG_SEPARATOR => '&';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 87 my ( $log );
17  
18 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my ( $pkg ) = @_;
20 0 return bless( {}, $pkg );
21 }
22  
23 ########################################
24 # URL PARSING
25  
26 0 ▶sub parse_absolute_to_relative {
Subroutine parse_absolute_to_relative: not called CC=1 SCAR=6.9
Pod undocumented
27 0 my ( $class, $url_absolute ) = @_;
28 0 my $deployed_under = DEPLOY_URL;
29 0 $url_absolute =~ s/^$deployed_under//;
30 0 return $url_absolute;
31 }
32  
33 0 ▶sub parse {
Subroutine parse: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my ( $class, $url_relative ) = @_;
35 0 return _parse( $url_relative );
36 }
37  
38 # Common alias
39 0 ▶sub parse_relative { goto &parse }
Subroutine parse_relative: not called CC=1 SCAR=6.9
Pod undocumented
40  
41 0 ▶sub parse_absolute {
Subroutine parse_absolute: not called CC=1 SCAR=6.9
Pod undocumented
42 0 my ( $class, $url_absolute ) = @_;
43 0 return _parse( $url_absolute, 'yes' );
44 }
45  
46 0 ▶sub _parse {
Subroutine _parse: not called CC=6 SCAR=37.4
47 0 my ( $url, $context_aware ) = @_;
48 0 my $path = URI->new( $url )->path();
49  
50 0 ▶ if ( $context_aware and DEPLOY_URL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context_aware and DEPLOY_URL()) 0 0 0
Condition0%0/3
$context_aware and DEPLOY_URL()
!ll&&!rl&&r
000
MC/DC0%0/2
$context_aware and DEPLOY_URL()
$context_aware DEPLOY_URL()
Truth table0%0/3
A and B
A= $context_awareB= DEPLOY_URL()
ABresult
0X0
100
111
51 0 my $deployed_under = DEPLOY_URL;
52 0 ▶ unless ( $path =~ s/^$deployed_under// ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ s/^$deployed_under//) 0 0 0
53 0 return ( undef, undef );
54 }
55 }
56  
57 0 my @path_items = split( /\//, $path );
58 0 ▶ shift @path_items unless ( $path_items[0] );
Branch: 1 branch
BranchTrueFalseTotal
unless $path_items[0] 0 0 0
59 0 ▶ pop @path_items unless ( $path_items[-1] );
Branch: 1 branch
BranchTrueFalseTotal
unless $path_items[-1] 0 0 0
60 0 return ( @path_items );
61 }
62  
63  
64 ########################################
65 # URL CREATION
66  
67  
68 0 ▶sub create_relative_to_absolute {
Subroutine create_relative_to_absolute: not called CC=3 SCAR=24.8
Pod undocumented
69 0 my ( $class, $url_relative ) = @_;
70 0 my $deployed_under = DEPLOY_URL;
71 0 ▶ return $url_relative unless ( $deployed_under );
Branch: 1 branch
BranchTrueFalseTotal
unless $deployed_under 0 0 0
72 0 ▶ unless ( $url_relative =~ /^$deployed_under/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url_relative =~ /^$deployed_under/) 0 0 0
73 0 $url_relative = join( '', $deployed_under, $url_relative );
74 }
75 0 return $url_relative;
76 }
77  
78 0 ▶sub create {
Subroutine create: not called CC=8 SCAR=42.8
Pod undocumented
79 0 my ( $class, $url_base, $params, $do_not_escape ) = @_;
80 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
81 my $param_info = ( $log->is_debug )
82 0 ? join( '; ', map { "$_ = $params->{$_}" }
83 0 ▶ keys %{ $params } )
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug ? : 0 0 0
84 : undef;
85 0 ▶ if ( $params->{IMAGE} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'IMAGE'}) { } 0 0 0
elsif ($params->{'STATIC'}) { } 0 0 0
86 0 delete $params->{IMAGE};
87 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Creating image URL from '${url_base}' ", "and params '${param_info}'") 0 0 0
88 $log->debug( "Creating image URL from '$url_base' ",
89 "and params '$param_info'" );
90 0 return $class->create_image( $url_base, $params, $do_not_escape );
91 }
92 elsif ( $params->{STATIC} ) {
93 0 delete $params->{STATIC};
94 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Creating static URL from '${url_base}' ", "and params '${param_info}'") 0 0 0
95 $log->debug( "Creating static URL from '$url_base' ",
96 "and params '$param_info'" );
97 0 return $class->create_image( $url_base, $params, $do_not_escape );
98 }
99 $log->is_debug &&
100 0 ▶ $log->debug( "Creating deployment URL from '$url_base' ",
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug("Creating deployment URL from '${url_base}' ", "and params '${param_info}'") 0 0 0
101 "and params '$param_info'" );
102 0 return $class->_create_deployment(
103 DEPLOY_URL, $url_base, $params, $do_not_escape );
104 }
105  
106 0 ▶sub create_image {
Subroutine create_image: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my ( $class, $url_base, $params, $do_not_escape ) = @_;
108 0 return $class->_create_deployment(
109 DEPLOY_IMAGE_URL, $url_base, $params, $do_not_escape );
110 }
111  
112 0 ▶sub create_static {
Subroutine create_static: not called CC=1 SCAR=6.9
Pod undocumented
113 0 my ( $class, $url_base, $params, $do_not_escape ) = @_;
114 0 return $class->_create_deployment(
115 DEPLOY_STATIC_URL, $url_base, $params, $do_not_escape );
116 }
117  
118 # TODO: Modify to check 'REDIRECT' parameter to see if we should use
119 # '&' or '&' for query argument separator?
120  
121 0 ▶sub _create_deployment {
Subroutine _create_deployment: not called CC=8 SCAR=42.8
122 0 my ( $class, $deploy_under, $url_base, $params, $do_not_escape ) = @_;
123 0 my ( $is_absolute ) = $url_base =~ /^\s*http:/;
124  
125 # do this first so values from URL_PARAMS get escaped too
126 0 ▶ if ( $params->{URL_PARAMS} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"URL_PARAMS"}) 0 0 0
127 0 $url_base =~ s|/$||;
128 my @rest_params = ( ref $params->{URL_PARAMS} )
129 0 ? @{ $params->{URL_PARAMS} }
130 0 ▶ : ( $params->{URL_PARAMS} );
Branch: 1 branch
BranchTrueFalseTotal
ref $params->{'URL_PARAMS'} ? : 0 0 0
131 0 $url_base .= '/' . join( '/', @rest_params );
132 0 delete $params->{URL_PARAMS};
133 }
134  
135 # Absolute URLs don't get touched, so the escaping +
136 # contextualization doesn't get done to them
137 0 ▶ unless ( $is_absolute ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_absolute) 0 0 0
138 0 ▶ unless ( $do_not_escape ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($do_not_escape) 0 0 0
139 0 $url_base = _url_escape( $url_base );
140 }
141 0 ▶ if ( $deploy_under ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deploy_under) 0 0 0
142 0 $url_base = join( '', $deploy_under, $url_base );
143 }
144 }
145 0 ▶ $params ||= {};
Condition0%0/2
$params ||= {}
l!l
00
MC/DC0%0/1
$params ||= {}
$params
Truth table0%0/2
A
A= $params
Aresult
11
00
146  
147 0 ▶ return $url_base unless ( scalar keys %{ $params } );
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$params;} 0 0 0
148  
149 0 return $class->add_params_to_url( $url_base, $params );
150 }
151  
152 0 ▶sub add_params_to_url {
Subroutine add_params_to_url: not called CC=2 SCAR=17.9
Pod undocumented
153 0 my ( $class, $url, $params ) = @_;
154 my $query = join( QUERY_ARG_SEPARATOR,
155 0 map { _url_escape( $_ ) . "=" . _url_escape( $params->{ $_ } ) }
156 0 grep { defined $params->{ $_ } }
157 0 keys %{ $params } );
158 0 ▶ return ( $url =~ /\?/ )
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 0 0 0
159 ? join( QUERY_ARG_SEPARATOR, $url, $query )
160 : "$url?$query";
161  
162 }
163  
164 # NOTE: Coupling to OI2::Context->action_table with the 'url_primary'
165 # key.
166 #
167 # TODO: instead of using {url_primary}, first check and see if the
168 # action has a ->url property set.
169  
170 0 ▶sub create_from_action {
Subroutine create_from_action: not called CC=6 SCAR=37.4
Pod undocumented
171 0 my ( $class, $action, $task, $params, $do_not_escape ) = @_;
172 0 ▶ $log ||= get_logger( LOG_OI );
Condition0%0/3
$log ||= get_logger("OI2")
l!l&&r!l&&!r
000
MC/DC0%0/2
$log ||= get_logger("OI2")
$log get_logger("OI2")
Truth table0%0/3
A ||= B
A= $logB= get_logger("OI2")
ABresult
1X1
011
000
173  
174 0 my $info = eval { CTX->lookup_action_info( $action ) };
175  
176 # ...if the action isn't found
177 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
178 0 $log->warn( "Request URL for action '$action' not found; called ",
179 "from: ", join( ' | ', caller ) );
180 0 return undef;
181 }
182  
183 # ...if a URL for the action isn't found
184 0 ▶ unless ( $info->{url_primary} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"url_primary"}) 0 0 0
185 0 $log->warn( "Request URL for action '$action' but ",
186 "primary URL was not found in action info; ",
187 "probably means it's not URL-accessible" );
188 0 return undef;
189 }
190  
191 0 ▶ my $url_base = ( $task ) ? "/$info->{url_primary}/$task/"
Branch: 1 branch
BranchTrueFalseTotal
$task ? : 0 0 0
192 : "/$info->{url_primary}/";
193 0 ▶ $log->is_debug &&
Branch: 1 branch
BranchTrueFalseTotal
$log->is_debug and $log->debug(uchk("Creating URL from action '%s' and task '%s' -- '%s'", $action, $task, $url_base)) 0 0 0
194 $log->debug( uchk( "Creating URL from action '%s' and task " .
195 "'%s' -- '%s'", $action, $task, $url_base ) );
196 0 return $class->create( $url_base, $params, $do_not_escape );
197 }
198  
199 0 ▶sub _url_escape {
Subroutine _url_escape: not called CC=2 SCAR=17.9
200 0 my ( $to_encode ) = shift;
201 0 ▶ return undef unless defined( $to_encode );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $to_encode 0 0 0
202 0 $to_encode =~ s/([^a-zA-Z0-9_~\.\-\*\'\(\)\/\s])/uc sprintf("%%%02x",ord($1))/eg;
203 0 $to_encode =~ s/\s/%20/g;
204 0 return $to_encode;
205 }
206  
207 ########################################
208 # URL REMOVAL
209  
210 0 ▶sub strip_deployment_context {
Subroutine strip_deployment_context: not called CC=4 SCAR=30.0
Pod undocumented
211 0 my ( $class, $url ) = @_;
212 0 ▶ return '' unless ( $url );
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
213 0 ▶ return '/' if ( $url eq '/' );
Branch: 1 branch
BranchTrueFalseTotal
if $url eq "/" 0 0 0
214 0 ▶ return '' if ( $url eq DEPLOY_URL );
Branch: 1 branch
BranchTrueFalseTotal
if $url eq DEPLOY_URL() 0 0 0
215  
216 # Since we've taken care of the '/context' case with the previous
217 # statement we can assume any deployment context ends with a '/'
218  
219 0 my $deployment_context = DEPLOY_URL;
220 0 $url =~ s|^$deployment_context/|/|;
221 0 return $url;
222 }
223  
224 87 1;
225  
226 __END__
« OpenInteract2/File.pm ↑ Summary OpenInteract2/ActionResolver.pm »