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   19249 use namespace::autoclean;
  1         3  
  1         13  
4 1     1   63 use Moose;
  1         2  
  1         7  
5 1     1   6289 use CatalystX::ASP;
  1         454  
  1         47  
6 1     1   10 use Scalar::Util qw(blessed);
  1         2  
  1         67  
7 1     1   483 use HTTP::Date qw(time2str);
  1         2686  
  1         62  
8 1     1   8 use Try::Tiny;
  1         2  
  1         449  
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 7421 my ( $self, $c, @args ) = @_;
49              
50 9         46 my $path = join '/', @args;
51              
52             try {
53 9     9   991 $self->render( $c, $path );
54              
55 7         190 my $resp = $self->asp->Response;
56 7         181 my $status = $resp->Status;
57 7 50       28 if ( $status >= 500 ) {
58 0         0 $c->error( "Erroring out because HTTP Status set to $status" );
59             } else {
60 7         181 my $charset = $resp->Charset;
61 7         230 my $content_type = $resp->ContentType;
62 7 100       27 $content_type .= "; charset=$charset" if $charset;
63 7         175 $c->response->content_type( $content_type );
64 7         1615 $resp->_flush_Cookies( $c );
65 7         164 $c->response->header( Cache_Control => $resp->CacheControl );
66 7 100       1667 $c->response->header( Expires => time2str( time + $resp->Expires ) ) if $resp->Expires;
67 7   100     338 $c->response->status( $resp->Status || 200 );
68 7         712 $c->response->body( $resp->Body );
69             }
70             } catch {
71              
72             # Passthrough $c->detach
73 2 50 33 2   196 $_->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   529 $self->asp->cleanup;
83 9         192 };
84              
85 7         243 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 39 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         830 local %ENV = %ENV;
103              
104 9         340 my $asp = $self->asp;
105 9 50       42 if ( $asp ) {
106 9         238 $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     229 my $compiled = $asp->compile_file( $c, $c->path_to( 'root', $path || $c->request->path ) );
113              
114 9         268 $asp->GlobalASA->Script_OnStart;
115 9         81 $asp->execute( $c, $compiled->{code} );
116 7         161 $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