| 1 | package PAGI::Middleware::ConditionalGet; | ||||||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::ConditionalGet::VERSION = '0.002002'; | |||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use parent 'PAGI::Middleware'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | PAGI::Middleware::ConditionalGet - Conditional GET/HEAD request handling | ||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||||||
| 17 | enable 'ETag'; # Generate ETags | ||||||||||||||||||||||||||||||||
| 18 | enable 'ConditionalGet'; # Handle If-None-Match | ||||||||||||||||||||||||||||||||
| 19 | $my_app; | ||||||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||
| 24 | PAGI::Middleware::ConditionalGet returns 304 Not Modified for GET/HEAD | ||||||||||||||||||||||||||||||||
| 25 | requests when the client's conditional headers match. Supports: | ||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||
| 27 | - If-None-Match: Compare against ETag header | ||||||||||||||||||||||||||||||||
| 28 | - If-Modified-Since: Compare against Last-Modified header | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | sub wrap { | ||||||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||||||||||||||||||||||||
| 33 | 4 | my ($self, $app) = @_; | |||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | return async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=21.4
| |||||||||||||||||||||||||||||||||
| 36 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||
| 37 | 4 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 38 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 39 | 0 | return; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | # Only handle GET and HEAD requests | ||||||||||||||||||||||||||||||||
| 43 | 4 | ▶ | my $method = uc($scope->{method} // ''); | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"method"} // ""
MC/DC0%0/1
$scope->{"method"} // ""
$scope->{"method"}
Truth table50%1/2
A
A= $scope->{"method"}
| |||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | unless ($method eq 'GET' || $method eq 'HEAD') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "GET" or $method eq "HEAD"
MC/DC50%1/2
$method eq "GET" or $method eq "HEAD"
$method eq "GET"
$method eq "HEAD"
Truth table66%2/3
A or B
A= $method eq "GET"B= $method eq "HEAD"
| |||||||||||||||||||||||||||||||||
| 45 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 46 | 1 | return; | |||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||
| 49 | # Get conditional request headers | ||||||||||||||||||||||||||||||||
| 50 | 3 | my $if_none_match = $self->_get_header($scope, 'if-none-match'); | |||||||||||||||||||||||||||||||
| 51 | 3 | my $if_modified_since = $self->_get_header($scope, 'if-modified-since'); | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | # No conditional headers? Pass through | ||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | unless (defined $if_none_match || defined $if_modified_since) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $if_none_match or defined $if_modified_since
MC/DC0%0/2
defined $if_none_match or defined $if_modified_since
defined $if_none_match
defined $if_modified_since
Truth table33%1/3
A or B
A= defined $if_none_matchB= defined $if_modified_since
| |||||||||||||||||||||||||||||||||
| 55 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||||||
| 56 | 0 | return; | |||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | # Capture response headers | ||||||||||||||||||||||||||||||||
| 60 | 3 | my $response_status; | |||||||||||||||||||||||||||||||
| 61 | my $response_headers; | ||||||||||||||||||||||||||||||||
| 62 | 3 | my $sent_304 = 0; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 6 | ▶ | my $wrapped_send = async sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=26.8
| |||||||||||||||||||||||||||||||||
| 65 | 6 | my ($event) = @_; | |||||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 67 | 3 | $response_status = $event->{status}; | |||||||||||||||||||||||||||||||
| 68 | 3 | $response_headers = $event->{headers}; | |||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||
| 70 | # Only handle 2xx responses | ||||||||||||||||||||||||||||||||
| 71 | 3 | ▶ | if ($response_status >= 200 && $response_status < 300) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$response_status >= 200 and $response_status < 300
MC/DC0%0/2
$response_status >= 200 and $response_status < 300
$response_status >= 200
$response_status < 300
Truth table33%1/3
A and B
A= $response_status >= 200B= $response_status < 300
| |||||||||||||||||||||||||||||||||
| 72 | 3 | my $etag = $self->_get_response_header($response_headers, 'etag'); | |||||||||||||||||||||||||||||||
| 73 | 3 | my $last_modified = $self->_get_response_header($response_headers, 'last-modified'); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 3 | my $not_modified = 0; | |||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | # Check If-None-Match | ||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if (defined $if_none_match && defined $etag) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
defined $if_none_match and defined $etag
MC/DC0%0/2
defined $if_none_match and defined $etag
defined $if_none_match
defined $etag
Truth table33%1/3
A and B
A= defined $if_none_matchB= defined $etag
| |||||||||||||||||||||||||||||||||
| 79 | 3 | $not_modified = $self->_etag_matches($if_none_match, $etag); | |||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||
| 81 | # Check If-Modified-Since (only if no If-None-Match) | ||||||||||||||||||||||||||||||||
| 82 | ▶ | elsif (defined $if_modified_since && defined $last_modified) { | |||||||||||||||||||||||||||||||
|
Condition0%0/3
defined $if_modified_since and defined $last_modified
MC/DC0%0/2
defined $if_modified_since and defined $last_modified
defined $if_modified_since
defined $last_modified
Truth table0%0/3
A and B
A= defined $if_modified_sinceB= defined $last_modified
| |||||||||||||||||||||||||||||||||
| 83 | 0 | $not_modified = $self->_not_modified_since($if_modified_since, $last_modified); | |||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | if ($not_modified) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | # Send 304 response | ||||||||||||||||||||||||||||||||
| 88 | 2 | my @headers_304 = $self->_filter_headers_for_304($response_headers); | |||||||||||||||||||||||||||||||
| 89 | 2 | await $send->({ | |||||||||||||||||||||||||||||||
| 90 | type => 'http.response.start', | ||||||||||||||||||||||||||||||||
| 91 | status => 304, | ||||||||||||||||||||||||||||||||
| 92 | headers => \@headers_304, | ||||||||||||||||||||||||||||||||
| 93 | }); | ||||||||||||||||||||||||||||||||
| 94 | 2 | await $send->({ | |||||||||||||||||||||||||||||||
| 95 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||
| 96 | body => '', | ||||||||||||||||||||||||||||||||
| 97 | more => 0, | ||||||||||||||||||||||||||||||||
| 98 | }); | ||||||||||||||||||||||||||||||||
| 99 | 2 | $sent_304 = 1; | |||||||||||||||||||||||||||||||
| 100 | 2 | return; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 1 | await $send->($event); | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | elsif ($event->{type} eq 'http.response.body') { | ||||||||||||||||||||||||||||||||
| 107 | 3 | ▶ | return if $sent_304; # Skip body if we sent 304 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 108 | 1 | await $send->($event); | |||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||||||
| 111 | 0 | await $send->($event); | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | 3 | }; | |||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 3 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||||||
| 116 | 4 | }; | |||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | 6 | ▶ | sub _get_header { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 120 | 6 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 6 | $name = lc($name); | |||||||||||||||||||||||||||||||
| 123 | 6 | ▶ | for my $h (@{$scope->{headers} // []}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$scope->{"headers"} // []
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
| |||||||||||||||||||||||||||||||||
| 124 | 6 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||
| 126 | 3 | return; | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 6 | ▶ | sub _get_response_header { | ||||||||||||||||||||||||||||||
|
Subroutine
_get_response_header: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 130 | 6 | my ($self, $headers, $name) = @_; | |||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 6 | $name = lc($name); | |||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | for my $h (@{$headers // []}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$headers // []
MC/DC0%0/1
$headers // []
$headers
Truth table50%1/2
A
A= $headers
| |||||||||||||||||||||||||||||||||
| 134 | 12 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||
| 136 | 3 | return; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 3 | ▶ | sub _etag_matches { | ||||||||||||||||||||||||||||||
|
Subroutine
_etag_matches: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 140 | 3 | my ($self, $if_none_match, $etag) = @_; | |||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||
| 142 | # Parse If-None-Match which can be comma-separated | ||||||||||||||||||||||||||||||||
| 143 | # ETags can be weak (W/"...") or strong ("...") | ||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||
| 145 | # Handle * wildcard | ||||||||||||||||||||||||||||||||
| 146 | 3 | ▶ | return 1 if $if_none_match eq '*'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | # Normalize ETags for comparison (weak comparison) | ||||||||||||||||||||||||||||||||
| 149 | 4 | ▶ | my $normalize = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 150 | 4 | my ($tag) = @_; | |||||||||||||||||||||||||||||||
| 151 | 4 | $tag =~ s/^\s+//; | |||||||||||||||||||||||||||||||
| 152 | 4 | $tag =~ s/\s+$//; | |||||||||||||||||||||||||||||||
| 153 | 4 | $tag =~ s/^W\///i; # Remove weak prefix for comparison | |||||||||||||||||||||||||||||||
| 154 | 4 | return $tag; | |||||||||||||||||||||||||||||||
| 155 | 2 | }; | |||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||
| 157 | 2 | my $normalized_etag = $normalize->($etag); | |||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | 2 | for my $tag (split /\s*,\s*/, $if_none_match) { | |||||||||||||||||||||||||||||||
| 160 | 2 | my $normalized_tag = $normalize->($tag); | |||||||||||||||||||||||||||||||
| 161 | 2 | ▶ | return 1 if $normalized_tag eq $normalized_etag; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||
| 163 | 1 | return 0; | |||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub _not_modified_since { | ||||||||||||||||||||||||||||||
|
Subroutine
_not_modified_since: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||
| 167 | 0 | my ($self, $if_modified_since, $last_modified) = @_; | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | # Parse HTTP dates and compare | ||||||||||||||||||||||||||||||||
| 170 | # This is a simplified comparison - both should be HTTP-date format | ||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $parse_date = sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 173 | 0 | my ($date_str) = @_; | |||||||||||||||||||||||||||||||
| 174 | # Try to parse common HTTP date formats | ||||||||||||||||||||||||||||||||
| 175 | # RFC 1123: Sun, 06 Nov 1994 08:49:37 GMT | ||||||||||||||||||||||||||||||||
| 176 | # RFC 850: Sunday, 06-Nov-94 08:49:37 GMT | ||||||||||||||||||||||||||||||||
| 177 | # asctime: Sun Nov 6 08:49:37 1994 | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 0 | require HTTP::Date; | |||||||||||||||||||||||||||||||
| 180 | 0 | return HTTP::Date::str2time($date_str); | |||||||||||||||||||||||||||||||
| 181 | 0 | }; | |||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | 0 | my $client_time = eval { $parse_date->($if_modified_since) }; | |||||||||||||||||||||||||||||||
| 184 | 0 | my $server_time = eval { $parse_date->($last_modified) }; | |||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | return 0 unless defined $client_time && defined $server_time; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $client_time and defined $server_time
MC/DC0%0/2
defined $client_time and defined $server_time
defined $client_time
defined $server_time
Truth table0%0/3
A and B
A= defined $client_timeB= defined $server_time
| |||||||||||||||||||||||||||||||||
| 187 | 0 | return $server_time <= $client_time; | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | sub _filter_headers_for_304 { | ||||||||||||||||||||||||||||||
|
Subroutine
_filter_headers_for_304: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||
| 191 | 2 | my ($self, $headers) = @_; | |||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | # RFC 7232: 304 response MUST include certain headers | ||||||||||||||||||||||||||||||||
| 194 | 2 | my @allowed = qw( | |||||||||||||||||||||||||||||||
| 195 | cache-control content-location date etag expires | ||||||||||||||||||||||||||||||||
| 196 | last-modified vary | ||||||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||||||
| 198 | 2 | my %allowed = map { $_ => 1 } @allowed; | |||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 2 | my @filtered; | |||||||||||||||||||||||||||||||
| 201 | 2 | ▶ | for my $h (@{$headers // []}) { | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$headers // []
MC/DC0%0/1
$headers // []
$headers
Truth table50%1/2
A
A= $headers
| |||||||||||||||||||||||||||||||||
| 202 | 4 | ▶ | push @filtered, $h if $allowed{lc($h->[0])}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||
| 204 | 2 | return @filtered; | |||||||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||
| 207 | 1 | 1; | |||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||
| 209 | __END__ | ||||||||||||||||||||||||||||||||