| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package PAGI::Middleware::Debug; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
365474
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
65
|
|
|
4
|
2
|
|
|
2
|
|
15
|
use warnings; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
91
|
|
|
5
|
2
|
|
|
2
|
|
679
|
use parent 'PAGI::Middleware'; |
|
|
2
|
|
|
|
|
540
|
|
|
|
2
|
|
|
|
|
10
|
|
|
6
|
2
|
|
|
2
|
|
102
|
use Future::AsyncAwait; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
9
|
|
|
7
|
2
|
|
|
2
|
|
88
|
use Time::HiRes qw(time); |
|
|
2
|
|
|
|
|
2
|
|
|
|
2
|
|
|
|
|
11
|
|
|
8
|
2
|
|
|
2
|
|
977
|
use JSON::MaybeXS (); |
|
|
2
|
|
|
|
|
25225
|
|
|
|
2
|
|
|
|
|
3069
|
|
|
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
|
|
|
|
|
|
|
sub _init { |
|
55
|
4
|
|
|
4
|
|
9
|
my ($self, $config) = @_; |
|
56
|
|
|
|
|
|
|
|
|
57
|
4
|
|
50
|
|
|
20
|
$self->{enabled} = $config->{enabled} // 0; |
|
58
|
4
|
|
100
|
|
|
17
|
$self->{show_headers} = $config->{show_headers} // 1; |
|
59
|
4
|
|
100
|
|
|
13
|
$self->{show_scope} = $config->{show_scope} // 1; |
|
60
|
4
|
|
100
|
|
|
14
|
$self->{show_timing} = $config->{show_timing} // 1; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub wrap { |
|
64
|
4
|
|
|
4
|
1
|
42
|
my ($self, $app) = @_; |
|
65
|
|
|
|
|
|
|
|
|
66
|
4
|
|
|
4
|
|
78
|
return async sub { |
|
67
|
4
|
|
|
|
|
11
|
my ($scope, $receive, $send) = @_; |
|
68
|
|
|
|
|
|
|
# Skip if not enabled or not HTTP |
|
69
|
4
|
100
|
66
|
|
|
19
|
if (!$self->{enabled} || $scope->{type} ne 'http') { |
|
70
|
1
|
|
|
|
|
3
|
await $app->($scope, $receive, $send); |
|
71
|
1
|
|
|
|
|
91
|
return; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
10
|
my $start_time = time(); |
|
75
|
3
|
|
|
|
|
20
|
my $response_status; |
|
76
|
|
|
|
|
|
|
my @response_headers; |
|
77
|
3
|
|
|
|
|
5
|
my $body = ''; |
|
78
|
3
|
|
|
|
|
5
|
my $is_html = 0; |
|
79
|
3
|
|
|
|
|
4
|
my $headers_sent = 0; |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# Wrap send to capture response and inject panel |
|
82
|
6
|
|
|
|
|
207
|
my $wrapped_send = async sub { |
|
83
|
6
|
|
|
|
|
9
|
my ($event) = @_; |
|
84
|
6
|
100
|
|
|
|
14
|
if ($event->{type} eq 'http.response.start') { |
|
85
|
3
|
|
|
|
|
5
|
$response_status = $event->{status}; |
|
86
|
3
|
|
50
|
|
|
3
|
@response_headers = @{$event->{headers} // []}; |
|
|
3
|
|
|
|
|
11
|
|
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# Check if HTML response |
|
89
|
3
|
|
|
|
|
5
|
for my $h (@response_headers) { |
|
90
|
3
|
100
|
66
|
|
|
26
|
if (lc($h->[0]) eq 'content-type' && $h->[1] =~ m{text/html}i) { |
|
91
|
2
|
|
|
|
|
3
|
$is_html = 1; |
|
92
|
2
|
|
|
|
|
3
|
last; |
|
93
|
|
|
|
|
|
|
} |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# If HTML, buffer; otherwise pass through |
|
97
|
3
|
100
|
|
|
|
8
|
if (!$is_html) { |
|
98
|
1
|
|
|
|
|
2
|
$headers_sent = 1; |
|
99
|
1
|
|
|
|
|
2
|
await $send->($event); |
|
100
|
|
|
|
|
|
|
} |
|
101
|
3
|
|
|
|
|
100
|
return; |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
|
|
104
|
3
|
50
|
|
|
|
11
|
if ($event->{type} eq 'http.response.body') { |
|
105
|
3
|
100
|
66
|
|
|
11
|
if ($is_html && !$headers_sent) { |
|
106
|
|
|
|
|
|
|
# Buffer body |
|
107
|
2
|
|
50
|
|
|
8
|
$body .= $event->{body} // ''; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# If this is the final chunk, inject panel |
|
110
|
2
|
50
|
|
|
|
5
|
if (!$event->{more}) { |
|
111
|
2
|
|
|
|
|
9
|
my $panel = $self->_build_panel($scope, $start_time, $response_status, \@response_headers); |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Inject before |