line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Haineko::HTTPD::Response; |
2
|
10
|
|
|
10
|
|
72
|
use parent 'Plack::Response'; |
|
10
|
|
|
|
|
22
|
|
|
10
|
|
|
|
|
252
|
|
3
|
10
|
|
|
10
|
|
47391
|
use strict; |
|
10
|
|
|
|
|
24
|
|
|
10
|
|
|
|
|
306
|
|
4
|
10
|
|
|
10
|
|
52
|
use warnings; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
5575
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub mime { |
7
|
18
|
|
|
18
|
1
|
41
|
my $class = shift; |
8
|
18
|
|
50
|
|
|
55
|
my $ctype = shift || 'plain'; |
9
|
18
|
|
|
|
|
108
|
my $types = { |
10
|
|
|
|
|
|
|
'json' => 'application/json', |
11
|
|
|
|
|
|
|
'html' => 'text/html; charset=utf-8', |
12
|
|
|
|
|
|
|
'plain' => 'text/plain; charset=utf-8', |
13
|
|
|
|
|
|
|
}; |
14
|
|
|
|
|
|
|
|
15
|
18
|
100
|
|
|
|
1279
|
$ctype = 'plain' if $ctype eq 'text'; |
16
|
18
|
50
|
|
|
|
57
|
$ctype = 'plain' unless exists $types->{ $ctype }; |
17
|
18
|
|
|
|
|
114
|
return $types->{ $ctype }; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub text { |
21
|
2
|
|
|
2
|
1
|
1148
|
my $self = shift; |
22
|
2
|
|
50
|
|
|
9
|
my $code = shift || 200; |
23
|
2
|
|
|
|
|
4
|
my $text = shift; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
21
|
$self->code( $code ); |
26
|
2
|
|
|
|
|
34
|
return $self->_res( $text, 'text' ); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub json { |
30
|
16
|
|
|
16
|
1
|
52
|
my $self = shift; |
31
|
16
|
|
50
|
|
|
48
|
my $code = shift || 200; |
32
|
16
|
|
|
|
|
34
|
my $data = shift; # (Ref->[HASH|ARRAY]) or JSON as a string |
33
|
16
|
|
|
|
|
32
|
my $json = q(); |
34
|
|
|
|
|
|
|
|
35
|
16
|
|
|
|
|
230
|
require Haineko::JSON; |
36
|
16
|
100
|
|
|
|
111
|
$json = ref $data ? Haineko::JSON->dumpjson( $data ) : $data; |
37
|
16
|
|
|
|
|
1971
|
$self->code( $code ); |
38
|
16
|
|
|
|
|
1438
|
return $self->_res( $json, 'json' ); |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub _res { |
42
|
18
|
|
|
18
|
|
43
|
my $self = shift; |
43
|
18
|
|
50
|
|
|
67
|
my $text = shift || q(); |
44
|
18
|
|
50
|
|
|
63
|
my $type = shift || 'json'; |
45
|
18
|
|
|
|
|
140
|
my $head = [ |
46
|
|
|
|
|
|
|
'Content-Type' => Haineko::HTTPD::Response->mime( $type ), |
47
|
|
|
|
|
|
|
'Content-Length' => length $text, |
48
|
|
|
|
|
|
|
'X-Content-Type-Options' => 'nosniff', |
49
|
|
|
|
|
|
|
]; |
50
|
|
|
|
|
|
|
|
51
|
18
|
50
|
|
|
|
73
|
$self->code(200) unless $self->code; |
52
|
18
|
|
|
|
|
247
|
$self->headers( $head ); |
53
|
18
|
|
|
|
|
2580
|
$self->body( $text ); |
54
|
18
|
|
|
|
|
574
|
return $self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
__END__ |