line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Engine::Middleware::Encode; |
2
|
1
|
|
|
1
|
|
433
|
use HTTP::Engine::Middleware; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
3
|
1
|
|
|
1
|
|
5
|
use Data::Visitor::Encode; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
18
|
|
4
|
1
|
|
|
1
|
|
8
|
use Encode (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
14
|
|
5
|
1
|
|
|
1
|
|
3
|
use Scalar::Util (); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
391
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
has 'detected_decode_by_header' => ( |
8
|
|
|
|
|
|
|
is => 'ro', |
9
|
|
|
|
|
|
|
isa => 'Bool', |
10
|
|
|
|
|
|
|
default => 0, |
11
|
|
|
|
|
|
|
); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'decode' => ( |
14
|
|
|
|
|
|
|
is => 'rw', |
15
|
|
|
|
|
|
|
isa => 'Str', |
16
|
|
|
|
|
|
|
default => 'utf-8', |
17
|
|
|
|
|
|
|
); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
has 'encode' => ( |
20
|
|
|
|
|
|
|
is => 'ro', |
21
|
|
|
|
|
|
|
isa => 'Str', |
22
|
|
|
|
|
|
|
default => 'utf-8', |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'content_type_charset' => ( |
26
|
|
|
|
|
|
|
is => 'ro', |
27
|
|
|
|
|
|
|
isa => 'Str', |
28
|
|
|
|
|
|
|
); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
before_handle { |
31
|
|
|
|
|
|
|
my ( $c, $self, $req ) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $encoding = $self->decode; |
34
|
|
|
|
|
|
|
if ($self->detected_decode_by_header) { |
35
|
|
|
|
|
|
|
if (( $req->headers->header('content-type') || '' ) =~ /charset\s*=\s*([^\s]+);?$/ ) { |
36
|
|
|
|
|
|
|
$encoding = $1; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# decode parameters |
41
|
|
|
|
|
|
|
for my $method (qw/params query_params body_params/) { |
42
|
|
|
|
|
|
|
$req->$method( Data::Visitor::Encode->decode( $encoding, $req->$method ) ); |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
$req; |
45
|
|
|
|
|
|
|
}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
after_handle { |
48
|
|
|
|
|
|
|
my ( $c, $self, $req, $res ) = @_; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
my $body = $res->body; |
51
|
|
|
|
|
|
|
return $res unless $body; |
52
|
|
|
|
|
|
|
if ((Scalar::Util::blessed($body) && $body->can('read')) || (ref($body) eq 'GLOB')) { |
53
|
|
|
|
|
|
|
return $res; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
if (Encode::is_utf8( $body )) { |
56
|
|
|
|
|
|
|
my $encoding = $self->encode; |
57
|
|
|
|
|
|
|
$res->body( Encode::encode( $encoding, $body ) ); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $content_type = $res->content_type || 'text/html'; |
60
|
|
|
|
|
|
|
if ($content_type =~ m!^text/!) { |
61
|
|
|
|
|
|
|
$encoding = $self->content_type_charset if $self->content_type_charset; |
62
|
|
|
|
|
|
|
unless ($content_type =~ s/charset\s*=\s*[^\s]*;?/charset=$encoding/ ) { |
63
|
|
|
|
|
|
|
$content_type .= '; ' unless $content_type =~ /;\s*$/; |
64
|
|
|
|
|
|
|
$content_type .= "charset=$encoding"; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
$res->content_type( $content_type ); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
$res; |
71
|
|
|
|
|
|
|
}; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
__MIDDLEWARE__ |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
__END__ |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 NAME |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
HTTP::Engine::Middleware::Encode - Encoding Filter |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 SYNOPSIS |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
default: in code = utf8, out code = utf8 |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
my $mw = HTTP::Engine::Middleware->new; |
86
|
|
|
|
|
|
|
$mw->install( |
87
|
|
|
|
|
|
|
'HTTP::Engine::Middleware::Encode', |
88
|
|
|
|
|
|
|
); |
89
|
|
|
|
|
|
|
HTTP::Engine->new( |
90
|
|
|
|
|
|
|
interface => { |
91
|
|
|
|
|
|
|
module => 'YourFavoriteInterfaceHere', |
92
|
|
|
|
|
|
|
request_handler => $mw->handler( \&handler ), |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
)->run(); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
in code = cp932, out code = cp932 (Shift-JIS) |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $mw = HTTP::Engine::Middleware->new; |
99
|
|
|
|
|
|
|
$mw->install( |
100
|
|
|
|
|
|
|
'HTTP::Engine::Middleware::Encode' => { |
101
|
|
|
|
|
|
|
decode => 'cp932', |
102
|
|
|
|
|
|
|
decode => 'cp932', |
103
|
|
|
|
|
|
|
content_type_charset => 'Shift-JIS', |
104
|
|
|
|
|
|
|
}, |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
HTTP::Engine->new( |
107
|
|
|
|
|
|
|
interface => { |
108
|
|
|
|
|
|
|
module => 'YourFavoriteInterfaceHere', |
109
|
|
|
|
|
|
|
request_handler => $mw->handler( \&handler ), |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
)->run(); |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
in code = detect by Content-Type header (default encoding is utf8), out code = utf8 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $mw = HTTP::Engine::Middleware->new; |
117
|
|
|
|
|
|
|
$mw->install( |
118
|
|
|
|
|
|
|
'HTTP::Engine::Middleware::Encode' => { |
119
|
|
|
|
|
|
|
detected_decode_by_header => 1, |
120
|
|
|
|
|
|
|
decode => 'utf8', |
121
|
|
|
|
|
|
|
}, |
122
|
|
|
|
|
|
|
); |
123
|
|
|
|
|
|
|
HTTP::Engine->new( |
124
|
|
|
|
|
|
|
interface => { |
125
|
|
|
|
|
|
|
module => 'YourFavoriteInterfaceHere', |
126
|
|
|
|
|
|
|
request_handler => $mw->handler( \&handler ), |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
)->run(); |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 AUTHORS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
precuredaisuki |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
yappo |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head1 SEE ALSO |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
L<Data::Visitor::Encode> |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=cut |