Tatsumaki/Handler.pm

filter: Statementstmt 50.2% 86 / 171 Branchbran 18.4% 7 / 38 Conditioncond 33.3% 5 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 48.0% 24 / 50 Podpod 0.0% 0 / 21 totaltotal 40.4% 123 / 304 CC 27 SCAR 18.2
CC
27
Cov
44%
finish
20.0
flush
17.6
multipart_xhr_push
12.0
CRAP 6.1
↑ Summary Tatsumaki/Request.pm »
1 package Tatsumaki::Handler;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Tatsumaki::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 has application => (is => 'rw', isa => 'Tatsumaki::Application');
13 4 has condvar => (is => 'rw', isa => 'AnyEvent::CondVar');
14 4 has request => (is => 'rw', isa => 'Tatsumaki::Request');
15 4 has response => (is => 'rw', isa => 'Tatsumaki::Response', lazy_build => 1);
16 4 has args => (is => 'rw', isa => 'ArrayRef');
17 4 has writer => (is => 'rw');
18 4 has mxhr => (is => 'rw', isa => 'Bool');
19 4 has mxhr_boundary => (is => 'rw', isa => 'Str', lazy => 1, lazy_build => 1);
20 0 ▶has json => (is => 'rw', isa => 'JSON', lazy => 1, default => sub { JSON->new->utf8 });
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 4 has binary => (is => 'rw', isa => 'Bool');
22  
23 2 ▶has _write_buffer => (is => 'rw', isa => 'ArrayRef', lazy => 1, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 0 ▶sub head { shift->get(@_) }
Subroutine head: not called CC=1 SCAR=6.9
Pod undocumented
26 0 ▶sub get { Tatsumaki::Error::HTTP->throw(405) }
Subroutine get: not called CC=1 SCAR=6.9
Pod undocumented
27 2 ▶sub post { Tatsumaki::Error::HTTP->throw(405) }
Subroutine post: called CC=1 SCAR=0.0
Pod undocumented
28 0 ▶sub put { Tatsumaki::Error::HTTP->throw(405) }
Subroutine put: not called CC=1 SCAR=6.9
Pod undocumented
29 0 ▶sub delete { Tatsumaki::Error::HTTP->throw(405) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod undocumented
30  
31 4 ▶sub prepare { }
Subroutine prepare: called CC=1 SCAR=0.0
Pod undocumented
32  
33 4 my $class_attr = {};
34  
35 6 ▶sub is_asynchronous {
Subroutine is_asynchronous: called CC=2 SCAR=8.1
Pod undocumented
36 6 ▶ my $class = ref $_[0] || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
600
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
37 6 return $class_attr->{$class}{is_asynchronous};
38 }
39  
40 2 ▶sub asynchronous {
Subroutine asynchronous: called CC=1 SCAR=0.0
Pod undocumented
41 2 my $class = shift;
42 2 $class_attr->{$class}{is_asynchronous} = shift;
43 }
44  
45 0 ▶sub nonblocking { shift->asynchronous(@_) } # alias
Subroutine nonblocking: not called CC=1 SCAR=6.9
Pod undocumented
46  
47 0 ▶sub multipart_xhr_push {
Subroutine multipart_xhr_push: not called CC=3 SCAR=24.8
Pod undocumented
48 0 my $self = shift;
49 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
50 0 ▶ Carp::croak("asynchronous should be set to do multipart XHR push")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_asynchronous 0 0 0
51 unless $self->is_asynchronous;
52 0 $self->response->content_type('multipart/mixed; boundary="' . $self->mxhr_boundary . '"');
53  
54 # HACK: Always write a boundary for the next event, so client JS can fire the event immediately
55 # Maybe DUI.Stream should respect the Content-Length header to look at the endFlag
56 0 $self->stream_write("--" . $self->mxhr_boundary. "\n");
57  
58 0 return $self->mxhr(1);
59 } else {
60 0 return $self->mxhr;
61 }
62 }
63  
64 0 ▶sub _build_mxhr_boundary {
Subroutine _build_mxhr_boundary: not called CC=1 SCAR=6.9
65 0 my $size = 2;
66 0 my $b = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
67 0 $b =~ s/[\W]/X/g; # ensure alnum only
68 0 $b;
69 }
70  
71 2 ▶sub _build_response {
Subroutine _build_response: called CC=1 SCAR=0.0
72 2 my $self = shift;
73 2 $self->request->new_response(200, [ 'Content-Type' => 'text/html; charset=utf-8' ]);
74 }
75  
76 4 my $supported;
77  
78 4 ▶sub supported_method {
Subroutine supported_method: called CC=2 SCAR=6.9
Pod undocumented
79 4 my($self, $method) = @_;
80 4 ▶ $supported ||= +{ map { $_ => 1 } qw( head get post put delete ) };
Condition100%2/2
$supported ||= {map({$_, 1;} "head", "get", "post", "put", "delete")}
l!l
22
MC/DC100%1/1
$supported ||= {map({$_, 1;} "head", "get", "post", "put", "delete")}
$supported
Truth table100%2/2
A
A= $supported
Aresult
11
00
81 4 return $supported->{$method};
82 }
83  
84 0 ▶sub async_cb {
Subroutine async_cb: not called CC=1 SCAR=6.9
Pod undocumented
85 0 my $self = shift;
86 0 my $cb = shift;
87 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 my @args = @_;
89 0 ▶ try { $cb->(@args) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ catch { $self->condvar->croak($_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 };
92 }
93  
94 4 ▶sub run {
Subroutine run: called CC=3 SCAR=16.9
Pod undocumented
95 4 my $self = shift;
96  
97 4 my $method = lc $self->request->method;
98 4 ▶ unless ($self->supported_method($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->supported_method($method)) 0 4 4
99 0 Tatsumaki::Error::HTTP->throw(400);
100 }
101  
102 2 ▶ my $catch = sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
103 2 ▶ if ($_->isa('Tatsumaki::Error::HTTP')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->isa('Tatsumaki::Error::HTTP')) { } 2 0 2
104 2 return [ $_->code, [ 'Content-Type' => 'text/plain' ], [ $_->message ] ];
105 } else {
106 0 $self->log($_);
107 0 return [ 500, [ 'Content-Type' => 'text/plain' ], [ "Internal Server Error" ] ];
108 }
109 4 };
110  
111 4 ▶ if ($self->is_asynchronous) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_asynchronous) { } 0 4 4
112 0 $self->condvar(my $cv = AE::cv);
113 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
114 0 my $start_response = shift;
115 0 ▶ $cv->cb(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 0 my $cv = shift;
117 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
118 0 my $res = $cv->recv;
119 0 my $w = $start_response->($res);
120 0 ▶ if (!$res->[2] && $w) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res->[2] and $w) 0 0 0
Condition0%0/3
not $res->[2] and $w
!ll&&!rl&&r
000
MC/DC0%0/2
not $res->[2] and $w
not $res->[2] $w
Truth table0%0/3
not(A) and B
A= not $res->[2]B= $w
ABresult
0X0
100
111
121 0 $self->writer($w);
122 0 $self->condvar(my $cv2 = AE::cv);
123 }
124 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 $start_response->($catch->());
126 0 };
127 0 });
128  
129 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 $self->prepare;
131 0 $self->$method(@{$self->args});
132 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 $cv->croak($_);
134 0 };
135  
136 0 ▶ unless ($self->request->env->{'psgi.nonblocking'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->request->env->{"psgi.nonblocking"}) 0 0 0
137 0 $self->log("Running an async handler in a blocking server. MQ based app should cause a deadlock.\n");
138 0 $self->condvar->recv for 1..2; # response and writer
139 }
140 0 };
141 } else {
142 4 ▶ my $res = try {
Subroutine __ANON__: called CC=1 SCAR=0.0
143 4 $self->prepare;
144 4 $self->$method(@{$self->args});
145 2 $self->flush;
146 2 return $self->response->finalize;
147 2 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 2 return $catch->();
149 4 };
150  
151 4 return $res;
152 }
153 }
154  
155 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
156 0 my($self, @stuff) = @_;
157 0 $self->request->env->{'psgi.errors'}->print(join '', @stuff);
158 }
159  
160 0 ▶sub get_writer {
Subroutine get_writer: not called CC=2 SCAR=17.9
Pod undocumented
161 0 my $self = shift;
162 0 ▶ $self->flush unless $self->writer;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->writer 0 0 0
163 0 return $self->writer;
164 }
165  
166 2 ▶sub get_chunk {
Subroutine get_chunk: called CC=4 SCAR=21.7
Pod undocumented
167 2 my $self = shift;
168 2 ▶ if (ref $_[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 0 2 2
elsif ($self->binary) { } 0 2 2
169 0 ▶ if ($self->mxhr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->mxhr) { } 0 0 0
170 0 my $json = $self->json->encode($_[0]);
171 0 return "Content-Type: application/json\n\n$json\n--" . $self->mxhr_boundary. "\n";
172 } else {
173 0 $self->response->content_type('application/json');
174 0 return $self->json->encode($_[0]);
175 }
176 } elsif ($self->binary) {
177 0 join '', @_;
178 } else {
179 2 join '', map Encode::encode_utf8($_), @_;
180 }
181 }
182  
183 0 ▶sub _write {
Subroutine _write: not called CC=1 SCAR=6.9
184 0 my $self = shift;
185 0 my @buf = @_;
186 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
187 0 $self->get_writer->write(@buf);
188 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
189 0 ▶ /Broken pipe/ and Tatsumaki::Error::ClientDisconnect->throw;
Branch: 1 branch
BranchTrueFalseTotal
/Broken pipe/ and "Tatsumaki::Error::ClientDisconnect"->throw 0 0 0
190 0 die $_;
191 }
192 0 }
193  
194 0 ▶sub stream_write {
Subroutine stream_write: not called CC=1 SCAR=6.9
Pod undocumented
195 0 my $self = shift;
196 0 $self->_write($self->get_chunk(@_));
197 }
198  
199 2 ▶sub write {
Subroutine write: called CC=1 SCAR=0.0
Pod undocumented
200 2 my $self = shift;
201 2 push @{$self->_write_buffer}, $self->get_chunk(@_);
202 }
203  
204 2 ▶sub flush {
Subroutine flush: called CC=7 SCAR=28.7
Pod undocumented
205 2 my $self = shift;
206 2 my($is_final) = @_;
207  
208 2 ▶ if ($self->writer) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->writer) { } 0 2 2
elsif (not $self->is_asynchronous or $is_final) { } 2 0 2
209 0 ▶ $self->_write(join '', @{$self->_write_buffer}) if @{$self->_write_buffer};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_write_buffer;} 0 0 0
210 0 $self->_write_buffer([]);
211 ▶ } elsif (!$self->is_asynchronous || $is_final) {
Condition33%1/3
not $self->is_asynchronous or $is_final
l!l&&r!l&&!r
200
MC/DC0%0/2
not $self->is_asynchronous or $is_final
not $self->is_asynchronous $is_final
Truth table33%1/3
not(A) or B
A= not $self->is_asynchronousB= $is_final
ABresult
1X1
011
000
212 2 ▶ my $body = $self->response->body || [];
Condition50%1/2
$self->response->body || []
l!l
02
MC/DC0%0/1
$self->response->body || []
$self->response->body
Truth table50%1/2
A
A= $self->response->body
Aresult
11
00
213 2 push @$body, @{$self->_write_buffer};
214 2 $self->_write_buffer([]);
215 2 $self->response->body($body);
216 } else {
217 0 my $res = $self->response->finalize;
218 0 delete $res->[2]; # gimme a writer
219 0 $self->condvar->send($res);
220 0 ▶ $self->writer or Carp::croak("Can't get a writer object back: you need servers with psgi.streaming");
Branch: 1 branch
BranchTrueFalseTotal
$self->writer or Carp::croak("Can't get a writer object back: you need servers with psgi.streaming") 0 0 0
221 0 $self->flush();
222 }
223 }
224  
225 0 ▶sub finish {
Subroutine finish: not called CC=4 SCAR=30.0
Pod undocumented
226 0 my($self, $chunk) = @_;
227 0 ▶ $self->write($chunk) if defined $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if defined $chunk 0 0 0
228 0 $self->flush(1);
229 0 ▶ if ($self->writer) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->writer) { } 0 0 0
elsif ($self->condvar) { } 0 0 0
230 0 $self->writer->close;
231 0 $self->condvar->send;
232 } elsif ($self->condvar) {
233 0 $self->condvar->send($self->response->finalize);
234 }
235 }
236  
237 0 ▶sub render {
Subroutine render: not called CC=2 SCAR=17.9
Pod undocumented
238 0 my($self, $file, $args) = @_;
239 0 ▶ $args ||= {};
Condition0%0/2
$args ||= {}
l!l
00
MC/DC0%0/1
$args ||= {}
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
240 0 $self->finish($self->application->render_file($file, { %$args, handler => $self })->as_string);
241 }
242  
243 4 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
244 4 __PACKAGE__->meta->make_immutable;
245  
246 4 1;
247 __END__
↑ Summary Tatsumaki/Request.pm »