File Coverage

blib/lib/Web/Request/Role/AbsoluteUriFor.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 4 4 100.0
pod 1 1 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             package Web::Request::Role::AbsoluteUriFor;
2              
3             # ABSTRACT: Construct an absolute URI honoring script_name
4              
5             our $VERSION = '1.003'; # VERSION
6              
7 1     1   3512 use 5.010;
  1         3  
8 1     1   5 use Moose::Role;
  1         2  
  1         9  
9 1     1   5201 use Plack::Response;
  1         7894  
  1         157  
10              
11             sub absolute_uri_for {
12 48     48 1 336759 my ( $self, $uri_for ) = @_;
13              
14 48         76 my $url;
15 48 100       132 if ( ref($uri_for) eq 'HASH' ) {
16 16         43 $url = $self->uri_for($uri_for);
17             }
18             else {
19 32         50 $url = $uri_for;
20             }
21              
22 48   100     1820 my $script_name = $self->script_name || '';
23 48         1515 my $base_uri = $self->base_uri;
24              
25 48         16102 $base_uri=~s{/+$}{};
26 48         523 $script_name=~s{/+$}{};
27 48         99 $script_name=~s{^/+}{};
28 48         303 $base_uri=~s{/*$script_name/*$}{}g;
29 48         264 $url=~s{^/}{};
30 48         168 $url=~s{^/*$script_name/*}{};
31              
32 48         91 return join( '/', grep {$_ } $base_uri, $script_name, $url );
  144         324  
33             }
34              
35             1;
36              
37             __END__
38              
39             =pod
40              
41             =encoding UTF-8
42              
43             =head1 NAME
44              
45             Web::Request::Role::AbsoluteUriFor - Construct an absolute URI honoring script_name
46              
47             =head1 VERSION
48              
49             version 1.003
50              
51             =head1 SYNOPSIS
52              
53             # Create a request handler
54             package My::App::Request;
55             use Moose;
56             extends 'Web::Request';
57             with 'Web::Request::Role::AbsoluteUriFor';
58              
59             # Make sure your app uses your request handler, e.g. using OX:
60             package My::App::OX;
61             sub request_class {'My::App::Request'}
62              
63             # in some controller action:
64              
65             # redirect
66             $req->absolute_uri_for({ controller=>'foo', action=>'bar' });
67             # http://yoursite.com/mountpoint/foo/bar
68              
69             =head1 DESCRIPTION
70              
71             C<Web::Request::Role::AbsoluteUriFor> provides a method to calculate the absolute URI of a given controller/action, including the host name and handling various issues with C<SCRIPTNAME> and reverse proxies.
72              
73             =head2 METHODS
74              
75             =head3 absolute_uri_for
76              
77             $req->absolute_uri_for( '/some/path' );
78             $req->absolute_uri_for( $ref_uri_for );
79              
80             Construct an absolute URI out of C<base_uri>, C<script_name> and the
81             passed in string. You can also pass a ref, which will be resolved by
82             calling C<uri_for> on the request object.
83              
84             =head1 THANKS
85              
86             Thanks to
87              
88             =over
89              
90             =item *
91              
92             L<validad.com|https://www.validad.com/> for supporting Open Source.
93              
94             =back
95              
96             =head1 AUTHOR
97              
98             Thomas Klausner <domm@plix.at>
99              
100             =head1 COPYRIGHT AND LICENSE
101              
102             This software is copyright (c) 2017 - 2021 by Thomas Klausner.
103              
104             This is free software; you can redistribute it and/or modify it under
105             the same terms as the Perl 5 programming language system itself.
106              
107             =cut