| 1 | package Dancer2::Serializer::YAML; | ||
| 2 | # ABSTRACT: Serializer for handling YAML data | ||
| 3 | 1 | $Dancer2::Serializer::YAML::VERSION = '2.3.0'; | |
| 4 | 9 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 9 | ▶ | use Carp 'croak'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 9 | ▶ | use Encode; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 9 | ▶ | use Module::Runtime 'use_module'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 9 | ▶ | use Sub::Defer; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | with 'Dancer2::Core::Role::Serializer'; | |
| 11 | |||
| 12 | 35 | ▶ | has '+content_type' => ( default => sub {'text/x-yaml'} ); |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | # deferred helpers. These are called as class methods, but need to | ||
| 15 | # ensure YAML is loaded. | ||
| 16 | # | ||
| 17 | # They run outside a Dancer2 app, so there is no app logger for the engine to | ||
| 18 | # send diagnostics to, and a failed parse would otherwise return undef and say | ||
| 19 | # nothing at all. Warn instead, so the caller still gets to see what went | ||
| 20 | # wrong. | ||
| 21 | 1 | ▶ | sub _warn_log_cb { |
|
Subroutine
_warn_log_cb: called CC=1 SCAR=0.0
| |||
| 22 | 1 | my ( $level, $message ) = @_; | |
| 23 | |||
| 24 | # The message is whatever the parser left in $@, and whether it ends in a | ||
| 25 | # newline varies by backend, so strip the line ending it came with and | ||
| 26 | # supply exactly one: no blank line after a message that already ended in | ||
| 27 | # one, and no "at <this file> line <n>" appended by warn to one that did not. | ||
| 28 | 1 | $message =~ s/\r?\n\z//; | |
| 29 | 1 | warn "$message\n"; | |
| 30 | 1 | return 1; | |
| 31 | } | ||
| 32 | |||
| 33 | 5 | ▶ | my $_from_yaml = defer_sub 'Dancer2::Serializer::YAML::from_yaml' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 34 | 5 | use_module('YAML'); | |
| 35 | 5 | ▶ | sub { __PACKAGE__->new( log_cb => \&_warn_log_cb )->deserialize(@_) }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 36 | 1 | }; | |
| 37 | |||
| 38 | 5 | ▶ | my $_to_yaml = defer_sub 'Dancer2::Serializer::YAML::to_yaml' => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 39 | 5 | use_module('YAML'); | |
| 40 | 5 | ▶ | sub { __PACKAGE__->new( log_cb => \&_warn_log_cb )->serialize(@_) }; |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 41 | 1 | }; | |
| 42 | |||
| 43 | # class definition | ||
| 44 | 35 | ▶ | sub BUILD { use_module('YAML') } |
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
undocumented
| |||
| 45 | |||
| 46 | 28 | ▶ | sub serialize { |
|
Subroutine
serialize: called CC=1 SCAR=0.0
Pod
documented
| |||
| 47 | 28 | my ( $self, $entity ) = @_; | |
| 48 | 28 | encode('UTF-8', YAML::Dump($entity)); | |
| 49 | } | ||
| 50 | |||
| 51 | 19 | ▶ | sub deserialize { |
|
Subroutine
deserialize: called CC=1 SCAR=0.0
Pod
documented
| |||
| 52 | 20 | my ( $self, $content ) = @_; | |
| 53 | |||
| 54 | # Content reaching here is untrusted -- for an app with 'serializer: YAML' | ||
| 55 | # (or Serializer::Mutable, which maps both text/x-yaml and text/html to | ||
| 56 | # this class) it is the raw request body. | ||
| 57 | # | ||
| 58 | # YAML tags can ask the loader to build things that are not data. | ||
| 59 | # !!perl/hash:Some::Class instantiates an arbitrary blessed object, which | ||
| 60 | # is the entry point for DESTROY/AUTOLOAD gadget chains, and !!perl/code | ||
| 61 | # asks for a string eval. Both are refused here. | ||
| 62 | # | ||
| 63 | # These are set explicitly rather than left to YAML.pm's defaults so the | ||
| 64 | # behaviour does not depend on which YAML.pm the user resolved: LoadBlessed | ||
| 65 | # only defaults to 0 from YAML 1.30, and the variable itself only exists | ||
| 66 | # from 1.25 (which is why cpanfile floors YAML -- see the note there). | ||
| 67 | # | ||
| 68 | # UseCode must be zeroed as well: YAML::Loader::Base decides on code | ||
| 69 | # loading with a plain OR -- load_code($YAML::LoadCode || $YAML::UseCode) -- | ||
| 70 | # so LoadCode = 0 alone is defeated by an ambient $YAML::UseCode = 1, and | ||
| 71 | # the !!perl/code string eval is not gated on LoadBlessed at all. | ||
| 72 | 20 | local $YAML::LoadBlessed = 0; | |
| 73 | 20 | local $YAML::LoadCode = 0; | |
| 74 | 20 | local $YAML::UseCode = 0; | |
| 75 | |||
| 76 | 20 | YAML::Load(decode('UTF-8', $content)); | |
| 77 | } | ||
| 78 | |||
| 79 | 1 | 1; | |
| 80 | |||
| 81 | __END__ | ||