]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/EmlContentFoo.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / EmlContentFoo.pm
index f507d548898269034a0fc16b11c08a8552b6446b..80fc7364fc9a1943759585c30487ab0c5d36ca0c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2020-2021 all contributors <meta@public-inbox.org>
 # Copyright (C) 2004- Simon Cozens, Casey West, Ricardo SIGNES
 # This library is free software; you can redistribute it and/or modify
 # it under the same terms as Perl itself.
@@ -9,15 +9,38 @@
 #
 # This license differs from the rest of public-inbox
 #
+# ABSTRACT: Parse a MIME Content-Type or Content-Disposition Header
+#
 # This is a fork of the Email::MIME::ContentType 1.022 with
 # minor improvements and incompatibilities; namely changes to
 # quiet warnings with legacy data.
 package PublicInbox::EmlContentFoo;
 use strict;
 use parent qw(Exporter);
-# ABSTRACT: Parse a MIME Content-Type or Content-Disposition Header
+use v5.10.1;
+
+# find_mime_encoding() only appeared in Encode 2.87+ (Perl 5.26+),
+# while we support 2.35 shipped with Perl 5.10.1
+use Encode 2.35 qw(find_encoding);
+my %mime_name_map; # $enc->mime_name => $enc object
+BEGIN {
+       eval { Encode->import('find_mime_encoding') };
+       if ($@) {
+               *find_mime_encoding = sub { $mime_name_map{lc($_[0])} };
+               %mime_name_map = map {;
+                       my $enc = find_encoding($_);
+                       my $m = lc($enc->mime_name // '');
+                       $m => $enc;
+               } Encode->encodings(':all');
+
+               # delete fallback for encodings w/o ->mime_name:
+               delete $mime_name_map{''};
+
+               # an extra alias see Encode::MIME::NAME
+               $mime_name_map{'utf8'} = find_encoding('UTF-8');
+       }
+}
 
-use Encode 2.87 qw(find_mime_encoding);
 our @EXPORT_OK = qw(parse_content_type parse_content_disposition);
 
 our $STRICT_PARAMS = 1;
@@ -79,11 +102,6 @@ sub parse_content_type {
                type       => $type,
                subtype => $subtype,
                attributes => $attributes,
-
-               # This is dumb.  Really really dumb.  For backcompat. -- rjbs,
-               # 2013-08-10
-               discrete   => $type,
-               composite  => $subtype,
        };
 }
 
@@ -167,7 +185,7 @@ sub _process_rfc2231 {
        foreach (keys %{$attribs}) {
                next unless $_ =~ m/^(.*)\*$/;
                my $key = $1;
-               next unless $attribs->{$_} =~ m/^$re_exvalue$/;
+               next unless ($attribs->{$_} // '') =~ m/^$re_exvalue$/;
                my ($charset, $value) = ($1, $2);
                $value =~ s/%([0-9A-Fa-f]{2})/pack('C', hex($1))/eg;
                if (length $charset) {
@@ -278,11 +296,6 @@ This routine parses email content type headers according to section 5.1 of RFC
 a hash as above, with entries for the C<type>, the C<subtype>, and a hash of
 C<attributes>.
 
-For backward compatibility with a really unfortunate misunderstanding of RFC
-2045 by the early implementors of this module, C<discrete> and C<composite> are
-also present in the returned hashref, with the values of C<type> and C<subtype>
-respectively.
-
 =func parse_content_disposition
 
 This routine is exported by default.