blib/lib/PAGI/Middleware/Maintenance.pm

filter: Statementstmt 72.7% 56 / 77 Branchbran 35.2% 12 / 34 Conditioncond 33.3% 9 / 27 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 76.9% 10 / 13 Podpod 100% 1 / 1 totaltotal 53.8% 90 / 167 CC 32 SCAR 27.2
CC
32
Cov
56%
_ip_in_cidr
30.0
_all_valid_octets
30.0
_should_bypass
21.7
CRAP 15.2
« lib/PAGI/Middleware/Static.pm ↑ Summary lib/PAGI/App/Loader.pm »
1 package PAGI::Middleware::Maintenance;
2 1 $PAGI::Middleware::Maintenance::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::Maintenance - Serve maintenance page when enabled
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'Maintenance',
18 enabled => $ENV{MAINTENANCE_MODE},
19 bypass_ips => ['10.0.0.0/8'],
20 retry_after => 3600;
21 $my_app;
22 };
23  
24 =head1 DESCRIPTION
25  
26 PAGI::Middleware::Maintenance serves a 503 Service Unavailable page
27 when maintenance mode is enabled. Supports IP-based bypass for admins.
28  
29 =head1 CONFIGURATION
30  
31 =over 4
32  
33 =item * enabled (default: 0)
34  
35 Enable maintenance mode. Can be a coderef for dynamic checking.
36  
37 =item * bypass_ips (default: [])
38  
39 Arrayref of IPs or CIDR ranges that bypass maintenance mode.
40  
41 =item * bypass_paths (default: [])
42  
43 Arrayref of paths that bypass maintenance mode (e.g., health checks).
44  
45 =item * retry_after (optional)
46  
47 Seconds until maintenance expected to end. Sets Retry-After header.
48  
49 =item * content_type (default: 'text/html')
50  
51 Content-Type of the maintenance page.
52  
53 =item * body (default: built-in HTML page)
54  
55 Custom maintenance page body.
56  
57 =back
58  
59 =cut
60  
61 4 ▶sub _init {
Subroutine _init: called CC=6 SCAR=18.6
62 4 my ($self, $config) = @_;
63  
64 4 ▶ $self->{enabled} = $config->{enabled} // 0;
Condition50%1/2
$config->{"enabled"} // 0
l!l
40
MC/DC0%0/1
$config->{"enabled"} // 0
$config->{"enabled"}
Truth table50%1/2
A
A= $config->{"enabled"}
Aresult
11
00
65 4 ▶ $self->{bypass_ips} = $config->{bypass_ips} // [];
Condition100%2/2
$config->{"bypass_ips"} // []
l!l
13
MC/DC100%1/1
$config->{"bypass_ips"} // []
$config->{"bypass_ips"}
Truth table100%2/2
A
A= $config->{"bypass_ips"}
Aresult
11
00
66 4 ▶ $self->{bypass_paths} = $config->{bypass_paths} // [];
Condition100%2/2
$config->{"bypass_paths"} // []
l!l
13
MC/DC100%1/1
$config->{"bypass_paths"} // []
$config->{"bypass_paths"}
Truth table100%2/2
A
A= $config->{"bypass_paths"}
Aresult
11
00
67 4 $self->{retry_after} = $config->{retry_after};
68 4 ▶ $self->{content_type} = $config->{content_type} // 'text/html';
Condition50%1/2
$config->{"content_type"} // "text/html"
l!l
04
MC/DC0%0/1
$config->{"content_type"} // "text/html"
$config->{"content_type"}
Truth table50%1/2
A
A= $config->{"content_type"}
Aresult
11
00
69 4 ▶ $self->{body} = $config->{body} // $self->_default_body();
Condition33%1/3
$config->{"body"} // $self->_default_body
l!l&&r!l&&!r
040
MC/DC0%0/2
$config->{"body"} // $self->_default_body
$config->{"body"} $self->_default_body
Truth table33%1/3
A // B
A= $config->{"body"}B= $self->_default_body
ABresult
1X1
011
000
70 }
71  
72 4 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.1
Pod documented
73 4 my ($self, $app) = @_;
74  
75 4 ▶ return async sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
76 4 my ($scope, $receive, $send) = @_;
77 4 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 4 4
78 0 await $app->($scope, $receive, $send);
79 0 return;
80 }
81  
82 # Check if maintenance is enabled
83 my $enabled = ref $self->{enabled} eq 'CODE'
84 ? $self->{enabled}->()
85 4 ▶ : $self->{enabled};
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'enabled'} eq 'CODE' ? : 0 4 4
86  
87 4 ▶ unless ($enabled) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($enabled) 1 3 4
88 1 await $app->($scope, $receive, $send);
89 1 return;
90 }
91  
92 # Check bypass conditions
93 3 ▶ if ($self->_should_bypass($scope)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_should_bypass($scope)) 2 1 3
94 2 await $app->($scope, $receive, $send);
95 2 return;
96 }
97  
98 # Serve maintenance page
99 1 await $self->_send_maintenance($send);
100 4 };
101 }
102  
103 3 ▶sub _should_bypass {
Subroutine _should_bypass: called CC=12 SCAR=30.8
104 3 my ($self, $scope) = @_;
105  
106 # Check bypass paths
107 3 ▶ my $path = $scope->{path} // '';
Condition50%1/2
$scope->{"path"} // ""
l!l
30
MC/DC0%0/1
$scope->{"path"} // ""
$scope->{"path"}
Truth table50%1/2
A
A= $scope->{"path"}
Aresult
11
00
108 3 for my $bypass_path (@{$self->{bypass_paths}}) {
109 1 ▶ if (ref $bypass_path eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $bypass_path eq 'Regexp') { } 0 1 1
110 0 ▶ return 1 if $path =~ $bypass_path;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /$bypass_path/ 0 0 0
111 } else {
112 1 ▶ return 1 if $path eq $bypass_path;
Branch: 1 branch
BranchTrueFalseTotal
if $path eq $bypass_path 1 0 1
113 }
114 }
115  
116 # Check bypass IPs
117 2 ▶ my $client_ip = exists $scope->{client} ? ($scope->{client}[0] // '') : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $scope->{'client'} ? : 2 0 2
Condition50%1/2
$scope->{"client"}[0] // ""
l!l
20
MC/DC0%0/1
$scope->{"client"}[0] // ""
$scope->{"client"}[0]
Truth table50%1/2
A
A= $scope->{"client"}[0]
Aresult
11
00
118 2 for my $bypass_ip (@{$self->{bypass_ips}}) {
119 1 ▶ if ($bypass_ip =~ m{/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bypass_ip =~ m[/]) { } 0 1 1
120 # CIDR notation
121 0 ▶ return 1 if $self->_ip_in_cidr($client_ip, $bypass_ip);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ip_in_cidr($client_ip, $bypass_ip) 0 0 0
122 } else {
123 # Exact match
124 1 ▶ return 1 if $client_ip eq $bypass_ip;
Branch: 1 branch
BranchTrueFalseTotal
if $client_ip eq $bypass_ip 1 0 1
125 }
126 }
127  
128 1 return 0;
129 }
130  
131 0 ▶sub _ip_in_cidr {
Subroutine _ip_in_cidr: not called CC=5 SCAR=34.0
132 0 my ($self, $ip, $cidr) = @_;
133  
134 0 my ($network, $bits) = split m{/}, $cidr;
135  
136 # Simple IPv4 check
137 0 ▶ return 0 unless $ip =~ /^[\d.]+$/ && $network =~ /^[\d.]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ip =~ /^[\d.]+$/ and $network =~ /^[\d.]+$/ 0 0 0
Condition0%0/3
$ip =~ /^[\d.]+$/ and $network =~ /^[\d.]+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$ip =~ /^[\d.]+$/ and $network =~ /^[\d.]+$/
$ip =~ /^[\d.]+$/ $network =~ /^[\d.]+$/
Truth table0%0/3
A and B
A= $ip =~ /^[\d.]+$/B= $network =~ /^[\d.]+$/
ABresult
0X0
100
111
138  
139 0 my $ip_num = $self->_ip_to_num($ip);
140 0 my $net_num = $self->_ip_to_num($network);
141  
142 0 ▶ return 0 unless defined $ip_num && defined $net_num;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ip_num and defined $net_num 0 0 0
Condition0%0/3
defined $ip_num and defined $net_num
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ip_num and defined $net_num
defined $ip_num defined $net_num
Truth table0%0/3
A and B
A= defined $ip_numB= defined $net_num
ABresult
0X0
100
111
143  
144 0 my $mask = ~((1 << (32 - $bits)) - 1) & 0xFFFFFFFF;
145 0 return ($ip_num & $mask) == ($net_num & $mask);
146 }
147  
148 0 ▶sub _ip_to_num {
Subroutine _ip_to_num: not called CC=3 SCAR=24.8
149 0 my ($self, $ip) = @_;
150  
151 0 my @octets = split /\./, $ip;
152 0 ▶ return unless @octets == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @octets == 4 0 0 0
153 0 ▶ return unless _all_valid_octets(@octets);
Branch: 1 branch
BranchTrueFalseTotal
unless _all_valid_octets(@octets) 0 0 0
154 0 return ($octets[0] << 24) + ($octets[1] << 16) + ($octets[2] << 8) + $octets[3];
155 }
156  
157 0 ▶sub _all_valid_octets {
Subroutine _all_valid_octets: not called CC=5 SCAR=34.0
158 0 for (@_) {
159 0 ▶ return 0 unless /^\d+$/ && $_ >= 0 && $_ <= 255;
Branch: 1 branch
BranchTrueFalseTotal
unless /^\d+$/ and $_ >= 0 and $_ <= 255 0 0 0
Condition0%0/6
/^\d+$/ and $_ >= 0 and $_ <= 255
!ll&&!rl&&r
000
/^\d+$/ and $_ >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
/^\d+$/ and $_ >= 0 and $_ <= 255
/^\d+$/ $_ >= 0 $_ <= 255
Truth table0%0/4
A and B and C
A= /^\d+$/B= $_ >= 0C= $_ <= 255
ABCresult
0XX0
10X0
1100
1111
160 }
161 0 return 1;
162 }
163  
164 1 ▶async sub _send_maintenance {
Subroutine _send_maintenance: called CC=2 SCAR=7.1
165 1 my ($self, $send) = @_;
166  
167 1 my $body = $self->{body};
168  
169 my @headers = (
170 1 ['Content-Type', $self->{content_type}],
171 ['Content-Length', length($body)],
172 );
173  
174 1 ▶ if (defined $self->{retry_after}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"retry_after"}) 0 1 1
175 0 push @headers, ['Retry-After', $self->{retry_after}];
176 }
177  
178 await $send->({
179 type => 'http.response.start',
180 status => 503,
181 headers => \@headers,
182 1 });
183 1 await $send->({
184 type => 'http.response.body',
185 body => $body,
186 more => 0,
187 });
188 1 }
189  
190 4 ▶sub _default_body {
Subroutine _default_body: called CC=1 SCAR=0.0
191 4 my ($self) = @_;
192  
193 4 return <<'HTML';
194 <!DOCTYPE html>
195 <html>
196 <head>
197 <meta charset="utf-8">
198 <meta name="viewport" content="width=device-width, initial-scale=1">
199 <title>Maintenance</title>
200 <style>
201 body {
202 font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
203 background: #f5f5f5;
204 display: flex;
205 justify-content: center;
206 align-items: center;
207 min-height: 100vh;
208 margin: 0;
209 }
210 .container {
211 background: white;
212 padding: 40px;
213 border-radius: 8px;
214 box-shadow: 0 2px 10px rgba(0,0,0,0.1);
215 text-align: center;
216 max-width: 500px;
217 }
218 h1 { color: #333; margin-bottom: 10px; }
219 p { color: #666; line-height: 1.6; }
220 .icon { font-size: 64px; margin-bottom: 20px; }
221 </style>
222 </head>
223 <body>
224 <div class="container">
225 <div class="icon">🔧</div>
226 <h1>Under Maintenance</h1>
227 <p>We're currently performing scheduled maintenance. Please check back soon.</p>
228 </div>
229 </body>
230 </html>
231 HTML
232 }
233  
234 1 1;
235  
236 __END__
« lib/PAGI/Middleware/Static.pm ↑ Summary lib/PAGI/App/Loader.pm »