lib/PAGI/Middleware/RequestId.pm

filter: Statementstmt 96.1% 50 / 52 Branchbran 75.0% 6 / 8 Conditioncond 72.7% 8 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 87.5% 77 / 88 CC 11 SCAR 13.6
CC
11
Cov
90%
__ANON__
7.2
_init
4.0
__ANON__
2.0
CRAP 3.9
« blib/lib/PAGI/Middleware/Session/State/Cookie.pm ↑ Summary blib/lib/PAGI/Endpoint/WebSocket.pm »
1 package PAGI::Middleware::RequestId;
2 2 $PAGI::Middleware::RequestId::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 PAGI::Utils::Random qw(secure_random_bytes);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PAGI::Middleware::RequestId - Unique request ID middleware
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Middleware::Builder;
16  
17 my $app = builder {
18 enable 'RequestId',
19 header => 'X-Request-ID',
20 trust_incoming => 0;
21 $my_app;
22 };
23  
24 =head1 DESCRIPTION
25  
26 PAGI::Middleware::RequestId generates unique request IDs and adds them
27 to both the scope and response headers. This is useful for request
28 tracing and log correlation.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * header (default: 'X-Request-ID')
35  
36 The header name to use for the request ID.
37  
38 =item * trust_incoming (default: 0)
39  
40 If true, use an existing request ID from the incoming request headers
41 instead of generating a new one.
42  
43 =item * generator (default: built-in UUID generator)
44  
45 A coderef that generates unique IDs. Receives the scope as argument.
46  
47 =back
48  
49 =cut
50  
51 # Simple counter for uniqueness within process
52 2 my $counter = 0;
53  
54 6 ▶sub _init {
Subroutine _init: called CC=4 SCAR=13.9
55 6 my ($self, $config) = @_;
56  
57 6 ▶ $self->{header} = $config->{header} // 'X-Request-ID';
Condition100%2/2
$config->{"header"} // "X-Request-ID"
l!l
24
MC/DC100%1/1
$config->{"header"} // "X-Request-ID"
$config->{"header"}
Truth table100%2/2
A
A= $config->{"header"}
Aresult
11
00
58 6 ▶ $self->{trust_incoming} = $config->{trust_incoming} // 0;
Condition100%2/2
$config->{"trust_incoming"} // 0
l!l
15
MC/DC100%1/1
$config->{"trust_incoming"} // 0
$config->{"trust_incoming"}
Truth table100%2/2
A
A= $config->{"trust_incoming"}
Aresult
11
00
59 6 ▶ $self->{generator} = $config->{generator} // \&_generate_id;
Condition50%1/2
$config->{"generator"} // \&PAGI::Middleware::RequestId::_generate_id
l!l
06
MC/DC0%0/1
$config->{"generator"} // \&PAGI::Middleware::RequestId::_generate_id
$config->{"generator"}
Truth table50%1/2
A
A= $config->{"generator"}
Aresult
11
00
60 }
61  
62 105 ▶sub _generate_id {
Subroutine _generate_id: called CC=1 SCAR=0.0
63 105 my ($scope) = @_;
64  
65 # Generate a UUID-like ID: timestamp + PID + counter + secure random
66 105 my $time = time();
67 105 $counter = ($counter + 1) % 0xFFFF;
68 105 my $rand_bytes = secure_random_bytes(6);
69 105 return sprintf('%08x-%04x-%04x-%s',
70 $time,
71 $$ & 0xFFFF,
72 $counter,
73 unpack('H12', $rand_bytes),
74 );
75 }
76  
77 4 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
78 4 my ($self, $app) = @_;
79  
80 4 my $header_name = lc($self->{header});
81  
82 5 ▶ return async sub {
Subroutine __ANON__: called CC=7 SCAR=19.8
83 5 my ($scope, $receive, $send) = @_;
84 # Only handle HTTP requests
85 5 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 5 5
86 0 await $app->($scope, $receive, $send);
87 0 return;
88 }
89  
90 # Check for existing request ID if trust_incoming is enabled
91 5 my $request_id;
92 5 ▶ if ($self->{trust_incoming}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"trust_incoming"}) 1 4 5
93 1 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
10
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
94 1 ▶ if (lc($h->[0]) eq $header_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq $header_name) 1 0 1
95 1 $request_id = $h->[1];
96 1 last;
97 }
98 }
99 }
100  
101 # Generate new ID if none found
102 5 ▶ $request_id //= $self->{generator}->($scope);
Condition66%2/3
$request_id //= $self->{"generator"}->($scope)
l!l&&r!l&&!r
140
MC/DC0%0/2
$request_id //= $self->{"generator"}->($scope)
$request_id $self->{"generator"}->($scope)
Truth table66%2/3
A //= B
A= $request_idB= $self->{"generator"}->($scope)
ABresult
1X1
011
000
103  
104 # Add request ID to scope
105 5 my $modified_scope = $self->modify_scope($scope, {
106 request_id => $request_id,
107 });
108  
109 # Intercept send to add request ID to response
110 10 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
111 10 my ($event) = @_;
112 10 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.start") 5 5 10
113 5 push @{$event->{headers}}, [$self->{header}, $request_id];
114 }
115 10 await $send->($event);
116 5 };
117  
118 5 await $app->($modified_scope, $receive, $wrapped_send);
119 4 };
120 }
121  
122 2 1;
123  
124 __END__
« blib/lib/PAGI/Middleware/Session/State/Cookie.pm ↑ Summary blib/lib/PAGI/Endpoint/WebSocket.pm »