blib/lib/PAGI/Middleware/Session/State/Header.pm

filter: Statementstmt 100% 30 / 30 Branchbran 100% 10 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 96.2% 51 / 53 CC 8 SCAR 10.5
CC
8
Cov
98%
_get_header
4.0
extract
4.0
new
2.0
CRAP 2.9
« blib/lib/PAGI/Middleware/Builder.pm ↑ Summary blib/lib/PAGI/Headers.pm »
1 package PAGI::Middleware::Session::State::Header;
2 2 $PAGI::Middleware::Session::State::Header::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::Session::State';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 PAGI::Middleware::Session::State::Header - Header-based session ID transport
10  
11 =head1 SYNOPSIS
12  
13 use PAGI::Middleware::Session::State::Header;
14  
15 my $state = PAGI::Middleware::Session::State::Header->new(
16 header_name => 'X-Session-ID',
17 );
18  
19 # With extraction pattern
20 my $state = PAGI::Middleware::Session::State::Header->new(
21 header_name => 'X-Auth-Token',
22 pattern => qr/^Token\s+(.+)$/i,
23 );
24  
25 # Extract session ID from request
26 my $id = $state->extract($scope);
27  
28 =head1 DESCRIPTION
29  
30 Implements the L<PAGI::Middleware::Session::State> interface using a custom
31 HTTP header for session ID transport. The session ID is read from the
32 specified request header. Injection is a no-op because the client is
33 responsible for managing header-based transport.
34  
35 =head1 CONFIGURATION
36  
37 =over 4
38  
39 =item * header_name (required)
40  
41 Name of the HTTP header containing the session ID.
42  
43 =item * pattern (optional)
44  
45 A regex with a capture group to extract the session ID from the header
46 value. If not provided, the full header value is used as the session ID.
47  
48 =back
49  
50 =cut
51  
52 14 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
53 14 my ($class, %options) = @_;
54  
55 14 ▶ die "header_name is required for $class" unless defined $options{header_name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options{"header_name"} 1 13 14
56  
57 13 return $class->SUPER::new(%options);
58 }
59  
60 =head2 extract
61  
62 my $session_id = $state->extract($scope);
63  
64 Find the configured header in C<$scope-E<gt>{headers}> (case-insensitive)
65 and return its value as the session ID. If a C<pattern> is configured,
66 apply it and return the first capture group. Returns undef if the header
67 is not found or the pattern does not match.
68  
69 =cut
70  
71 12 ▶sub extract {
Subroutine extract: called CC=4 SCAR=13.9
Pod documented
72 12 my ($self, $scope) = @_;
73  
74 12 my $value = $self->_get_header($scope, $self->{header_name});
75 12 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 5 7 12
76  
77 7 ▶ if (my $pattern = $self->{pattern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pattern = $self->{"pattern"}) 4 3 7
78 4 ▶ if ($value =~ $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$pattern/) 2 2 4
79 2 return $1;
80 }
81 2 return;
82 }
83  
84 3 return $value;
85 }
86  
87 =head2 inject
88  
89 $state->inject(\@headers, $id, \%options);
90  
91 No-op. Header-based session transport is managed by the client, so the
92 server does not inject any response headers.
93  
94 =cut
95  
96 4 ▶sub inject {
Subroutine inject: called CC=1 SCAR=0.0
Pod documented
97 4 my ($self, $headers, $id, $options) = @_;
98  
99 # No-op: client manages header-based transport
100 4 return;
101 }
102  
103 12 ▶sub _get_header {
Subroutine _get_header: called CC=4 SCAR=13.9
104 12 my ($self, $scope, $name) = @_;
105  
106 12 $name = lc($name);
107 12 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
120
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
108 9 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 7 2 9
109 }
110 5 return;
111 }
112  
113 2 1;
114  
115 __END__
« blib/lib/PAGI/Middleware/Builder.pm ↑ Summary blib/lib/PAGI/Headers.pm »