X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FEml.pm;h=2ccbb6597de406680376ad9a02a419ee53d1015b;hb=1dde99e0badebfaf509cee4f15bbfa284996c8fc;hp=1adaff04f7be871d1ab9343c4bb737436b603d09;hpb=d35a49e114579a4df7bcb23d013ef0101cbb3ec2;p=public-inbox.git diff --git a/lib/PublicInbox/Eml.pm b/lib/PublicInbox/Eml.pm index 1adaff04..2ccbb659 100644 --- a/lib/PublicInbox/Eml.pm +++ b/lib/PublicInbox/Eml.pm @@ -41,6 +41,7 @@ $PublicInbox::EmlContentFoo::STRICT_PARAMS = 0; our $MAXPARTS = 1000; # same as SpamAssassin our $MAXDEPTH = 20; # seems enough, Perl sucks, here our $MAXBOUNDLEN = 2048; # same as postfix +our $header_size_limit = 102400; # same as postfix my %MIME_ENC = (qp => \&enc_qp, base64 => \&encode_base64); my %MIME_DEC = (qp => \&dec_qp, base64 => \&decode_base64); @@ -68,19 +69,47 @@ sub re_memo ($) { /ismx } +sub hdr_truncate ($) { + my $len = length($_[0]); + substr($_[0], $header_size_limit, $len) = ''; + my $end = rindex($_[0], "\n"); + if ($end >= 0) { + ++$end; + substr($_[0], $end, $len) = ''; + warn "header of $len bytes truncated to $end bytes\n"; + } else { + $_[0] = ''; + warn <= 0) { + # likely on *nix + my $hdr = substr($$ref, 0, $pos + 2, ''); # sv_chop on $$ref + chop($hdr); # lower SvCUR + hdr_truncate($hdr) if length($hdr) > $header_size_limit; + bless { hdr => \$hdr, crlf => "\n", bdy => $ref }, __PACKAGE__; + } elsif ($$ref =~ /\r?\n(\r?\n)/s) { + my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref substr($hdr, -(length($1))) = ''; # lower SvCUR + hdr_truncate($hdr) if length($hdr) > $header_size_limit; bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__; } elsif ($$ref =~ /^[a-z0-9-]+[ \t]*:/ims && $$ref =~ /(\r?\n)\z/s) { # body is optional :P - bless { hdr => \($$ref), crlf => $1 }, __PACKAGE__; + 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__; @@ -90,8 +119,8 @@ sub new { sub new_sub { my (undef, $ref) = @_; # special case for messages like <85k5su9k59.fsf_-_@lola.goethe.zz> - $$ref =~ /\A(?:(\r?\n))/gs or goto &new; - my $hdr = substr($$ref, 0, pos($$ref), ''); # sv_chop on $$ref + $$ref =~ /\A(\r?\n)/s or goto &new; + my $hdr = substr($$ref, 0, $+[0], ''); # sv_chop on $$ref bless { hdr => \$hdr, crlf => $1, bdy => $ref }, __PACKAGE__; } @@ -131,8 +160,12 @@ sub mp_descend ($$) { # Cut at the the first epilogue, not subsequent ones. # *sigh* just the regexp match alone seems to bump RSS by # length($$bdy) on a ~30M string: - $$bdy =~ /((?:\r?\n)?^--$bnd--[ \t]*\r?$)/gsm and - substr($$bdy, pos($$bdy) - length($1)) = ''; + my $epilogue_missing; + if ($$bdy =~ /(?:\r?\n)?^--$bnd--[ \t]*\r?$/sm) { + substr($$bdy, $-[0]) = ''; + } else { + $epilogue_missing = 1; + } # *Sigh* split() doesn't work in-place and return CoW strings # because Perl wants to "\0"-terminate strings. So split() @@ -150,6 +183,10 @@ sub mp_descend ($$) { if (@parts) { # the usual path if we got this far: undef $bdy; # release memory ASAP if $nr > 0 + + # compatibility with Email::MIME + $parts[-1] =~ s/\n\r?\n\z/\n/s if $epilogue_missing; + @parts = grep /[^ \t\r\n]/s, @parts; # ignore empty parts # Keep "From: someone..." from preamble in old,