]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/LeiCurl.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / LeiCurl.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 # common option and torsocks(1) wrapping for curl(1)
5 # Eventually, we may support using libcurl via Inline::C and/or
6 # WWW::Curl; but curl(1) is most prevalent and widely-installed.
7 # n.b. curl may support a daemon/client model like lei someday:
8 #   https://github.com/curl/curl/wiki/curl-tool-master-client
9 package PublicInbox::LeiCurl;
10 use v5.12;
11 use PublicInbox::Spawn qw(which);
12 use PublicInbox::Config;
13
14 # Ensures empty strings are quoted, we don't need more
15 # sophisticated quoting than for empty strings: curl -d ''
16 use overload '""' => sub {
17         join(' ', map { $_ eq '' ?  "''" : $_ } @{$_[0]});
18 };
19
20 my %lei2curl = (
21         'curl-config=s@' => 'config|K=s@',
22 );
23
24 # prepares a common command for curl(1) based on $lei command
25 sub new {
26         my ($cls, $lei, $curl) = @_;
27         $curl //= which('curl') // return $lei->fail('curl not found');
28         my $opt = $lei->{opt};
29         my @cmd = ($curl, qw(-gSf));
30         $cmd[-1] .= 's' if $opt->{quiet}; # already the default for "lei q"
31         $cmd[-1] .= 'v' if $opt->{verbose}; # we use ourselves, too
32         for my $o ($lei->curl_opt) {
33                 if (my $lei_spec = $lei2curl{$o}) {
34                         $o = $lei_spec;
35                 }
36                 $o =~ s/\|[a-z0-9]\b//i; # remove single char short option
37                 if ($o =~ s/=[is]@\z//) {
38                         my $ary = $opt->{$o} or next;
39                         push @cmd, map { ("--$o", $_) } @$ary;
40                 } elsif ($o =~ s/=[is]\z//) {
41                         my $val = $opt->{$o} // next;
42                         push @cmd, "--$o", $val;
43                 } elsif ($opt->{$o}) {
44                         push @cmd, "--$o";
45                 }
46         }
47         push @cmd, '-v' if $opt->{verbose}; # lei uses this itself
48         bless \@cmd, $cls;
49 }
50
51 sub torsocks { # useful for "git clone" and "git fetch", too
52         my ($self, $lei, $uri)= @_;
53         my $opt = $lei->{opt};
54         $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
55         my $torsocks = $opt->{torsocks} //= 'auto';
56         if ($torsocks eq 'auto' && substr($uri->host, -6) eq '.onion' &&
57                 ($PublicInbox::Config::LD_PRELOAD//'') !~ m!/libtorsocks\b!) {
58                 # "auto" continues anyways if torsocks is missing;
59                 # a proxy may be specified via CLI, curlrc,
60                 # environment variable, or even firewall rule
61                 [ ($lei->{torsocks} //= which('torsocks')) // () ]
62         } elsif (PublicInbox::Config::git_bool($torsocks)) {
63                 my $x = $lei->{torsocks} //= which('torsocks');
64                 $x or return $lei->fail(<<EOM);
65 --torsocks=yes specified but torsocks not found in PATH=$ENV{PATH}
66 EOM
67                 [ $x ];
68         } else { # the common case for current Internet :<
69                 [];
70         }
71 }
72
73 # completes the result of cmd() for $uri
74 sub for_uri {
75         my ($self, $lei, $uri, @opt) = @_;
76         my $pfx = torsocks($self, $lei, $uri) or return; # error
77         if ($uri->scheme =~ /\Ahttps?\z/i) {
78                 my $cfg = $lei->_lei_cfg;
79                 my $p = $cfg ? $cfg->urlmatch('http.Proxy', $$uri, 1) : undef;
80                 push(@opt, '--proxy', $p) if defined($p);
81         }
82         bless [ @$pfx, @$self, @opt, $uri->as_string ], ref($self);
83 }
84
85 1;