]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Eml.pm
eml: header_raw converts octets to Perl UTF-8
[public-inbox.git] / lib / PublicInbox / Eml.pm
index 3c681ba5bc2eb6efc6b0252b67a26ebd2f3b6904..8b999e1a88ef8d11b78e7c5bd0c022dbd7d0f7be 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Lazy MIME parser, it still slurps the full message but keeps short
@@ -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__;
        }
 }
 
@@ -143,6 +144,7 @@ sub header_raw {
        my $re = re_memo($_[1]);
        my @v = (${ $_[0]->{hdr} } =~ /$re/g);
        for (@v) {
+               utf8::decode($_); # SMTPUTF8
                # for compatibility w/ Email::Simple::Header,
                s/\s+\z//s;
                s/\A\s+//s;
@@ -358,14 +360,15 @@ sub header_set {
        $pfx .= ': ';
        my $len = 78 - length($pfx);
        @vals = map {;
+               utf8::encode(my $v = $_); # to bytes, support SMTPUTF8
                # folding differs from Email::Simple::Header,
                # we favor tabs for visibility (and space savings :P)
                if (length($_) >= $len && (/\n[^ \t]/s || !/\n/s)) {
                        local $Text::Wrap::columns = $len;
                        local $Text::Wrap::huge = 'overflow';
-                       $pfx . wrap('', "\t", $_) . $self->{crlf};
+                       $pfx . wrap('', "\t", $v) . $self->{crlf};
                } else {
-                       $pfx . $_ . $self->{crlf};
+                       $pfx . $v . $self->{crlf};
                }
        } @vals;
        $$hdr =~ s!$re!shift(@vals) // ''!ge; # replace current headers, first