Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #1587 from CPAN-API/profile-extra-json
Fix json/utf8 en/de-coding in profile.extra
  • Loading branch information
oalders committed Aug 28, 2015
2 parents 309025a + 15e06bd commit fb28c90
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/MetaCPAN/Web/Controller/Account.pm
Expand Up @@ -82,9 +82,9 @@ sub profile : Local {
$data->{$_} = $req->params->{$_} eq q{} ? undef : $req->params->{$_}
for (qw(name asciiname gravatar_url city region country));
$data->{$_} = [ grep {$_} $req->param($_) ] for (qw(website email));
$data->{extra} = eval {
JSON::MaybeXS->new->relaxed->utf8->decode( $req->params->{extra} );
};

$data->{extra} = $req->json_param('extra');

$data->{donation} = undef unless ( $req->params->{donations} );

my $res
Expand Down
23 changes: 22 additions & 1 deletion lib/MetaCPAN/Web/Role/Request.pm
@@ -1,9 +1,13 @@
package MetaCPAN::Web::Role::Request;

use utf8;
use Moose::Role;
use Plack::Session;

use JSON::MaybeXS ();
use MetaCPAN::Web::Types qw( PositiveInt );
use Try::Tiny;

use namespace::autoclean;

sub page {
my $page = shift->parameters->{p};
Expand All @@ -26,4 +30,21 @@ sub get_page_size {
return $page_size;
}

sub json_param {
my ( $self, $name ) = @_;
return try {
JSON::MaybeXS->new->relaxed->utf8( $self->params_are_decoded ? 0 : 1 )
->decode( $self->params->{$name} );
}
catch {
warn "Failed to decode JSON: $_[0]";
undef;
};
}

sub params_are_decoded {
my ($self) = @_;
return $self->params->{utf8} eq "🐪";
}

1;
4 changes: 3 additions & 1 deletion lib/MetaCPAN/Web/View/HTML.pm
Expand Up @@ -90,7 +90,9 @@ Template::Alloy->define_vmethod( 'text',
Template::Alloy->define_vmethod(
'hash',
pretty_json => sub {
JSON::MaybeXS->new->utf8->pretty->encode(shift);

# Use utf8(0) because Catatlyst expects our view to be a character string.
JSON::MaybeXS->new->utf8(0)->pretty->encode(shift);
}
);

Expand Down
3 changes: 2 additions & 1 deletion root/account/profile.html
Expand Up @@ -6,7 +6,8 @@ <h4 class="alert-heading">Error</h4>
In order to change your profile you have to <a href="<% oauth_prefix %>&amp;choice=pause" onclick="return logInPAUSE(this)">connect your account to PAUSE</a>.
</div>
<% ELSE -%>
<form method="POST" action="" class="form-horizontal" role="form" id="account-profile">
<form method="POST" action="" class="form-horizontal" role="form" id="account-profile" accept-charset="utf-8">
<% utf8_form_input | none %>
<% IF errors -%>
<fieldset><legend style="color: #600">Errors</legend>
<ul>
Expand Down
2 changes: 2 additions & 0 deletions root/preprocess.html
Expand Up @@ -53,6 +53,8 @@
vimeo = { name = 'Vimeo', url = 'http://vimeo.com/%s' },
youtube = { name = 'Youtube', url = 'http://www.youtube.com/user/%s' },
};

utf8_form_input = '<input type="hidden" name="utf8" value="🐪" />';
-%>
<%-

Expand Down
8 changes: 8 additions & 0 deletions t/controller/account.t
@@ -0,0 +1,8 @@
use strict;
use warnings;
use Test::More;

local $TODO = 'Write account tests';
ok 0, 'no tests';

done_testing;

0 comments on commit fb28c90

Please sign in to comment.