]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Eml.pm
eml: keep body if no headers are found
[public-inbox.git] / lib / PublicInbox / Eml.pm
index 69c26932f65162d76696a067c69243ef2f90e224..485f637a3e7b41a9117cb37a9acfd8e7649dadfa 100644 (file)
@@ -122,9 +122,10 @@ sub new {
                my $hdr = substr($$ref, 0, $header_size_limit + 1);
                hdr_truncate($hdr) if length($hdr) > $header_size_limit;
                bless { hdr => \$hdr, crlf => $1 }, __PACKAGE__;
-       } else { # nothing useful
-               my $hdr = $$ref = '';
-               bless { hdr => \$hdr, crlf => "\n" }, __PACKAGE__;
+       } else { # just a body w/o header?
+               my $hdr = '';
+               my $eol = ($$ref =~ /(\r?\n)/) ? $1 : "\n";
+               bless { hdr => \$hdr, crlf => $eol, bdy => $ref }, __PACKAGE__;
        }
 }
 
@@ -333,15 +334,18 @@ sub body_set {
 }
 
 sub body_str_set {
-       my ($self, $body_str) = @_;
+       my ($self, $str) = @_;
        my $cs = ct($self)->{attributes}->{charset} //
                croak('body_str was given, but no charset is defined');
        my $enc = find_encoding($cs) // croak "unknown encoding `$cs'";
-       $body_str = do {
-               local $SIG{__WARN__} = \&croak;
-               $enc->encode($body_str, Encode::FB_WARN);
+       my $tmp;
+       {
+               my @w;
+               local $SIG{__WARN__} = sub { push @w, @_ };
+               $tmp = $enc->encode($str, Encode::FB_WARN);
+               croak(@w) if @w;
        };
-       body_set($self, \$body_str);
+       body_set($self, \$tmp);
 }
 
 sub content_type { scalar header($_[0], 'Content-Type') }
@@ -466,8 +470,11 @@ sub body_str {
        my $enc = find_encoding($cs) or croak "unknown encoding `$cs'";
        my $tmp = body($self);
        # workaround https://rt.cpan.org/Public/Bug/Display.html?id=139622
-       local $SIG{__WARN__} = \&croak;
-       $enc->decode($tmp, Encode::FB_WARN);
+       my @w;
+       local $SIG{__WARN__} = sub { push @w, @_ };
+       my $ret = $enc->decode($tmp, Encode::FB_WARN);
+       croak(@w) if @w;
+       $ret;
 }
 
 sub as_string {