blib/lib/PAGI/Middleware/RequestId.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 81 CC 5 SCAR 26.1
CC
5
Cov
0%
wrap
30.0
_init
2.0
_generate_id
2.0
CRAP 13.7
1 package PAGI::Middleware::RequestId;
2 $PAGI::Middleware::RequestId::VERSION = '0.002001';
3 use strict;
4 use warnings;
5 use parent 'PAGI::Middleware';
6 use Future::AsyncAwait;
7 use PAGI::Utils::Random qw(secure_random_bytes);
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 my $counter = 0;
53  
54 sub _init {
55 my ($self, $config) = @_;
56  
57 $self->{header} = $config->{header} // 'X-Request-ID';
58 $self->{trust_incoming} = $config->{trust_incoming} // 0;
59 $self->{generator} = $config->{generator} // \&_generate_id;
60 }
61  
62 sub _generate_id {
63 my ($scope) = @_;
64  
65 # Generate a UUID-like ID: timestamp + PID + counter + secure random
66 my $time = time();
67 $counter = ($counter + 1) % 0xFFFF;
68 my $rand_bytes = secure_random_bytes(6);
69 return sprintf('%08x-%04x-%04x-%s',
70 $time,
71 $$ & 0xFFFF,
72 $counter,
73 unpack('H12', $rand_bytes),
74 );
75 }
76  
77 sub wrap {
78 my ($self, $app) = @_;
79  
80 my $header_name = lc($self->{header});
81  
82 return async sub {
83 my ($scope, $receive, $send) = @_;
84 # Only handle HTTP requests
85 if ($scope->{type} ne 'http') {
86 await $app->($scope, $receive, $send);
87 return;
88 }
89  
90 # Check for existing request ID if trust_incoming is enabled
91 my $request_id;
92 if ($self->{trust_incoming}) {
93 for my $h (@{$scope->{headers} // []}) {
94 if (lc($h->[0]) eq $header_name) {
95 $request_id = $h->[1];
96 last;
97 }
98 }
99 }
100  
101 # Generate new ID if none found
102 $request_id //= $self->{generator}->($scope);
103  
104 # Add request ID to scope
105 my $modified_scope = $self->modify_scope($scope, {
106 request_id => $request_id,
107 });
108  
109 # Intercept send to add request ID to response
110 my $wrapped_send = async sub {
111 my ($event) = @_;
112 if ($event->{type} eq 'http.response.start') {
113 push @{$event->{headers}}, [$self->{header}, $request_id];
114 }
115 await $send->($event);
116 };
117  
118 await $app->($modified_scope, $receive, $wrapped_send);
119 };
120 }
121  
122 1;
123  
124 __END__