| 1 | package PAGI::Middleware::ETag; | ||||||||||||||
| 2 | 1 | $PAGI::Middleware::ETag::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 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | |||||||||||||||
| 9 | =head1 NAME | ||||||||||||||
| 10 | |||||||||||||||
| 11 | PAGI::Middleware::ETag - ETag generation middleware | ||||||||||||||
| 12 | |||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||
| 14 | |||||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||||
| 16 | |||||||||||||||
| 17 | my $app = builder { | ||||||||||||||
| 18 | enable 'ETag'; | ||||||||||||||
| 19 | $my_app; | ||||||||||||||
| 20 | }; | ||||||||||||||
| 21 | |||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||
| 23 | |||||||||||||||
| 24 | PAGI::Middleware::ETag generates ETag headers for responses based on | ||||||||||||||
| 25 | the response body content. Works best with buffered (non-streaming) responses. | ||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 CONFIGURATION | ||||||||||||||
| 28 | |||||||||||||||
| 29 | =over 4 | ||||||||||||||
| 30 | |||||||||||||||
| 31 | =item * weak (default: 0) | ||||||||||||||
| 32 | |||||||||||||||
| 33 | If true, generate weak ETags (W/"..."). | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =back | ||||||||||||||
| 36 | |||||||||||||||
| 37 | =cut | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 3 | ▶ | sub _init { | ||||||||||||
|
Subroutine
_init: called CC=2 SCAR=6.9
| |||||||||||||||
| 40 | 3 | my ($self, $config) = @_; | |||||||||||||
| 41 | |||||||||||||||
| 42 | 3 | ▶ | $self->{weak} = $config->{weak} // 0; | ||||||||||||
|
Condition100%2/2
$config->{"weak"} // 0
MC/DC100%1/1
$config->{"weak"} // 0
$config->{"weak"}
Truth table100%2/2
A
A= $config->{"weak"}
| |||||||||||||||
| 43 | } | ||||||||||||||
| 44 | |||||||||||||||
| 45 | 3 | ▶ | sub wrap { | ||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||
| 46 | 3 | my ($self, $app) = @_; | |||||||||||||
| 47 | |||||||||||||||
| 48 | 3 | ▶ | return async sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.4
| |||||||||||||||
| 49 | 3 | my ($scope, $receive, $send) = @_; | |||||||||||||
| 50 | 3 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | await $app->($scope, $receive, $send); | |||||||||||||
| 52 | 0 | return; | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 3 | my @body_parts; | |||||||||||||
| 56 | my $original_headers; | ||||||||||||||
| 57 | 3 | my $status; | |||||||||||||
| 58 | 3 | my $is_streaming = 0; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 6 | ▶ | my $wrapped_send = async sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.0
| |||||||||||||||
| 61 | 6 | my ($event) = @_; | |||||||||||||
| 62 | 6 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 63 | 3 | $status = $event->{status}; | |||||||||||||
| 64 | 3 | $original_headers = $event->{headers}; | |||||||||||||
| 65 | # Check if already has ETag | ||||||||||||||
| 66 | 3 | ▶ | for my $h (@{$original_headers // []}) { | ||||||||||||
|
Condition50%1/2
$original_headers // []
MC/DC0%0/1
$original_headers // []
$original_headers
Truth table50%1/2
A
A= $original_headers
| |||||||||||||||
| 67 | 4 | ▶ | if (lc($h->[0]) eq 'etag') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | # Already has ETag, pass through | ||||||||||||||
| 69 | 1 | await $send->($event); | |||||||||||||
| 70 | 1 | $is_streaming = 1; # Flag to pass through body | |||||||||||||
| 71 | 1 | return; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | } | ||||||||||||||
| 75 | elsif ($event->{type} eq 'http.response.body') { | ||||||||||||||
| 76 | 3 | ▶ | if ($is_streaming) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 77 | 1 | await $send->($event); | |||||||||||||
| 78 | 1 | return; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | 2 | ▶ | push @body_parts, $event->{body} // ''; | ||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||
| 82 | |||||||||||||||
| 83 | # If streaming, can't generate ETag | ||||||||||||||
| 84 | 2 | ▶ | if ($event->{more}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | $is_streaming = 1; | |||||||||||||
| 86 | 0 | await $send->({ | |||||||||||||
| 87 | type => 'http.response.start', | ||||||||||||||
| 88 | status => $status, | ||||||||||||||
| 89 | headers => $original_headers, | ||||||||||||||
| 90 | }); | ||||||||||||||
| 91 | 0 | for my $part (@body_parts) { | |||||||||||||
| 92 | 0 | await $send->({ | |||||||||||||
| 93 | type => 'http.response.body', | ||||||||||||||
| 94 | body => $part, | ||||||||||||||
| 95 | more => 1, | ||||||||||||||
| 96 | }); | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | 0 | @body_parts = (); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | else { | ||||||||||||||
| 102 | 0 | await $send->($event); | |||||||||||||
| 103 | } | ||||||||||||||
| 104 | 3 | }; | |||||||||||||
| 105 | |||||||||||||||
| 106 | 3 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||
| 107 | |||||||||||||||
| 108 | 3 | ▶ | return if $is_streaming; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | # Generate ETag from body | ||||||||||||||
| 111 | 2 | my $body = join('', @body_parts); | |||||||||||||
| 112 | 2 | my $etag = $self->_generate_etag($body); | |||||||||||||
| 113 | |||||||||||||||
| 114 | # Add ETag to headers | ||||||||||||||
| 115 | 2 | ▶ | my @new_headers = @{$original_headers // []}; | ||||||||||||
|
Condition50%1/2
$original_headers // []
MC/DC0%0/1
$original_headers // []
$original_headers
Truth table50%1/2
A
A= $original_headers
| |||||||||||||||
| 116 | 2 | push @new_headers, ['ETag', $etag]; | |||||||||||||
| 117 | |||||||||||||||
| 118 | 2 | await $send->({ | |||||||||||||
| 119 | type => 'http.response.start', | ||||||||||||||
| 120 | status => $status, | ||||||||||||||
| 121 | headers => \@new_headers, | ||||||||||||||
| 122 | }); | ||||||||||||||
| 123 | 2 | await $send->({ | |||||||||||||
| 124 | type => 'http.response.body', | ||||||||||||||
| 125 | body => $body, | ||||||||||||||
| 126 | more => 0, | ||||||||||||||
| 127 | }); | ||||||||||||||
| 128 | 3 | }; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 2 | ▶ | sub _generate_etag { | ||||||||||||
|
Subroutine
_generate_etag: called CC=2 SCAR=6.9
| |||||||||||||||
| 132 | 2 | my ($self, $body) = @_; | |||||||||||||
| 133 | |||||||||||||||
| 134 | 2 | my $hash = md5_hex($body); | |||||||||||||
| 135 | 2 | ▶ | if ($self->{weak}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | 1 | return qq{W/"$hash"}; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | 1 | return qq{"$hash"}; | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 1 | 1; | |||||||||||||
| 142 | |||||||||||||||
| 143 | __END__ | ||||||||||||||