| 1 | package Mason::Plugin::WithEncoding; | ||||||||||
| 2 | 1 | $Mason::Plugin::WithEncoding::VERSION = '0.2'; | |||||||||
| 3 | |||||||||||
| 4 | # ABSTRACT: Add encoding support to Poet/Mason apps | ||||||||||
| 5 | |||||||||||
| 6 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Poet qw($poet $conf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Mason 2.13 (); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | with 'Mason::Plugin'; | |||||||||
| 11 | |||||||||||
| 12 | |||||||||||
| 13 | # So in short: | ||||||||||
| 14 | # If someone is able write an UTF8 __PLUGIN__ for Mason2, need to do 5 things: | ||||||||||
| 15 | # | ||||||||||
| 16 | # 1.) add "use utf8;" into every obj. file (this should be done by | ||||||||||
| 17 | # plugin regardless of the next) | ||||||||||
| 18 | # 2.) allow adding additional pradmas into the obj source - this is done already! | ||||||||||
| 19 | # 3.) encode everything what going from Mason ---to---> Plack | ||||||||||
| 20 | # 4.) decode everything what coming from Pack --to--> Mason | ||||||||||
| 21 | # 5.) Add UTF8 safe FillInForm into Filters (and check other filters) | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | # This sidesteps Poet::Plack::Request, but - at the moment (Jan 2016) - that's | ||||||||||
| 25 | # just an empty subclass of Plack::Request | ||||||||||
| 26 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||
| 27 | 1 | my $app_name = $poet->app_name; | |||||||||
| 28 | 1 | my $enc = $conf->get('server.encoding.request', undef); | |||||||||
| 29 | |||||||||||
| 30 | 1 | ▶ | eval <<EVAL; ## no critic (ProhibitStringyEval) | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||
| 31 | package ${app_name}::Plack::Request; | ||||||||||
| 32 | use parent ('Plack::Request::WithEncoding', 'Poet::Plack::Request'); | ||||||||||
| 33 | |||||||||||
| 34 | sub new { | ||||||||||
| 35 | my \$self = shift->SUPER::new(\@_); | ||||||||||
| 36 | \$self->env->{'plack.request.withencoding.encoding'} = '$enc'; | ||||||||||
| 37 | return \$self; | ||||||||||
| 38 | } | ||||||||||
| 39 | EVAL | ||||||||||
| 40 | |||||||||||
| 41 | 1 | ▶ | die $@ if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 1 | 1; | |||||||||
| 45 | |||||||||||
| 46 | package Mason::Plugin::WithEncoding::Request; | ||||||||||
| 47 | 1 | $Mason::Plugin::WithEncoding::Request::VERSION = '0.2'; | |||||||||
| 48 | 1 | ▶ | use Mason::PluginRole; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 1 | ▶ | use Poet qw($conf); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 50 | 1 | ▶ | use Encode qw(encode decode); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 1 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 52 | |||||||||||
| 53 | 7 | ▶ | around 'run' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=15.4
| |||||||||||
| 54 | 7 | my $orig = shift; | |||||||||
| 55 | 7 | my $self = shift; | |||||||||
| 56 | |||||||||||
| 57 | 7 | my $enc = $conf->get( 'server.encoding.response' => undef ); | |||||||||
| 58 | |||||||||||
| 59 | 7 | ▶ | $self->res->content_type( $conf->get( 'server.default_content_type' => undef ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 60 | if !$self->res->content_type(); | ||||||||||
| 61 | |||||||||||
| 62 | 7 | my $result = $self->$orig(@_); # this call loads content into the Plack::Response object | |||||||||
| 63 | |||||||||||
| 64 | 0 | ▶ | if ($self->res->content_type ne "application/json") { # already UTF8 encoded | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 0 | my $content = $self->res->content; | |||||||||
| 66 | 0 | my $bytes = encode($enc, $content); | |||||||||
| 67 | 0 | $self->res->content($bytes); | |||||||||
| 68 | } | ||||||||||
| 69 | |||||||||||
| 70 | 0 | return $result; # will be discarded | |||||||||
| 71 | 1 | }; | |||||||||
| 72 | |||||||||||
| 73 | # we just need to update the content-type header | ||||||||||
| 74 | 0 | ▶ | around 'send_json' => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 75 | 0 | my $orig = shift; | |||||||||
| 76 | 0 | my $self = shift; | |||||||||
| 77 | 0 | my $data = shift; | |||||||||
| 78 | |||||||||||
| 79 | 0 | my $enc = $conf->get( 'server.encoding.response' => undef ); | |||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | # calls JSON::XS::encode_json($data), which encodes as UTF8 | ||||||||||
| 83 | 0 | $self->$orig($data); # aborts | |||||||||
| 84 | } | ||||||||||
| 85 | 0 | ▶ | catch { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 86 | 0 | my $err = $_; | |||||||||
| 87 | 0 | ▶ | if ($self->aborted($err)) { # expected | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 0 | ▶ | $self->res->content_type("application/json; charset=$enc") if $enc; # it's already set, but without charset | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 0 | $err->rethrow; | |||||||||
| 90 | } | ||||||||||
| 91 | else { | ||||||||||
| 92 | 0 | die $err; # unexpected | |||||||||
| 93 | } | ||||||||||
| 94 | 0 | }; | |||||||||
| 95 | 1 | }; | |||||||||
| 96 | |||||||||||
| 97 | 1 | 1; | |||||||||
| 98 | |||||||||||
| 99 | package Mason::Plugin::WithEncoding::Compilation; | ||||||||||
| 100 | 1 | $Mason::Plugin::WithEncoding::Compilation::VERSION = '0.2'; | |||||||||
| 101 | 1 | ▶ | use Mason::PluginRole; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 102 | |||||||||||
| 103 | # None of this is strictly required for the encode/decode cycle, but it's good stuff | ||||||||||
| 104 | # to have for the encoding-aware developer. Such as, for example, in the tests. | ||||||||||
| 105 | # Since the component source used in testing is stored as UTF8 encoded bytes, | ||||||||||
| 106 | # we need the 'use utf8' to be able to read them properly. | ||||||||||
| 107 | |||||||||||
| 108 | 6 | ▶ | around 'output_class_header' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 109 | 6 | my $orig = shift; | |||||||||
| 110 | 6 | my $self = shift; | |||||||||
| 111 | |||||||||||
| 112 | 6 | return join "\n", $self->$orig, | |||||||||
| 113 | q/use utf8; | ||||||||||
| 114 | use 5.012; | ||||||||||
| 115 | use encoding::warnings; | ||||||||||
| 116 | use Encode qw(encode decode); | ||||||||||
| 117 | /; | ||||||||||
| 118 | 1 | }; | |||||||||
| 119 | |||||||||||
| 120 | 1 | 1; | |||||||||
| 121 | |||||||||||
| 122 | __END__ | ||||||||||