1 package Catalyst::Plugin::Unicode::Encoding;
4 use base 'Class::Data::Inheritable';
11 our $CHECK = Encode::FB_CROAK | Encode::LEAVE_SRC;
15 __PACKAGE__->mk_classdata('_encoding');
22 # Let it be set to undef
23 if (my $wanted = shift) {
24 $encoding = Encode::find_encoding($wanted)
25 or Carp::croak( qq/Unknown encoding '$wanted'/ );
29 ? $c->{encoding} = $encoding
30 : $c->_encoding($encoding);
32 $encoding = ref $c && exists $c->{encoding}
40 sub finalize_headers {
43 my $body = $c->response->body;
45 return $c->next::method(@_)
46 unless defined($body);
48 my $enc = $c->encoding;
50 return $c->next::method(@_)
53 my ($ct, $ct_enc) = $c->response->content_type;
55 # Only touch 'text-like' contents
56 return $c->next::method(@_)
57 unless $c->response->content_type =~ /^text|xml$|javascript$/;
59 if ($ct_enc && $ct_enc =~ /charset=([^;]*)/) {
60 if (uc($1) ne uc($enc->mime_name)) {
61 $c->log->debug("Unicode::Encoding is set to encode in '" .
63 "', content type is '$1', not encoding ");
64 return $c->next::method(@_);
67 $c->res->content_type($c->res->content_type . "; charset=" . $enc->mime_name);
70 # Encode expects plain scalars (IV, NV or PV) and segfaults on ref's
71 $c->response->body( $c->encoding->encode( $body, $CHECK ) )
72 if ref(\$body) eq 'SCALAR';
77 # Note we have to hook here as uploads also add to the request parameters
83 my $enc = $c->encoding;
85 for my $key (qw/ parameters query_parameters body_parameters /) {
86 for my $value ( values %{ $c->request->{$key} } ) {
87 # N.B. Check if already a character string and if so do not try to double decode.
88 # http://www.mail-archive.com/catalyst@lists.scsys.co.uk/msg02350.html
89 # this avoids exception if we have already decoded content, and is _not_ the
90 # same as not encoding on output which is bad news (as it does the wrong thing
91 # for latin1 chars for example)..
92 $value = $c->_handle_unicode_decoding($value);
95 for my $value ( values %{ $c->request->uploads } ) {
96 # skip if it fails for uploads, as we don't usually want uploads touched
98 $_->{filename} = try {
99 $enc->decode( $_->{filename}, $CHECK )
101 $c->handle_unicode_encoding_exception({
102 param_value => $_->{filename},
104 encoding_step => 'uploads',
106 } for ( ref($value) eq 'ARRAY' ? @{$value} : $value );
113 my $ret = $c->next::method(@_);
115 foreach (@{$c->req->arguments}, @{$c->req->captures}) {
116 $_ = $c->_handle_param_unicode_decoding($_);
125 my $conf = $self->config;
127 # Allow an explict undef encoding to disable default of utf-8
128 my $enc = exists $conf->{encoding} ? delete $conf->{encoding} : 'UTF-8';
129 $self->encoding( $enc );
131 return $self->next::method(@_);
134 sub _handle_unicode_decoding {
135 my ( $self, $value ) = @_;
137 return unless defined $value;
139 if ( ref $value eq 'ARRAY' ) {
140 foreach ( @$value ) {
141 $_ = $self->_handle_unicode_decoding($_);
145 elsif ( ref $value eq 'HASH' ) {
146 foreach ( values %$value ) {
147 $_ = $self->_handle_unicode_decoding($_);
152 return $self->_handle_param_unicode_decoding($value);
156 sub _handle_param_unicode_decoding {
157 my ( $self, $value ) = @_;
158 my $enc = $self->encoding;
160 Encode::is_utf8( $value ) ?
162 : $enc->decode( $value, $CHECK );
165 $self->handle_unicode_encoding_exception({
166 param_value => $value,
168 encoding_step => 'params',
173 sub handle_unicode_encoding_exception {
174 my ( $self, $exception_ctx ) = @_;
175 die $exception_ctx->{error_msg};
184 Catalyst::Plugin::Unicode::Encoding - Unicode aware Catalyst
188 use Catalyst qw[Unicode::Encoding];
190 MyApp->config( encoding => 'UTF-8' ); # A valid Encode encoding
195 On request, decodes all params from encoding into a sequence of
196 logical characters. On response, encodes body into encoding.
204 Returns an instance of an C<Encode> encoding
206 print $c->encoding->name
210 =head1 OVERLOADED METHODS
214 =item finalize_headers
216 Encodes body into encoding.
218 =item prepare_uploads
220 Decodes parameters, query_parameters, body_parameters and filenames
221 in file uploads into a sequence of logical characters.
225 Decodes request arguments (i.e. C<< $c->request->arguments >>) and
226 captures (i.e. C<< $c->request->captures >>).
230 Setups C<< $c->encoding >> with encoding specified in C<< $c->config->{encoding} >>.
232 =item handle_unicode_encoding_exception ($exception_context)
234 Method called when decoding process for a request fails.
236 An C<$exception_context> hashref is provided to allow you to override the
237 behaviour of your application when given data with incorrect encodings.
239 The default method throws exceptions in the case of invalid request parameters
240 (resulting in a 500 error), but ignores errors in upload filenames.
242 The keys passed in the C<$exception_context> hash are:
248 The value which was not able to be decoded.
252 The exception received from L<Encode>.
256 What type of data was being decoded. Valid values are (currently)
257 C<params> - for request parameters / arguments / captures
258 and C<uploads> - for request upload filenames.
266 L<Encode>, L<Encode::Encoding>, L<Catalyst::Plugin::Unicode>, L<Catalyst>.
270 Christian Hansen, C<ch@ngmedia.com>
274 Tomas Doran, C<bobtfish@bobtfish.net>
278 This library is free software . You can redistribute it and/or modify
279 it under the same terms as perl itself.