blib/lib/PAGI/Middleware/FormBody.pm

filter: Statementstmt 82.2% 65 / 79 Branchbran 57.6% 15 / 26 Conditioncond 45.4% 5 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 10 / 12 Podpod 100% 1 / 1 totaltotal 71.1% 96 / 135 CC 21 SCAR 22.8
CC
21
Cov
73%
__ANON__
20.5
_parse_urlencoded
6.0
_get_header
4.4
CRAP 9.8
« blib/lib/PAGI/Middleware/HTTPSRedirect.pm ↑ Summary lib/PAGI/Middleware/SecurityHeaders.pm »
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
l!l
04
MC/DC0%0/1
$config->{"max_size"} // 1048576
$config->{"max_size"}
Truth table50%1/2
A
A= $config->{"max_size"}
Aresult
11
00
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
BranchTrueFalseTotal
if ($scope->{"type"} ne "http") 0 4 4
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") // ""
l!l
40
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")
Aresult
11
00
64 4 my $is_form = $content_type =~ m{^application/x-www-form-urlencoded}i;
65  
66 4 ▶ unless ($is_form) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_form) 1 3 4
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
BranchTrueFalseTotal
unless $event and $event->{"type"} 0 3 3
Condition33%1/3
$event and $event->{"type"}
!ll&&!rl&&r
003
MC/DC0%0/2
$event and $event->{"type"}
$event $event->{"type"}
Truth table33%1/3
A and B
A= $eventB= $event->{"type"}
ABresult
0X0
100
111
78  
79 3 ▶ if ($event->{type} eq 'http.request') {
Branch: 2 branches
BranchTrueFalseTotal
if ($event->{'type'} eq 'http.request') { } 3 0 3
elsif ($event->{'type'} eq 'http.disconnect') { } 0 0 0
80 3 ▶ $body .= $event->{body} // '';
Condition50%1/2
$event->{"body"} // ""
l!l
30
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
Aresult
11
00
81 3 ▶ if (length($body) > $self->{max_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $body > $self->{"max_size"}) 0 3 3
82 0 $too_large = 1;
83 0 last;
84 }
85 3 ▶ last unless $event->{more};
Branch: 1 branch
BranchTrueFalseTotal
unless $event->{"more"} 3 0 3
86 }
87 elsif ($event->{type} eq 'http.disconnect') {
88 0 last;
89 }
90 }
91  
92 3 ▶ if ($too_large) {
Branch: 1 branch
BranchTrueFalseTotal
if ($too_large) 0 3 3
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
BranchTrueFalseTotal
unless defined $key 0 8 8
123  
124 8 $key = $self->_url_decode($key);
125 8 ▶ $value = defined $value ? $self->_url_decode($value) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 8 0 8
126  
127 # Handle multiple values for same key
128 8 ▶ if (exists $result{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result{$key}) { } 2 6 8
129 2 ▶ if (ref $result{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result{$key} eq 'ARRAY') { } 1 1 2
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"} // []
l!l
40
MC/DC0%0/1
$scope->{"headers"} // []
$scope->{"headers"}
Truth table50%1/2
A
A= $scope->{"headers"}
Aresult
11
00
155 4 ▶ return $h->[1] if lc($h->[0]) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
if lc $h->[0] eq $name 4 0 4
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__
« blib/lib/PAGI/Middleware/HTTPSRedirect.pm ↑ Summary lib/PAGI/Middleware/SecurityHeaders.pm »