| 1 | package PAGI::Middleware::Rewrite; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::Rewrite::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 parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | PAGI::Middleware::Rewrite - URL rewriting middleware | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||
| 17 | enable 'Rewrite', | ||||||||||||||||||||||||||||
| 18 | rules => [ | ||||||||||||||||||||||||||||
| 19 | { from => qr{^/old/(.*)}, to => '/new/$1' }, | ||||||||||||||||||||||||||||
| 20 | { from => '/legacy', to => '/modern' }, | ||||||||||||||||||||||||||||
| 21 | ]; | ||||||||||||||||||||||||||||
| 22 | $my_app; | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | PAGI::Middleware::Rewrite rewrites request paths before passing to the | ||||||||||||||||||||||||||||
| 28 | inner application. Supports both exact matches and regex patterns. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =over 4 | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =item * rules (required) | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | Arrayref of rewrite rules. Each rule is a hashref with: | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | { from => '/old-path', to => '/new-path' } | ||||||||||||||||||||||||||||
| 39 | { from => qr{^/user/(\d+)}, to => '/users/$1' } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | =item * redirect (default: 0) | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | If true, send redirect response instead of rewriting internally. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =item * redirect_code (default: 301) | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | HTTP status code for redirects. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =back | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 54 | 4 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | $self->{rules} = $config->{rules} | ||||||||||||||||||||||||||||
| 57 | 4 | ▶ | // die "Rewrite middleware requires 'rules' option"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"rules"} // die("Rewrite middleware requires 'rules' option")
MC/DC0%0/1
$config->{"rules"} // die("Rewrite middleware requires 'rules' option")
$config->{"rules"}
Truth table50%1/2
A
A= $config->{"rules"}
| |||||||||||||||||||||||||||||
| 58 | 4 | ▶ | $self->{redirect} = $config->{redirect} // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"redirect"} // 0
MC/DC100%1/1
$config->{"redirect"} // 0
$config->{"redirect"}
Truth table100%2/2
A
A= $config->{"redirect"}
| |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | $self->{redirect_code} = $config->{redirect_code} // 301; | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"redirect_code"} // 301
MC/DC100%1/1
$config->{"redirect_code"} // 301
$config->{"redirect_code"}
Truth table100%2/2
A
A= $config->{"redirect_code"}
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.2
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 4 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 4 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=20.9
| |||||||||||||||||||||||||||||
| 66 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 67 | 4 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 69 | 0 | return; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | my $path = $scope->{path}; | |||||||||||||||||||||||||||
| 73 | 4 | my $new_path = $self->_apply_rules($path); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # No rewrite needed | ||||||||||||||||||||||||||||
| 76 | 4 | ▶ | if ($new_path eq $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 78 | 1 | return; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | # Redirect mode | ||||||||||||||||||||||||||||
| 82 | 3 | ▶ | if ($self->{redirect}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 1 | my $location = $new_path; | |||||||||||||||||||||||||||
| 84 | 1 | ▶ | if (defined $scope->{query_string} && $scope->{query_string} ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $scope->{"query_string"} and $scope->{"query_string"} ne ""
MC/DC0%0/2
defined $scope->{"query_string"} and $scope->{"query_string"} ne ""
defined $scope->{"query_string"}
$scope->{"query_string"} ne ""
Truth table33%1/3
A and B
A= defined $scope->{"query_string"}B= $scope->{"query_string"} ne ""
| |||||||||||||||||||||||||||||
| 85 | 0 | $location .= '?' . $scope->{query_string}; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | 1 | await $self->_send_redirect($send, $location); | |||||||||||||||||||||||||||
| 88 | 1 | return; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # Internal rewrite | ||||||||||||||||||||||||||||
| 92 | my $new_scope = $self->modify_scope($scope, { | ||||||||||||||||||||||||||||
| 93 | path => $new_path, | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | original_path => $scope->{original_path} // $path, | ||||||||||||||||||||||||||
|
Condition33%1/3
$scope->{"original_path"} // $path
MC/DC0%0/2
$scope->{"original_path"} // $path
$scope->{"original_path"}
$path
Truth table33%1/3
A // B
A= $scope->{"original_path"}B= $path
| |||||||||||||||||||||||||||||
| 95 | }); | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 2 | await $app->($new_scope, $receive, $send); | |||||||||||||||||||||||||||
| 98 | 4 | }; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | sub _apply_rules { | ||||||||||||||||||||||||||
|
Subroutine
_apply_rules: called CC=8 SCAR=21.5
| |||||||||||||||||||||||||||||
| 102 | 4 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 4 | for my $rule (@{$self->{rules}}) { | |||||||||||||||||||||||||||
| 105 | 4 | my $from = $rule->{from}; | |||||||||||||||||||||||||||
| 106 | 4 | my $to = $rule->{to}; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 4 | ▶ | if (ref $from eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 1 | ▶ | if ($path =~ $from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 1 | my @captures = ($path =~ $from); | |||||||||||||||||||||||||||
| 111 | 1 | my $new_path = $to; | |||||||||||||||||||||||||||
| 112 | 1 | for my $i (0 .. $#captures) { | |||||||||||||||||||||||||||
| 113 | 1 | my $n = $i + 1; | |||||||||||||||||||||||||||
| 114 | 1 | $new_path =~ s/\$$n/$captures[$i]/g; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | 1 | return $new_path; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | } else { | ||||||||||||||||||||||||||||
| 119 | 3 | ▶ | if ($path eq $from) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 2 | return $to; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | # Also check prefix match for directory-like rules | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | if ($path =~ m{^\Q$from\E(/.*)?$}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $suffix = $1 // ''; | ||||||||||||||||||||||||||
|
Condition0%0/2
$1 // ""
MC/DC0%0/1
$1 // ""
$1
Truth table0%0/2
A
A= $1
| |||||||||||||||||||||||||||||
| 125 | 0 | return $to . $suffix; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | return $path; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 1 | ▶ | async sub _send_redirect { | ||||||||||||||||||||||||||
|
Subroutine
_send_redirect: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 134 | 1 | my ($self, $send, $location) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 1 | my $status = $self->{redirect_code}; | |||||||||||||||||||||||||||
| 137 | 1 | my $body = "Redirecting to $location"; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 140 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 141 | status => $status, | ||||||||||||||||||||||||||||
| 142 | headers => [ | ||||||||||||||||||||||||||||
| 143 | ['Content-Type', 'text/plain'], | ||||||||||||||||||||||||||||
| 144 | ['Content-Length', length($body)], | ||||||||||||||||||||||||||||
| 145 | ['Location', $location], | ||||||||||||||||||||||||||||
| 146 | ], | ||||||||||||||||||||||||||||
| 147 | }); | ||||||||||||||||||||||||||||
| 148 | 1 | await $send->({ | |||||||||||||||||||||||||||
| 149 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 150 | body => $body, | ||||||||||||||||||||||||||||
| 151 | more => 0, | ||||||||||||||||||||||||||||
| 152 | }); | ||||||||||||||||||||||||||||
| 153 | 1 | } | |||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | 1; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | __END__ | ||||||||||||||||||||||||||||