From 30c63a59d7be3eac12c49d1f440308812f046004 Mon Sep 17 00:00:00 2001 From: Colin GAGNAIRE Date: Fri, 19 Jan 2018 12:11:16 +0100 Subject: [PATCH] create new class --- centreon-plugins/centreon/plugins/http.pm | 19 ++----- .../centreon/plugins/useragent.pm | 49 +++++++++++++++++++ 2 files changed, 53 insertions(+), 15 deletions(-) create mode 100644 centreon-plugins/centreon/plugins/useragent.pm diff --git a/centreon-plugins/centreon/plugins/http.pm b/centreon-plugins/centreon/plugins/http.pm index ee0c4d7d7..681b9124e 100644 --- a/centreon-plugins/centreon/plugins/http.pm +++ b/centreon-plugins/centreon/plugins/http.pm @@ -22,23 +22,11 @@ package centreon::plugins::http; use strict; use warnings; -use LWP::UserAgent; +use centreon::plugins::useragent; use HTTP::Cookies; use URI; use IO::Socket::SSL; -{ - package CentreonUserAgent; - our @ISA = qw(LWP::UserAgent); - - sub get_basic_credentials { - my($self, $realm, $uri, $proxy) = @_; - return if $proxy; - return $centreon::plugins::http::request_options->{username}, $centreon::plugins::http::request_options->{password} if $centreon::plugins::http::request_options->{credentials}; - return undef, undef; - } -} - sub new { my ($class, %options) = @_; my $self = {}; @@ -216,14 +204,15 @@ sub set_proxy { sub request { my ($self, %options) = @_; - our $request_options = { %{$self->{options}} }; + my $request_options = { %{$self->{options}} }; foreach (keys %options) { $request_options->{$_} = $options{$_} if (defined($options{$_})); } $self->check_options(request => $request_options); if (!defined($self->{ua})) { - $self->{ua} = CentreonUserAgent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}); + $self->{ua} = centreon::plugins::useragent->new(keep_alive => 1, protocols_allowed => ['http', 'https'], timeout => $request_options->{timeout}, + credentials => $request_options->{credentials}, username => $request_options->{username}, password => $request_options->{password}); if (defined($request_options->{cookies_file})) { $self->{ua}->cookie_jar(HTTP::Cookies->new(file => $request_options->{cookies_file}, autosave => 1)); diff --git a/centreon-plugins/centreon/plugins/useragent.pm b/centreon-plugins/centreon/plugins/useragent.pm new file mode 100644 index 000000000..245194894 --- /dev/null +++ b/centreon-plugins/centreon/plugins/useragent.pm @@ -0,0 +1,49 @@ +# +# Copyright 2017 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package centreon::plugins::useragent; + +use strict; +use warnings; +use base 'LWP::UserAgent'; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + $self = LWP::UserAgent::new(@_); + $self->agent("centreon::plugins::useragent"); + + $self->{credentials} = $options{credentials} if defined($options{credentials}); + $self->{username} = $options{username} if defined($options{username}); + $self->{password} = $options{password} if defined($options{password}); + + return $self; +} + +sub get_basic_credentials { + my($self, $realm, $uri, $proxy) = @_; + return if $proxy; + return $self->{username}, $self->{password} if $self->{credentials}; + return undef, undef; +} + +1;