| 1 | |||||||||||||
| 2 | package ASP4::ConfigNode::Web; | ||||||||||||
| 3 | |||||||||||||
| 4 | 15 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 15 | ▶ | use warnings 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 15 | ▶ | use base 'ASP4::ConfigNode'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 15 | ▶ | use Carp 'confess'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 15 | ▶ | use JSON::XS; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | |||||||||||||
| 11 | sub new | ||||||||||||
| 12 | 15 | ▶ | { | ||||||||||
|
Subroutine
new: called CC=10 SCAR=24.4
Pod
undocumented
| |||||||||||||
| 13 | 15 | my $class = shift; | |||||||||||
| 14 | |||||||||||||
| 15 | 15 | my $s = $class->SUPER::new( @_ ); | |||||||||||
| 16 | 15 | ▶ | $s->{handler_resolver} ||= 'ASP4::HTTPContext::HandlerResolver'; | ||||||||||
|
Condition50%1/2
$s->{"handler_resolver"} ||= "ASP4::HTTPContext::HandlerResolver"
MC/DC0%0/1
$s->{"handler_resolver"} ||= "ASP4::HTTPContext::HandlerResolver"
$s->{"handler_resolver"}
Truth table50%1/2
A
A= $s->{"handler_resolver"}
| |||||||||||||
| 17 | 15 | ▶ | $s->{handler_runner} ||= 'ASP4::HTTPContext::HandlerRunner'; | ||||||||||
|
Condition50%1/2
$s->{"handler_runner"} ||= "ASP4::HTTPContext::HandlerRunner"
MC/DC0%0/1
$s->{"handler_runner"} ||= "ASP4::HTTPContext::HandlerRunner"
$s->{"handler_runner"}
Truth table50%1/2
A
A= $s->{"handler_runner"}
| |||||||||||||
| 18 | 15 | ▶ | $s->{filter_resolver} ||= 'ASP4::HTTPContext::FilterResolver'; | ||||||||||
|
Condition50%1/2
$s->{"filter_resolver"} ||= "ASP4::HTTPContext::FilterResolver"
MC/DC0%0/1
$s->{"filter_resolver"} ||= "ASP4::HTTPContext::FilterResolver"
$s->{"filter_resolver"}
Truth table50%1/2
A
A= $s->{"filter_resolver"}
| |||||||||||||
| 19 | |||||||||||||
| 20 | map { | ||||||||||||
| 21 | 15 | ▶ | $_->{uri_match} = undef unless defined($_->{uri_match}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 22 | 30 | ▶ | $_->{uri_equals} = undef unless defined($_->{uri_equals}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | 30 | $_ = $class->SUPER::new( $_ ); | |||||||||||
| 24 | } $s->request_filters; | ||||||||||||
| 25 | map { | ||||||||||||
| 26 | 15 | ▶ | $_->{uri_match} = undef unless defined($_->{uri_match}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 27 | 60 | ▶ | $_->{uri_equals} = undef unless defined($_->{uri_equals}); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 28 | 60 | ▶ | $_->{disable_session} ||= 0; | ||||||||||
|
Condition50%1/2
$_->{"disable_session"} ||= 0
MC/DC0%0/1
$_->{"disable_session"} ||= 0
$_->{"disable_session"}
Truth table50%1/2
A
A= $_->{"disable_session"}
| |||||||||||||
| 29 | 60 | ▶ | $_->{disable_application} ||= 0; | ||||||||||
|
Condition50%1/2
$_->{"disable_application"} ||= 0
MC/DC0%0/1
$_->{"disable_application"} ||= 0
$_->{"disable_application"}
Truth table50%1/2
A
A= $_->{"disable_application"}
| |||||||||||||
| 30 | 60 | $_ = $class->SUPER::new( $_ ); | |||||||||||
| 31 | } $s->disable_persistence; | ||||||||||||
| 32 | |||||||||||||
| 33 | # Do we have "routes"?: | ||||||||||||
| 34 | 15 | eval { require Router::Generic }; | |||||||||||
| 35 | 15 | $s->{__has_router} = ! $@; | |||||||||||
| 36 | |||||||||||||
| 37 | 15 | return $s; | |||||||||||
| 38 | }# end new() | ||||||||||||
| 39 | |||||||||||||
| 40 | |||||||||||||
| 41 | sub request_filters | ||||||||||||
| 42 | 46 | ▶ | { | ||||||||||
|
Subroutine
request_filters: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 43 | 46 | my $s = shift; | |||||||||||
| 44 | |||||||||||||
| 45 | 46 | @{ $s->{request_filters} }; | |||||||||||
| 46 | }# end request_filters() | ||||||||||||
| 47 | |||||||||||||
| 48 | |||||||||||||
| 49 | sub disable_persistence | ||||||||||||
| 50 | 5160 | ▶ | { | ||||||||||
|
Subroutine
disable_persistence: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 51 | 5160 | my $s = shift; | |||||||||||
| 52 | |||||||||||||
| 53 | 5160 | @{ $s->{disable_persistence} }; | |||||||||||
| 54 | }# end disable_persistence() | ||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | sub router | ||||||||||||
| 58 | 0 | ▶ | { | ||||||||||
|
Subroutine
router: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 59 | 0 | my $s = shift; | |||||||||||
| 60 | 0 | ▶ | $s->_parse_routes() unless $s->{__parsed_routes}++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | $s->{router}; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | sub routes | ||||||||||||
| 65 | 0 | ▶ | { | ||||||||||
|
Subroutine
routes: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 66 | 0 | my $s = shift; | |||||||||||
| 67 | 0 | ▶ | return unless $s->{__has_router}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 0 | ▶ | $s->_parse_routes() unless $s->{__parsed_routes}++; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 69 | 0 | $s->{routes}; | |||||||||||
| 70 | }# end routes() | ||||||||||||
| 71 | |||||||||||||
| 72 | |||||||||||||
| 73 | sub _parse_routes | ||||||||||||
| 74 | 0 | ▶ | { | ||||||||||
|
Subroutine
_parse_routes: not called CC=5 SCAR=34.0
| |||||||||||||
| 75 | 0 | my $s = shift; | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | my @original = @{ $s->{routes} }; | |||||||||||
| 78 | 0 | my $app_root = $s->application_root; | |||||||||||
| 79 | 0 | @{ $s->{routes} } = map { | |||||||||||
| 80 | 0 | ▶ | $_->{include_routes} ? do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 0 | my $item = $_; | |||||||||||
| 82 | 0 | $item->{include_routes} =~ s/\@ServerRoot\@/$app_root/sg; | |||||||||||
| 83 | 0 | $item->{include_routes} =~ s{\\\\}{\\}g; | |||||||||||
| 84 | open my $ifh, '<', $item->{include_routes} | ||||||||||||
| 85 | 0 | ▶ | or die "Cannot open '$item->{include_routes}' for reading: $!"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 0 | local $/; | |||||||||||
| 87 | 0 | ▶ | my $json = eval { decode_json( scalar(<$ifh>) ) } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 88 | or confess "Error parsing '$item->{include_routes}': $@"; | ||||||||||||
| 89 | 0 | ▶ | ref($json) eq 'ARRAY' | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | or confess "File '$item->{include_routes}' should be an arrayref but it's a '@{[ ref($json) ]}' instead."; | |||||||||||
| 91 | 0 | @$json; | |||||||||||
| 92 | } : $_ | ||||||||||||
| 93 | } @original; | ||||||||||||
| 94 | |||||||||||||
| 95 | 0 | my $router = Router::Generic->new(); | |||||||||||
| 96 | 0 | map { $router->add_route( %$_ ) } @{ $s->{routes} }; | |||||||||||
| 97 | 0 | $s->{router} = $router; | |||||||||||
| 98 | }# end _parse_routes() | ||||||||||||
| 99 | |||||||||||||
| 100 | 15 | 1;# return true: | |||||||||||
| 101 | |||||||||||||
| 102 | =pod | ||||||||||||
| 103 | |||||||||||||
| 104 | =head1 NAME | ||||||||||||
| 105 | |||||||||||||
| 106 | ASP4::ConfigNode::Web - The $Config->web object. | ||||||||||||
| 107 | |||||||||||||
| 108 | =head1 SYNOPSIS | ||||||||||||
| 109 | |||||||||||||
| 110 | Given the following configuration... | ||||||||||||
| 111 | |||||||||||||
| 112 | { | ||||||||||||
| 113 | ... | ||||||||||||
| 114 | web: { | ||||||||||||
| 115 | application_name: "DefaultApp", | ||||||||||||
| 116 | application_root: "@ServerRoot@", | ||||||||||||
| 117 | www_root: "@ServerRoot@/htdocs", | ||||||||||||
| 118 | handler_root: "@ServerRoot@/handlers", | ||||||||||||
| 119 | page_cache_root: "/tmp/PAGE_CACHE", | ||||||||||||
| 120 | handler_resolver: "ASP4::HandlerResolver", | ||||||||||||
| 121 | handler_runner: "ASP4::HandlerRunner", | ||||||||||||
| 122 | filter_resolver: "ASP4::FilterResolver", | ||||||||||||
| 123 | request_filters: [ | ||||||||||||
| 124 | { | ||||||||||||
| 125 | uri_match: "^/.*", | ||||||||||||
| 126 | filter_class: "My::Filter" | ||||||||||||
| 127 | } | ||||||||||||
| 128 | ], | ||||||||||||
| 129 | disable_persistence: [ | ||||||||||||
| 130 | { | ||||||||||||
| 131 | uri_match: "^/nostate/.*", | ||||||||||||
| 132 | disable_session: true, | ||||||||||||
| 133 | disable_application: false | ||||||||||||
| 134 | } | ||||||||||||
| 135 | ] | ||||||||||||
| 136 | } | ||||||||||||
| 137 | ... | ||||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | You would access it like this: | ||||||||||||
| 141 | |||||||||||||
| 142 | $Config->web->application_name; # 'MyApp' | ||||||||||||
| 143 | $Config->web->application_root; # '/usr/local/projects/mysite.com' | ||||||||||||
| 144 | $Config->web->handler_root; # '/usr/local/projects/mysite.com/handlers' | ||||||||||||
| 145 | $Config->web->www_root; # '/usr/local/projects/mysite.com/htdocs' | ||||||||||||
| 146 | $Config->web->page_cache_root; # '/tmp/PAGE_CACHE' | ||||||||||||
| 147 | |||||||||||||
| 148 | You will never need to do this: | ||||||||||||
| 149 | foreach my $filter ( $Config->web->request_filters ) | ||||||||||||
| 150 | { | ||||||||||||
| 151 | my $regexp = $filter->uri_match; | ||||||||||||
| 152 | my $class = $filter->class; | ||||||||||||
| 153 | }# end foreach() | ||||||||||||
| 154 | |||||||||||||
| 155 | =head1 DESCRIPTION | ||||||||||||
| 156 | |||||||||||||
| 157 | ASP4::ConfigNode::Web provides access to the C<web> portion of the configuration. | ||||||||||||
| 158 | |||||||||||||
| 159 | =head1 PUBLIC PROPERTIES | ||||||||||||
| 160 | |||||||||||||
| 161 | =head2 application_name | ||||||||||||
| 162 | |||||||||||||
| 163 | Returns the name of the application. | ||||||||||||
| 164 | |||||||||||||
| 165 | =head2 application_root | ||||||||||||
| 166 | |||||||||||||
| 167 | Returns the absolute path to the root of the application, i.e. C</usr/local/projects/mysite.com> | ||||||||||||
| 168 | |||||||||||||
| 169 | =head2 handler_root | ||||||||||||
| 170 | |||||||||||||
| 171 | Returns the absolute path to where the 'handlers' are installed, i.e. C</usr/local/projects/mysite.com/handlers> | ||||||||||||
| 172 | |||||||||||||
| 173 | =head2 www_root | ||||||||||||
| 174 | |||||||||||||
| 175 | Returns the absolute path to where the normal website files (ASP, images, css, javascripts, etc) are located, | ||||||||||||
| 176 | i.e. C</usr/local/projects/mysite.com/htdocs> | ||||||||||||
| 177 | |||||||||||||
| 178 | =head2 page_cache_root | ||||||||||||
| 179 | |||||||||||||
| 180 | Returns the absolute path to where 'compiled' ASP scripts are stored, i.e. C</tmp/PAGE_CACHE> | ||||||||||||
| 181 | |||||||||||||
| 182 | Since the 'compiled' ASP scripts are overwritten whenever the source ASP script has been changed on disk, | ||||||||||||
| 183 | the webserver process must have read/write access to this location. | ||||||||||||
| 184 | |||||||||||||
| 185 | It is recommended that a temporary path is used - '/tmp' on most Linux distributions. | ||||||||||||
| 186 | |||||||||||||
| 187 | =head2 request_filters | ||||||||||||
| 188 | |||||||||||||
| 189 | Returns a list of ConfigNodes that represent individual C<filter> elements in the configuration. | ||||||||||||
| 190 | |||||||||||||
| 191 | =head2 router | ||||||||||||
| 192 | |||||||||||||
| 193 | B<*IF*> you have defined a "routes" section in your config - like this: | ||||||||||||
| 194 | |||||||||||||
| 195 | ... | ||||||||||||
| 196 | "web": { | ||||||||||||
| 197 | ... | ||||||||||||
| 198 | "routes": [ | ||||||||||||
| 199 | { | ||||||||||||
| 200 | "name": "Wiki", | ||||||||||||
| 201 | "path": "/:lang-:locale/{*page}", | ||||||||||||
| 202 | "target": "/wiki-page.asp", | ||||||||||||
| 203 | "defaults": { | ||||||||||||
| 204 | "page": "home", | ||||||||||||
| 205 | "lang": "en", | ||||||||||||
| 206 | "locale": "us" | ||||||||||||
| 207 | } | ||||||||||||
| 208 | } | ||||||||||||
| 209 | ] | ||||||||||||
| 210 | ... | ||||||||||||
| 211 | } | ||||||||||||
| 212 | |||||||||||||
| 213 | Then the C<router> property will return a L<Router::Generic> object based on your routes. | ||||||||||||
| 214 | |||||||||||||
| 215 | You can access it from the C<$Config> like this: | ||||||||||||
| 216 | |||||||||||||
| 217 | $Config->web->router | ||||||||||||
| 218 | |||||||||||||
| 219 | =head1 SEE ALSO | ||||||||||||
| 220 | |||||||||||||
| 221 | L<ASP4::RequestFilter> | ||||||||||||
| 222 | |||||||||||||
| 223 | =head1 BUGS | ||||||||||||
| 224 | |||||||||||||
| 225 | It's possible that some bugs have found their way into this release. | ||||||||||||
| 226 | |||||||||||||
| 227 | Use RT L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ASP4> to submit bug reports. | ||||||||||||
| 228 | |||||||||||||
| 229 | =head1 HOMEPAGE | ||||||||||||
| 230 | |||||||||||||
| 231 | Please visit the ASP4 homepage at L<http://0x31337.org/code/> to see examples | ||||||||||||
| 232 | of ASP4 in action. | ||||||||||||
| 233 | |||||||||||||
| 234 | =head1 AUTHOR | ||||||||||||
| 235 | |||||||||||||
| 236 | John Drago <jdrago_999@yahoo.com> | ||||||||||||
| 237 | |||||||||||||
| 238 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 239 | |||||||||||||
| 240 | Copyright 2007 John Drago, All rights reserved. | ||||||||||||
| 241 | |||||||||||||
| 242 | This software is free software. It may be used and distributed under the | ||||||||||||
| 243 | same terms as Perl itself. | ||||||||||||
| 244 | |||||||||||||
| 245 | =cut | ||||||||||||
| 246 | |||||||||||||