Permalink
Browse files

use Storable/b64 to avoid JSON utf8 decoding woes

  • Loading branch information...
1 parent 3dcc2ae commit 0611d614a71174a6793c185fc922a04f789d2cd1 @miyagawa committed Apr 7, 2010
Showing with 14 additions and 4 deletions.
  1. +2 −0 lib/Sunaba/Handler/Create.pm
  2. +7 −1 lib/Sunaba/Model/App.pm
  3. +4 −2 lib/Sunaba/Runner.pm
  4. +1 −1 lib/Sunaba/View.pm
@@ -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},
@@ -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;
@@ -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;
@@ -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 {
@@ -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})));
}
};
};
View
@@ -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');

0 comments on commit 0611d61

Please sign in to comment.