| 1 | # Copyrights 2007-2021 by [Mark Overmeer <markov@cpan.org>]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of distribution XML-Compile-SOAP. Meta-POD processed | ||
| 6 | # with OODoc into POD and HTML manual-pages. See README.md | ||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||
| 8 | |||
| 9 | package XML::Compile::XOP::Include; | ||
| 10 | use vars '$VERSION'; | ||
| 11 | $VERSION = '3.27'; | ||
| 12 | |||
| 13 | |||
| 14 | use warnings; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | use Log::Report 'xml-compile-soap'; | ||
| 18 | use XML::Compile::SOAP::Util qw/:xop10/; | ||
| 19 | use HTTP::Message (); | ||
| 20 | use File::Slurper qw/read_binary write_binary/; | ||
| 21 | use Encode qw/decode FB_CROAK/; | ||
| 22 | |||
| 23 | |||
| 24 | use overload '""' => 'content' | ||
| 25 | , fallback => 1; | ||
| 26 | |||
| 27 | |||
| 28 | sub new(@) | ||
| 29 | { my ($class, %args) = @_; | ||
| 30 | $args{bytes} = \(delete $args{bytes}) | ||
| 31 | if defined $args{bytes} && ref $args{bytes} ne 'SCALAR'; | ||
| 32 | bless \%args, $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub fromMime($) | ||
| 37 | { my ($class, $http) = @_; | ||
| 38 | |||
| 39 | my $cid = $http->header('Content-ID') || '<NONE>'; | ||
| 40 | if($cid !~ s/^\s*\<(.*?)\>\s*$/$1/ ) | ||
| 41 | { warning __x"part has illegal Content-ID: `{cid}'", cid => $cid; | ||
| 42 | return (); | ||
| 43 | } | ||
| 44 | |||
| 45 | my $content = $http->decoded_content(ref => 1) || $http->content(ref => 1); | ||
| 46 | $class->new | ||
| 47 | ( bytes => $content | ||
| 48 | , cid => $cid | ||
| 49 | , type => scalar $http->content_type | ||
| 50 | , charset => scalar $http->content_type_charset | ||
| 51 | ); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | sub cid { shift->{cid} } | ||
| 56 | |||
| 57 | |||
| 58 | sub content(;$) | ||
| 59 | { my ($self, $byref) = @_; | ||
| 60 | unless($self->{bytes}) | ||
| 61 | { my $f = $self->{file}; | ||
| 62 | my $bytes = try { read_binary $f }; | ||
| 63 | fault "failed reading XOP file {fn}", fn => $f if $@; | ||
| 64 | $self->{bytes} = \$bytes; | ||
| 65 | } | ||
| 66 | $byref ? $self->{bytes} : ${$self->{bytes}}; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub string() { | ||
| 71 | my $self = shift; | ||
| 72 | my $cs = $self->contentCharset || 'UTF-8'; | ||
| 73 | decode $cs, $self->content, FB_CROAK; | ||
| 74 | } | ||
| 75 | |||
| 76 | |||
| 77 | sub contentType() { shift->{type} } | ||
| 78 | sub contentCharset() { shift->{charset} } | ||
| 79 | |||
| 80 | #--------- | ||
| 81 | |||
| 82 | sub xmlNode($$$$) | ||
| 83 | { my ($self, $doc, $path, $tag) = @_; | ||
| 84 | my $node = $doc->createElement($tag); | ||
| 85 | $node->setNamespace($self->{xmime}, 'xmime', 0); | ||
| 86 | $node->setAttributeNS($self->{xmime}, contentType => $self->{type}); | ||
| 87 | |||
| 88 | my $include = $node->addChild($doc->createElement('Include')); | ||
| 89 | $include->setNamespace($self->{xop}, 'xop', 1); | ||
| 90 | $include->setAttribute(href => 'cid:'.$self->{cid}); | ||
| 91 | $node; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub mimePart(;$) | ||
| 96 | { my ($self, $headers) = @_; | ||
| 97 | my $mime = HTTP::Message->new($headers); | ||
| 98 | $mime->header | ||
| 99 | ( Content_Type => $self->{type} | ||
| 100 | , Content_Transfer_Encoding => 'binary' | ||
| 101 | , Content_ID => '<'.$self->{cid}.'>' | ||
| 102 | ); | ||
| 103 | |||
| 104 | $mime->content_ref($self->content(1)); | ||
| 105 | $mime; | ||
| 106 | } | ||
| 107 | |||
| 108 | |||
| 109 | sub write($) | ||
| 110 | { my ($self, $file) = @_; | ||
| 111 | write_binary $file, $self->content(1); | ||
| 112 | } | ||
| 113 | |||
| 114 | 1; |