diff options
-rw-r--r-- | t/cfg.t | 32 | ||||
-rw-r--r-- | urpm/download.pm | 30 |
2 files changed, 59 insertions, 3 deletions
@@ -1,11 +1,13 @@ #!/usr/bin/perl -use Test::More tests => 4; +use Test::More 'no_plan'; use MDK::Common; BEGIN { use_ok 'urpm::cfg' } +BEGIN { use_ok 'urpm::download' } my $file = 'testurpmi.cfg'; +my $proxyfile = $urpm::download::PROXY_CFG = 'testproxy.cfg'; open my $f, '>', $file or die $!; print $f (my $cfgtext = <<URPMICFG); { @@ -52,4 +54,30 @@ $cfgtext2 =~ s/# generated.*\n//; is( $cfgtext, $cfgtext2, 'config is the same' ) or system qw( diff -u ), $file, $file.2; -END { unlink $file, $file.2 } +open $f, '>', $proxyfile or die $!; +print $f ($cfgtext = <<PROXYCFG); +http_proxy=http://foo:8080/ +local:http_proxy=http://yoyodyne:8080/ +local:proxy_user=rafael:richard +PROXYCFG +close $f; + +my $p = get_proxy(); +is( $p->{http_proxy}, 'http://foo:8080/', 'read proxy' ); +ok( !defined $p->{user}, 'no user defined' ); +$p = get_proxy('local'); +is( $p->{http_proxy}, 'http://yoyodyne:8080/', 'read media proxy' ); +is( $p->{user}, 'rafael', 'proxy user' ); +is( $p->{pwd}, 'richard', 'proxy password' ); +ok( dump_proxy_config(), 'dump_proxy_config' ); +$cfgtext2 = cat_($proxyfile); +$cfgtext2 =~ s/# generated.*\n//; +is( $cfgtext, $cfgtext2, 'dumped correctly' ); +set_proxy_config(http_proxy => ''); +ok( dump_proxy_config(), 'dump_proxy_config erased' ); +$cfgtext2 = cat_($proxyfile); +$cfgtext2 =~ s/# generated.*\n//; +$cfgtext =~ s/^http_proxy.*\n//; +is( $cfgtext, $cfgtext2, 'dumped correctly' ); + +END { unlink $file, $file.2, $proxyfile } diff --git a/urpm/download.pm b/urpm/download.pm index f94ba20a..b0e7b01d 100644 --- a/urpm/download.pm +++ b/urpm/download.pm @@ -17,11 +17,13 @@ sub import () { foreach my $symbol (qw(get_proxy propagate_sync_callback sync_file sync_wget sync_curl sync_rsync sync_ssh + set_proxy_config dump_proxy_config )) { *{$c.'::'.$symbol} = *$symbol; } } +#- parses proxy.cfg (private) sub load_proxy_config () { return if defined $proxy_config; open my $f, $PROXY_CFG or $proxy_config = {}, return; @@ -32,7 +34,7 @@ sub load_proxy_config () { $proxy_config->{$1 || ''}{$2} = $3; next; } - if (/^(?:(.*):\s*)?proxy_user\s*=\s*(.*)(?::(.*))?$/) { + if (/^(?:(.*):\s*)?proxy_user\s*=\s*([^:]*)(?::(.*))?$/) { $proxy_config->{$1 || ''}{user} = $2; $proxy_config->{$1 || ''}{pwd} = $3 if defined $3; next; @@ -41,6 +43,26 @@ sub load_proxy_config () { close $f; } +#- writes proxy.cfg +sub dump_proxy_config () { + return 0 unless defined $proxy_config; #- hasn't been read yet + open my $f, '>', $PROXY_CFG or return 0; + print $f "# generated ".(scalar localtime)."\n"; + for ('', sort grep { !/^(|cmd_line)$/ } keys %$proxy_config) { + my $m = $_ eq '' ? '' : "$_:"; + my $p = $proxy_config->{$_}; + for (qw(http_proxy ftp_proxy)) { + defined $p->{$_} && $p->{$_} ne '' + and print $f "$m$_=$p->{$_}\n"; + } + defined $p->{user} && $p->{user} ne '' + and print $f "${m}proxy_user=$p->{user}:$p->{pwd}\n"; + } + close $f; + chmod 0600, $PROXY_CFG; #- may contain passwords + return 1; +} + #- reads and loads the proxy.cfg file ; #- returns the global proxy settings (without arguments) or the #- proxy settings for the specified media (with a media name as argument) @@ -70,6 +92,12 @@ sub set_cmdline_proxy { $proxy_config->{cmd_line}{$_} = $h{$_} for keys %h; } +#- changes permanently the proxy settings +sub set_proxy_config { + my ($key, $value, $o_media) = @_; + $proxy_config->{$o_media || ''}{$key} = $value; +} + #- set up the environment for proxy usage for the appropriate tool. #- returns an array of command-line arguments. sub set_proxy { |