]> Sergey Matveev's repositories - public-inbox.git/commitdiff
wwwattach: avoid anonymous sub for msg_iter
authorEric Wong <e@80x24.org>
Wed, 25 Dec 2019 07:51:01 +0000 (07:51 +0000)
committerEric Wong <e@80x24.org>
Fri, 27 Dec 2019 20:00:38 +0000 (20:00 +0000)
We can pass arguments to msg_iter for msg_iter to pass
to our user-supplied callback, now.

lib/PublicInbox/WwwAttach.pm

index 2de568041f31a17b28f76faf28e68a892b755a5e..cda1c6c8db3a0745e8ad28a1cffaad05e10de6cf 100644 (file)
@@ -10,34 +10,39 @@ use Email::MIME::ContentType qw(parse_content_type);
 use PublicInbox::MIME;
 use PublicInbox::MsgIter;
 
+sub get_attach_i { # msg_iter callback
+       my ($part, $depth, @idx) = @{$_[0]};
+       my $res = $_[1];
+       return if join('.', @idx) ne $res->[3]; # $idx
+       $res->[0] = 200;
+       my $ct = $part->content_type;
+       $ct = parse_content_type($ct) if $ct;
+
+       # discrete == type, we remain Debian wheezy-compatible
+       if ($ct && (($ct->{discrete} || '') eq 'text')) {
+               # display all text as text/plain:
+               my $cset = $ct->{attributes}->{charset};
+               if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
+                       $res->[1]->[1] .= qq(; charset=$cset);
+               }
+       } else { # TODO: allow user to configure safe types
+               $res->[1]->[1] = 'application/octet-stream';
+       }
+       $part = $part->body;
+       push @{$res->[1]}, 'Content-Length', bytes::length($part);
+       $res->[2]->[0] = $part;
+}
+
 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
 sub get_attach ($$$) {
        my ($ctx, $idx, $fn) = @_;
        my $res = [ 404, [ 'Content-Type', 'text/plain' ], [ "Not found\n" ] ];
        my $mime = $ctx->{-inbox}->msg_by_mid($ctx->{mid}) or return $res;
        $mime = PublicInbox::MIME->new($mime);
-       msg_iter($mime, sub {
-               my ($part, $depth, @idx) = @{$_[0]};
-               return if join('.', @idx) ne $idx;
-               $res->[0] = 200;
-               my $ct = $part->content_type;
-               $ct = parse_content_type($ct) if $ct;
-
-               # discrete == type, we remain Debian wheezy-compatible
-               if ($ct && (($ct->{discrete} || '') eq 'text')) {
-                       # display all text as text/plain:
-                       my $cset = $ct->{attributes}->{charset};
-                       if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
-                               $res->[1]->[1] .= qq(; charset=$cset);
-                       }
-               } else { # TODO: allow user to configure safe types
-                       $res->[1]->[1] = 'application/octet-stream';
-               }
-               $part = $part->body;
-               push @{$res->[1]}, 'Content-Length', bytes::length($part);
-               $res->[2]->[0] = $part;
-       });
-       $res;
+       $res->[3] = $idx;
+       msg_iter($mime, \&get_attach_i, $res);
+       pop @$res; # cleanup before letting PSGI server see it
+       $res
 }
 
 1;