create new class

This commit is contained in:
Colin GAGNAIRE 2018-01-19 12:11:16 +01:00
parent 06e81eb696
commit 30c63a59d7
2 changed files with 53 additions and 15 deletions

View File

@ -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));

View File

@ -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;