| 1 | package PAGI::Middleware::Head; | ||||||||||||||||||
| 2 | 2 | $PAGI::Middleware::Head::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::Head - HEAD request handling middleware | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||
| 17 | enable 'Head'; | ||||||||||||||||||
| 18 | $my_app; | ||||||||||||||||||
| 19 | }; | ||||||||||||||||||
| 20 | |||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||
| 22 | |||||||||||||||||||
| 23 | PAGI::Middleware::Head handles HEAD requests by suppressing the response | ||||||||||||||||||
| 24 | body while preserving all headers. The inner application runs normally | ||||||||||||||||||
| 25 | (as if it were a GET request), allowing Content-Length and other headers | ||||||||||||||||||
| 26 | to be calculated, but the body is not sent to the client. | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | This middleware changes the method from HEAD to GET before passing to the | ||||||||||||||||||
| 29 | inner app, then suppresses the body in the response. | ||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | =head1 CONFIGURATION | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | No configuration options. | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | =cut | ||||||||||||||||||
| 36 | |||||||||||||||||||
| 37 | 13 | ▶ | sub wrap { | ||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 38 | 13 | my ($self, $app) = @_; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 12 | ▶ | return async sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 41 | 12 | my ($scope, $receive, $send) = @_; | |||||||||||||||||
| 42 | # Skip for non-HTTP requests | ||||||||||||||||||
| 43 | 12 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 44 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||
| 45 | 1 | return; | |||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | |||||||||||||||||||
| 48 | # Only handle HEAD requests | ||||||||||||||||||
| 49 | 11 | my $is_head = $scope->{method} eq 'HEAD'; | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 11 | ▶ | if (!$is_head) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | 6 | await $app->($scope, $receive, $send); | |||||||||||||||||
| 53 | 6 | return; | |||||||||||||||||
| 54 | } | ||||||||||||||||||
| 55 | |||||||||||||||||||
| 56 | # Change HEAD to GET for inner app | ||||||||||||||||||
| 57 | 5 | my $modified_scope = $self->modify_scope($scope, { method => 'GET' }); | |||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | # Intercept send to suppress body | ||||||||||||||||||
| 60 | 13 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||
| 61 | 13 | my ($event) = @_; | |||||||||||||||||
| 62 | 13 | my $type = $event->{type}; | |||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | 13 | ▶ | if ($type eq 'http.response.start') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 65 | # Pass through headers as-is (including Content-Length) | ||||||||||||||||||
| 66 | 5 | await $send->($event); | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | elsif ($type eq 'http.response.body') { | ||||||||||||||||||
| 69 | # Suppress body content but preserve the event structure | ||||||||||||||||||
| 70 | # Send an empty body with more => 0 to complete the response | ||||||||||||||||||
| 71 | 7 | ▶ | if (!$event->{more}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 72 | 5 | await $send->({ | |||||||||||||||||
| 73 | type => 'http.response.body', | ||||||||||||||||||
| 74 | body => '', | ||||||||||||||||||
| 75 | more => 0, | ||||||||||||||||||
| 76 | }); | ||||||||||||||||||
| 77 | } | ||||||||||||||||||
| 78 | # Otherwise, skip the event entirely (streaming chunks) | ||||||||||||||||||
| 79 | } | ||||||||||||||||||
| 80 | elsif ($type eq 'http.response.trailers') { | ||||||||||||||||||
| 81 | # Skip trailers for HEAD requests | ||||||||||||||||||
| 82 | } | ||||||||||||||||||
| 83 | else { | ||||||||||||||||||
| 84 | # Pass through other events | ||||||||||||||||||
| 85 | 0 | await $send->($event); | |||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | 5 | }; | |||||||||||||||||
| 88 | |||||||||||||||||||
| 89 | 5 | await $app->($modified_scope, $receive, $wrapped_send); | |||||||||||||||||
| 90 | 13 | }; | |||||||||||||||||
| 91 | } | ||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 2 | 1; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | __END__ | ||||||||||||||||||