X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FLeiCurl.pm;h=5ffade9976cb43a194e784c955d4b515d2b8a90f;hb=23af251dd607c4e75ab1e68063f2c885c48cc035;hp=c8747d4f0c07f2040efd6e882ab9ebc931ebff5e;hpb=cfc2f64069e245a700b60113705be477857c51e5;p=public-inbox.git diff --git a/lib/PublicInbox/LeiCurl.pm b/lib/PublicInbox/LeiCurl.pm index c8747d4f..5ffade99 100644 --- a/lib/PublicInbox/LeiCurl.pm +++ b/lib/PublicInbox/LeiCurl.pm @@ -2,12 +2,26 @@ # License: AGPL-3.0+ # 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 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@', +); + # prepares a common command for curl(1) based on $lei command sub new { my ($cls, $lei, $curl) = @_; @@ -17,6 +31,9 @@ sub new { $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) { + if (my $lei_spec = $lei2curl{$o}) { + $o = $lei_spec; + } $o =~ s/\|[a-z0-9]\b//i; # remove single char short option if ($o =~ s/=[is]@\z//) { my $ary = $opt->{$o} or next; @@ -38,7 +55,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 @@ -56,10 +73,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) : undef; + push(@opt, "--proxy=$p") if defined($p); + } + bless [ @$pfx, @$self, @opt, $uri->as_string ], ref($self); } 1;