Mason2 incorrectly encodes utf8 with the "go" method

Bit long question because AFAIK Poet / Mason2 is not a very commonly used framework, so I am trying to be detailed.

Two years ago I asked the question how to make Mason2 utf8 clean. As far as I know, little is known here in Mason / Poet in this area - and unfortunately I am facing another problem today . Simple test case:

$ poet new my   #create new poet application
$ cd my

      

Override some methods to use utf8 in components:

Add to ./lib/My/Mason/Compilation.pm

override 'output_class_header' => sub {
    return join("\n",
        super(), qq(
            use utf8;
            use Encode qw(encode decode);
        )
    );
};

      

The above adds to each compiled Mason component use utf8...

.

It also needs to encode the output from Mason (bytes required), so in: ./lib/My/Mason/Request.pm

override 'run' => sub {
    my($self, $path, $args) = @_;
    my $result = super();
    $result->output( Encode::encode('UTF-8', $result->output()) );
    return $result;
};

      

Now you can create such a component page.mc

, for example, with content:

% sub { uc($_[0]) } {{
a quick brown fox jumps over the lazy dog.
διαφυλάξτε γενικά τη ζωή σας από βαθειά ψυχικά τραύματα.
árvíztűrő tükörfúrógép.
dość gróźb fuzją, klnę, pych i małżeństw!
, ,     () – !
kŕdeľ šťastných ďatľov učí pri ústí váhu mĺkveho koňa obhrýzať kôru a žrať čerstvé mäso.
zwölf boxkämpfer jagen viktor quer über den großen sylter deich.
% }}

      

After launching the poet app, bin/run.pl

you can go to: http://0:5000/page

and get the correct content.

QUICK VIEW LEXY ON THE CLIMBING DOG. ΔΙΑΦΥΛΆΞΤΕ ΓΕΝΙΚΆ ΤΗ ΖΩΉ ΣΑΣ ΑΠΌ ΒΑΘΕΙΆ ΨΥΧΙΚΆ ΤΡΑΎΜΑΤΑ. ÁRVÍZTŰRŐ TÜKÖRFÚRÓGÉP. DOŚĆ GRÓŹB FUZJĄ, KLNĘ, PYCH I MAŁŻEŃSTW! EH, STRANGER, GENERAL HAT PRICES (YUFT) - SPLASH! KŔDEĽ ŠŤASTNÝCH ĎATĽOV UČÍ PRI ÚSTÍ VÁHU MĹKVEHO KOŇA OBHRÝZAŤ KÔRU A ŽRAŤ ČERSTVÉ MÄSO. ZWÖLF BOXKÄMPFER JAGEN VIKTOR QUER ÜBER DEN GROSSEN SYLTER DEICH.

But when creating another component let's say go.mc

with content

% $m->go('/page');

      

internal redirection (method go

) messes up the content somewhat and will produce:

QUICK VIEW LEXY ON THE CLIMBING DOG. ÎÎÎÎ | Î ¥ ÎÎÎΤΠΤΡÎÎÎÎΤÎ. ÃRVÃZTÅ ° RÅ TÃKÃRFÃRÃGÃP. DON'T FUZJÄ, KLNÄ, PYCH I MAY "EESTW! OO, OO, OO, OO, OO, OO, OO, OO, OO, OO, O, O, O, O, O, O, O, O, O, O, O O OO, OO, O, O ABOUT KÅDEĽ ŠŤASTNÃCH ÄATĽOV UÄà PRI ÃSTà VÃHU MĹKVEHO KOÅA OBHRÃZAŤ KÃRU A ŽRAŤ ÄERSTVà MÃSO. ZWÃMPLFER BOXKMS

Strange, $m->visit()

works correctly. So somewhere in Poet / Mason something needs to be done to get the correct output for the method go

.

Can anyone please help?

+3


source to share


1 answer


I've been working on a plugin for Mason to handle encoding. $result->output

is the wrong place to encode the output, because it visit

will run the subquery, encoding its own content at the end, before returning to the original component, which will then recode everything when it is complete. Thus, the content visit

is encoded twice. I'm surprised you're having a problem with go

, because this discards all previous content and starts up again, which should be fine.



Have a look at https://github.com/davebaird/mason-plugin-withencoding

+2


source







All Articles