line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::JSONParser; |
2
|
3
|
|
|
3
|
|
328642
|
use strict; |
|
3
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
110
|
|
3
|
3
|
|
|
3
|
|
17
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
92
|
|
4
|
3
|
|
|
3
|
|
4087
|
use JSON; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
use Hash::MultiValue; |
6
|
|
|
|
|
|
|
use Plack::Request; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
use parent 'Plack::Middleware'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub call { |
13
|
|
|
|
|
|
|
my ($self, $env) = @_; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
my $content_type = $env->{'CONTENT_TYPE'}; |
16
|
|
|
|
|
|
|
if ($content_type && $content_type =~ |
17
|
|
|
|
|
|
|
m{\Aapplication/json}o) { |
18
|
|
|
|
|
|
|
my $req = Plack::Request->new( $env ); |
19
|
|
|
|
|
|
|
my $raw_body = $req->raw_body(); |
20
|
|
|
|
|
|
|
return $self->app->($env) unless ($raw_body); |
21
|
|
|
|
|
|
|
my $json; |
22
|
|
|
|
|
|
|
{ |
23
|
|
|
|
|
|
|
local $@; |
24
|
|
|
|
|
|
|
$json = eval { decode_json($raw_body) }; |
25
|
|
|
|
|
|
|
if ($@) { |
26
|
|
|
|
|
|
|
die $@ if $self->{die_when_failed}; |
27
|
|
|
|
|
|
|
$env->{'plack.middleware.jsonparser.error'} = $@; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
if ($env->{'plack.request.body'}) { |
31
|
|
|
|
|
|
|
$env->{'plack.request.body'}->merge_mixed( |
32
|
|
|
|
|
|
|
$json |
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
} else { |
35
|
|
|
|
|
|
|
$env->{'plack.request.body'} = Hash::MultiValue->from_mixed( |
36
|
|
|
|
|
|
|
$json |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
$self->app->($env); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
1; |
44
|
|
|
|
|
|
|
__END__ |