Skip to content

Commit

Permalink
use Storable/b64 to avoid JSON utf8 decoding woes
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Apr 7, 2010
1 parent 3dcc2ae commit 0611d61
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/Sunaba/Handler/Create.pm
Expand Up @@ -22,6 +22,8 @@ sub post {
user_agent => $self->request->user_agent,
};

# TODO specify Tatsumaki::Request to get UTF-8 bytes of 'code'

my $data = {
id => gen_random(),
code => $self->request->parameters->{code},
Expand Down
8 changes: 7 additions & 1 deletion lib/Sunaba/Model/App.pm
Expand Up @@ -6,6 +6,7 @@ has code => (is => 'rw', isa => 'Str');
has meta => (is => 'rw', isa => 'Str');
has _meta => (is => 'rw', isa => 'HashRef', lazy_build => 1);

use Encode;
use Data::Dump;
use JSON;

Expand Down Expand Up @@ -54,9 +55,14 @@ sub compile_runtime {
$code .= "my \$_env = " . Data::Dump::pp($env) . ";\n";
$code .= "\$_env->{'psgi.input'} = do { open my \$io, '<', \$_env->{'psgi.input'}; \$io };\n";
$code .= "\$_env->{'psgi.errors'} = \\*STDOUT;\n";
$code .= "use JSON;\nprint STDOUT JSON::encode_json(\$_app->(\$_env));";
$code .= "use Storable;\nuse MIME::Base64;\nprint STDOUT encode_base64(Storable::nfreeze(\$_app->(\$_env)));";

return $code;
}

sub ucode {
my $self = shift;
Encode::decode_utf8($self->code);
}

1;
6 changes: 4 additions & 2 deletions lib/Sunaba/Runner.pm
Expand Up @@ -7,6 +7,8 @@ use AnyEvent::HTTP;
use URI::Escape;
use JSON;
use Sunaba::DB;
use Storable;
use MIME::Base64;

sub to_app {
return sub {
Expand All @@ -30,8 +32,8 @@ sub to_app {
my($body, $hdr) = @_;
my $json = ($body =~ /^sunaba\((.*)\);$/s)[0];
if ($json) {
my $res = JSON::decode_json($json);
$respond->(JSON::decode_json($res->{stdout}));
my $res = JSON::from_json($json);
$respond->(Storable::thaw(MIME::Base64::decode_base64($res->{stdout})));
}
};
};
Expand Down
2 changes: 1 addition & 1 deletion lib/Sunaba/View.pm
Expand Up @@ -67,7 +67,7 @@ template 'app' => sub {
};

form(action => '/app/' . $app->id, method=>'post') {
textarea(class=>'view',rows=>24, cols=>80,name=>'code') { $app->code };
textarea(class=>'view',rows=>24, cols=>80,name=>'code') { $app->ucode };
if ($app->can_edit($stash->{handler}->request)) {
div('#run') {
input(type=>'submit', value=>'Update your code');
Expand Down

0 comments on commit 0611d61

Please sign in to comment.