Changeset 29976

Show
Ignore:
Timestamp:
02/12/09 23:07:06 (4 years ago)
Author:
yappo
Message:

mergeed Encode/Unicode to Encode

Location:
lang/perl/HTTP-Engine-Middleware/trunk
Files:
2 removed
2 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/HTTP-Engine-Middleware/trunk/lib/HTTP/Engine/Middleware/Encode.pm

    r29830 r29976  
    22use HTTP::Engine::Middleware; 
    33use Data::Visitor::Encode; 
     4use Encode (); 
     5use Scalar::Util (); 
     6 
     7has 'detected_decode_by_header' => ( 
     8    is      => 'ro', 
     9    isa     => 'Bool', 
     10    default => 0, 
     11); 
     12 
     13has 'decode' => ( 
     14    is      => 'rw', 
     15    isa     => 'Str', 
     16    default => 'utf-8', 
     17); 
     18 
     19has 'encode' => ( 
     20    is      => 'ro', 
     21    isa     => 'Str', 
     22    default => 'utf-8', 
     23); 
     24 
     25has 'content_type_charset' => ( 
     26    is      => 'ro', 
     27    isa     => 'Str', 
     28); 
    429 
    530before_handle { 
    631    my ( $c, $self, $req ) = @_; 
    732 
    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; 
    1637        } 
    1738    } 
     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    } 
    1844    $req; 
     45}; 
     46 
     47after_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; 
    1968}; 
    2069 
     
    2574=head1 NAME 
    2675 
    27 HTTP::Engine::Middleware::Encode - documentation is TODO 
     76HTTP::Engine::Middleware::Encode - Encoding Filter 
    2877 
    2978=head1 SYNOPSIS 
    3079 
     80default: in code = utf8, out code = utf8 
     81 
    3182    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 
     93in 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 
     111in 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    ); 
    33120    HTTP::Engine->new( 
    34121        interface => { 
  • lang/perl/HTTP-Engine-Middleware/trunk/t/200_middlewares/encode.t

    r29321 r29976  
    1212eval q{ use HTTP::Engine::Middleware }; 
    1313 
    14 plan tests => 3 * blocks; 
     14plan tests => 5 * blocks; 
    1515 
    1616use Encode; 
    1717use URI; 
    1818 
    19 filters { params => [qw/eval/], }; 
     19filters { params => [qw/eval/], config => [qw/eval/] }; 
    2020 
    2121run { 
    2222    my $block = shift; 
    23  
     23    my $config = $block->config || +{}; 
    2424    my $mw = HTTP::Engine::Middleware->new; 
    25     $mw->install( 'HTTP::Engine::Middleware::Encode', ); 
     25    $mw->install( 'HTTP::Engine::Middleware::Encode' => $config ); 
    2626 
    2727    my $request = HTTP::Request->new( 
     
    3232    my $do_test = sub { 
    3333        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です!') ); 
    3737    }; 
    3838 
     
    4444    )->run($request); 
    4545 
    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   } 
    4757}; 
    4858 
    4959__END__ 
    5060 
    51 === ascii 
    52 --- uri: http://localhost/?nite=nipotan 
    53 --- content_type: text/plain;charset=ascii 
    54 --- 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}" } 
    5565 
    56 === utf-8 
     66=== header utf-8 
    5767--- uri: http://localhost/?nite=%E3%81%97%E3%83%BC%E3%81%88%E3%81%99%E3%81%88%E3%81%99 
    5868--- content_type : text/plain; charset=utf-8 
    5969--- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 
     70--- config: { detected_decode_by_header => 1 } 
    6071 
    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 
    6279--- uri: http://localhost/?nite=%A4%B7%A1%BC%A4%A8%A4%B9%A4%A8%A4%B9 
    6380--- content_type: text/plain; charset=euc-jp 
    6481--- params: { nite => "\x{3057}\x{30fc}\x{3048}\x{3059}\x{3048}\x{3059}" } 
     82--- config: { detected_decode_by_header => 1 } 
    6583 
     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