| 1 | package PAGI::Middleware::ContentLength; | ||
| 2 | $PAGI::Middleware::ContentLength::VERSION = '0.002001'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent 'PAGI::Middleware'; | ||
| 6 | use Future::AsyncAwait; | ||
| 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 | sub _init { | ||
| 44 | my ($self, $config) = @_; | ||
| 45 | |||
| 46 | $self->{auto_chunked} = $config->{auto_chunked} // 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub wrap { | ||
| 50 | my ($self, $app) = @_; | ||
| 51 | |||
| 52 | return async sub { | ||
| 53 | my ($scope, $receive, $send) = @_; | ||
| 54 | # Skip for non-HTTP requests | ||
| 55 | if ($scope->{type} ne 'http') { | ||
| 56 | await $app->($scope, $receive, $send); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | my @buffered_events; | ||
| 61 | my $has_content_length = 0; | ||
| 62 | my $is_streaming = 0; | ||
| 63 | my $status; | ||
| 64 | my @headers; | ||
| 65 | |||
| 66 | # Create intercepting send to buffer response | ||
| 67 | my $wrapped_send = async sub { | ||
| 68 | my ($event) = @_; | ||
| 69 | my $type = $event->{type}; | ||
| 70 | |||
| 71 | if ($type eq 'http.response.start') { | ||
| 72 | $status = $event->{status}; | ||
| 73 | @headers = @{$event->{headers} // []}; | ||
| 74 | |||
| 75 | # Check if Content-Length already present | ||
| 76 | for my $h (@headers) { | ||
| 77 | if (lc($h->[0]) eq 'content-length') { | ||
| 78 | $has_content_length = 1; | ||
| 79 | last; | ||
| 80 | } | ||
| 81 | # If Transfer-Encoding is chunked, don't add Content-Length | ||
| 82 | if (lc($h->[0]) eq 'transfer-encoding' && lc($h->[1]) eq 'chunked') { | ||
| 83 | $is_streaming = 1; | ||
| 84 | last; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | # If already has Content-Length or is streaming, pass through | ||
| 89 | if ($has_content_length || $is_streaming || $self->{auto_chunked}) { | ||
| 90 | await $send->($event); | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Buffer the start event to add Content-Length later | ||
| 95 | push @buffered_events, $event; | ||
| 96 | } | ||
| 97 | elsif ($type eq 'http.response.body') { | ||
| 98 | # If we're passing through (has Content-Length or streaming) | ||
| 99 | if ($has_content_length || $is_streaming || $self->{auto_chunked}) { | ||
| 100 | await $send->($event); | ||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | # Check if this is a streaming response (more => 1) | ||
| 105 | if ($event->{more}) { | ||
| 106 | $is_streaming = 1; | ||
| 107 | |||
| 108 | # Flush buffered events and switch to pass-through | ||
| 109 | for my $buffered (@buffered_events) { | ||
| 110 | await $send->($buffered); | ||
| 111 | } | ||
| 112 | @buffered_events = (); | ||
| 113 | await $send->($event); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Buffer body events | ||
| 118 | push @buffered_events, $event; | ||
| 119 | } | ||
| 120 | else { | ||
| 121 | # Pass through other events (trailers, etc.) | ||
| 122 | await $send->($event); | ||
| 123 | } | ||
| 124 | }; | ||
| 125 | |||
| 126 | # Run the inner app | ||
| 127 | await $app->($scope, $receive, $wrapped_send); | ||
| 128 | |||
| 129 | # If we have buffered events, calculate Content-Length and send | ||
| 130 | if (@buffered_events && !$has_content_length && !$is_streaming) { | ||
| 131 | # Calculate total body length | ||
| 132 | my $body_length = 0; | ||
| 133 | for my $event (@buffered_events) { | ||
| 134 | if ($event->{type} eq 'http.response.body') { | ||
| 135 | $body_length += length($event->{body} // ''); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | # Send start with Content-Length | ||
| 140 | for my $event (@buffered_events) { | ||
| 141 | if ($event->{type} eq 'http.response.start') { | ||
| 142 | push @{$event->{headers}}, ['content-length', $body_length]; | ||
| 143 | } | ||
| 144 | await $send->($event); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ |