| 1 | package Web::Request::Role::AbsoluteUriFor; | ||||||||||||
| 2 | |||||||||||||
| 3 | # ABSTRACT: Construct an absolute URI honoring script_name | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | our $VERSION = '1.003'; # VERSION | |||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use 5.010; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Moose::Role; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Plack::Response; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | sub absolute_uri_for { | ||||||||||||
| 12 | 48 | ▶ | my ( $self, $uri_for ) = @_; | ||||||||||
|
Subroutine
absolute_uri_for: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 13 | |||||||||||||
| 14 | 48 | my $url; | |||||||||||
| 15 | 48 | ▶ | if ( ref($uri_for) eq 'HASH' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 16 | 16 | $url = $self->uri_for($uri_for); | |||||||||||
| 17 | } | ||||||||||||
| 18 | else { | ||||||||||||
| 19 | 32 | $url = $uri_for; | |||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | 48 | ▶ | my $script_name = $self->script_name || ''; | ||||||||||
|
Condition100%2/2
$self->script_name || ''
MC/DC100%1/1
$self->script_name || ''
$self->script_name
Truth table100%2/2
A
A= $self->script_name
| |||||||||||||
| 23 | 48 | my $base_uri = $self->base_uri; | |||||||||||
| 24 | |||||||||||||
| 25 | 48 | $base_uri=~s{/+$}{}; | |||||||||||
| 26 | 48 | $script_name=~s{/+$}{}; | |||||||||||
| 27 | 48 | $script_name=~s{^/+}{}; | |||||||||||
| 28 | 48 | $base_uri=~s{/*$script_name/*$}{}g; | |||||||||||
| 29 | 48 | $url=~s{^/}{}; | |||||||||||
| 30 | 48 | $url=~s{^/*$script_name/*}{}; | |||||||||||
| 31 | |||||||||||||
| 32 | 48 | return join( '/', grep {$_ } $base_uri, $script_name, $url ); | |||||||||||
| 33 | } | ||||||||||||
| 34 | |||||||||||||
| 35 | 2 | 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 | ||||||||||||