blib/lib/PAGI/Middleware/ContentNegotiation.pm

filter: Statementstmt 97.4% 76 / 78 Branchbran 75.0% 15 / 20 Conditioncond 76.4% 13 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 100% 11 / 11 Podpod 100% 1 / 1 totaltotal 87.5% 120 / 137 CC 24 SCAR 16.8
CC
24
Cov
90%
_negotiate
9.0
__ANON__
6.1
_parse_accept
5.0
CRAP 5.3
« lib/PAGI/App/URLMap.pm ↑ Summary blib/lib/PAGI/Middleware/CSRF.pm »
1 package PAGI::Middleware::ContentNegotiation;
2 1 $PAGI::Middleware::ContentNegotiation::VERSION = '0.002002';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use parent 'PAGI::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 PAGI::Middleware::ContentNegotiation - HTTP content negotiation middleware
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'ContentNegotiation',
18 supported_types => ['application/json', 'text/html', 'text/plain'],
19 default_type => 'application/json';
20 $my_app;
21 };
22  
23 # In your app:
24 async sub app {
25 my ($scope, $receive, $send) = @_;
26  
27 my $preferred = $scope->{'pagi.preferred_content_type'};
28 if ($preferred eq 'application/json') {
29 # Return JSON
30 } else {
31 # Return HTML
32 }
33 }
34  
35 =head1 DESCRIPTION
36  
37 PAGI::Middleware::ContentNegotiation parses the Accept header and determines
38 the best content type to return. It adds the preferred type to the scope
39 for the application to use.
40  
41 =head1 CONFIGURATION
42  
43 =over 4
44  
45 =item * supported_types (required)
46  
47 Array of MIME types the application supports.
48  
49 =item * default_type (optional)
50  
51 Default type when no Accept header or no match. Defaults to first supported type.
52  
53 =item * strict (default: 0)
54  
55 If true, return 406 Not Acceptable when no supported type matches.
56  
57 =back
58  
59 =cut
60  
61 6 ▶sub _init {
Subroutine _init: called CC=4 SCAR=14.1
62 6 my ($self, $config) = @_;
63  
64 $self->{supported_types} = $config->{supported_types}
65 6 ▶ // die "ContentNegotiation requires 'supported_types' option";
Condition50%1/2
$config->{"supported_types"} // die("ContentNegotiation requires 'supported_types' option")
l!l
60
MC/DC0%0/1
$config->{"supported_types"} // die("ContentNegotiation requires 'supported_types' option")
$config->{"supported_types"}
Truth table50%1/2
A
A= $config->{"supported_types"}
Aresult
11
00
66 $self->{default_type} = $config->{default_type}
67 6 ▶ // $self->{supported_types}[0];
Condition66%2/3
$config->{"default_type"} // $self->{"supported_types"}[0]
l!l&&r!l&&!r
150
MC/DC0%0/2
$config->{"default_type"} // $self->{"supported_types"}[0]
$config->{"default_type"} $self->{"supported_types"}[0]
Truth table66%2/3
A // B
A= $config->{"default_type"}B= $self->{"supported_types"}[0]
ABresult
1X1
011
000
68 6 ▶ $self->{strict} = $config->{strict} // 0;
Condition100%2/2
$config->{"strict"} // 0
l!l
15
MC/DC100%1/1
$config->{"strict"} // 0
$config->{"strict"}
Truth table100%2/2
A
A= $config->{"strict"}
Aresult
11
00
69 }
70  
71 6 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
72 6 my ($self, $app) = @_;
73  
74 6 ▶ return async sub {
Subroutine __ANON__: called CC=6 SCAR=18.1
75 6 my ($scope, $receive, $send) = @_;
76 6 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 6 6
77 0 await $app->($scope, $receive, $send);
78 0 return;
79 }
80  
81 # Parse Accept header
82 6 ▶ my $accept = $self->_get_header($scope, 'accept') // '*/*';
Condition100%2/2
$self->_get_header($scope, "accept") // "*/*"
l!l
51
MC/DC100%1/1
$self->_get_header($scope, "accept") // "*/*"
$self->_get_header($scope, "accept")
Truth table100%2/2
A
A= $self->_get_header($scope, "accept")
Aresult
11
00
83 6 my $preferred = $self->_negotiate($accept);
84  
85 6 ▶ if (!$preferred && $self->{strict}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $preferred and $self->{"strict"}) 1 5 6
Condition100%3/3
not $preferred and $self->{"strict"}
!ll&&!rl&&r
411
MC/DC100%2/2
not $preferred and $self->{"strict"}
not $preferred $self->{"strict"}
Truth table100%3/3
not(A) and B
A= not $preferredB= $self->{"strict"}
ABresult
0X0
100
111
86 1 await $self->_send_not_acceptable($send);
87 1 return;
88 }
89  
90 5 ▶ $preferred //= $self->{default_type};
Condition66%2/3
$preferred //= $self->{"default_type"}
l!l&&r!l&&!r
410
MC/DC0%0/2
$preferred //= $self->{"default_type"}
$preferred $self->{"default_type"}
Truth table66%2/3
A //= B
A= $preferredB= $self->{"default_type"}
ABresult
1X1
011
000
91  
92 # Add preferred type to scope
93 5 my @accepted = $self->_parse_accept($accept);
94 5 my $new_scope = $self->modify_scope($scope, {
95 'pagi.preferred_content_type' => $preferred,
96 'pagi.accepted_types' => \@accepted,
97 });
98  
99 5 await $app->($new_scope, $receive, $send);
100 6 };
101 }
102  
103 6 ▶sub _negotiate {
Subroutine _negotiate: called CC=9 SCAR=22.0
104 6 my ($self, $accept) = @_;
105  
106 6 my @accepted = $self->_parse_accept($accept);
107 6 ▶ return unless @accepted;
Branch: 1 branch
BranchTrueFalseTotal
unless @accepted 0 6 6
108  
109 6 for my $item (@accepted) {
110 6 my $type = $item->{type};
111  
112 # Check for exact match
113 6 for my $supported (@{$self->{supported_types}}) {
114 12 ▶ return $supported if lc($type) eq lc($supported);
Branch: 1 branch
BranchTrueFalseTotal
if lc $type eq lc $supported 1 11 12
115 }
116  
117 # Check for wildcard matches
118 5 ▶ if ($type eq '*/*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "*/*") 2 3 5
119 2 return $self->{supported_types}[0];
120 }
121  
122 3 ▶ if ($type =~ m{^([^/]+)/\*$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[^([^/]+)/\*$]) 1 2 3
123 1 my $major = lc($1);
124 1 for my $supported (@{$self->{supported_types}}) {
125 2 ▶ return $supported if $supported =~ m{^$major/}i;
Branch: 1 branch
BranchTrueFalseTotal
if $supported =~ m[^$major/]i 1 1 2
126 }
127 }
128 }
129  
130 2 return;
131 }
132  
133 11 ▶sub _parse_accept {
Subroutine _parse_accept: called CC=5 SCAR=16.2
134 11 my ($self, $accept) = @_;
135  
136 11 my @items;
137  
138 11 for my $part (split /\s*,\s*/, $accept) {
139 13 my ($type, @params) = split /\s*;\s*/, $part;
140 13 ▶ next unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 0 13 13
141  
142 13 my $q = 1.0;
143 13 for my $param (@params) {
144 2 ▶ if ($param =~ /^q\s*=\s*([0-9.]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^q\s*=\s*([0-9.]+)$/i) 2 0 2
145 2 $q = $1 + 0;
146 2 last;
147 }
148 }
149  
150 13 push @items, { type => $type, q => $q };
151 }
152  
153 # Sort by quality value, descending
154 11 @items = sort { $b->{q} <=> $a->{q} } @items;
155  
156 11 return @items;
157 }
158  
159 6 ▶sub _get_header {
Subroutine _get_header: called CC=4 SCAR=14.2
160 6 my ($self, $scope, $name) = @_;
161  
162 6 $name = lc($name);
163 6 ▶ for my $h (@{$scope->{headers} // []}) {
Condition50%1/2
$scope->{"headers"} // []
l!l
60
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
164 5 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 5 0 5
165 }
166 1 return;
167 }
168  
169 1 ▶async sub _send_not_acceptable {
Subroutine _send_not_acceptable: called CC=1 SCAR=0.0
170 1 my ($self, $send) = @_;
171  
172 1 my $supported = join(', ', @{$self->{supported_types}});
173 1 my $body = "Not Acceptable. Supported types: $supported";
174  
175 1 await $send->({
176 type => 'http.response.start',
177 status => 406,
178 headers => [
179 ['Content-Type', 'text/plain'],
180 ['Content-Length', length($body)],
181 ],
182 });
183 1 await $send->({
184 type => 'http.response.body',
185 body => $body,
186 more => 0,
187 });
188 1 }
189  
190 1 1;
191  
192 __END__
« lib/PAGI/App/URLMap.pm ↑ Summary blib/lib/PAGI/Middleware/CSRF.pm »