From c383f648a22c88d17ed2ad524b4af5d7aa7a7ab7 Mon Sep 17 00:00:00 2001 From: Comment Date: Sun, 17 May 2015 12:51:13 +0100 Subject: [PATCH] Item13378: unicode --- lib/Foswiki/Contrib/JsonRpcContrib/Request.pm | 28 ++++++++++--------- lib/Foswiki/Contrib/JsonRpcContrib/Server.pm | 9 +++--- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/Foswiki/Contrib/JsonRpcContrib/Request.pm b/lib/Foswiki/Contrib/JsonRpcContrib/Request.pm index 1410e7d..1a766a3 100644 --- a/lib/Foswiki/Contrib/JsonRpcContrib/Request.pm +++ b/lib/Foswiki/Contrib/JsonRpcContrib/Request.pm @@ -20,6 +20,7 @@ use strict; use warnings; use JSON (); +use Encode (); use Foswiki::Contrib::JsonRpcContrib::Error (); use Error qw( :try ); use Foswiki::Func (); @@ -37,7 +38,7 @@ sub new { # get json-rpc request object my $data = $request->param('POSTDATA'); if ($data) { - $data = fromUtf8($data); + $data = toSiteCharSet($data); } else { @@ -60,7 +61,7 @@ sub new { # override json-rpc params using url params foreach my $key ( $request->multi_param() ) { next if $key =~ /^(POSTDATA|method|id|jsonrpc)$/; # these are different - my @vals = map( fromUtf8($_), $request->multi_param($key) ); + my @vals = map( toSiteCharSet($_), $request->multi_param($key) ); if ( scalar(@vals) == 1 ) { $this->param( $key => $vals[0] ) ; # set json-rpc params using url params @@ -76,7 +77,7 @@ sub new { $this->id($id) if defined $id; # copy method to json-rpc request - $method = $request->param("method") if defined $request->param("method"); + $method = $request->param('method') if defined $request->param("method"); $this->method($method) if defined $method; # check that this is a http POST @@ -195,22 +196,23 @@ sub writeDebug { } ############################################################################### -sub fromUtf8 { +sub toSiteCharSet { my $string = shift; return $string unless $string; - return $string - if ( $Foswiki::cfg{Site}{CharSet} || 'utf-8' ) =~ /^utf-?8/i; + # Convert to unicode if the core supports it + return $string if $Foswiki::UNICODE; return $string - if $Foswiki::Plugins::VERSION > - 2.1; # not required on "newer" foswikis, is it? - - # SMELL: CDot doesn't understand why you would ever want to convert - # params to unicode, especially not in a pre-1.2 wiki..... - require Encode; - return Encode::decode_utf8($string); + if ( $Foswiki::cfg{Site}{CharSet} =~ /^utf-?8/i ); + + # If the site charset is not utf-8, need to convert it + return Encode::encode( + $Foswiki::cfg{Site}{CharSet}, + Encode::decode_utf8($string), + Encode::FB_PERLQQ + ); } 1; diff --git a/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm b/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm index 0e98db3..3b765ad 100644 --- a/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm +++ b/lib/Foswiki/Contrib/JsonRpcContrib/Server.pm @@ -92,7 +92,8 @@ sub dispatch { # get topic parameter and set the location overriding any # other value derived from the namespace param - my $topic = $request->param("topic") || $Foswiki::cfg{HomeTopicName}; + my $topic = $request->param('topic') + || $Foswiki::cfg{HomeTopicName}; ( $session->{webName}, $session->{topicName} ) = Foswiki::Func::normalizeWebTopicName( $Foswiki::cfg{UsersWebName}, $topic ); @@ -113,10 +114,10 @@ sub dispatch { } # if there's login info, try and apply it - my $userName = $request->param("username"); + my $userName = $request->param('username'); if ($userName) { writeDebug("checking password for $userName") if TRACE; - my $pass = $request->param("password") || ''; + my $pass = $request->param('password') || ''; unless ( $session->{users}->checkPassword( $userName, $pass ) ) { Foswiki::Contrib::JsonRpcContrib::Response->print( $session, @@ -183,7 +184,7 @@ sub dispatch { }; # finally - my $redirectto = $request->param("redirectto"); + my $redirectto = $request->param('redirectto'); if ( $code == 0 && defined $redirectto ) { my $url; if ( $redirectto =~ /^https?:/ ) {