| 1 | package PAGI::Middleware::JSONBody; | ||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::Middleware::JSONBody::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 JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 NAME | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | PAGI::Middleware::JSONBody - JSON request body parsing middleware | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use PAGI::Middleware::Builder; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | my $app = builder { | ||||||||||||||||||||||||||||
| 18 | enable 'JSONBody'; | ||||||||||||||||||||||||||||
| 19 | $my_app; | ||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # In your app: | ||||||||||||||||||||||||||||
| 23 | async sub app { | ||||||||||||||||||||||||||||
| 24 | my ($scope, $receive, $send) = @_; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | my $json_data = $scope->{pagi.parsed_body}; | ||||||||||||||||||||||||||||
| 27 | # $json_data is a hashref/arrayref from JSON | ||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | PAGI::Middleware::JSONBody parses JSON request bodies and makes the | ||||||||||||||||||||||||||||
| 33 | parsed data available in C<< $scope->{'pagi.parsed_body'} >>. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =over 4 | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =item * max_size (default: 1MB) | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Maximum body size to parse (in bytes). | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =item * content_types (default: application/json) | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Content-Type patterns to parse. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =back | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 5 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 52 | 5 | my ($self, $config) = @_; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 5 | ▶ | $self->{max_size} = $config->{max_size} // 1024 * 1024; # 1MB | ||||||||||||||||||||||||||
|
Condition100%2/2
$config->{"max_size"} // 1048576
MC/DC100%1/1
$config->{"max_size"} // 1048576
$config->{"max_size"}
Truth table100%2/2
A
A= $config->{"max_size"}
| |||||||||||||||||||||||||||||
| 55 | $self->{content_types} = $config->{content_types} // [ | ||||||||||||||||||||||||||||
| 56 | 5 | ▶ | 'application/json', | ||||||||||||||||||||||||||
|
Condition50%1/2
$config->{"content_types"} // ["application/json", "application/json; charset=utf-8"]
MC/DC0%0/1
$config->{"content_types"} // ["application/json", "application/json; charset=utf-8"]
$config->{"content_types"}
Truth table50%1/2
A
A= $config->{"content_types"}
| |||||||||||||||||||||||||||||
| 57 | 'application/json; charset=utf-8', | ||||||||||||||||||||||||||||
| 58 | ]; | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | ▶ | sub wrap { | ||||||||||||||||||||||||||
|
Subroutine
wrap: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 5 | my ($self, $app) = @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 5 | ▶ | return async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=13 SCAR=27.4
| |||||||||||||||||||||||||||||
| 65 | 5 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||
| 66 | 5 | ▶ | if ($scope->{type} ne 'http') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 68 | 0 | return; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Check content type | ||||||||||||||||||||||||||||
| 72 | 5 | ▶ | 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")
| |||||||||||||||||||||||||||||
| 73 | 5 | my $is_json = $self->_is_json_content_type($content_type); | |||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 5 | ▶ | unless ($is_json) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 1 | await $app->($scope, $receive, $send); | |||||||||||||||||||||||||||
| 77 | 1 | return; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | # Read and parse body | ||||||||||||||||||||||||||||
| 81 | 4 | my $body = ''; | |||||||||||||||||||||||||||
| 82 | 4 | my $too_large = 0; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | while (1) { | |||||||||||||||||||||||||||
| 85 | 4 | my $event = await $receive->(); | |||||||||||||||||||||||||||
| 86 | 4 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 4 | ▶ | if ($event->{type} eq 'http.request') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | 4 | ▶ | $body .= $event->{body} // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"body"} // ""
MC/DC0%0/1
$event->{"body"} // ""
$event->{"body"}
Truth table50%1/2
A
A= $event->{"body"}
| |||||||||||||||||||||||||||||
| 90 | 4 | ▶ | if (length($body) > $self->{max_size}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | $too_large = 1; | |||||||||||||||||||||||||||
| 92 | 1 | last; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 3 | ▶ | last unless $event->{more}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | elsif ($event->{type} eq 'http.disconnect') { | ||||||||||||||||||||||||||||
| 97 | 0 | last; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 4 | ▶ | if ($too_large) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1 | await $self->_send_error($send, 413, 'Request body too large'); | |||||||||||||||||||||||||||
| 103 | 1 | return; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # Parse JSON | ||||||||||||||||||||||||||||
| 107 | 3 | my $parsed; | |||||||||||||||||||||||||||
| 108 | 3 | eval { | |||||||||||||||||||||||||||
| 109 | 3 | $parsed = JSON::MaybeXS::decode_json($body); | |||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 1 | await $self->_send_error($send, 400, 'Invalid JSON: ' . $@); | |||||||||||||||||||||||||||
| 113 | 1 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Create modified scope with parsed body | ||||||||||||||||||||||||||||
| 117 | 2 | my $new_scope = $self->modify_scope($scope, { | |||||||||||||||||||||||||||
| 118 | 'pagi.parsed_body' => $parsed, | ||||||||||||||||||||||||||||
| 119 | 'pagi.raw_body' => $body, | ||||||||||||||||||||||||||||
| 120 | }); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Create a receive that returns empty (body already consumed) | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | my $empty_receive = async sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | return { type => 'http.request', body => '', more => 0 }; | |||||||||||||||||||||||||||
| 125 | 2 | }; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 2 | await $app->($new_scope, $empty_receive, $send); | |||||||||||||||||||||||||||
| 128 | 5 | }; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | ▶ | sub _is_json_content_type { | ||||||||||||||||||||||||||
|
Subroutine
_is_json_content_type: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 132 | 5 | my ($self, $content_type) = @_; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 5 | $content_type = lc($content_type); | |||||||||||||||||||||||||||
| 135 | 5 | $content_type =~ s/\s+//g; # Remove whitespace | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 5 | for my $pattern (@{$self->{content_types}}) { | |||||||||||||||||||||||||||
| 138 | 7 | my $lc_pattern = lc($pattern); | |||||||||||||||||||||||||||
| 139 | 7 | $lc_pattern =~ s/\s+//g; | |||||||||||||||||||||||||||
| 140 | 7 | ▶ | return 1 if index($content_type, $lc_pattern) == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Also match any application/XXX+json | ||||||||||||||||||||||||||||
| 144 | 2 | ▶ | return 1 if $content_type =~ m{^application/[^;]+\+json}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | return 0; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 5 | ▶ | sub _get_header { | ||||||||||||||||||||||||||
|
Subroutine
_get_header: called CC=4 SCAR=14.9
| |||||||||||||||||||||||||||||
| 150 | 5 | my ($self, $scope, $name) = @_; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | $name = lc($name); | |||||||||||||||||||||||||||
| 153 | 5 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 154 | 5 | ▶ | return $h->[1] if lc($h->[0]) eq $name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | 0 | return; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 2 | ▶ | async sub _send_error { | ||||||||||||||||||||||||||
|
Subroutine
_send_error: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 2 | my ($self, $send, $status, $message) = @_; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 2 | my $body = JSON::MaybeXS::encode_json({ error => $message }); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 2 | await $send->({ | |||||||||||||||||||||||||||
| 165 | type => 'http.response.start', | ||||||||||||||||||||||||||||
| 166 | status => $status, | ||||||||||||||||||||||||||||
| 167 | headers => [ | ||||||||||||||||||||||||||||
| 168 | ['Content-Type', 'application/json'], | ||||||||||||||||||||||||||||
| 169 | ['Content-Length', length($body)], | ||||||||||||||||||||||||||||
| 170 | ], | ||||||||||||||||||||||||||||
| 171 | }); | ||||||||||||||||||||||||||||
| 172 | 2 | await $send->({ | |||||||||||||||||||||||||||
| 173 | type => 'http.response.body', | ||||||||||||||||||||||||||||
| 174 | body => $body, | ||||||||||||||||||||||||||||
| 175 | more => 0, | ||||||||||||||||||||||||||||
| 176 | }); | ||||||||||||||||||||||||||||
| 177 | 1 | } | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 1 | 1; | |||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | __END__ | ||||||||||||||||||||||||||||