Permalink
Browse files

add Plugin::Encode and finished Plugin::Mobile::Encode and etc...

  • Loading branch information...
1 parent 8d0b7b6 commit 71c6e324ab36fa3d2c25e0705273346823f58973 @nekokak committed Oct 26, 2009
@@ -0,0 +1,28 @@
+<html>
+<head>
+ <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=shift-jis" />
+</head>
+<body>
+ hi kamui.<br />
+ i am [% name %].<br />
+ my nick name is [% nick %]<br />
+ [% args %]<br />
+ [% before_dispatch %] : [% after_dispatch %]<br />
+
+ agent is [% agent %] <br />
+ plugin: [% plugin_foo %]<br />
+ method:[% method %]<br /><br />
+
+ [% word = 'あいう' %]
+ <a href="/?foo=[% word | sjis | uri %]">あいう</a>
+ [% IF validator %]
+ [% FOR valid_msg IN validator.get_error_messages %]
+ <strong>[% valid_msg %]</strong>
+ [% END %]
+ [% END %]
+ <form action="/" method="POST">
+ <input type="text" name="name" value="" />
+ <input type="submit" value="do post request" />
+ </form>
+</body>
+</html>
View
@@ -21,6 +21,9 @@
? URI::Escape::uri_escape_utf8($_[0])
: URI::Escape::uri_escape($_[0]);
},
+ sjis => sub {
+ Encode::encode('sjis', $_[0])
+ },
},
},
json => +{
@@ -1,8 +1,9 @@
package MyAPP::Web::Controller::Root;
use Kamui::Web::Controller -base;
use HTTP::MobileAttribute plugins => [qw/IS/];
+use Encode;
-__PACKAGE__->authorizer('+MyAPP::Web::Authorizer::BasicAuth');
+#__PACKAGE__->authorizer('+MyAPP::Web::Authorizer::BasicAuth');
__PACKAGE__->add_trigger(
'before_dispatch' => sub{
@@ -51,6 +52,32 @@ sub do_index {
}
}
+sub do_mobile {
+ my ($class, $c, $args) = @_;
+ $c->stash->{nick} = $c->req->param('nick') || 'nekokak';
+ $c->stash->{name} = 'コバヤシアツシ';
+ if ($args->{p} eq 'do_redirect') {
+ $c->redirect('/redirect_done');
+ } else {
+ $c->stash->{args} = $args->{p};
+ }
+
+ warn $c->req->param('foo');
+ warn Encode::is_utf8($c->req->param('foo'));
+
+ $c->foo->call; # call plugin method;
+
+ if ( $c->mobile->is_non_mobile ) {
+ $c->stash->{agent} = 'pc';
+ } else {
+ if ($c->mobile->is_ezweb) {
+ $c->stash->{agent} = 'ez';
+ }
+ }
+
+ $c->stash->{method} = 'get';
+}
+
sub do_moge : auth('Null') {
}
@@ -9,6 +9,10 @@ on '/' => run {
return 'Root', 'index', FALSE, +{};
};
+on '/mobile' => run {
+ return 'Root', 'mobile', FALSE, +{ };
+};
+
on '/json' => run {
return 'Root', 'json', FALSE, +{ };
};
@@ -2,7 +2,8 @@ package MyAPP::Web::Handler;
use Kamui::Web::Handler;
use_container 'MyAPP::Container';
-use_plugins [qw/+MyAPP::Plugin::Foo MobileAttribute/];
+use_plugins [qw/+MyAPP::Plugin::Foo Mobile::Attribute Encode/];
+#use_plugins [qw/+MyAPP::Plugin::Foo Mobile::Attribute Mobile::Encode/];
use_context 'MyAPP::Web::Context';
use_view 'Kamui::View::TT';
use_dispatcher 'MyAPP::Web::Dispatcher';
@@ -0,0 +1,25 @@
+package Kamui::Plugin::Encode;
+use Kamui;
+use base 'Kamui::Plugin';
+use Encode;
+use Data::Visitor::Encode;
+
+sub register_method {
+ +{
+ prepare_encoding => sub {
+ my $c = shift;
+ $c->req->parameters(
+ Data::Visitor::Encode->decode( 'utf8', $c->req->parameters )
+ );
+ },
+ finalize_encoding => sub {
+ my $c = shift;
+ my $res = $c->res;
+ my $body = $res->body;
+ $res->body(Encode::encode('utf8',$body));
+ },
+ },
+};
+
+1;
+
@@ -1,7 +1,7 @@
package Kamui::Plugin::Mobile::Attribute;
use Kamui;
use base 'Kamui::Plugin';
-use HTTP::MobileAttribute plugins => [qw/Core IS/];
+use HTTP::MobileAttribute plugins => [qw/Core IS Encoding/];
sub register_method {
+{
@@ -1,63 +1,53 @@
package Kamui::Plugin::Mobile::Encode;
use Kamui;
use base 'Kamui::Plugin';
-
-sub do_initialize { 1 }
-sub do_finalize { 1 }
-sub register_method {
- +{
- mobile_encode => sub {
- my $c = shift;
- Kamui::Plugin::Mobile::Encode::Backend->new($c);
- },
- };
-}
-
-package Kamui::Plugin::Mobile::Encode::Backend;
-use Kamui;
use Data::Visitor::Encode;
use Encode ();
use Encode::JP::Mobile ();
use Scalar::Util ();
-sub new {
- my ($class, $c) = @_;
- bless {c => $c}, $class;
-}
-
-sub c { $_[0]->{c} }
-
-sub finalize {
- my ($self, $res) = @_;
-
- if ( $res && $res->status == 200
- && $res->content_type =~ /html/
- && not( Scalar::Util::blessed( $res->body ) )
- && $res->body )
- {
- my $body = $res->body;
-
- if (Encode::is_utf8( $body )) {
- $res->body( Encode::encode( $self->c->mobile->encoding, $body ) );
+sub register_method {
+ +{
+ prepare_encoding => sub {
+ my $c = shift;
+ $c->req->parameters(
+ Data::Visitor::Encode->decode( $c->mobile->encoding, $c->req->parameters )
+ );
+ },
- my $content_type = $res->content_type || 'text/html';
- if ($content_type =~ m!^text/!) {
- my $charset = $self->c->mobile->can_display_utf8 ? 'UTF-8' : 'Shift_JIS';
- unless ($content_type =~ s/charset\s*=\s*[^\s]*;?/charset=$charset/ ) {
- $content_type .= '; ' unless $content_type =~ /;\s*$/;
- $content_type .= "charset=$charset";
- }
- if ( $content_type =~ m!^text/html!) {
- if ( $self->c->mobile->is_docomo && $self->c->mobile->xhtml_compliant ) {
- $content_type =~ s!text/html!application/xhtml+xml!;
+ finalize_encoding => sub {
+ my $c = shift;
+ my $res = $c->res;
+
+ if ( $res && $res->status == 200
+ && $res->content_type =~ /html/
+ && not( Scalar::Util::blessed( $res->body ) )
+ && $res->body )
+ {
+ my $body = $res->body;
+
+ if (Encode::is_utf8( $body )) {
+ $res->body( Encode::encode( $c->mobile->encoding, $body ) );
+
+ my $content_type = $res->content_type || 'text/html';
+ if ($content_type =~ m!^text/!) {
+ my $charset = $c->mobile->can_display_utf8 ? 'UTF-8' : 'Shift_JIS';
+ unless ($content_type =~ s/charset\s*=\s*[^\s]*;?/charset=$charset/ ) {
+ $content_type .= '; ' unless $content_type =~ /;\s*$/;
+ $content_type .= "charset=$charset";
+ }
+ if ( $content_type =~ m!^text/html!) {
+ if ( $c->mobile->is_docomo && $c->mobile->xhtml_compliant ) {
+ $content_type =~ s!text/html!application/xhtml+xml!;
+ }
+ }
+ $res->content_type( $content_type );
}
}
- $res->content_type( $content_type );
}
- }
- }
+ },
+ };
}
1;
-# HTTP::MobileAttribute依存かな
@@ -32,7 +32,7 @@ sub render {
my $res = $context->res;
$res->status('200');
- $res->body(Encode::encode('utf8',$output));
+ $res->body($output);
$res->headers([ 'Content-Type' => 'text/html' ]);
$res;
}
@@ -168,6 +168,7 @@ sub is_detach {
sub initialize {
my $self = shift;
+ $self->prepare_encoding();
$self->initialize_plugins;
}
@@ -183,6 +184,7 @@ sub finalize {
my ($self, $response) = @_;
$self->finalize_plugins($response);
+ $self->finalize_encoding;
}
sub finalize_plugins {

0 comments on commit 71c6e32

Please sign in to comment.