Skip to content
Add encoding support to Poet/Mason apps
Perl Perl 6
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib/Mason/Plugin
t
xt
.gitignore
Changes
LICENSE
Makefile.PL
README.md
cpanfile
dist.ini
perlcritic.rc
weaver.ini

README.md

NAME

Mason::Plugin::WithEncoding - Add encoding support to Poet/Mason apps

VERSION

version 0.2

SYNOPSIS

In your Poet config file:

    # This is important or the first (and *only* the first) request will
    # not use Plack::Request::WithEncoding
    server.load_modules:
        - Mason::Plugin::WithEncoding

    mason:
      extra_plugins:
        - WithEncoding

    server.encoding.request: UTF-8
    server.encoding.response: UTF-8
    server.default_content_type: text/html; charset=UTF-8

DESCRIPTION

To decode the request, the plugin loads Plack::Request::WithEncoding and configures it with the encoding specified in server.encoding.request. If no such setting is found, the request is not decoded. This matches the default behaviour of Poet/Mason, but is different from Plack::Request::WithEncoding, (which would normally default to UTF-8).

Output generated by your Mason templates is encoded according to the setting in server.encoding.response. If no such setting is found, the response is not encoded. This is the default behaviour of Poet/Mason.

The content-type default header is set according to server.default_content_type. If no such setting is found, the default header is text/html, as in the default Poet/Mason setup. This tells the client what encoding to use when decoding our content, and also (AFAIK) tells the client what encoding to use when sending us data.

Output sent through send_json is also encoded and the content-type header set accordingly.

Caveats

This plugin only works inside a Poet environment.

The send_json method added to the Mason request object by Poet always encodes its output as UTF8, which is pretty much always the correct thing to do. But if you are using some other encoding, you'll need to patch this plugin to get send_json to use that instead of UTF8.

Some background

http://stackoverflow.com/questions/27806684/mason2-wrong-utf8-encoding-with-the-go-method
http://stackoverflow.com/questions/5858596/how-to-make-mason2-utf-8-clean
https://www.mail-archive.com/mason-users@lists.sourceforge.net/msg03450.html

AUTHOR

Dave Baird <dave@zerofive.co.uk>

COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by David R. Baird.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.

You can’t perform that action at this time.