lib/PAGI/Middleware/ContentLength.pm

filter: Statementstmt 95.6% 66 / 69 Branchbran 90.9% 20 / 22 Conditioncond 74.0% 20 / 27 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 100% 8 / 8 Podpod 100% 1 / 1 totaltotal 86.5% 122 / 141 CC 26 SCAR 24.4
CC
26
Cov
90%
__ANON__
16.8
__ANON__
10.2
_init
2.0
CRAP 11.5
« blib/lib/PAGI/App/Directory.pm ↑ Summary blib/lib/PAGI/Request/BodyStream.pm »
1 package PAGI::Middleware::ContentLength;
2 2 $PAGI::Middleware::ContentLength::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  
8 =head1 NAME
9  
10 PAGI::Middleware::ContentLength - Auto Content-Length header middleware
11  
12 =head1 SYNOPSIS
13  
14 use PAGI::Middleware::Builder;
15  
16 my $app = builder {
17 enable 'ContentLength';
18 $my_app;
19 };
20  
21 =head1 DESCRIPTION
22  
23 PAGI::Middleware::ContentLength automatically adds a Content-Length header
24 to responses that don't already have one. It buffers the response body
25 to calculate the length, then sends the complete response.
26  
27 This middleware is useful when the application doesn't know the body
28 length upfront, but you want to avoid chunked encoding.
29  
30 =head1 CONFIGURATION
31  
32 =over 4
33  
34 =item * auto_chunked (default: 0)
35  
36 If true, skip adding Content-Length and let chunked encoding be used instead.
37 This is useful for large responses where buffering would be expensive.
38  
39 =back
40  
41 =cut
42  
43 7 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
44 7 my ($self, $config) = @_;
45  
46 7 ▶ $self->{auto_chunked} = $config->{auto_chunked} // 0;
Condition100%2/2
$config->{"auto_chunked"} // 0
l!l
16
MC/DC100%1/1
$config->{"auto_chunked"} // 0
$config->{"auto_chunked"}
Truth table100%2/2
A
A= $config->{"auto_chunked"}
Aresult
11
00
47 }
48  
49 7 ▶sub wrap {
Subroutine wrap: called CC=1 SCAR=0.0
Pod documented
50 7 my ($self, $app) = @_;
51  
52 7 ▶ return async sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
53 7 my ($scope, $receive, $send) = @_;
54 # Skip for non-HTTP requests
55 7 ▶ if ($scope->{type} ne 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 1 6 7
56 1 await $app->($scope, $receive, $send);
57 1 return;
58 }
59  
60 6 my @buffered_events;
61 6 my $has_content_length = 0;
62 6 my $is_streaming = 0;
63 6 my $status;
64 my @headers;
65  
66 # Create intercepting send to buffer response
67 13 ▶ my $wrapped_send = async sub {
Subroutine __ANON__: called CC=16 SCAR=28.2
68 13 my ($event) = @_;
69 13 my $type = $event->{type};
70  
71 13 ▶ if ($type eq 'http.response.start') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'http.response.start') { } 6 7 13
elsif ($type eq 'http.response.body') { } 7 0 7
72 6 $status = $event->{status};
73 6 ▶ @headers = @{$event->{headers} // []};
Condition50%1/2
$event->{"headers"} // []
l!l
60
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
Aresult
11
00
74  
75 # Check if Content-Length already present
76 6 for my $h (@headers) {
77 6 ▶ if (lc($h->[0]) eq 'content-length') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "content-length") 1 5 6
78 1 $has_content_length = 1;
79 1 last;
80 }
81 # If Transfer-Encoding is chunked, don't add Content-Length
82 5 ▶ if (lc($h->[0]) eq 'transfer-encoding' && lc($h->[1]) eq 'chunked') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $h->[0] eq "transfer-encoding" and lc $h->[1] eq "chunked") 0 5 5
Condition33%1/3
lc $h->[0] eq "transfer-encoding" and lc $h->[1] eq "chunked"
!ll&&!rl&&r
500
MC/DC0%0/2
lc $h->[0] eq "transfer-encoding" and lc $h->[1] eq "chunked"
lc $h->[0] eq "transfer-encoding" lc $h->[1] eq "chunked"
Truth table33%1/3
A and B
A= lc $h->[0] eq "transfer-encoding"B= lc $h->[1] eq "chunked"
ABresult
0X0
100
111
83 0 $is_streaming = 1;
84 0 last;
85 }
86 }
87  
88 # If already has Content-Length or is streaming, pass through
89 6 ▶ if ($has_content_length || $is_streaming || $self->{auto_chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_content_length or $is_streaming or $self->{"auto_chunked"}) 2 4 6
Condition83%5/6
$has_content_length or $is_streaming or $self->{"auto_chunked"}
l!l&&r!l&&!r
114
$has_content_length or $is_streaming
l!l&&r!l&&!r
105
MC/DC66%2/3
$has_content_length or $is_streaming or $self->{"auto_chunked"}
$has_content_length $is_streaming $self->{"auto_chunked"}
Truth table75%3/4
A or B or C
A= $has_content_lengthB= $is_streamingC= $self->{"auto_chunked"}
ABCresult
1XX1
01X1
0011
0000
90 2 await $send->($event);
91 2 return;
92 }
93  
94 # Buffer the start event to add Content-Length later
95 4 push @buffered_events, $event;
96 }
97 elsif ($type eq 'http.response.body') {
98 # If we're passing through (has Content-Length or streaming)
99 7 ▶ if ($has_content_length || $is_streaming || $self->{auto_chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_content_length or $is_streaming or $self->{"auto_chunked"}) 3 4 7
Condition100%6/6
$has_content_length or $is_streaming or $self->{"auto_chunked"}
l!l&&r!l&&!r
214
$has_content_length or $is_streaming
l!l&&r!l&&!r
115
MC/DC100%3/3
$has_content_length or $is_streaming or $self->{"auto_chunked"}
$has_content_length $is_streaming $self->{"auto_chunked"}
Truth table100%4/4
A or B or C
A= $has_content_lengthB= $is_streamingC= $self->{"auto_chunked"}
ABCresult
1XX1
01X1
0011
0000
100 3 await $send->($event);
101 3 return;
102 }
103  
104 # Check if this is a streaming response (more => 1)
105 4 ▶ if ($event->{more}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"more"}) 1 3 4
106 1 $is_streaming = 1;
107  
108 # Flush buffered events and switch to pass-through
109 1 for my $buffered (@buffered_events) {
110 1 await $send->($buffered);
111 }
112 1 @buffered_events = ();
113 1 await $send->($event);
114 1 return;
115 }
116  
117 # Buffer body events
118 3 push @buffered_events, $event;
119 }
120 else {
121 # Pass through other events (trailers, etc.)
122 0 await $send->($event);
123 }
124 6 };
125  
126 # Run the inner app
127 6 await $app->($scope, $receive, $wrapped_send);
128  
129 # If we have buffered events, calculate Content-Length and send
130 6 ▶ if (@buffered_events && !$has_content_length && !$is_streaming) {
Branch: 1 branch
BranchTrueFalseTotal
if (@buffered_events and not $has_content_length and not $is_streaming) 3 3 6
Condition66%4/6
@buffered_events and not $has_content_length and not $is_streaming
!ll&&!rl&&r
303
@buffered_events and not $has_content_length
!ll&&!rl&&r
303
MC/DC33%1/3
@buffered_events and not $has_content_length and not $is_streaming
@buffered_events not $has_content_length not $is_streaming
Truth table50%2/4
A and not(B) and not(C)
A= @buffered_eventsB= not $has_content_lengthC= not $is_streaming
ABCresult
0XX0
10X0
1100
1111
131 # Calculate total body length
132 3 my $body_length = 0;
133 3 for my $event (@buffered_events) {
134 6 ▶ if ($event->{type} eq 'http.response.body') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.body") 3 3 6
135 3 ▶ $body_length += length($event->{body} // '');
Condition50%1/2
$event->{"body"} // ""
l!l
30
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
Aresult
11
00
136 }
137 }
138  
139 # Send start with Content-Length
140 3 for my $event (@buffered_events) {
141 6 ▶ if ($event->{type} eq 'http.response.start') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{"type"} eq "http.response.start") 3 3 6
142 3 push @{$event->{headers}}, ['content-length', $body_length];
143 }
144 6 await $send->($event);
145 }
146 }
147 7 };
148 }
149  
150 2 1;
151  
152 __END__
« blib/lib/PAGI/App/Directory.pm ↑ Summary blib/lib/PAGI/Request/BodyStream.pm »