Changeset 29976
- Timestamp:
- 02/12/09 23:07:06 (4 years ago)
- Location:
- lang/perl/HTTP-Engine-Middleware/trunk
- Files:
-
- 2 removed
- 2 modified
-
lib/HTTP/Engine/Middleware/Encode.pm (modified) (2 diffs)
-
lib/HTTP/Engine/Middleware/Unicode.pm (deleted)
-
t/200_middlewares/encode.t (modified) (3 diffs)
-
t/200_middlewares/unicode.t (deleted)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/Encode.pm
r29830 r29976 2 2 use HTTP::Engine::Middleware; 3 3 use Data::Visitor::Encode; 4 use Encode (); 5 use Scalar::Util (); 6 7 has 'detected_decode_by_header' => ( 8 is => 'ro', 9 isa => 'Bool', 10 default => 0, 11 ); 12 13 has 'decode' => ( 14 is => 'rw', 15 isa => 'Str', 16 default => 'utf-8', 17 ); 18 19 has 'encode' => ( 20 is => 'ro', 21 isa => 'Str', 22 default => 'utf-8', 23 ); 24 25 has 'content_type_charset' => ( 26 is => 'ro', 27 isa => 'Str', 28 ); 4 29 5 30 before_handle { 6 31 my ( $c, $self, $req ) = @_; 7 32 8 if (( $req->headers->header('Content-Type') || '' ) =~ /charset=(.+);?$/ ) 9 { 10 11 # decode parameters 12 my $encoding = $1; 13 for my $method (qw/params query_params body_params/) { 14 $req->$method( 15 Data::Visitor::Encode->decode( $encoding, $req->$method ) ); 33 my $encoding = $self->decode; 34 if ($self->detected_decode_by_header) { 35 if (( $req->headers->header('content-type') || '' ) =~ /charset\s*=\s*([^\s]+);?$/ ) { 36 $encoding = $1; 16 37 } 17 38 } 39 40 # decode parameters 41 for my $method (qw/params query_params body_params/) { 42 $req->$method( Data::Visitor::Encode->decode( $encoding, $req->$method ) ); 43 } 18 44 $req; 45 }; 46 47 after_handle { 48 my ( $c, $self, $req, $res ) = @_; 49 50 my $body = $res->body; 51 return $res unless $body; 52 if ((Scalar::Util::blessed($body) && $body->can('read')) || (ref($body) eq 'GLOB')) { 53 return $res; 54 } 55 if (Encode::is_utf8( $body )) { 56 my $encoding = $self->encode; 57 $res->body( Encode::encode( $encoding, $body ) ); 58 59 $encoding = $self->content_type_charset if $self->content_type_charset; 60 my $content_type = $res->content_type; 61 unless ($content_type =~ s/charset\s*=\s*[^\s]*;?/charset=$encoding/ ) { 62 $content_type .= "charset=$encoding;"; 63 } 64 $res->content_type( $content_type ); 65 } 66 67 $res; 19 68 }; 20 69 … … 25 74 =head1 NAME 26 75 27 HTTP::Engine::Middleware::Encode - documentation is TODO76 HTTP::Engine::Middleware::Encode - Encoding Filter 28 77 29 78 =head1 SYNOPSIS 30 79 80 default: in code = utf8, out code = utf8 81 31 82 my $mw = HTTP::Engine::Middleware->new; 32 $mw->install(qw/ HTTP::Engine::Middleware::Encode /); 83 $mw->install( 84 'HTTP::Engine::Middleware::Encode', 85 ); 86 HTTP::Engine->new( 87 interface => { 88 module => 'YourFavoriteInterfaceHere', 89 request_handler => $mw->handler( \&handler ), 90 } 91 )->run(); 92 93 in code = cp932, out code = cp932 (Shift-JIS) 94 95 my $mw = HTTP::Engine::Middleware->new; 96 $mw->install( 97 'HTTP::Engine::Middleware::Encode' => { 98 decode => 'cp932', 99 decode => 'cp932', 100 content_type_charset => 'Shift-JIS', 101 }, 102 ); 103 HTTP::Engine->new( 104 interface => { 105 module => 'YourFavoriteInterfaceHere', 106 request_handler => $mw->handler( \&handler ), 107 } 108 )->run(); 109 110 111 in code = detect by Content-Type header (default encoding is utf8), out code = utf8 112 113 my $mw = HTTP::Engine::Middleware->new; 114 $mw->install( 115 'HTTP::Engine::Middleware::Encode' => { 116 detected_decode_by_header => 1, 117 decode => 'utf8', 118 }, 119 ); 33 120 HTTP::Engine->new( 34 121 interface => { -
lang/perl/HTTP-Engine-Middleware/trunk/t/200_middlewares/encode.t
r29321 r29976 12 12 eval q{ use HTTP::Engine::Middleware }; 13 13 14 plan tests => 3* blocks;14 plan tests => 5 * blocks; 15 15 16 16 use Encode; 17 17 use URI; 18 18 19 filters { params => [qw/eval/], };19 filters { params => [qw/eval/], config => [qw/eval/] }; 20 20 21 21 run { 22 22 my $block = shift; 23 23 my $config = $block->config || +{}; 24 24 my $mw = HTTP::Engine::Middleware->new; 25 $mw->install( 'HTTP::Engine::Middleware::Encode' ,);25 $mw->install( 'HTTP::Engine::Middleware::Encode' => $config ); 26 26 27 27 my $request = HTTP::Request->new( … … 32 32 my $do_test = sub { 33 33 my $req = shift; 34 ok Encode::is_utf8( $req->params->{'nite'} ) ;35 is_deeply $req->params, $block->params, $block->name ;36 HTTP::Engine::Response->new( body => 'OK!');34 ok Encode::is_utf8( $req->params->{'nite'} ), 'params is utf8'; 35 is_deeply $req->params, $block->params, $block->name . ' params'; 36 HTTP::Engine::Response->new( body => decode('utf8', 'OKです!') ); 37 37 }; 38 38 … … 44 44 )->run($request); 45 45 46 is $response->content, 'OK!'; 46 my $content = $response->content; 47 ok !Encode::is_utf8( $content ), 'not utf8'; 48 $content = encode('utf8', decode($config->{encode}, $content)) if $config->{encode}; 49 is $content, 'OKです!', 'content'; 50 51 if ($config->{encode}) { 52 my $encode = $config->{content_type_charset} || $config->{encode}; 53 like $response->header('content-type'), qr/$encode/, 'content-type charset ' . $encode; 54 } else { 55 like $response->header('content-type'), qr/utf-?8/, 'content-type charset'; 56 } 47 57 }; 48 58 49 59 __END__ 50 60 51 === ascii52 --- uri: http://localhost/?nite= nipotan53 --- content_type : text/plain;charset=ascii54 --- params : {nite => 'nipotan'}61 === default 62 --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 63 --- content_type : text/plain 64 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 55 65 56 === utf-866 === header utf-8 57 67 --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 58 68 --- content_type : text/plain; charset=utf-8 59 69 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 70 --- config: { detected_decode_by_header => 1 } 60 71 61 === euc-jp 72 === header ascii 73 --- uri: http://localhost/?nite=nipotan 74 --- content_type: text/plain; charset=ascii 75 --- params : {nite => 'nipotan'} 76 --- config: { detected_decode_by_header => 1 } 77 78 === header euc-jp 62 79 --- uri: http://localhost/?nite=%A4%B7%A1%BC%A4%A8%A4%B9%A4%A8%A4%B9 63 80 --- content_type: text/plain; charset=euc-jp 64 81 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 82 --- config: { detected_decode_by_header => 1 } 65 83 84 === config utf-8 85 --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 86 --- content_type : text/plain 87 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 88 --- config: { decode => 'utf-8' } 89 90 === config ascii 91 --- uri: http://localhost/?nite=nipotan 92 --- content_type: text/plain 93 --- params : {nite => 'nipotan'} 94 --- config: { decode => 'ascii' } 95 96 === config euc-jp 97 --- uri: http://localhost/?nite=%A4%B7%A1%BC%A4%A8%A4%B9%A4%A8%A4%B9 98 --- content_type: text/plain 99 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 100 --- config: { decode => 'euc-jp' } 101 102 === encode utf-8 103 --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 104 --- content_type : text/plain 105 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 106 --- config: { encode => 'utf-8' } 107 108 === encode ascii 109 --- uri: http://localhost/?nite=nipotan 110 --- content_type: text/plain 111 --- params : {nite => 'nipotan'} 112 --- config: { encode => 'cp932', content_type_charset => 'Shift-JIS' } 113 114 === encode euc-jp 115 --- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 116 --- content_type : text/plain 117 --- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 118 --- config: { encode => 'euc-jp' } 119
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)