| 1 | package HTTP::Engine::Middleware::Encode; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use HTTP::Engine::Middleware; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Data::Visitor::Encode; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Encode (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | has 'detected_decode_by_header' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 8 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | default => 0, | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | has 'decode' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||
| 16 | default => 'utf-8', | ||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | has 'encode' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | default => 'utf-8', | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 1 | has 'content_type_charset' => ( | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | before_handle { | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 13 | ▶ | my ( $c, $self, $req ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=22.7
| |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 13 | my $encoding = $self->decode; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 13 | ▶ | if ($self->detected_decode_by_header) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if (( $req->headers->header('content-type') || '' ) =~ /charset\s*=\s*([^\s]+);?$/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$req->headers->header('content-type') || ''
MC/DC0%0/1
$req->headers->header('content-type') || ''
$req->headers->header('content-type')
Truth table0%0/2
A
A= $req->headers->header('content-type')
| |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $encoding = $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # decode parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 13 | for my $method (qw/params query_params body_params/) { | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | 13 | $req->$method( Data::Visitor::Encode->decode( $encoding, $req->$method ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | $req; | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | after_handle { | ||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my ( $c, $self, $req, $res ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $body = $res->body; | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | return $res unless $body; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | if ((Scalar::Util::blessed($body) && $body->can('read')) || (ref($body) eq 'GLOB')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
builtin::blessed($body) and $body->can("read") or ref $body eq "GLOB"
builtin::blessed($body) and $body->can("read")
MC/DC0%0/3
builtin::blessed($body) and $body->can("read") or ref $body eq "GLOB"
builtin::blessed($body)
$body->can("read")
ref $body eq "GLOB"
Truth table0%0/5
A and B or C
A= builtin::blessed($body)B= $body->can("read")C= ref $body eq "GLOB"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | if (Encode::is_utf8( $body )) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $encoding = $self->encode; | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $res->body( Encode::encode( $encoding, $body ) ); | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | my $content_type = $res->content_type || 'text/html'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$res->content_type || 'text/html'
MC/DC0%0/1
$res->content_type || 'text/html'
$res->content_type
Truth table0%0/2
A
A= $res->content_type
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ($content_type =~ m!^text/!) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | $encoding = $self->content_type_charset if $self->content_type_charset; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ($content_type =~ s/charset\s*=\s*[^\s]*;?/charset=$encoding/ ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $content_type .= '; ' unless $content_type =~ /;\s*$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | $content_type .= "charset=$encoding"; | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $res->content_type( $content_type ); | |||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $res; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | __MIDDLEWARE__ | |||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | HTTP::Engine::Middleware::Encode - Encoding Filter | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | default: in code = utf8, out code = utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | my $mw = HTTP::Engine::Middleware->new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | $mw->install( | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 'HTTP::Engine::Middleware::Encode', | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | HTTP::Engine->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | interface => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 91 | module => 'YourFavoriteInterfaceHere', | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | request_handler => $mw->handler( \&handler ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | )->run(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||
| 96 | in code = cp932, out code = cp932 (Shift-JIS) | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||
| 98 | my $mw = HTTP::Engine::Middleware->new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | $mw->install( | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 'HTTP::Engine::Middleware::Encode' => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | decode => 'cp932', | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | decode => 'cp932', | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | content_type_charset => 'Shift-JIS', | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | HTTP::Engine->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | interface => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | module => 'YourFavoriteInterfaceHere', | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | request_handler => $mw->handler( \&handler ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | )->run(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | in code = detect by Content-Type header (default encoding is utf8), out code = utf8 | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | my $mw = HTTP::Engine::Middleware->new; | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | $mw->install( | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 'HTTP::Engine::Middleware::Encode' => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | detected_decode_by_header => 1, | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | decode => 'utf8', | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 123 | HTTP::Engine->new( | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | interface => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | module => 'YourFavoriteInterfaceHere', | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | request_handler => $mw->handler( \&handler ), | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | )->run(); | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =head1 AUTHORS | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | precuredaisuki | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | yappo | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||||
| 138 | L<Data::Visitor::Encode> | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||