removed CE::PSGI from author deps testing
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Plugin / Unicode / Encoding.pm
index 5cfa30e..022efd2 100644 (file)
@@ -23,6 +23,10 @@ sub encoding {
         if (my $wanted = shift)  {
             $encoding = Encode::find_encoding($wanted)
               or Carp::croak( qq/Unknown encoding '$wanted'/ );
+            binmode(STDERR, ':encoding(' . $encoding->name . ')');
+        }
+        else {
+            binmode(STDERR);
         }
 
         $encoding = ref $c
@@ -81,6 +85,7 @@ sub prepare_uploads {
     $c->next::method(@_);
 
     my $enc = $c->encoding;
+    return unless $enc;
 
     for my $key (qw/ parameters query_parameters body_parameters /) {
         for my $value ( values %{ $c->request->{$key} } ) {
@@ -95,15 +100,17 @@ sub prepare_uploads {
     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 );
+        for my $inner_value ( ref($value) eq 'ARRAY' ? @{$value} : $value ) {
+            $inner_value->{filename} = try {
+                $enc->decode( $inner_value->{filename}, $CHECK )
+            } catch {
+                $c->handle_unicode_encoding_exception({
+                    param_value => $inner_value->{filename},
+                    error_msg => $_,
+                    encoding_step => 'uploads',
+                });
+            };
+        }
     }
 }
 
@@ -112,6 +119,9 @@ sub prepare_action {
 
     my $ret = $c->next::method(@_);
 
+    my $enc = $c->encoding;
+    return $ret unless $enc;
+
     foreach (@{$c->req->arguments}, @{$c->req->captures}) {
       $_ = $c->_handle_param_unicode_decoding($_);
     }
@@ -124,11 +134,12 @@ sub setup {
 
     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';
+    # Allow an explicit undef encoding to disable default of utf-8
+    my $enc = delete $conf->{encoding};
     $self->encoding( $enc );
 
-    return $self->next::method(@_);
+    return $self->next::method(@_)
+      unless $self->setup_finished; ## hack to stop possibly meaningless test fail... (jnap)
 }
 
 sub _handle_unicode_decoding {
@@ -185,97 +196,24 @@ Catalyst::Plugin::Unicode::Encoding - Unicode aware Catalyst
 
 =head1 SYNOPSIS
 
-    use Catalyst qw[Unicode::Encoding];
+    use Catalyst;
 
     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>.
+This plugin is automatically loaded by apps. Even though is not a core component
+yet, it will vanish as soon as the code is fully integrated. For more
+information, please refer to L<Catalyst/ENCODING>.
 
 =head1 AUTHORS
 
-Christian Hansen, C<ch@ngmedia.com>
-
-Masahiro Chiba
-
-Tomas Doran, C<bobtfish@bobtfish.net>
+Catalyst Contributors, see Catalyst.pm
 
-=head1 LICENSE
+=head1 COPYRIGHT
 
-This library is free software . You can redistribute it and/or modify
-it under the same terms as perl itself.
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
 
 =cut