/usr/share/perl5/Catalyst/Plugin/Unicode/Encoding.pm is in libcatalyst-plugin-unicode-encoding-perl 1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | package Catalyst::Plugin::Unicode::Encoding;
use strict;
use base 'Class::Data::Inheritable';
use Carp ();
use MRO::Compat;
use Try::Tiny;
use Encode 2.21 ();
our $CHECK = Encode::FB_CROAK | Encode::LEAVE_SRC;
our $VERSION = '1.3';
__PACKAGE__->mk_classdata('_encoding');
sub encoding {
my $c = shift;
my $encoding;
if ( scalar @_ ) {
# Let it be set to undef
if (my $wanted = shift) {
$encoding = Encode::find_encoding($wanted)
or Carp::croak( qq/Unknown encoding '$wanted'/ );
}
$encoding = ref $c
? $c->{encoding} = $encoding
: $c->_encoding($encoding);
} else {
$encoding = ref $c && exists $c->{encoding}
? $c->{encoding}
: $c->_encoding;
}
return $encoding;
}
sub finalize_headers {
my $c = shift;
my $body = $c->response->body;
return $c->next::method(@_)
unless defined($body);
my $enc = $c->encoding;
return $c->next::method(@_)
unless $enc;
my ($ct, $ct_enc) = $c->response->content_type;
# Only touch 'text-like' contents
return $c->next::method(@_)
unless $c->response->content_type =~ /^text|xml$|javascript$/;
if ($ct_enc && $ct_enc =~ /charset=(.*?)$/) {
if (uc($1) ne $enc->mime_name) {
$c->log->debug("Unicode::Encoding is set to encode in '" .
$enc->mime_name .
"', content type is '$1', not encoding ");
return $c->next::method(@_);
}
} else {
$c->res->content_type($c->res->content_type . "; charset=" . $enc->mime_name);
}
# Encode expects plain scalars (IV, NV or PV) and segfaults on ref's
$c->response->body( $c->encoding->encode( $body, $CHECK ) )
if ref(\$body) eq 'SCALAR';
$c->next::method(@_);
}
# Note we have to hook here as uploads also add to the request parameters
sub prepare_uploads {
my $c = shift;
$c->next::method(@_);
my $enc = $c->encoding;
for my $key (qw/ parameters query_parameters body_parameters /) {
for my $value ( values %{ $c->request->{$key} } ) {
# N.B. Check if already a character string and if so do not try to double decode.
# http://www.mail-archive.com/catalyst@lists.scsys.co.uk/msg02350.html
# this avoids exception if we have already decoded content, and is _not_ the
# same as not encoding on output which is bad news (as it does the wrong thing
# for latin1 chars for example)..
$value = $c->_handle_unicode_decoding($value);
}
}
for my $value ( values %{ $c->request->uploads } ) {
# skip if it fails for uploads, as we don't usually want uploads touched
# in any way
$_->{filename} = try {
$enc->decode( $_->{filename}, $CHECK )
} catch {
$c->handle_unicode_encoding_exception({
param_value => $_->{filename},
error_msg => $_,
encoding_step => 'uploads',
});
} for ( ref($value) eq 'ARRAY' ? @{$value} : $value );
}
}
sub prepare_action {
my $c = shift;
my $ret = $c->next::method(@_);
foreach (@{$c->req->arguments}, @{$c->req->captures}) {
$_ = $c->_handle_param_unicode_decoding($_);
}
return $ret;
}
sub setup {
my $self = shift;
my $conf = $self->config;
# Allow an explict undef encoding to disable default of utf-8
my $enc = exists $conf->{encoding} ? delete $conf->{encoding} : 'UTF-8';
$self->encoding( $enc );
return $self->next::method(@_);
}
sub _handle_unicode_decoding {
my ( $self, $value ) = @_;
return unless defined $value;
if ( ref $value eq 'ARRAY' ) {
foreach ( @$value ) {
$_ = $self->_handle_unicode_decoding($_);
}
return $value;
}
elsif ( ref $value eq 'HASH' ) {
foreach ( values %$value ) {
$_ = $self->_handle_unicode_decoding($_);
}
return $value;
}
else {
return $self->_handle_param_unicode_decoding($value);
}
}
sub _handle_param_unicode_decoding {
my ( $self, $value ) = @_;
my $enc = $self->encoding;
return try {
Encode::is_utf8( $value ) ?
$value
: $enc->decode( $value, $CHECK );
}
catch {
$self->handle_unicode_encoding_exception({
param_value => $value,
error_msg => $_,
encoding_step => 'params',
});
};
}
sub handle_unicode_encoding_exception {
my ( $self, $exception_ctx ) = @_;
die $exception_ctx->{error_msg};
}
1;
__END__
=head1 NAME
Catalyst::Plugin::Unicode::Encoding - Unicode aware Catalyst
=head1 SYNOPSIS
use Catalyst qw[Unicode::Encoding];
MyApp->config( encoding => 'UTF-8' ); # A valid Encode encoding
=head1 DESCRIPTION
On request, decodes all params from encoding into a sequence of
logical characters. On response, encodes body into encoding.
=head1 METHODS
=over 4
=item encoding
Returns an instance of an C<Encode> encoding
print $c->encoding->name
=back
=head1 OVERLOADED METHODS
=over
=item finalize_headers
Encodes body into encoding.
=item prepare_uploads
Decodes parameters, query_parameters, body_parameters and filenames
in file uploads into a sequence of logical characters.
=item prepare_action
Decodes request arguments (i.e. C<< $c->request->arguments >>) and
captures (i.e. C<< $c->request->captures >>).
=item setup
Setups C<< $c->encoding >> with encoding specified in C<< $c->config->{encoding} >>.
=item handle_unicode_encoding_exception ($exception_context)
Method called when decoding process for a request fails.
An C<$exception_context> hashref is provided to allow you to override the
behaviour of your application when given data with incorrect encodings.
The default method throws exceptions in the case of invalid request parameters
(resulting in a 500 error), but ignores errors in upload filenames.
The keys passed in the C<$exception_context> hash are:
=over
=item param_value
The value which was not able to be decoded.
=item error_msg
The exception recieved from L<Encode>.
=item encoding_step
What type of data was being decoded. Valid values are (currently)
C<params> - for request parameters / arguments / captures
and C<uploads> - for request upload filenames.
=back
=back
=head1 SEE ALSO
L<Encode>, L<Encode::Encoding>, L<Catalyst::Plugin::Unicode>, L<Catalyst>.
=head1 AUTHORS
Christian Hansen, C<ch@ngmedia.com>
Masahiro Chiba
Tomas Doran, C<bobtfish@bobtfish.net>
=head1 LICENSE
This library is free software . You can redistribute it and/or modify
it under the same terms as perl itself.
=cut
|