blib/lib/Kossy/Connection.pm

filter: Statementstmt 49.2% 32 / 65 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 64.2% 9 / 14 Podpod 0.0% 0 / 9 totaltotal 35.9% 41 / 114 CC 9 SCAR 30.0
CC
9
Cov
38%
render_json
56.0
render
12.0
escape
2.0
CRAP 20.1
↑ Summary blib/lib/Kossy/Response.pm »
1 package Kossy::Connection;
2  
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 use Class::Accessor::Lite (
6 10 new => 1,
7 rw => [qw/req res stash args tx debug/]
8 10 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 ▶use JSON qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Kossy::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 our $VERSION = '0.50';
13 10 my $_JSON = JSON->new()->allow_blessed(1)->convert_blessed(1)->ascii(1);
14  
15 # for IE7 JSON venularity.
16 # see http://www.atmarkit.co.jp/fcoding/articles/webapp/05/webapp05a.html
17 # Copy from Amon2::Plugin::Web::JSON => Fixed to escape only string parts
18 10 my %_ESCAPE = (
19 '+' => '\\u002b', # do not eval as UTF-7
20 '<' => '\\u003c', # do not eval as HTML
21 '>' => '\\u003e', # ditto.
22 );
23 sub escape {
24 0 ▶ my $self = shift;
Subroutine escape: not called CC=1 SCAR=6.9
Pod undocumented
25 0 my $body = shift;
26 0 $body =~ s!([+<>])!$_ESCAPE{$1}!g;
27 0 return qq("$body");
28 }
29 sub escape_json {
30 0 ▶ my $self = shift;
Subroutine escape_json: not called CC=1 SCAR=6.9
Pod undocumented
31 0 my $body = shift;
32 # escape only string parts
33 0 $body =~ s/"((?:\\"|[^"])*)"/$self->escape($1)/eg;
34 0 return $body;
35 }
36  
37 10 *request = \&req;
38 10 *response = \&res;
39  
40 sub env {
41 1 ▶ $_[0]->{req}->env;
Subroutine env: called CC=1 SCAR=0.0
Pod undocumented
42 }
43  
44 sub halt {
45 12 ▶ my $self = shift;
Subroutine halt: called CC=1 SCAR=0.0
Pod undocumented
46 12 die Kossy::Exception->new(@_);
47 }
48  
49 sub halt_text {
50 3 ▶ my ($self, $code, $message) = @_;
Subroutine halt_text: called CC=1 SCAR=0.0
Pod undocumented
51 3 $self->res->content_type('text/plain');
52 3 $self->res->body($message);
53 3 die Kossy::Exception->new($code, response => $self->res);
54 }
55  
56 sub halt_no_content {
57 2 ▶ my ($self, $code) = @_;
Subroutine halt_no_content: called CC=1 SCAR=0.0
Pod undocumented
58 2 $self->res->headers->remove_content_headers;
59 2 $self->res->content_length(0);
60 2 die Kossy::Exception->new($code, response => $self->res);
61 }
62  
63 sub redirect {
64 0 ▶ my $self = shift;
Subroutine redirect: not called CC=1 SCAR=6.9
Pod undocumented
65 0 $self->res->redirect(@_);
66 0 $self->res;
67 }
68  
69 sub render {
70 0 ▶ my $self = shift;
Subroutine render: not called CC=3 SCAR=24.8
Pod undocumented
71 0 my $file = shift;
72 0 ▶ my %args = ( @_ && ref $_[0] ) ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[0] ? : 0 0 0
Condition0%0/3
@_ and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table0%0/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
73 0 my %vars = (
74 c => $self,
75 stash => $self->stash,
76 %args,
77 );
78  
79 0 my $body = $self->tx->render($file, \%vars);
80 0 $self->res->status( 200 );
81 0 $self->res->content_type('text/html; charset=UTF-8');
82 0 $self->res->body( $body );
83 0 $self->res;
84 }
85  
86 sub render_json {
87 0 ▶ my $self = shift;
Subroutine render_json: not called CC=7 SCAR=40.3
Pod undocumented
88 0 my $obj = shift;
89  
90 # defense from JSON hijacking
91 # Copy from Amon2::Plugin::Web::JSON
92 0 ▶ if ( exists $self->req->env->{'HTTP_X_REQUESTED_WITH'} &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} and ($self->req->env->{"HTTP_USER_AGENT"} || "") =~ /android/i and exists $self->req->env->{"HTTP_COOKIE"} and ($self->req->method || "GET") eq "GET") 0 0 0
Condition0%0/13
exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} and ($self->req->env->{"HTTP_USER_AGENT"} || "") =~ /android/i and exists $self->req->env->{"HTTP_COOKIE"} and ($self->req->method || "GET") eq "GET"
!ll&&!rl&&r
000
exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} and ($self->req->env->{"HTTP_USER_AGENT"} || "") =~ /android/i and exists $self->req->env->{"HTTP_COOKIE"}
!ll&&!rl&&r
000
exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} and ($self->req->env->{"HTTP_USER_AGENT"} || "") =~ /android/i
!ll&&!rl&&r
000
$self->req->env->{'HTTP_USER_AGENT'} || ''
l!l
00
$self->req->method || 'GET'
l!l
00
MC/DC0%0/4
exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} and ($self->req->env->{"HTTP_USER_AGENT"} || "") =~ /android/i and exists $self->req->env->{"HTTP_COOKIE"} and ($self->req->method || "GET") eq "GET"
exists $self->req->env->{"HTTP_X_REQUESTED_WITH"} $self->req->env->{'HTTP_USER_AGENT'} exists $self->req->env->{"HTTP_COOKIE"} $self->req->method
Truth table0%0/5
A and B and C and D
A= exists $self->req->env->{"HTTP_X_REQUESTED_WITH"}B= $self->req->env->{'HTTP_USER_AGENT'}C= exists $self->req->env->{"HTTP_COOKIE"}D= $self->req->method
ABCDresult
0XXX0
10XX0
110X0
11100
11111
93 ($self->req->env->{'HTTP_USER_AGENT'}||'') =~ /android/i &&
94 exists $self->req->env->{'HTTP_COOKIE'} &&
95 ($self->req->method||'GET') eq 'GET'
96 ) {
97 0 $self->halt(403,"Your request is maybe JSON hijacking.\nIf you are not a attacker, please add 'X-Requested-With' header to each request.");
98 }
99  
100 0 my $body = $_JSON->encode($obj);
101 0 $body = $self->escape_json($body);
102  
103 0 $self->res->status( 200 );
104 0 $self->res->content_type('application/json; charset=UTF-8');
105 0 $self->res->header( 'X-Content-Type-Options' => 'nosniff' ); # defense from XSS
106 0 $self->res->body( $body );
107 0 $self->res;
108 }
109  
110  
111  
112 10 1;
113  
↑ Summary blib/lib/Kossy/Response.pm »