| 1 | package Catalyst::View::JSON; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.37'; | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.008_001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use base qw( Catalyst::View ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Encode (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use MRO::Compat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Catalyst::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | __PACKAGE__->mk_accessors(qw( | |||||||||||||||||||||||||||
| 14 | allow_callback callback_param expose_stash | ||||||||||||||||||||||||||||
| 15 | encoding json_dumper no_x_json_header json_encoder_args | ||||||||||||||||||||||||||||
| 16 | use_force_bom)); | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | my($class, $c, $arguments) = @_; | |||||||||||||||||||||||||||
| 20 | 2 | my $self = $class->next::method($c); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | for my $field (keys %$arguments) { | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # Remove catalyst_component_name (and future Cat specific params) | ||||||||||||||||||||||||||||
| 25 | 6 | ▶ | next if $field =~ /^catalyst/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # no longer supported | ||||||||||||||||||||||||||||
| 28 | 4 | ▶ | warn('json_driver is no longer supported'), next if $field eq 'json_driver'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 4 | ▶ | if ($self->can($field)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 4 | $self->$field($arguments->{$field}); | |||||||||||||||||||||||||||
| 32 | } else { | ||||||||||||||||||||||||||||
| 33 | 0 | $c->log->debug("Unknown config parameter '$field'"); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | if (my $method = $self->can('encode_json')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $self->json_dumper( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | 1 | my($data, $self, $c) = @_; | |||||||||||||||||||||||||||
| 40 | 1 | $method->($self, $c, $data); | |||||||||||||||||||||||||||
| 41 | 1 | } ); | |||||||||||||||||||||||||||
| 42 | } else { | ||||||||||||||||||||||||||||
| 43 | 1 | require JSON::MaybeXS; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | my %args = (utf8=>1, %{$self->json_encoder_args ||+{}}); | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->json_encoder_args || {}
MC/DC0%0/1
$self->json_encoder_args || {}
$self->json_encoder_args
Truth table50%1/2
A
A= $self->json_encoder_args
| |||||||||||||||||||||||||||||
| 45 | 1 | my $json = JSON::MaybeXS->new(%args); | |||||||||||||||||||||||||||
| 46 | 1 | ▶ | $self->json_dumper(sub { $json->encode($_[0]) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | return $self; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 12 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=20 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 12 | my($self, $c) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # get the response data from stash | ||||||||||||||||||||||||||||
| 56 | 12 | ▶ | my $cond = sub { 1 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 12 | my $single_key; | |||||||||||||||||||||||||||
| 59 | 12 | ▶ | if (my $expose = $self->expose_stash) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 11 | ▶ | if (ref($expose) eq 'Regexp') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 61 | 9 | ▶ | $cond = sub { $_[0] =~ $expose }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | } elsif (ref($expose) eq 'ARRAY') { | ||||||||||||||||||||||||||||
| 63 | 0 | my %match = map { $_ => 1 } @$expose; | |||||||||||||||||||||||||||
| 64 | 0 | ▶ | $cond = sub { $match{$_[0]} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | } elsif (!ref($expose)) { | ||||||||||||||||||||||||||||
| 66 | 1 | $single_key = $expose; | |||||||||||||||||||||||||||
| 67 | } else { | ||||||||||||||||||||||||||||
| 68 | 1 | $c->log->warn("expose_stash should be an array reference, Regexp object, or key for a single stash entry."); | |||||||||||||||||||||||||||
| 69 | 1 | $c->log->warn("Returning all stash entries"); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 12 | my $data; | |||||||||||||||||||||||||||
| 74 | 12 | ▶ | if ($single_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 1 | $data = $c->stash->{$single_key}; | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 19 | ▶ | $data = { map { $cond->($_) ? ($_ => $c->stash->{$_}) : () } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 11 | keys %{$c->stash} }; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 12 | ▶ | my $cb_param = $self->allow_callback | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->callback_param or "callback"
MC/DC0%0/1
$self->callback_param or "callback"
$self->callback_param
Truth table50%1/2
A
A= $self->callback_param
| |||||||||||||||||||||||||||||
| 82 | ? ($self->callback_param || 'callback') : undef; | ||||||||||||||||||||||||||||
| 83 | 12 | ▶ | my $cb = $cb_param ? $c->req->param($cb_param) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 12 | ▶ | $self->validate_callback_param($cb) if $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # When you set encoding option in View::JSON, this plugin DWIMs | ||||||||||||||||||||||||||||
| 87 | 11 | ▶ | my $encoding = $self->encoding || 'utf-8'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->encoding || 'utf-8'
MC/DC100%1/1
$self->encoding || 'utf-8'
$self->encoding
Truth table100%2/2
A
A= $self->encoding
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 11 | $c->res->content_type("application/json; charset=$encoding"); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 11 | ▶ | if ($c->req->header('X-Prototype-Version') && !$self->no_x_json_header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$c->req->header("X-Prototype-Version") and not $self->no_x_json_header
MC/DC100%2/2
$c->req->header("X-Prototype-Version") and not $self->no_x_json_header
$c->req->header("X-Prototype-Version")
not $self->no_x_json_header
Truth table100%3/3
A and not(B)
A= $c->req->header("X-Prototype-Version")B= not $self->no_x_json_header
| |||||||||||||||||||||||||||||
| 92 | 1 | $c->res->header('X-JSON' => 'eval("("+this.transport.responseText+")")'); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 11 | my $json = $self->render($c, $data); | |||||||||||||||||||||||||||
| 96 | 11 | my $output; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | ## add UTF-8 BOM if the client meets a test and the application wants it. | ||||||||||||||||||||||||||||
| 99 | 11 | ▶ | if ($self->use_force_bom && $encoding eq 'utf-8') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->use_force_bom and $encoding eq "utf-8"
MC/DC100%2/2
$self->use_force_bom and $encoding eq "utf-8"
$self->use_force_bom
$encoding eq "utf-8"
Truth table100%3/3
A and B
A= $self->use_force_bomB= $encoding eq "utf-8"
| |||||||||||||||||||||||||||||
| 100 | 9 | ▶ | my $user_agent = $c->req->user_agent || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$c->req->user_agent || ''
MC/DC100%1/1
$c->req->user_agent || ''
$c->req->user_agent
Truth table100%2/2
A
A= $c->req->user_agent
| |||||||||||||||||||||||||||||
| 101 | 9 | ▶ | if ($self->user_agent_bom_test($user_agent)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 2 | $output = "\xEF\xBB\xBF"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 11 | ▶ | $output .= "$cb(" if $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 11 | $output .= $json; | |||||||||||||||||||||||||||
| 108 | 11 | ▶ | $output .= ");" if $cb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 11 | $c->res->output($output); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # allow for called as $c, $template, $data || $c, $data so that we are compatible | ||||||||||||||||||||||||||||
| 114 | # with the semi standard render method that a lot of views use. | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 11 | ▶ | sub render { | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | 11 | my $self = shift; | |||||||||||||||||||||||||||
| 118 | 11 | my $c = shift; | |||||||||||||||||||||||||||
| 119 | 11 | my $data = pop; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 11 | return $self->json_dumper->($data, $self, $c); # weird order to be backward compat | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 9 | ▶ | sub user_agent_bom_test { | ||||||||||||||||||||||||||
|
Subroutine
user_agent_bom_test: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 125 | 9 | my ($self, $user_agent) = @_; | |||||||||||||||||||||||||||
| 126 | 9 | ▶ | return(($user_agent =~ m/\bSafari\b/) and ($user_agent !~ m/\bChrome\b/)); | ||||||||||||||||||||||||||
|
Condition66%2/3
$user_agent =~ /\bSafari\b/ && !($user_agent =~ /\bChrome\b/)
MC/DC50%1/2
$user_agent =~ /\bSafari\b/ && !($user_agent =~ /\bChrome\b/)
$user_agent =~ /\bSafari\b/
!($user_agent =~ /\bChrome\b/)
Truth table66%2/3
A && not(B)
A= $user_agent =~ /\bSafari\b/B= !($user_agent =~ /\bChrome\b/)
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | sub validate_callback_param { | ||||||||||||||||||||||||||
|
Subroutine
validate_callback_param: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 2 | my($self, $param) = @_; | |||||||||||||||||||||||||||
| 132 | 2 | ▶ | $param =~ /^[a-zA-Z0-9\.\_\[\]]+$/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | or Catalyst::Exception->throw("Invalid callback parameter $param"); | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2 | 1; | |||||||||||||||||||||||||||
| 137 | __END__ | ||||||||||||||||||||||||||||