]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/WwwAttach.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / WwwAttach.pm
1 # Copyright (C) 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         return 1 if $env->{REQUEST_METHOD} eq 'POST';
15         my $referer = lc($env->{HTTP_REFERER} // '');
16         return 1 if $referer eq ''; # no referer is always OK for wget/curl
17
18         # prevent deep-linking from other domains on some browsers (Firefox)
19         # n.b.: $ctx->{ibx}->base_url($env) with INBOX_URL won't work
20         # with dillo, we can only match "$url_scheme://$HTTP_HOST/" without
21         # path components
22         my $base_url = lc($env->{'psgi.url_scheme'} . '://' .
23                         ($env->{HTTP_HOST} //
24                          "$env->{SERVER_NAME}:$env->{SERVER_PORT}") . '/');
25         index($referer, $base_url) == 0;
26 }
27
28 sub get_attach_i { # ->each_part callback
29         my ($part, $depth, $idx) = @{$_[0]};
30         my $ctx = $_[1];
31         return if $idx ne $ctx->{idx}; # [0-9]+(?:\.[0-9]+)+
32         my $res = $ctx->{res};
33         $res->[0] = 200;
34         my $ct = $part->ct;
35         if ($ct && (($ct->{type} || '') eq 'text')) {
36                 # display all text as text/plain:
37                 my $cset = $ct->{attributes}->{charset};
38                 if ($cset && ($cset =~ /\A[a-zA-Z0-9_\-]+\z/)) {
39                         $res->[1]->[1] .= qq(; charset=$cset);
40                 }
41                 $ctx->{gz} = PublicInbox::GzipFilter::gz_or_noop($res->[1],
42                                                                 $ctx->{env});
43                 $part = $ctx->zflush($part->body);
44         } else { # TODO: allow user to configure safe types
45                 if (referer_match($ctx)) {
46                         $res->[1]->[1] = 'application/octet-stream';
47                         $part = $part->body;
48                 } else {
49                         $res->[0] = 403;
50                         $res->[1]->[1] = 'text/html';
51                         $part = <<"";
52 <html><head><title>download
53 attachment</title><body><pre>Deep-linking prevented</pre><form
54 method=post\naction=""><input type=submit value="Download attachment"
55 /></form></body></html>
56
57                 }
58         }
59         push @{$res->[1]}, 'Content-Length', length($part);
60         $res->[2]->[0] = $part;
61 }
62
63 sub async_eml { # for async_blob_cb
64         my ($ctx, $eml) = @_;
65         eval { $eml->each_part(\&get_attach_i, $ctx, 1) };
66         if ($@) {
67                 $ctx->{res}->[0] = 500;
68                 warn "E: $@";
69         }
70 }
71
72 sub async_next {
73         my ($http) = @_;
74         my $ctx = $http->{forward} or return; # client aborted
75         # finally, we call the user-supplied callback
76         eval { $ctx->{wcb}->($ctx->{res}) };
77         warn "E: $@" if $@;
78 }
79
80 sub scan_attach ($) { # public-inbox-httpd only
81         my ($ctx) = @_;
82         $ctx->{env}->{'psgix.io'}->{forward} = $ctx;
83         $ctx->smsg_blob($ctx->{smsg});
84 }
85
86 # /$LISTNAME/$MESSAGE_ID/$IDX-$FILENAME
87 sub get_attach ($$$) {
88         my ($ctx, $idx, $fn) = @_;
89         $ctx->{res} = [ 404, [ 'Content-Type' => 'text/plain' ],
90                                 [ "Not found\n" ] ];
91         $ctx->{idx} = $idx;
92         bless $ctx, __PACKAGE__;
93         my $eml;
94         if ($ctx->{smsg} = $ctx->{ibx}->smsg_by_mid($ctx->{mid})) {
95                 return sub { # public-inbox-httpd-only
96                         $ctx->{wcb} = $_[0];
97                         scan_attach($ctx);
98                 } if $ctx->{env}->{'pi-httpd.async'};
99                 # generic PSGI:
100                 $eml = $ctx->{ibx}->smsg_eml($ctx->{smsg});
101         } elsif (!$ctx->{ibx}->over) {
102                 if (my $bref = $ctx->{ibx}->msg_by_mid($ctx->{mid})) {
103                         $eml = PublicInbox::Eml->new($bref);
104                 }
105         }
106         $eml->each_part(\&get_attach_i, $ctx, 1) if $eml;
107         $ctx->{res}
108 }
109
110 1;