Gantry/Engine/MP20.pm untested

filter: Statementstmt 0.0% 0 / 243 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 62 Podpod 0.0% 0 / 45 totaltotal 0.0% 0 / 461 CC 38 SCAR 33.8
CC
38
Cov
0%
status_const
132.0
get_config
110.0
cast_custom_error
12.0
CRAP 29.4
1 package Gantry::Engine::MP20;
2 require Exporter;
3  
4 use strict;
5 use Carp qw( croak );
6 use File::Basename;
7  
8 use Apache2::Const -compile => qw(:common :http HTTP_UNAUTHORIZED);
9 use Apache2::Access;
10 use Apache2::Connection;
11 use Apache2::Request;
12 use Apache2::RequestIO;
13 use Apache2::RequestRec;
14 use Apache2::RequestUtil;
15 use Apache2::ServerUtil;;
16 use Apache2::Response ();
17 use Apache2::Upload;
18  
19 use Gantry::Conf;
20 use Gantry::Utils::DBConnHelper::MP20;
21  
22 use vars qw( @ISA @EXPORT );
23  
24 ############################################################
25 # Variables #
26 ############################################################
27 @ISA = qw( Exporter );
28 @EXPORT = qw(
29 apache_param_hash
30 apache_uf_param_hash
31 apache_request
32 base_server
33 cast_custom_error
34 consume_post_body
35 declined_response
36 dispatch_location
37 engine
38 engine_init
39 err_header_out
40 file_upload
41 fish_config
42 fish_location
43 fish_method
44 fish_path_info
45 fish_uri
46 fish_user
47 log_error
48 get_arg_hash
49 get_auth_dbh
50 get_cached_config
51 get_config
52 get_dbh
53 get_post_body
54 header_in
55 header_out
56 hostname
57 is_connection_secure
58 is_status_declined
59 port
60 print_output
61 remote_ip
62 redirect_response
63 send_http_header
64 send_error_output
65 server_root
66 set_cached_config
67 set_content_type
68 set_no_cache
69 set_req_params
70 status_const
71 success_code
72 url_encode
73 url_decode
74 );
75  
76 ############################################################
77 # Functions #
78 ############################################################
79  
80 #-------------------------------------------------
81 # $self->file_upload( param_name )
82 #-------------------------------------------------
83 sub file_upload {
84 my( $self, $param ) = @_;
85  
86 die "param required" if ! $param;
87
88 my $apr = $self->ap_req;
89 my $upload = $apr->upload( $param );
90
91 my $filename = $upload->filename;
92 $filename =~ s/\\/\//g;
93
94 my( $name, $path, $suffix ) = fileparse(
95 $filename,
96 qr/\.(tar\.gz$|[^.]*)/
97 );
98
99 return( {
100 unique_key => time . rand( 6 ),
101 name => $name,
102 suffix => $suffix,
103 fullname => ( $name . $suffix ),
104 size => ( $upload->size || 0 ),
105 mime => $upload->type,
106 filehandle => $upload->fh,
107 } );
108  
109 }
110  
111 #-------------------------------------------------
112 # $self->log_error( error )
113 #-------------------------------------------------
114 sub log_error {
115 my( $self, $msg ) = @_;
116  
117 $self->r->log_error( $msg );
118
119 }
120  
121 #-------------------------------------------------
122 # $self->cast_custom_error( error )
123 #-------------------------------------------------
124 sub cast_custom_error {
125 my( $self, $error_page, $die_msg ) = @_;
126  
127 my $status = ( $self->status() ? $self->status()
128 : $self->status_const( 'BAD_REQUEST' ) );
129
130 $self->r->log_error( $die_msg ) if defined $die_msg;
131
132 $self->r->custom_response( $status, $error_page );
133
134 return( $status );
135 }
136  
137 #-------------------------------------------------
138 # $self->apache_param_hash( $req )
139 #-------------------------------------------------
140 sub apache_param_hash {
141 my( $self, $req ) = @_;
142  
143 # If the application has specified that they want the unfiltered params
144 # by default, then make it happen.
145 if ($self->fish_config( 'unfiltered_params' ) && $self->fish_config( 'unfiltered_params' ) =~ /(1|on)/i) {
146 return $self->apache_uf_param_hash( $req );
147 }
148  
149 my $hash = {};
150 my @param_names = $req->param;
151  
152 foreach my $p ( @param_names ) {
153 my @values = $req->param( $p );
154  
155 # Replace angle brackets and quotes with named-entity equivalents.
156 $_ =~ s/</&lt;/g foreach @values;
157 $_ =~ s/>/&gt;/g foreach @values;
158 $_ =~ s/"/&#34;/g foreach @values;
159 $_ =~ s/'/&#39;/g foreach @values;
160
161 # Trim leading / trailing whitespace.
162 $_ =~ s/^\s+//o foreach @values;
163 $_ =~ s/\s+$//o foreach @values;
164  
165 $hash->{$p} = ( scalar @values == 1 ) ? shift @values : [ @values ];
166 }
167  
168 return( $hash );
169  
170 } # end: apache_param_hash
171  
172 #-------------------------------------------------
173 # $self->apache_uf_param_hash( $req )
174 #-------------------------------------------------
175 sub apache_uf_param_hash {
176 my( $self, $req ) = @_;
177  
178 my $hash = {};
179 my @param_names = $req->param;
180  
181 foreach my $p ( @param_names ) {
182 my @values = $req->param( $p );
183  
184 $hash->{$p} = ( scalar @values == 1 ) ? shift @values : [ @values ];
185 }
186  
187 return( $hash );
188  
189 } # end: apache_uf_param_hash
190  
191 #-------------------------------------------------
192 # $self->apache_request( )
193 #-------------------------------------------------
194 sub apache_request {
195 my ( $self, $r ) = @_;
196  
197 unless ( $self->{__AP_REQ__} ) {
198 $self->{__AP_REQ__} = Apache2::Request->new(
199 $self->r,
200 POST_MAX => ( $self->fish_config( 'post_max' ) || '20000000' )
201 );
202 }
203  
204 return $self->{__AP_REQ__};
205 } # end: apache_request
206  
207 #-------------------------------------------------
208 # $self->base_server( $r )
209 #-------------------------------------------------
210 sub base_server {
211 my( $self, $r ) = ( shift, shift );
212  
213 return (
214 $r ? $r->connection->base_server
215 : $self->r->connection->base_server );
216  
217 } # end base_server
218  
219 #-------------------------------------------------
220 # $self->consume_post_body( $r )
221 #-------------------------------------------------
222 sub consume_post_body {
223 my $self = shift;
224 my $r = shift;
225  
226 my $content_length = $r->headers_in->{'Content-length'};
227  
228 return unless $content_length;
229  
230 $content_length = 1e6 if $content_length > 1e6; # limit to ~ 1Meg
231  
232 my ( $content, $buffer );
233  
234 while ( $r->read( $buffer, $content_length ) ) {
235 $content .= $buffer;
236 }
237  
238 $self->{__POST_BODY__} = $content;
239 }
240  
241 #-------------------------------------------------
242 # $self->hostname( $r )
243 #-------------------------------------------------
244 sub hostname {
245 my( $self, $r ) = ( shift, shift );
246  
247 return(
248 $r ? $r->hostname
249 : $self->r->hostname );
250
251 } # end hostname
252  
253 #-------------------------------------------------
254 # $self->declined_response( )
255 #-------------------------------------------------
256 sub declined_response {
257 my $self = shift;
258  
259 return $self->status_const( 'DECLINED' );
260 } # END declined_response
261  
262 #-------------------------------------------------
263 # $self->dispatch_location( )
264 #-------------------------------------------------
265 sub dispatch_location {
266 my $self = shift;
267  
268 return $self->uri, $self->location;
269 } # END dispatch_location
270  
271 #-------------------------------------------------
272 # $self->engine
273 #-------------------------------------------------
274 sub engine {
275 return __PACKAGE__;
276  
277 } # end engine
278  
279 #-------------------------------------------------
280 # $self->engine_init
281 #-------------------------------------------------
282 sub engine_init {
283 my $self = shift;
284 my $r = shift;
285  
286 $self->r( $r );
287  
288 # set request body paramater variables
289 #$self->ap_req( $self->apache_request( $r ) );
290 #$self->params( $self->apache_param_hash( $self->ap_req ) );
291 } # END engine_init
292  
293 #-------------------------------------------------
294 # $self->err_header_out( $header_key, $header_value )
295 #-------------------------------------------------
296 sub err_header_out {
297 my( $self, $k, $v ) = @_;
298  
299 $self->r->err_headers_out->add( $k => $v );
300  
301 } # end err_header_out
302  
303 #-------------------------------------------------
304 # $self->fish_config( $param )
305 #-------------------------------------------------
306 sub fish_config {
307 my ( $self, $param ) = @_;
308  
309 # see if there Gantry::Conf data
310 my $conf = $self->get_config();
311  
312 return $$conf{ $param } if ( defined $conf and defined $$conf{ $param } );
313  
314 # otherwise, use dir_config for traditional approach
315 return $self->r()->dir_config( $param );
316  
317 } # END fish_config
318  
319 #-------------------------------------------------
320 # $self->fish_location( )
321 #-------------------------------------------------
322 sub fish_location {
323 my $self = shift;
324  
325 return $self->r()->location;
326 } # END fish_location
327  
328 #-------------------------------------------------
329 # $self->fish_method( )
330 #-------------------------------------------------
331 sub fish_method {
332 my $self = shift;
333  
334 return $self->r()->method;
335 } # END fish_method
336  
337 #-------------------------------------------------
338 # $self->fish_path_info( )
339 #-------------------------------------------------
340 sub fish_path_info {
341 my $self = shift;
342  
343 return $self->r()->path_info;
344 } # END fish_path_info
345  
346 #-------------------------------------------------
347 # $self->fish_uri( )
348 #-------------------------------------------------
349 sub fish_uri {
350 my $self = shift;
351  
352 return $self->r()->uri;
353 } # END fish_uri
354  
355 #-------------------------------------------------
356 # $self->fish_user( )
357 #-------------------------------------------------
358 sub fish_user {
359 my $self = shift;
360  
361 return $self->user() || $self->r()->user;
362 } # END fish_uri
363  
364 #-------------------------------------------------
365 # $self->get_arg_hash
366 #-------------------------------------------------
367 sub get_arg_hash {
368 my( $self, $r ) = @_;
369  
370 my %args;
371 if ( $r ) {
372 %args = $r->args;
373 }
374 else {
375 %args = $self->r->args;
376 }
377 return wantarray ? %args : \%args;
378
379 } # end get_arg_hash
380  
381 #-------------------------------------------------
382 # $self->get_auth_dbh( )
383 #-------------------------------------------------
384 sub get_auth_dbh {
385 return Gantry::Utils::DBConnHelper::MP20->get_auth_dbh;
386 }
387  
388 #-------------------------------------------------
389 # $self->get_config( )
390 #-------------------------------------------------
391 sub get_config {
392 my ( $self ) = @_;
393  
394 # see if there Gantry::Conf data
395 my $instance = $self->r()->dir_config( 'GantryConfInstance' );
396  
397 return unless defined $instance;
398  
399 my $file = $self->r()->dir_config( 'GantryConfFile' );
400  
401 my $conf;
402 my $cached = 0;
403 my $location = '';
404
405 eval {
406 $location = $self->location;
407 };
408
409 $conf = $self->get_cached_config( $instance, $location );
410  
411 if ( defined $conf ) {
412 return $conf;
413 }
414
415 my $gantry_cache = 0;
416 my $gantry_cache_key = '';
417 my $gantry_cache_hit = 0;
418 eval {
419 ++$gantry_cache if $self->cache_inited();
420 };
421
422 # are we using gantry cache ?
423 if ( $gantry_cache ) {
424  
425 $self->cache_namespace('gantry');
426  
427 # blow the gantry conf cache when server starts
428 if ( $self->engine_cycle() == 1 ) {
429
430 eval {
431 foreach my $key ( @{ $self->cache_keys() } ) {
432 my @a = split( ':', $key );
433 if ( $a[0] eq 'gantryconf' ) {
434 $self->cache_del( $key );
435 }
436 }
437 };
438 }
439
440 # build cache key
441 $gantry_cache_key = join( ':',
442 "gantryconf",
443 ( $self->namespace() || '' ),
444 $instance,
445 $location
446 );
447
448 $conf = $self->cache_get( $gantry_cache_key );
449
450 ++$gantry_cache_hit if defined $conf;
451 }
452
453 $conf ||= Gantry::Conf->retrieve(
454 {
455 instance => $instance,
456 config_file => $file,
457 location => $location
458 }
459 );
460  
461 if ( defined $conf ) {
462 $self->set_cached_config( $instance, $location, $conf );
463
464 if ( $gantry_cache && ! $gantry_cache_hit ) {
465 $self->cache_set( $gantry_cache_key, $conf );
466 }
467 }
468  
469 return $conf;
470  
471 } # END get_config
472  
473 #-------------------------------------------------
474 # $self->get_cached_config( $instance, $location )
475 #-------------------------------------------------
476 sub get_cached_config {
477 my $self = shift;
478 my $instance = shift;
479 my $location = shift;
480  
481 return $self->r()->pnotes( "conf_${instance}_${location}" );
482 }
483  
484 #-------------------------------------------------
485 # $self->set_cached_config( $instance, $location, $conf )
486 #-------------------------------------------------
487 sub set_cached_config {
488 my $self = shift;
489 my $instance = shift;
490 my $location = shift;
491 my $conf = shift;
492  
493 $self->r()->pnotes( "conf_${instance}_${location}", $conf );
494 }
495  
496 #-------------------------------------------------
497 # $self->get_dbh( )
498 #-------------------------------------------------
499 sub get_dbh {
500 return Gantry::Utils::DBConnHelper::MP20->get_dbh;
501 }
502  
503 #-------------------------------------------------
504 # $self->get_post_body( )
505 #-------------------------------------------------
506 sub get_post_body {
507 my $self = shift;
508  
509 return $self->{__POST_BODY__};
510 }
511  
512 #-------------------------------------------------
513 # $self->header_in( )
514 #-------------------------------------------------
515 sub header_in {
516 my( $self, $key ) = @_;
517
518 return $self->r->headers_in->{ $key };
519  
520 } # end header_in
521  
522 #-------------------------------------------------
523 # $self->header_out( $header_key, $header_value )
524 #-------------------------------------------------
525 sub header_out {
526 my( $self, $k, $v ) = @_;
527
528 $self->r->headers_out->set( $k => $v );
529  
530 } # end header_out
531  
532 #-------------------------------------------------
533 # $self->is_connection_secure()
534 #-------------------------------------------------
535 sub is_connection_secure {
536 my $self = shift;
537  
538 return $self->r->subprocess_env('HTTPS') ? 1 : 0;
539 } # END is_connection_secure
540  
541 #-------------------------------------------------
542 # $self->is_status_declined( $status )
543 #-------------------------------------------------
544 sub is_status_declined {
545 my $self = shift;
546  
547 my $status = $self->status || '';
548  
549 return 1 if ( $status eq $self->status_const( 'DECLINED' ) );
550 } # END is_status_declined
551  
552 #-------------------------------------------------
553 # $self->port( $r )
554 #-------------------------------------------------
555 sub port {
556 my( $self, $r ) = ( shift, shift );
557
558 my $s = Apache2::ServerUtil->server;
559 return( $s->port );
560  
561 } # end port
562  
563 #-------------------------------------------------
564 # $self->print_output( )
565 #-------------------------------------------------
566 sub print_output {
567 my $self = shift;
568 my $response = shift;
569  
570 $self->r()->print( $response );
571 } # END print_output
572  
573 #-------------------------------------------------
574 # $self->redirect_response( )
575 #-------------------------------------------------
576 sub redirect_response {
577 my $self = shift;
578  
579 return $self->status_const( 'REDIRECT' );
580  
581 } # END redirect_response
582  
583 #-------------------------------------------------
584 # $self->remote_ip( $r )
585 #-------------------------------------------------
586 sub remote_ip {
587 my( $self, $r ) = ( shift, shift );
588
589 return(
590 $r ? $r->connection->remote_ip
591 : $self->r->connection->remote_ip );
592  
593 } # end remote_ip
594  
595 #-------------------------------------------------
596 # $self->send_error_output( $@ )
597 #-------------------------------------------------
598 sub send_error_output {
599 my $self = shift;
600  
601 $self->do_error( $@ );
602 return( $self->custom_error( $@ ) );
603  
604 } # END send_error_output
605  
606 #-------------------------------------------------
607 # $self->send_http_header( )
608 #-------------------------------------------------
609 sub send_http_header {
610
611 # do nothing for mod_perl 2.0
612  
613 } # send_http_header
614  
615 #-------------------------------------------------
616 # $self->server_root( $r )
617 #-------------------------------------------------
618 sub server_root {
619 my( $self ) = ( shift );
620
621 return( Apache2::ServerUtil::server_root() );
622  
623 } # end server_root
624  
625 #-------------------------------------------------
626 # $self->set_content_type( )
627 #-------------------------------------------------
628 sub set_content_type {
629 my $self = shift;
630  
631 $self->r()->content_type( $self->content_type );
632 } # END set_content_type
633  
634 #-------------------------------------------------
635 # $self->set_no_cache( )
636 #-------------------------------------------------
637 sub set_no_cache {
638 my $self = shift;
639  
640 $self->r()->no_cache( 1 ) if ( $self->no_cache );
641 } # END set_no_cache
642  
643 #-------------------------------------------------
644 # $self->set_req_params( )
645 #-------------------------------------------------
646 sub set_req_params {
647 my $self = shift;
648  
649 $self->ap_req( $self->apache_request( $self->r ) );
650 $self->params( $self->apache_param_hash( $self->ap_req ) );
651 $self->uf_params( $self->apache_uf_param_hash( $self->ap_req ) );
652 } # END set_req_params
653  
654 #-------------------------------------------------
655 # $self->status_const( 'OK | DECLINED | REDIRECT' )
656 #-------------------------------------------------
657 sub status_const {
658 my( $self, $status ) = @_;
659  
660 # Upper case our status
661 $status = uc($status);
662  
663 return Apache2::Const::DECLINED if $status eq 'DECLINED';
664 return Apache2::Const::OK if $status eq 'OK';
665 return Apache2::Const::REDIRECT if $status eq 'REDIRECT';
666 return Apache2::Const::HTTP_MOVED_PERMANENTLY
667 if $status eq 'MOVED_PERMANENTLY';
668 return Apache2::Const::FORBIDDEN if $status eq 'FORBIDDEN';
669 return Apache2::Const::SERVER_ERROR if $status eq 'SERVER_ERROR';
670
671 return Apache2::Const::HTTP_BAD_REQUEST if $status eq 'BAD_REQUEST';
672 return Apache2::Const::HTTP_BAD_REQUEST if $status eq 'HTTP_BAD_REQUEST';
673
674 return Apache2::Const::HTTP_UNAUTHORIZED if $status eq 'UNAUTHORIZED';
675 return Apache2::Const::HTTP_UNAUTHORIZED if $status eq 'HTTP_UNAUTHORIZED';
676
677 die( "Undefined constant $status" );
678  
679 } # end status_const
680  
681 #-------------------------------------------------
682 # $self->success_code( )
683 #-------------------------------------------------
684 sub success_code {
685 my $self = shift;
686  
687 return $self->status_const( 'OK' );
688 } # END success_code
689  
690 #-------------------------------------------------
691 # $self->url_encode( )
692 #-------------------------------------------------
693 sub url_encode {
694 my $self = shift;
695 my $value = shift;
696
697 return APR::Request::encode( $value );
698 } # END url_encode
699  
700 #-------------------------------------------------
701 # $self->url_decode( )
702 #-------------------------------------------------
703 sub url_decode {
704 my $self = shift;
705 my $value = shift;
706
707 return APR::Request::decode( $value );
708 } # END url_decode
709  
710 # EOF
711 1;
712  
713 __END__