File Coverage

blib/lib/CatalystX/ASP/View.pm
Criterion Covered Total %
statement 46 52 88.4
branch 7 12 58.3
condition 4 14 28.5
subroutine 11 11 100.0
pod 2 2 100.0
total 70 91 76.9


line stmt bran cond sub pod time code
1             package CatalystX::ASP::View;
2              
3 1     1   12899 use namespace::autoclean;
  1         2  
  1         5  
4 1     1   44 use Moose;
  1         2  
  1         4  
5 1     1   4398 use CatalystX::ASP;
  1         338  
  1         43  
6 1     1   7 use Scalar::Util qw(blessed);
  1         1  
  1         60  
7 1     1   591 use HTTP::Date qw(time2str);
  1         2853  
  1         53  
8 1     1   5 use Try::Tiny;
  1         2  
  1         429  
9              
10             extends 'Catalyst::View';
11              
12             has 'asp' => (
13             is => 'rw',
14             isa => 'CatalystX::ASP',
15             );
16              
17             =head1 NAME
18              
19             CatalystX::ASP::View - Catalyst View for processing ASP scripts
20              
21             =head1 SYNOPSIS
22              
23             package MyApp::Controller::Foo;
24              
25             sub 'asp' : Regex('\.asp$') {
26             my ($self, $c, @args) = @_;
27             $c->forward( $c->view( 'ASP' ), \@args );
28             }
29              
30             =head1 DESCRIPTION
31              
32             This is the Catalyst View to handle ASP scripts. Given a C<$path> to the ASP
33             script, this will render the ASP script and populate C<< $c->response >> with
34             the computed headers and body.
35              
36             =head1 METHODS
37              
38             =over
39              
40             =item $self->process($c, @args)
41              
42             Takes a C<$path> or guesses base off C<< $c->request->path >>. After ASP
43             renders the output, this will populate C<< $c->response >> accordingly
44              
45             =cut
46              
47             sub process {
48 9     9 1 4415 my ( $self, $c, @args ) = @_;
49              
50 9         20 my $path = join '/', @args;
51              
52             try {
53 9     9   424 $self->render( $c, $path );
54              
55 7         181 my $resp = $self->asp->Response;
56 7         173 my $status = $resp->Status;
57 7 50       16 if ( $status >= 500 ) {
58 0         0 $c->error( "Erroring out because HTTP Status set to $status" );
59             } else {
60 7         180 my $charset = $resp->Charset;
61 7         203 my $content_type = $resp->ContentType;
62 7 100       15 $content_type .= "; charset=$charset" if $charset;
63 7         132 $c->response->content_type( $content_type );
64 7         1344 $resp->_flush_Cookies( $c );
65 7         151 $c->response->header( Cache_Control => $resp->CacheControl );
66 7 100       1403 $c->response->header( Expires => time2str( time + $resp->Expires ) ) if $resp->Expires;
67 7   100     294 $c->response->status( $resp->Status || 200 );
68 7         647 $c->response->body( $resp->Body );
69             }
70             } catch {
71              
72             # Passthrough $c->detach
73 2 50 33 2   94 $_->rethrow if blessed( $_ ) && $_->isa( 'Catalyst::Exception::Detach' );
74              
75             # If error in other ASP code, return HTTP 500
76 0 0 0     0 if ( blessed( $_ ) && !$_->isa( 'CatalystX::ASP::Exception::End' ) && !$c->has_errors ) {
      0        
77 0         0 $c->error( "Encountered application error: $_" );
78             }
79             } finally {
80              
81             # Ensure destruction!
82 9     9   404 $self->asp->cleanup;
83 9         108 };
84              
85 7         157 return 1;
86             }
87              
88             =item $self->render($c, $path)
89              
90             This does the bulk work of ASP processing. First parse file, the compile. During
91             execution, kick off any hooks configured. Finally, properly handle errors,
92             passing through C<< $c->detach >> if called as a result of
93             C<< $Response->Redirect >> or C<< $Response->End >> if called in ASP script.
94              
95             =cut
96              
97             sub render {
98 9     9 1 20 my ( $self, $c, $path ) = @_;
99              
100             # Create localized ENV because ASP modifies and assumes ENV being populated
101             # with Request headers as in CGI
102 9         461 local %ENV = %ENV;
103              
104 9         258 my $asp = $self->asp;
105 9 50       29 if ( $asp ) {
106 9         198 $asp->c( $c );
107             } else {
108 0         0 $asp = CatalystX::ASP->new( %{ $c->config->{'CatalystX::ASP'} }, c => $c );
  0         0  
109 0         0 $self->asp( $asp );
110             }
111              
112 9   33     173 my $compiled = $asp->compile_file( $c, $c->path_to( 'root', $path || $c->request->path ) );
113              
114 9         206 $asp->GlobalASA->Script_OnStart;
115 9         38 $asp->execute( $c, $compiled->{code} );
116 7         169 $asp->GlobalASA->Script_OnFlush;
117             }
118              
119             __PACKAGE__->meta->make_immutable;
120              
121             =back
122              
123             =head1 SEE ALSO
124              
125             =over
126              
127             =item * L<CatalystX::ASP>
128              
129             =item * L<CatalystX::ASP::Role>
130              
131             =back