]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/LeiCurl.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / LeiCurl.pm
index 38b17c7859565e6e0e9ded1b50d835a7e0150536..48c66ee9c0a93564b76e4a66580d5bc8b9da6ae9 100644 (file)
@@ -1,13 +1,22 @@
-# Copyright (C) 2021 all contributors <meta@public-inbox.org>
+# Copyright (C) all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # common option and torsocks(1) wrapping for curl(1)
+# Eventually, we may support using libcurl via Inline::C and/or
+# WWW::Curl; but curl(1) is most prevalent and widely-installed.
+# n.b. curl may support a daemon/client model like lei someday:
+#   https://github.com/curl/curl/wiki/curl-tool-master-client
 package PublicInbox::LeiCurl;
-use strict;
-use v5.10.1;
+use v5.12;
 use PublicInbox::Spawn qw(which);
 use PublicInbox::Config;
 
+# Ensures empty strings are quoted, we don't need more
+# sophisticated quoting than for empty strings: curl -d ''
+use overload '""' => sub {
+       join(' ', map { $_ eq '' ?  "''" : $_ } @{$_[0]});
+};
+
 my %lei2curl = (
        'curl-config=s@' => 'config|K=s@',
 );
@@ -17,7 +26,7 @@ sub new {
        my ($cls, $lei, $curl) = @_;
        $curl //= which('curl') // return $lei->fail('curl not found');
        my $opt = $lei->{opt};
-       my @cmd = ($curl, qw(-Sf));
+       my @cmd = ($curl, qw(-gSf));
        $cmd[-1] .= 's' if $opt->{quiet}; # already the default for "lei q"
        $cmd[-1] .= 'v' if $opt->{verbose}; # we use ourselves, too
        for my $o ($lei->curl_opt) {
@@ -45,7 +54,7 @@ sub torsocks { # useful for "git clone" and "git fetch", too
        $opt->{torsocks} = 'false' if $opt->{'no-torsocks'};
        my $torsocks = $opt->{torsocks} //= 'auto';
        if ($torsocks eq 'auto' && substr($uri->host, -6) eq '.onion' &&
-                       (($lei->{env}->{LD_PRELOAD}//'') !~ /torsocks/)) {
+               ($PublicInbox::Config::LD_PRELOAD//'') !~ m!/libtorsocks\b!) {
                # "auto" continues anyways if torsocks is missing;
                # a proxy may be specified via CLI, curlrc,
                # environment variable, or even firewall rule
@@ -63,10 +72,14 @@ EOM
 
 # completes the result of cmd() for $uri
 sub for_uri {
-       my ($self, $lei, $uri) = @_;
+       my ($self, $lei, $uri, @opt) = @_;
        my $pfx = torsocks($self, $lei, $uri) or return; # error
-       [ @$pfx, @$self, substr($uri->path, -3) eq '.gz' ? () : '--compressed',
-               $uri->as_string ]
+       if ($uri->scheme =~ /\Ahttps?\z/i) {
+               my $cfg = $lei->_lei_cfg;
+               my $p = $cfg ? $cfg->urlmatch('http.Proxy', $$uri, 1) : undef;
+               push(@opt, '--proxy', $p) if defined($p);
+       }
+       bless [ @$pfx, @$self, @opt, $uri->as_string ], ref($self);
 }
 
 1;