| 1 | package PAGI::App::Redirect; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::App::Redirect::VERSION = '0.002002'; | |||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use PAGI::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | PAGI::App::Redirect - URL redirect app for the dynamic case | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # The static case is just a response value (preferred): | ||||||||||||||||||||||||||||
| 17 | use PAGI::Response; | ||||||||||||||||||||||||||||
| 18 | $router->mount('/old' => PAGI::Response->redirect('/new', 301)); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # PAGI::App::Redirect is for the dynamic case — a coderef target | ||||||||||||||||||||||||||||
| 21 | # and/or query-string preservation: | ||||||||||||||||||||||||||||
| 22 | use PAGI::App::Redirect; | ||||||||||||||||||||||||||||
| 23 | my $app = PAGI::App::Redirect->new( | ||||||||||||||||||||||||||||
| 24 | to => sub { my ($scope) = @_; compute_target($scope) }, | ||||||||||||||||||||||||||||
| 25 | status => 302, | ||||||||||||||||||||||||||||
| 26 | preserve_query => 1, | ||||||||||||||||||||||||||||
| 27 | )->to_app; | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =cut | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 6 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 6 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | return bless { | ||||||||||||||||||||||||||||
| 35 | to => $args{to}, | ||||||||||||||||||||||||||||
| 36 | status => $args{status} // 302, | ||||||||||||||||||||||||||||
| 37 | 6 | ▶ | preserve_query => $args{preserve_query} // 1, | ||||||||||||||||||||||||||
|
Condition100%4/4
$args{"status"} // 302
$args{"preserve_query"} // 1
MC/DC100%1/1
$args{"status"} // 302
$args{"status"}
MC/DC100%1/1
$args{"preserve_query"} // 1
$args{"preserve_query"}
Truth table100%2/2
A
A= $args{"status"}
Truth table100%2/2
A
A= $args{"preserve_query"}
| |||||||||||||||||||||||||||||
| 38 | }, $class; | ||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 6 | ▶ | sub to_app { | ||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 6 | my $to = $self->{to}; | |||||||||||||||||||||||||||
| 45 | 6 | my $status = $self->{status}; | |||||||||||||||||||||||||||
| 46 | 6 | my $preserve_query = $self->{preserve_query}; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 6 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 49 | 6 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 50 | 6 | ▶ | my $location = ref $to eq 'CODE' ? $to->($scope) : $to; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | ▶ | if ($preserve_query && $scope->{query_string}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$preserve_query and $scope->{"query_string"}
MC/DC100%2/2
$preserve_query and $scope->{"query_string"}
$preserve_query
$scope->{"query_string"}
Truth table100%3/3
A and B
A= $preserve_queryB= $scope->{"query_string"}
| |||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my $sep = $location =~ /\?/ ? '&' : '?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 1 | $location .= $sep . $scope->{query_string}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | await PAGI::Response->new($scope) | ||||||||||||||||||||||||||||
| 58 | ->content_type('text/plain') | ||||||||||||||||||||||||||||
| 59 | ->redirect($location, $status) | ||||||||||||||||||||||||||||
| 60 | 6 | ->respond($send); | |||||||||||||||||||||||||||
| 61 | 6 | }; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 1 | 1; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | __END__ | ||||||||||||||||||||||||||||