blib/lib/PAGI/Middleware/Debug.pm

filter: Statementstmt 93.2% 97 / 104 Branchbran 80.7% 21 / 26 Conditioncond 73.0% 19 / 26 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 85.8% 158 / 184 CC 29 SCAR 22.2
CC
29
Cov
88%
__ANON__
17.1
_build_panel
8.1
_init
5.0
CRAP 9.2
« lib/PAGI/Middleware/AccessLog.pm ↑ Summary lib/PAGI/Middleware/Auth/Basic.pm »
1 package PAGI::Middleware::Debug;
2 2 $PAGI::Middleware::Debug::VERSION = '0.002002';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 PAGI::Middleware::Debug - Development debug panel middleware
13  
14 =head1 SYNOPSIS
15  
16 use PAGI::Middleware::Builder;
17  
18 my $app = builder {
19 enable 'Debug',
20 enabled => $ENV{PAGI_DEBUG};
21 $my_app;
22 };
23  
24 =head1 DESCRIPTION
25  
26 PAGI::Middleware::Debug injects a debug panel into HTML responses
27 showing request/response details, timing breakdown, and headers.
28 Only enabled in development mode.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * enabled (default: 0)
35  
36 Enable the debug panel. Should only be true in development.
37  
38 =item * show_headers (default: 1)
39  
40 Show request/response headers in debug panel.
41  
42 =item * show_scope (default: 1)
43  
44 Show scope contents in debug panel.
45  
46 =item * show_timing (default: 1)
47  
48 Show timing breakdown in debug panel.
49  
50 =back
51  
52 =cut
53  
54 4 ▶sub _init {
Subroutine _init: called CC=5 SCAR=16.1
55 4 my ($self, $config) = @_;
56  
57 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
58 4 ▶ $self->{show_headers} = $config->{show_headers} // 1;
Condition100%2/2
$config->{"show_headers"} // 1
l!l
13
MC/DC100%1/1
$config->{"show_headers"} // 1
$config->{"show_headers"}
Truth table100%2/2
A
A= $config->{"show_headers"}
Aresult
11
00
59 4 ▶ $self->{show_scope} = $config->{show_scope} // 1;
Condition100%2/2
$config->{"show_scope"} // 1
l!l
13
MC/DC100%1/1
$config->{"show_scope"} // 1
$config->{"show_scope"}
Truth table100%2/2
A
A= $config->{"show_scope"}
Aresult
11
00
60 4 ▶ $self->{show_timing} = $config->{show_timing} // 1;
Condition100%2/2
$config->{"show_timing"} // 1
l!l
13
MC/DC100%1/1
$config->{"show_timing"} // 1
$config->{"show_timing"}
Truth table100%2/2
A
A= $config->{"show_timing"}
Aresult
11
00
61 }
62  
63 4 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
64 4 my ($self, $app) = @_;
65  
66 4 ▶ return async sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
67 4 my ($scope, $receive, $send) = @_;
68 # Skip if not enabled or not HTTP
69 4 ▶ if (!$self->{enabled} || $scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"enabled"} or $scope->{"type"} ne "http") 1 3 4
Condition66%2/3
not $self->{"enabled"} or $scope->{"type"} ne "http"
l!l&&r!l&&!r
103
MC/DC50%1/2
not $self->{"enabled"} or $scope->{"type"} ne "http"
not $self->{"enabled"} $scope->{"type"} ne "http"
Truth table66%2/3
not(A) or B
A= not $self->{"enabled"}B= $scope->{"type"} ne "http"
ABresult
1X1
011
000
70 1 await $app->($scope, $receive, $send);
71 1 return;
72 }
73  
74 3 my $start_time = time();
75 3 my $response_status;
76 my @response_headers;
77 3 my $body = '';
78 3 my $is_html = 0;
79 3 my $headers_sent = 0;
80  
81 # Wrap send to capture response and inject panel
82 6 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=15 SCAR=28.4
83 6 my ($event) = @_;
84 6 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.start") 3 3 6
85 3 $response_status = $event->{status};
86 3 ▶ @response_headers = @{$event->{headers} // []};
Condition50%1/2
$event->{"headers"} // []
l!l
30
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
Aresult
11
00
87  
88 # Check if HTML response
89 3 for my $h (@response_headers) {
90 3 ▶ if (lc($h->[0]) eq 'content-type' && $h->[1] =~ m{text/html}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "content-type" and $h->[1] =~ m[text/html]i) 2 1 3
Condition66%2/3
lc $h->[0] eq "content-type" and $h->[1] =~ m[text/html]i
!ll&&!rl&&r
012
MC/DC50%1/2
lc $h->[0] eq "content-type" and $h->[1] =~ m[text/html]i
lc $h->[0] eq "content-type" $h->[1] =~ m[text/html]i
Truth table66%2/3
A and B
A= lc $h->[0] eq "content-type"B= $h->[1] =~ m[text/html]i
ABresult
0X0
100
111
91 2 $is_html = 1;
92 2 last;
93 }
94 }
95  
96 # If HTML, buffer; otherwise pass through
97 3 ▶ if (!$is_html) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_html) 1 2 3
98 1 $headers_sent = 1;
99 1 await $send->($event);
100 }
101 3 return;
102 }
103  
104 3 ▶ if ($event->{type} eq 'http.response.body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.body") 3 0 3
105 3 ▶ if ($is_html && !$headers_sent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_html and not $headers_sent) { } 2 1 3
Condition66%2/3
$is_html and not $headers_sent
!ll&&!rl&&r
102
MC/DC50%1/2
$is_html and not $headers_sent
$is_html not $headers_sent
Truth table66%2/3
A and not(B)
A= $is_htmlB= not $headers_sent
ABresult
0X0
100
111
106 # Buffer body
107 2 ▶ $body .= $event->{body} // '';
Condition50%1/2
$event->{"body"} // ""
l!l
20
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
Aresult
11
00
108  
109 # If this is the final chunk, inject panel
110 2 ▶ if (!$event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($event->{"more"}) 2 0 2
111 2 my $panel = $self->_build_panel($scope, $start_time, $response_status, \@response_headers);
112  
113 # Inject before </body> or at end
114 2 ▶ if ($body =~ s{(</body>)}{$panel$1}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ s[(</body>)][$panel$1]i) { } 2 0 2
115 # Injected before </body>
116 } else {
117 0 $body .= $panel;
118 }
119  
120 # Update Content-Length if present
121 2 for my $h (@response_headers) {
122 2 ▶ if (lc($h->[0]) eq 'content-length') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "content-length") 0 2 2
123 0 $h->[1] = length($body);
124 0 last;
125 }
126 }
127  
128 2 $headers_sent = 1;
129 2 await $send->({
130 type => 'http.response.start',
131 status => $response_status,
132 headers => \@response_headers,
133 });
134 2 await $send->({
135 type => 'http.response.body',
136 body => $body,
137 more => 0,
138 });
139 }
140 } else {
141 1 await $send->($event);
142 }
143 3 return;
144 }
145  
146 0 await $send->($event);
147 3 };
148  
149 3 await $app->($scope, $receive, $wrapped_send);
150 4 };
151 }
152  
153 2 ▶sub _build_panel {
Subroutine _build_panel: called CC=8 SCAR=21.0
154 2 my ($self, $scope, $start_time, $status, $response_headers) = @_;
155  
156 2 my $duration = sprintf("%.3f", (time() - $start_time) * 1000);
157  
158 2 my $html = qq{
159 <style>
160 #pagi-debug-panel {
161 position: fixed;
162 bottom: 0;
163 right: 0;
164 width: 400px;
165 max-height: 50vh;
166 background: #1a1a2e;
167 color: #eee;
168 font-family: monospace;
169 font-size: 12px;
170 overflow: auto;
171 border: 1px solid #333;
172 border-radius: 8px 0 0 0;
173 z-index: 99999;
174 box-shadow: 0 0 10px rgba(0,0,0,0.5);
175 }
176 #pagi-debug-panel h3 {
177 background: #16213e;
178 margin: 0;
179 padding: 8px 12px;
180 cursor: pointer;
181 }
182 #pagi-debug-panel .content {
183 padding: 10px;
184 }
185 #pagi-debug-panel table {
186 width: 100%;
187 border-collapse: collapse;
188 }
189 #pagi-debug-panel th, #pagi-debug-panel td {
190 text-align: left;
191 padding: 4px;
192 border-bottom: 1px solid #333;
193 }
194 #pagi-debug-panel th { color: #0f4c75; }
195 #pagi-debug-panel .section { margin-bottom: 15px; }
196 #pagi-debug-panel .section-title { color: #3282b8; font-weight: bold; margin-bottom: 5px; }
197 </style>
198 <div id="pagi-debug-panel">
199 <h3 onclick="this.parentElement.classList.toggle('collapsed')">PAGI Debug Panel</h3>
200 <div class="content">
201 };
202  
203 # Timing section
204 2 ▶ if ($self->{show_timing}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"show_timing"}) 1 1 2
205 1 $html .= qq{
206 <div class="section">
207 <div class="section-title">Timing</div>
208 <table>
209 <tr><th>Total Time</th><td>${duration}ms</td></tr>
210 <tr><th>Status</th><td>$status</td></tr>
211 </table>
212 </div>
213 };
214 }
215  
216 # Request section
217 2 ▶ if ($self->{show_scope}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"show_scope"}) 2 0 2
218 2 my $method = _html_escape($scope->{method});
219 2 my $path = _html_escape($scope->{path});
220 2 my $query = _html_escape($scope->{query_string});
221 2 my $scheme = _html_escape($scope->{scheme});
222 2 $html .= qq{
223 <div class="section">
224 <div class="section-title">Request</div>
225 <table>
226 <tr><th>Method</th><td>$method</td></tr>
227 <tr><th>Path</th><td>$path</td></tr>
228 <tr><th>Query</th><td>$query</td></tr>
229 <tr><th>Scheme</th><td>$scheme</td></tr>
230 </table>
231 </div>
232 };
233 }
234  
235 # Request headers section
236 2 ▶ if ($self->{show_headers} && $scope->{headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"show_headers"} and $scope->{"headers"}) 1 1 2
Condition66%2/3
$self->{"show_headers"} and $scope->{"headers"}
!ll&&!rl&&r
101
MC/DC50%1/2
$self->{"show_headers"} and $scope->{"headers"}
$self->{"show_headers"} $scope->{"headers"}
Truth table66%2/3
A and B
A= $self->{"show_headers"}B= $scope->{"headers"}
ABresult
0X0
100
111
237 1 $html .= qq{
238 <div class="section">
239 <div class="section-title">Request Headers</div>
240 <table>
241 };
242 1 for my $h (@{$scope->{headers}}) {
243 0 my $name = _html_escape($h->[0]);
244 0 my $value = _html_escape($h->[1]);
245 0 $html .= qq{<tr><th>$name</th><td>$value</td></tr>\n};
246 }
247 1 $html .= qq{</table></div>};
248 }
249  
250 # Response headers section
251 2 ▶ if ($self->{show_headers}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"show_headers"}) 1 1 2
252 1 $html .= qq{
253 <div class="section">
254 <div class="section-title">Response Headers</div>
255 <table>
256 };
257 1 for my $h (@$response_headers) {
258 1 my $name = _html_escape($h->[0]);
259 1 my $value = _html_escape($h->[1]);
260 1 $html .= qq{<tr><th>$name</th><td>$value</td></tr>\n};
261 }
262 1 $html .= qq{</table></div>};
263 }
264  
265 2 $html .= qq{
266 </div>
267 </div>
268 };
269  
270 2 return $html;
271 }
272  
273 10 ▶sub _html_escape {
Subroutine _html_escape: called CC=2 SCAR=6.9
274 10 ▶ my $str = shift // '';
Condition100%2/2
shift() // ""
l!l
91
MC/DC100%1/1
shift() // ""
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
275 10 $str =~ s/&/&amp;/g;
276 10 $str =~ s/</&lt;/g;
277 10 $str =~ s/>/&gt;/g;
278 10 $str =~ s/"/&quot;/g;
279 10 return $str;
280 }
281  
282 2 1;
283  
284 __END__
« lib/PAGI/Middleware/AccessLog.pm ↑ Summary lib/PAGI/Middleware/Auth/Basic.pm »