File Coverage

blib/lib/RapidApp/Plack/Middleware.pm
Criterion Covered Total %
statement 20 20 100.0
branch 2 2 100.0
condition 1 2 50.0
subroutine 5 5 100.0
pod 1 1 100.0
total 29 30 96.6


line stmt bran cond sub pod time code
1             package RapidApp::Plack::Middleware;
2 4     4   3181 use parent 'Plack::Middleware';
  4         16  
  4         40  
3              
4 4     4   293 use strict;
  4         8  
  4         81  
5 4     4   20 use warnings;
  4         7  
  4         130  
6              
7             # ABSTRACT: Default Middleware for RapidApp
8              
9 4     4   22 use RapidApp::Util qw(:all);
  4         7  
  4         2661  
10              
11             sub call {
12 50     50 1 101378 my ($self, $env) = @_;
13            
14             # RapidApp currently doesn't like PATH_INFO of ""
15 50   50     213 $env->{PATH_INFO} ||= '/';
16              
17             # --- GitHub Issue #153
18             # New: handing for magic path keyword '_ra-rel-mnt_' ("RapidApp Relative Mount")
19             #
20             # If the now reserved keyword/string '/_ra-rel-mnt_/' appears anyplace
21             # in the path, we munge it and strip everything in the path *up to
22             # that point*. The reason this is being done is to provide an
23             # efficient mechanism for generated markups such as <img> 'src', css
24             # urls, <link> tags, etc, to reference paths (such as simplecas and
25             # asset urls) w/o needing to know the current mount_url and, more
26             # importantly, be able to remain valid if the mount_url is changed
27             # after the fact. This is essentially a means to supply an absolute
28             # url path but the form of a *relative* url path from the perspective
29             # of the browser. This is a better way to handle this case than from
30             # within the module dispatch because it avoids the associated
31             # unnecessary overhead (which can vary from module to module) and is
32             # also even more flexible, to also work via locally-defined controller
33             # actions which internally re-dispatch to modules.
34 50         126 my $keyword = '_ra-rel-mnt_';
35 50 100       371 if($env->{PATH_INFO} =~ /\/\Q${keyword}\E\//) {
36 12         115 my @parts = split(/\/\Q${keyword}\E\//,$env->{PATH_INFO});
37 12         50 $env->{PATH_INFO} = '/' . pop(@parts);
38             }
39             # ---
40              
41             # FIXME: RapidApp applies logic based on uri in places,
42             # so we need it to match PATH_INFO
43 50         133 $env->{REQUEST_URI} = $env->{PATH_INFO};
44            
45 50         211 $self->app->($env)
46             }
47              
48              
49             1;