blib/lib/PAGI/Middleware/Head.pm untested

filter: Statementstmt 0.0% 0 / 35 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 53 CC 7 SCAR 35.9
CC
7
Cov
0%
wrap
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 36.4
1 package PAGI::Middleware::Head;
2 $PAGI::Middleware::Head::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::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 sub wrap {
38 my ($self, $app) = @_;
39  
40 return async sub {
41 my ($scope, $receive, $send) = @_;
42 # Skip for non-HTTP requests
43 if ($scope->{type} ne 'http') {
44 await $app->($scope, $receive, $send);
45 return;
46 }
47  
48 # Only handle HEAD requests
49 my $is_head = $scope->{method} eq 'HEAD';
50  
51 if (!$is_head) {
52 await $app->($scope, $receive, $send);
53 return;
54 }
55  
56 # Change HEAD to GET for inner app
57 my $modified_scope = $self->modify_scope($scope, { method => 'GET' });
58  
59 # Intercept send to suppress body
60 my $wrapped_send = async sub {
61 my ($event) = @_;
62 my $type = $event->{type};
63  
64 if ($type eq 'http.response.start') {
65 # Pass through headers as-is (including Content-Length)
66 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 if (!$event->{more}) {
72 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 await $send->($event);
86 }
87 };
88  
89 await $app->($modified_scope, $receive, $wrapped_send);
90 };
91 }
92  
93 1;
94  
95 __END__