| 1 | package Plack::Middleware::JSON; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use parent qw/Plack::Middleware/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | our $VERSION = 0.01; | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Plack::Util::Accessor qw/json_key callback_key/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 4 | ▶ | sub prepare_app { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepare_app: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | unless (defined $self->callback_key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | $self->callback_key('callback'); | |||||||||||||||||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | ▶ | if (defined $self->json_key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | my $json_key = $self->json_key() . '|' . $self->callback_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | $self->json_key($json_key); | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 20 | else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | $self->json_key($self->callback_key); | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 4 | ▶ | sub call { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call: called CC=1 SCAR=0.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 4 | my($self, $env) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 27 | 4 | my $res = $self->app->($env); | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | $self->response_cb($res, sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=12 SCAR=26.1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 4 | my $res = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 4 | ▶ | if (defined $res->[2]) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | my $h = Plack::Util::headers($res->[1]); | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | my $json_key = $self->json_key; | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | my $content_type = $h->get('Content-Type') || ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$h->get('Content-Type') || ''
MC/DC0%0/1
$h->get('Content-Type') || ''
$h->get('Content-Type')
Truth table50%1/2
A
A= $h->get('Content-Type')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if (($json_key and $env->{QUERY_STRING} =~ /(?:^|&)($json_key)=([^&]+)/) or $content_type =~ m!/(?:json|javascript)!) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ or $content_type =~ m[/(?:json|javascript)]
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/
MC/DC0%0/3
$json_key and $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/ or $content_type =~ m[/(?:json|javascript)]
$json_key
$env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/
$content_type =~ m[/(?:json|javascript)]
Truth table40%2/5
A and B or C
A= $json_keyB= $env->{"QUERY_STRING"} =~ /(?:^|&)($json_key)=([^&]+)/C= $content_type =~ m[/(?:json|javascript)]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # json | ||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | if ((ref $res->[2][0] eq 'ARRAY') or (ref $res->[2][0] eq 'HASH')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $res->[2][0] eq "ARRAY" or ref $res->[2][0] eq "HASH"
MC/DC0%0/2
ref $res->[2][0] eq "ARRAY" or ref $res->[2][0] eq "HASH"
ref $res->[2][0] eq "ARRAY"
ref $res->[2][0] eq "HASH"
Truth table33%1/3
A or B
A= ref $res->[2][0] eq "ARRAY"B= ref $res->[2][0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 4 | $res->[2] = [ encode_json($res->[2][0]) ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # jsonp | ||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | if (defined $self->callback_key and $1 and $1 eq $self->callback_key) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $self->callback_key and $1 and $1 eq $self->callback_key
defined $self->callback_key and $1
MC/DC66%2/3
defined $self->callback_key and $1 and $1 eq $self->callback_key
defined $self->callback_key
$1
$1 eq $self->callback_key
Truth table75%3/4
A and B and C
A= defined $self->callback_keyB= $1C= $1 eq $self->callback_key
| |||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | my $cb = URI::Escape::uri_unescape($2); | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | if ($cb =~ /^[\w\.\[\]]+$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | my $body; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | Plack::Util::foreach($res->[2], sub { $body .= $_[0] }); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | my $jsonp = "$cb($body)"; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | 1 | $res->[2] = [ $jsonp ]; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 4 | $h->set('Content-Length', length $res->[2][0]); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | $h->set('Content-Type', 'application/json; charset=utf-8'); | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | }); | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||