| 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
MC/DC100%1/1
$config->{"auto_chunked"} // 0
$config->{"auto_chunked"}
Truth table100%2/2
A
A= $config->{"auto_chunked"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 6 | $status = $event->{status}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | ▶ | @headers = @{$event->{headers} // []}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"headers"} // []
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
lc $h->[0] eq "transfer-encoding" and lc $h->[1] eq "chunked"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$has_content_length or $is_streaming or $self->{"auto_chunked"}
$has_content_length or $is_streaming
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
$has_content_length or $is_streaming or $self->{"auto_chunked"}
$has_content_length or $is_streaming
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
@buffered_events and not $has_content_length and not $is_streaming
@buffered_events and not $has_content_length
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | $body_length += length($event->{body} // ''); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||