]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAttach.pm
get rid of unnecessary bytes::length usage
[public-inbox.git] / lib / PublicInbox / WwwAttach.pm
1 # Copyright (C) 2016-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # For retrieving attachments from messages in the WWW interface
5 package PublicInbox::WwwAttach; # internal package
6 use strict;
7 use v5.10.1;
8 use parent qw(PublicInbox::GzipFilter);
9 use PublicInbox::Eml;
10
11 sub referer_match ($) {
12         my ($ctx) = @_;
13         my $env = $ctx->{env};
14         my $referer = $env->{HTTP_REFERER} // '';
15         return 1 if $referer eq ''; # no referer is always OK for wget/curl
16
17         # prevent deep-linking from other domains on some browsers (Firefox)
18         # n.b.: $ctx->{ibx}->base_url($env) with INBOX_URL won't work
19         # with dillo, we can only match "$url_scheme://$HTTP_HOST/" without
20         # path components
21         my $base_url = $env->{'psgi.url_scheme'} . '://' .
22                         ($env->{HTTP_HOST} //
23                          "$env->{SERVER_NAME}:$env->{SERVER_PORT}") . '/';
24         index($referer, $base_url) == 0;
25 }
26
27 sub get_attach_i { # ->each_part callback
28         my ($part, $depth, $idx) = @{$_[0]};
29         my $ctx = $_[1];
30         return if $idx ne $ctx->{idx}; # [0-9]+(?:\.[0-9]+)+
31         my $res = $ctx->{res};
32         $res->[0] = 200;
33         my $ct = $part->ct;
34         if ($ct && (($ct->{type} || '') eq 'text')) {
35                 # display all text as text/plain:
36                 my $cset = $ct->{attributes}->{charset};
37                 if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
38                         $res->[1]->[1] .= qq(; charset=$cset);
39                 }
40                 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res->[1],
41                                                                 $ctx->{env});
42                 $part = $ctx->zflush($part->body);
43         } else { # TODO: allow user to configure safe types
44                 if (referer_match($ctx)) {
45                         $res->[1]->[1] = 'application/octet-stream';
46                         $part = $part->body;
47                 } else {
48                         $res->[0] = 403;
49                         $res->[1]->[1] = 'text/plain';
50                         $part = "Deep-linking prevented\n";
51                 }
52         }
53         push @{$res->[1]}, 'Content-Length', length($part);
54         $res->[2]->[0] = $part;
55 }
56
57 sub async_eml { # for async_blob_cb
58         my ($ctx, $eml) = @_;
59         eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
60         if ($@) {
61                 $ctx->{res}->[0] = 500;
62                 warn "E: $@";
63         }
64 }
65
66 sub async_next {
67         my ($http) = @_;
68         my $ctx = $http->{forward} or return; # client aborted
69         # finally, we call the user-supplied callback
70         eval { $ctx->{wcb}->($ctx->{res}) };
71         warn "E: $@" if $@;
72 }
73
74 sub scan_attach ($) { # public-inbox-httpd only
75         my ($ctx) = @_;
76         $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
77         $ctx->smsg_blob($ctx->{smsg});
78 }
79
80 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
81 sub get_attach ($$$) {
82         my ($ctx, $idx, $fn) = @_;
83         $ctx->{res} = [ 404, [ 'Content-Type' => 'text/plain' ],
84                                 [ "Not found\n" ] ];
85         $ctx->{idx} = $idx;
86         bless $ctx, __PACKAGE__;
87         my $eml;
88         if ($ctx->{smsg} = $ctx->{ibx}->smsg_by_mid($ctx->{mid})) {
89                 return sub { # public-inbox-httpd-only
90                         $ctx->{wcb} = $_[0];
91                         scan_attach($ctx);
92                 } if $ctx->{env}->{'pi-httpd.async'};
93                 # generic PSGI:
94                 $eml = $ctx->{ibx}->smsg_eml($ctx->{smsg});
95         } elsif (!$ctx->{ibx}->over) {
96                 if (my $bref = $ctx->{ibx}->msg_by_mid($ctx->{mid})) {
97                         $eml = PublicInbox::Eml->new($bref);
98                 }
99         }
100         $eml->each_part(\&get_attach_i, $ctx, 1) if $eml;
101         $ctx->{res}
102 }
103
104 1;