public inbox for pmg-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pmg-devel@lists.proxmox.com
Subject: [pmg-devel] [PATCH pmg-api 1/2] add lwp_user_agent to PMG::Utils
Date: Tue, 17 Nov 2020 09:54:00 +0100	[thread overview]
Message-ID: <20201117085401.23407-2-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20201117085401.23407-1-s.ivanov@proxmox.com>

The sub returns a LWP::UserAgent instance with properly configured
http-proxy settings (and a custom UserAgent identifier).

refactored from API2::APT, since we need it for updating KAM.cf.

Signed-off-by: Stoiko Ivanov <s.ivanov@proxmox.com>
---
 src/PMG/API2/APT.pm | 14 ++------------
 src/PMG/Utils.pm    | 18 ++++++++++++++++++
 2 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/PMG/API2/APT.pm b/src/PMG/API2/APT.pm
index 51b998f..09b11e5 100644
--- a/src/PMG/API2/APT.pm
+++ b/src/PMG/API2/APT.pm
@@ -8,7 +8,6 @@ use File::stat ();
 use IO::File;
 use File::Basename;
 use JSON;
-use LWP::UserAgent;
 
 use PVE::Tools qw(extract_param);
 use PVE::SafeSyslog;
@@ -20,6 +19,7 @@ use PVE::JSONSchema qw(get_standard_option);
 use PMG::RESTEnvironment;
 use PMG::pmgcfg;
 use PMG::Config;
+use PMG::Utils;
 
 use AptPkg::Cache;
 use AptPkg::Version;
@@ -429,21 +429,11 @@ __PACKAGE__->register_method({
 
 	my $data = "";
 
-	my $pmg_cfg = PMG::Config->new();
-	my $proxy = $pmg_cfg->get('admin', 'http_proxy');
-
-	my $ua = LWP::UserAgent->new;
-	$ua->agent("PMG/1.0");
+	my $ua = PMG::Utils::lwp_user_agent();
 	$ua->timeout(10);
 	$ua->max_size(1024*1024);
 	$ua->ssl_opts(verify_hostname => 0); # don't care for changelogs
 
-	if ($proxy) {
-	    $ua->proxy(['http', 'https'], $proxy);
-	} else {
-	    $ua->env_proxy;
-	}
-
 	my $username;
 	my $pw;
 
diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index d0654e1..844cb96 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -10,6 +10,7 @@ use File::stat;
 use POSIX qw(strftime);
 use File::stat;
 use File::Basename;
+use LWP::UserAgent;
 use MIME::Entity;
 use MIME::Words;
 use MIME::Parser;
@@ -1417,4 +1418,21 @@ sub reload_smtp_filter {
     return kill (10, $pid); # send SIGUSR1
 }
 
+# returns a LWP::UserAgent instance with option proxy from the config
+sub lwp_user_agent {
+    my $pmg_cfg = PMG::Config->new();
+    my $proxy = $pmg_cfg->get('admin', 'http_proxy');
+
+    my $ua = LWP::UserAgent->new;
+    $ua->agent("PMG/1.0");
+
+    if ($proxy) {
+	$ua->proxy(['http', 'https'], $proxy);
+    } else {
+	$ua->env_proxy;
+    }
+
+    return $ua;
+}
+
 1;
-- 
2.20.1





  reply	other threads:[~2020-11-17  8:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-17  8:53 [pmg-devel] [PATCH pmg-api 0/2] update KAM.cf in pmg-daily Stoiko Ivanov
2020-11-17  8:54 ` Stoiko Ivanov [this message]
2020-11-17  8:54 ` [pmg-devel] [PATCH pmg-api 2/2] " Stoiko Ivanov
2020-11-17  9:11   ` Dietmar Maurer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201117085401.23407-2-s.ivanov@proxmox.com \
    --to=s.ivanov@proxmox.com \
    --cc=pmg-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal