| 1 | package PAGI::Middleware::FormBody; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::FormBody::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::FormBody - Form request body parsing middleware | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | my $app = builder { | ||||||||||||||||||||||||||||
| 17 | enable 'FormBody'; | ||||||||||||||||||||||||||||
| 18 | $my_app; | ||||||||||||||||||||||||||||
| 19 | }; | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # In your app: | ||||||||||||||||||||||||||||
| 22 | async sub app { | ||||||||||||||||||||||||||||
| 23 | my ($scope, $receive, $send) = @_; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | my $form_data = $scope->{pagi.parsed_body}; | ||||||||||||||||||||||||||||
| 26 | # $form_data is a hashref like { name => 'value', ... } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | PAGI::Middleware::FormBody parses URL-encoded form request bodies and | ||||||||||||||||||||||||||||
| 32 | makes the parsed data available in C<< $scope->{'pagi.parsed_body'} >>. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =over 4 | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =item * max_size (default: 1MB) | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Maximum body size to parse (in bytes). | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =back | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =cut | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 4 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 47 | 4 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 4 | ▶ | $self->{max_size} = $config->{max_size} // 1024 * 1024; # 1MB | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"max_size"} // 1048576
MC/DC0%0/1
$config->{"max_size"} // 1048576
$config->{"max_size"}
Truth table50%1/2
A
A= $config->{"max_size"}
| |||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 4 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.5
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 4 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=30.2
| |||||||||||||||||||||||||||||
| 56 | 4 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 57 | 4 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 59 | 0 | return; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # Check content type | ||||||||||||||||||||||||||||
| 63 | 4 | ▶ | my $content_type = $self->_get_header($scope, 'content-type') // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->_get_header($scope, "content-type") // ""
MC/DC0%0/1
$self->_get_header($scope, "content-type") // ""
$self->_get_header($scope, "content-type")
Truth table50%1/2
A
A= $self->_get_header($scope, "content-type")
| |||||||||||||||||||||||||||||
| 64 | 4 | my $is_form = $content_type =~ m{^application/x-www-form-urlencoded}i; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | ▶ | unless ($is_form) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 68 | 1 | return; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Read body | ||||||||||||||||||||||||||||
| 72 | 3 | my $body = ''; | |||||||||||||||||||||||||||
| 73 | 3 | my $too_large = 0; | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | while (1) { | |||||||||||||||||||||||||||
| 76 | 3 | my $event = await $receive->(); | |||||||||||||||||||||||||||
| 77 | 3 | ▶ | last unless $event && $event->{type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$event and $event->{"type"}
MC/DC0%0/2
$event and $event->{"type"}
$event
$event->{"type"}
Truth table33%1/3
A and B
A= $eventB= $event->{"type"}
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | ▶ | if ($event->{type} eq 'http.request') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $body .= $event->{body} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||
| 81 | 3 | ▶ | if (length($body) > $self->{max_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | $too_large = 1; | |||||||||||||||||||||||||||
| 83 | 0 | last; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | last unless $event->{more}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | elsif ($event->{type} eq 'http.disconnect') { | ||||||||||||||||||||||||||||
| 88 | 0 | last; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | if ($too_large) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | await $self->_send_error($send, 413, 'Request body too large'); | |||||||||||||||||||||||||||
| 94 | 0 | return; | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # Parse form data | ||||||||||||||||||||||||||||
| 98 | 3 | my $parsed = $self->_parse_urlencoded($body); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # Create modified scope with parsed body | ||||||||||||||||||||||||||||
| 101 | 3 | my $new_scope = $self->modify_scope($scope, { | |||||||||||||||||||||||||||
| 102 | 'pagi.parsed_body' => $parsed, | ||||||||||||||||||||||||||||
| 103 | 'pagi.raw_body' => $body, | ||||||||||||||||||||||||||||
| 104 | }); | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Create a receive that returns empty (body already consumed) | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | my $empty_receive = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 0 | return { type => 'http.request', body => '', more => 0 }; | |||||||||||||||||||||||||||
| 109 | 3 | }; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 3 | await $app->($new_scope, $empty_receive, $send); | |||||||||||||||||||||||||||
| 112 | 4 | }; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 3 | ▶ | sub _parse_urlencoded { | ||||||||||||||||||||||||||
|
Subroutine
_parse_urlencoded: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 116 | 3 | my ($self, $body) = @_; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | my %result; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | for my $pair (split /&/, $body) { | |||||||||||||||||||||||||||
| 121 | 8 | my ($key, $value) = split /=/, $pair, 2; | |||||||||||||||||||||||||||
| 122 | 8 | ▶ | next unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 8 | $key = $self->_url_decode($key); | |||||||||||||||||||||||||||
| 125 | 8 | ▶ | $value = defined $value ? $self->_url_decode($value) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | # Handle multiple values for same key | ||||||||||||||||||||||||||||
| 128 | 8 | ▶ | if (exists $result{$key}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 2 | ▶ | if (ref $result{$key} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 1 | push @{$result{$key}}, $value; | |||||||||||||||||||||||||||
| 131 | } else { | ||||||||||||||||||||||||||||
| 132 | 1 | $result{$key} = [$result{$key}, $value]; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } else { | ||||||||||||||||||||||||||||
| 135 | 6 | $result{$key} = $value; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 3 | return \%result; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 16 | ▶ | sub _url_decode { | ||||||||||||||||||||||||||
|
Subroutine
_url_decode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 16 | my ($self, $str) = @_; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 16 | $str =~ s/\+/ /g; | |||||||||||||||||||||||||||
| 146 | 16 | $str =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | |||||||||||||||||||||||||||
| 147 | 16 | return $str; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 4 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 151 | 4 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 4 | $name = lc($name); | |||||||||||||||||||||||||||
| 154 | 4 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 155 | 4 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | 0 | return; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | async sub _send_error { | ||||||||||||||||||||||||||
|
Subroutine
_send_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 161 | 0 | my ($self, $send, $status, $message) = @_; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | await $send->({ | |||||||||||||||||||||||||||
| 164 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 165 | status => $status, | ||||||||||||||||||||||||||||
| 166 | headers => [ | ||||||||||||||||||||||||||||
| 167 | ['Content-Type', 'text/plain'], | ||||||||||||||||||||||||||||
| 168 | ['Content-Length', length($message)], | ||||||||||||||||||||||||||||
| 169 | ], | ||||||||||||||||||||||||||||
| 170 | }); | ||||||||||||||||||||||||||||
| 171 | 0 | await $send->({ | |||||||||||||||||||||||||||
| 172 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 173 | body => $message, | ||||||||||||||||||||||||||||
| 174 | more => 0, | ||||||||||||||||||||||||||||
| 175 | }); | ||||||||||||||||||||||||||||
| 176 | 1 | } | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | 1; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | __END__ | ||||||||||||||||||||||||||||