Permalink
Browse files

get mobile agent middleware as outer Middleware

  • Loading branch information...
1 parent b7e38d6 commit 0c2360ee75f49c3d8c7becf2ca577f76d6e1d3d9 @dann committed Feb 1, 2009
@@ -1,36 +1,17 @@
-package Angelos::Middleware::MobileAgent;
+package HTTP::Engine::Middleware::MobileAgent;
use HTTP::Engine::Middleware;
use HTTP::MobileAgent;
-BEGIN {
- ## no critic.
- sub HTTP::MobileAgent::can_display_utf8 {
- my $self = shift;
- if ( $self->is_non_mobile
- || ( $self->is_vodafone && $self->is_type_3gc )
- || $self->xhtml_compliant )
- {
- return 1;
- }
- else {
- return 0;
- }
- }
-
- ## no critic.
- sub HTTP::MobileAgent::encoding {
- my $self = shift;
- $self->can_display_utf8() ? 'utf-8' : 'cp932';
- }
-}
-
-middleware_method 'mobile_attribute' => sub {
+middleware_method 'mobile_agent' => sub {
my $self = shift;
- $self->{mobile_agent} ||= HTTP::MobileAgent->new( $self->headers );
+ $self->{mobile_agent} ||= HTTP::MobileAgent->new($self);
};
__MIDDLEWARE__
+__END__
+__MIDDLEWARE__
+
__END__
=head1 NAME
@@ -1,49 +1,61 @@
package Angelos::Middleware::MobileJP;
-use 5.00800;
-our $VERSION = '0.01';
use HTTP::Engine::Middleware;
use Encode;
use Encode::JP::Mobile ':props';
use Encode::JP::Mobile::Character;
use HTTP::MobileAgent::Plugin::Charset;
use Data::Visitor::Encode;
-has 'encoding' => (
- is => 'rw',
-);
+has 'encoding' => ( is => 'rw', );
-# outer_middleware 'HTTP::Engine::Middleware::MobileAgent';
+outer_middleware 'HTTP::Engine::Middleware::MobileAgent';
before_handle {
my ( $c, $self, $req ) = @_;
- $self->setup_encoding($req) unless $self->encoding;
+ my $encoding = $self->_get_mogile_agent($c)->encoding;
+ $self->setup_encoding($encoding);
$self->decode_params($req);
$req;
};
after_handle {
my ( $c, $self, $req, $res ) = @_;
$self->escape_specialchars($res);
+ $self->setup_content_type( $self->_get_mogile_agent($c), $res );
$res;
};
+sub _get_mogile_agent {
+ my ( $self, $c ) = @_;
+ $c->instance_of('Angelos::Middleware::MobileAgent')->mobile_agent;
+}
+
sub setup_encoding {
- my ($self, $req) = @_;
- $self->encoding(do {
- my $encoding = $req->mobile_agent->encoding;
- ref($encoding) && $encoding->isa('Encode::Encoding')
- ? $encoding
- : Encode::find_encoding($encoding);
- });
+ my ( $self, $encoding, $req ) = @_;
+ $self->encoding(
+ do {
+
+ ref($encoding) && $encoding->isa('Encode::Encoding')
+ ? $encoding
+ : Encode::find_encoding($encoding);
+ }
+ );
}
sub decode_params {
my ( $self, $req ) = @_;
- my $encoding = $req->mobile_agent->encoding;
for my $method (qw/params query_params body_params/) {
- $req->$method(
- Data::Visitor::Encode->decode( $encoding, $req->$method ) );
+
+ for my $value ( values %{ $req->$method } ) {
+ next if ref $value && ref $value ne 'ARRAY';
+
+ for my $v ( ref($value) ? @$value : $value ) {
+ next if Encode::is_utf8($v);
+ $v = $self->encoding->decode($v);
+ }
+ }
}
+
}
my %htmlspecialchars
@@ -65,8 +77,7 @@ sub escape_specialchars {
my $out = Encode::JP::Mobile::FB_CHARACTER()->($char);
if ( $res->content_type =~ /html$|xml$/ ) {
- $out =~ s/([$htmlspecialchars])/$htmlspecialchars{$1}/ego
- ; # for (>3<)
+ $out =~ s/([$htmlspecialchars])/$htmlspecialchars{$1}/ego;
}
$out;
@@ -78,7 +89,23 @@ sub escape_specialchars {
}
+sub setup_content_type {
+ my ( $self, $mobile_agent, $res ) = @_;
+ $res->content_type(
+ do {
+ if ( $mobile_agent->is_docomo ) {
+ 'application/xhtml+xml';
+ }
+ else {
+ 'text/html; charset=' . $self->encoding->mime_name;
+ }
+ }
+ );
+ $res;
+}
+
__MIDDLEWARE__
__END__
+

0 comments on commit 0c2360e

Please sign in to comment.