Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Decoding utf-8 encoded parameters #2

Merged
merged 2 commits into from

2 participants

@losomo

WebNano encodes the message body to utf8, which is fine. However, the behavior should become symmetric, i.e. decoding request parameters in Plack::Request. The same operation in Tatsumaki framework looks like this: https://metacpan.org/source/MIYAGAWA/Tatsumaki-0.1013/lib/Tatsumaki/Request.pm

Since WebNano doesn't subclass Plack::Request, I am decoding the parameters just after creating the Plack::Request object.

Without this modification, applications would have to decode all parameters all over the place whenever they are used.

@zby
Owner

Thanks for the patch! Sounds reasonable - let me sleep with it and test it with Nblog. Could you add some tests as well?

Cheers,
Zbigniew

@losomo

Good idea. They are included.

@zby zby merged commit 0221962 into zby:master
@zby
Owner
zby commented

Thanks again! By the way it also need some documentation. I am going on vacations tomorrow - so don't expect quick responses.

Cheers,
Zbigniew

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 31, 2012
  1. Decoding utf8 parameters in request

    Václav Novák authored
  2. Testfile for UTF-8 encoded parameters

    Václav Novák authored
This page is out of date. Refresh to see the latest.
Showing with 72 additions and 0 deletions.
  1. +12 −0 lib/WebNano/Controller.pm
  2. +60 −0 t/utf8.t
View
12 lib/WebNano/Controller.pm
@@ -5,6 +5,7 @@ package WebNano::Controller;
use URI::Escape 'uri_unescape';
use Plack::Request;
+use Encode;
use WebNano::FindController 'find_nested';
use Object::Tiny::RW qw/ app env self_url url_map _req path /;
@@ -15,6 +16,8 @@ sub req {
my $self = shift;
return $self->_req if defined $self->_req;
my $req = Plack::Request->new( $self->env );
+ _decode_parameters($req->query_parameters);
+ _decode_parameters($req->body_parameters);
$self->_req( $req );
return $req;
}
@@ -92,6 +95,15 @@ sub handle {
sub search_subcontrollers { 0 }
+sub _decode_parameters {
+ my ($multihash) = @_;
+ for my $key (keys %$multihash) {
+ my @vals = $multihash->get_all($key);
+ $multihash->remove($key);
+ $multihash->set(decode_utf8($key), map decode_utf8($_), @vals);
+ }
+}
+
1;
__END__
View
60 t/utf8.t
@@ -0,0 +1,60 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Plack::Test;
+use HTTP::Request::Common;
+use Encode;
+use URI::Escape;
+use utf8;
+
+my $PLAIN_TEST_KEY = 'param1';
+my $UTF_TEST_KEY = '키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다';
+my $UTF_TEST_VALUE = 'Příliš žluťoučký kůň úpěl ďábelské ódy';
+
+test_psgi(
+ app => MyApp->new()->psgi_app,
+ client => sub {
+ my $cb = shift;
+ my $res = $cb->(POST "http://localhost/", [$PLAIN_TEST_KEY => $UTF_TEST_VALUE]);
+ is(decode_utf8($res->content), $UTF_TEST_VALUE, 'UTF-8 values in POSTed form');
+ }
+);
+
+test_psgi(
+ app => MyApp->new()->psgi_app,
+ client => sub {
+ my $cb = shift;
+ my $res = $cb->(POST "http://localhost/", [$UTF_TEST_KEY => $UTF_TEST_VALUE]);
+ is(decode_utf8($res->content), $UTF_TEST_VALUE, 'UTF-8 keys and values in POSTed form');
+ }
+);
+
+test_psgi(
+ app => MyApp->new()->psgi_app,
+ client => sub {
+ my $cb = shift;
+ my $res = $cb->(GET "http://localhost/?" . uri_escape_utf8($UTF_TEST_KEY) . '=' . uri_escape_utf8($UTF_TEST_VALUE));
+ is(decode_utf8($res->content), $UTF_TEST_VALUE, 'UTF-8 values as GET parameters');
+ }
+);
+
+done_testing;
+
+{
+ package MyApp;
+ use base 'WebNano';
+ 1;
+}
+
+{
+ package MyApp::Controller;
+ use base 'WebNano::Controller';
+
+ sub index_action {
+ my $self = shift;
+ return $self->req->param($PLAIN_TEST_KEY) || $self->req->param($UTF_TEST_KEY);
+ }
+ 1;
+}
+
Something went wrong with that request. Please try again.