blib/lib/PAGI/Middleware/ETag.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 81.2% 13 / 16 Conditioncond 62.5% 5 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 83.8% 88 / 105 CC 15 SCAR 18.5
CC
15
Cov
84%
__ANON__
12.1
__ANON__
4.2
_init
2.0
CRAP 6.3
« blib/lib/PAGI/Request/MultipartStream.pm ↑ Summary lib/PAGI/Middleware/ErrorHandler.pm »
1 package PAGI::Middleware::ETag;
2 1 $PAGI::Middleware::ETag::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 1 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 PAGI::Middleware::ETag - ETag generation middleware
12  
13 =head1 SYNOPSIS
14  
15 use PAGI::Middleware::Builder;
16  
17 my $app = builder {
18 enable 'ETag';
19 $my_app;
20 };
21  
22 =head1 DESCRIPTION
23  
24 PAGI::Middleware::ETag generates ETag headers for responses based on
25 the response body content. Works best with buffered (non-streaming) responses.
26  
27 =head1 CONFIGURATION
28  
29 =over 4
30  
31 =item * weak (default: 0)
32  
33 If true, generate weak ETags (W/"...").
34  
35 =back
36  
37 =cut
38  
39 3 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
40 3 my ($self, $config) = @_;
41  
42 3 ▶ $self->{weak} = $config->{weak} // 0;
Condition100%2/2
$config->{"weak"} // 0
l!l
12
MC/DC100%1/1
$config->{"weak"} // 0
$config->{"weak"}
Truth table100%2/2
A
A= $config->{"weak"}
Aresult
11
00
43 }
44  
45 3 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.1
Pod documented
46 3 my ($self, $app) = @_;
47  
48 3 ▶ return async sub {
Subroutine __ANON__: called CC=4 SCAR=14.4
49 3 my ($scope, $receive, $send) = @_;
50 3 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 3 3
51 0 await $app->($scope, $receive, $send);
52 0 return;
53 }
54  
55 3 my @body_parts;
56 my $original_headers;
57 3 my $status;
58 3 my $is_streaming = 0;
59  
60 6 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=10 SCAR=25.0
61 6 my ($event) = @_;
62 6 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'http.response.start') { } 3 3 6
elsif ($event->{'type'} eq 'http.response.body') { } 3 0 3
63 3 $status = $event->{status};
64 3 $original_headers = $event->{headers};
65 # Check if already has ETag
66 3 ▶ for my $h (@{$original_headers // []}) {
Condition50%1/2
$original_headers // []
l!l
30
MC/DC0%0/1
$original_headers // []
$original_headers
Truth table50%1/2
A
A= $original_headers
Aresult
11
00
67 4 ▶ if (lc($h->[0]) eq 'etag') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "etag") 1 3 4
68 # Already has ETag, pass through
69 1 await $send->($event);
70 1 $is_streaming = 1; # Flag to pass through body
71 1 return;
72 }
73 }
74 }
75 elsif ($event->{type} eq 'http.response.body') {
76 3 ▶ if ($is_streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_streaming) 1 2 3
77 1 await $send->($event);
78 1 return;
79 }
80  
81 2 ▶ push @body_parts, $event->{body} // '';
Condition50%1/2
$event->{"body"} // ""
l!l
20
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
Aresult
11
00
82  
83 # If streaming, can't generate ETag
84 2 ▶ if ($event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"more"}) 0 2 2
85 0 $is_streaming = 1;
86 0 await $send->({
87 type => 'http.response.start',
88 status => $status,
89 headers => $original_headers,
90 });
91 0 for my $part (@body_parts) {
92 0 await $send->({
93 type => 'http.response.body',
94 body => $part,
95 more => 1,
96 });
97 }
98 0 @body_parts = ();
99 }
100 }
101 else {
102 0 await $send->($event);
103 }
104 3 };
105  
106 3 await $app->($scope, $receive, $wrapped_send);
107  
108 3 ▶ return if $is_streaming;
Branch: 1 branch
BranchTrueFalseTotal
if $is_streaming 1 2 3
109  
110 # Generate ETag from body
111 2 my $body = join('', @body_parts);
112 2 my $etag = $self->_generate_etag($body);
113  
114 # Add ETag to headers
115 2 ▶ my @new_headers = @{$original_headers // []};
Condition50%1/2
$original_headers // []
l!l
20
MC/DC0%0/1
$original_headers // []
$original_headers
Truth table50%1/2
A
A= $original_headers
Aresult
11
00
116 2 push @new_headers, ['ETag', $etag];
117  
118 2 await $send->({
119 type => 'http.response.start',
120 status => $status,
121 headers => \@new_headers,
122 });
123 2 await $send->({
124 type => 'http.response.body',
125 body => $body,
126 more => 0,
127 });
128 3 };
129 }
130  
131 2 ▶sub _generate_etag {
Subroutine _generate_etag: called CC=2 SCAR=6.9
132 2 my ($self, $body) = @_;
133  
134 2 my $hash = md5_hex($body);
135 2 ▶ if ($self->{weak}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"weak"}) 1 1 2
136 1 return qq{W/"$hash"};
137 }
138 1 return qq{"$hash"};
139 }
140  
141 1 1;
142  
143 __END__
« blib/lib/PAGI/Request/MultipartStream.pm ↑ Summary lib/PAGI/Middleware/ErrorHandler.pm »