[Catalyst-commits] r11765 - in Catalyst-Plugin-Unicode-Encoding/trunk: . lib/Catalyst/Plugin/Unicode t t/lib

t0m at dev.catalyst.perl.org t0m at dev.catalyst.perl.org
Thu Nov 5 01:54:30 GMT 2009


Author: t0m
Date: 2009-11-05 01:54:28 +0000 (Thu, 05 Nov 2009)
New Revision: 11765

Added:
   Catalyst-Plugin-Unicode-Encoding/trunk/t/06request_decode.t
Modified:
   Catalyst-Plugin-Unicode-Encoding/trunk/
   Catalyst-Plugin-Unicode-Encoding/trunk/lib/Catalyst/Plugin/Unicode/Encoding.pm
   Catalyst-Plugin-Unicode-Encoding/trunk/t/lib/TestApp.pm
Log:
 r11616 at t0mlaptop (orig r11581):  chiba | 2009-10-16 18:43:47 +0100
 create branche for support upload filename decode.
 
 r11617 at t0mlaptop (orig r11582):  chiba | 2009-10-16 19:02:06 +0100
 add support_upload_filename
 
 r11797 at t0mlaptop (orig r11762):  t0m | 2009-11-05 01:39:56 +0000
 More tests
 r11798 at t0mlaptop (orig r11763):  t0m | 2009-11-05 01:49:46 +0000
 Controversially (maybe?) do the right thing to avoid exceptions on already decoded content
 r11799 at t0mlaptop (orig r11764):  t0m | 2009-11-05 01:53:38 +0000
 Update authors



Property changes on: Catalyst-Plugin-Unicode-Encoding/trunk
___________________________________________________________________
Name: svk:merge
   + 4ad37cd2-5fec-0310-835f-b3785c72a374:/Catalyst-Plugin-Unicode-Encoding/branches/support_upload_filename:11764
4ad37cd2-5fec-0310-835f-b3785c72a374:/trunk/Catalyst-Plugin-Unicode-Encoding:11574

Modified: Catalyst-Plugin-Unicode-Encoding/trunk/lib/Catalyst/Plugin/Unicode/Encoding.pm
===================================================================
--- Catalyst-Plugin-Unicode-Encoding/trunk/lib/Catalyst/Plugin/Unicode/Encoding.pm	2009-11-05 01:53:38 UTC (rev 11764)
+++ Catalyst-Plugin-Unicode-Encoding/trunk/lib/Catalyst/Plugin/Unicode/Encoding.pm	2009-11-05 01:54:28 UTC (rev 11765)
@@ -72,22 +72,35 @@
     $c->next::method(@_);
 }
 
-sub prepare_parameters {
+# 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 $value ( values %{ $c->request->{parameters} } ) {
+    for my $key (qw/ parameters query_parameters body_parameters /) {
+        for my $value ( values %{ $c->request->{$key} } ) {
 
-        # TODO: Hash support from the Params::Nested
-        if ( ref $value && ref $value ne 'ARRAY' ) {
-            next;
+            # TODO: Hash support from the Params::Nested
+            if ( ref $value && ref $value ne 'ARRAY' ) {
+                next;
+            }
+            for ( ref($value) ? @{$value} : $value ) {
+                # 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)..
+                $_ = Encode::is_utf8( $_ ) ? $_ : $enc->decode( $_, $CHECK );
+            }
         }
-
-        $_ = $enc->decode( $_, $CHECK ) for ( ref($value) ? @{$value} : $value );
     }
+    for my $value ( values %{ $c->request->uploads } ) {
+        $_->{filename} = $enc->decode( $_->{filename}, $CHECK )
+            for ( ref($value) eq 'ARRAY' ? @{$value} : $value );
+    }
 }
 
 sub setup {
@@ -136,15 +149,16 @@
 
 =head1 OVERLOADED METHODS
 
-=over 4
+=over
 
 =item finalize
 
 Encodes body into encoding.
 
-=item prepare_parameters
+=item prepare_uploads
 
-Decodes parameters into a sequence of logical characters.
+Decodes parameters, query_parameters, body_parameters and filenames
+in file uploads into a sequence of logical characters.
 
 =item setup
 
@@ -156,10 +170,14 @@
 
 L<Encode>, L<Encode::Encoding>, L<Catalyst::Plugin::Unicode>, L<Catalyst>.
 
-=head1 AUTHOR
+=head1 AUTHORS
 
 Christian Hansen, C<ch at ngmedia.com>
 
+Masahiro Chiba
+
+Tomas Doran, C<bobtfish at bobtfish.net>
+
 =head1 LICENSE
 
 This library is free software . You can redistribute it and/or modify

Added: Catalyst-Plugin-Unicode-Encoding/trunk/t/06request_decode.t
===================================================================
--- Catalyst-Plugin-Unicode-Encoding/trunk/t/06request_decode.t	                        (rev 0)
+++ Catalyst-Plugin-Unicode-Encoding/trunk/t/06request_decode.t	2009-11-05 01:54:28 UTC (rev 11765)
@@ -0,0 +1,49 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More tests => 5 * 3;
+use utf8;
+
+# setup library path
+use FindBin qw($Bin);
+use lib "$Bin/lib";
+
+use Catalyst::Test 'TestApp';
+use Encode;
+use HTTP::Request::Common;
+use URI::Escape;
+
+my $encode_str = "\x{e3}\x{81}\x{82}"; # e38182 is japanese 'あ'
+my $decode_str = Encode::decode('utf-8' => $encode_str);
+my $escape_str = uri_escape_utf8($decode_str);
+
+check_parameter(GET "/?foo=$escape_str");
+check_parameter(POST '/', ['foo' => $encode_str]);
+check_parameter(POST '/',
+    Content_Type => 'form-data',
+    Content => [
+        'foo' => [
+            "$Bin/06request_decode.t",
+            $encode_str,
+        ]
+    ],
+);
+
+
+sub check_parameter {
+    my ( undef, $c ) = ctx_request(shift);
+    is $c->res->output => '<h1>It works</h1>';
+
+    my $foo = $c->req->param('foo');
+    ok utf8::is_utf8($foo);
+    is $foo => $decode_str;
+
+    my $other_foo = $c->req->method eq 'POST'
+        ? $c->req->upload('foo')
+            ? $c->req->upload('foo')->filename
+            : $c->req->body_parameters->{foo}
+        : $c->req->query_parameters->{foo};
+    ok utf8::is_utf8($other_foo);
+    is $other_foo => $decode_str;
+}

Modified: Catalyst-Plugin-Unicode-Encoding/trunk/t/lib/TestApp.pm
===================================================================
--- Catalyst-Plugin-Unicode-Encoding/trunk/t/lib/TestApp.pm	2009-11-05 01:53:38 UTC (rev 11764)
+++ Catalyst-Plugin-Unicode-Encoding/trunk/t/lib/TestApp.pm	2009-11-05 01:54:28 UTC (rev 11765)
@@ -1,13 +1,15 @@
 package TestApp;
 use strict;
 use warnings;
-
+use base qw/Catalyst/;
 use Catalyst qw/Unicode::Encoding/;
 
 __PACKAGE__->config(
   encoding => $ENV{TESTAPP_ENCODING}
 ) if $ENV{TESTAPP_ENCODING};
 
+__PACKAGE__->config('name' => 'TestApp');
+
 __PACKAGE__->setup;
 
 1;




More information about the Catalyst-commits mailing list