mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-07-27 15:44:21 +02:00
add first-header method in http backend
This commit is contained in:
parent
237b2c3904
commit
cdf1b270ca
@ -162,6 +162,9 @@ sub manage_selection {
|
|||||||
my $webcontent = $self->{http}->request();
|
my $webcontent = $self->{http}->request();
|
||||||
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
|
||||||
|
|
||||||
|
use Data::Dumper;
|
||||||
|
print Data::Dumper::Dumper($self->{http}->get_first_header(name => 'location'));
|
||||||
|
|
||||||
$self->{global} = { time => $timeelapsed, content => $webcontent, code => $self->{http}->get_code() };
|
$self->{global} = { time => $timeelapsed, content => $webcontent, code => $self->{http}->get_code() };
|
||||||
|
|
||||||
if (defined($self->{option_results}->{extracted_pattern}) && $self->{option_results}->{extracted_pattern} ne '' &&
|
if (defined($self->{option_results}->{extracted_pattern}) && $self->{option_results}->{extracted_pattern} ne '' &&
|
||||||
|
@ -238,31 +238,26 @@ sub set_extra_curl_opt {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub parse_headers {
|
sub cb_get_header {
|
||||||
my ($self, %options) = @_;
|
my ($easy, $header, $uservar) = @_;
|
||||||
|
|
||||||
$self->{response_headers_parse} = {};
|
$header =~ s/[\r\n]//g;
|
||||||
my ($header, $value);
|
if ($header =~ /^[\r\n]*$/) {
|
||||||
foreach (split /\n/, $self->{response_headers}) {
|
$uservar->{nheaders}++;
|
||||||
s/\r//g;
|
} else {
|
||||||
if (/^(\S(?:.*?))\s*:\s*(.*)/) {
|
$uservar->{response_headers}->[$uservar->{nheaders}] = {}
|
||||||
if (defined($value)) {
|
if (!defined($uservar->{response_headers}->[$uservar->{nheaders}]));
|
||||||
$self->{response_headers_parse}->{$header} = []
|
if ($header =~ /^(\S(?:.*?))\s*:\s*(.*)/) {
|
||||||
if (!defined($self->{response_headers_parse}->{$header}));
|
my $header_name = lc($1);
|
||||||
push @{$self->{response_headers_parse}->{$header}}, $value;
|
$uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name} = []
|
||||||
}
|
if (!defined($uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name}));
|
||||||
$header = lc($1);
|
push @{$uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name}}, $2;
|
||||||
$value = $2;
|
} else {
|
||||||
} elsif (/^\s+(.*)/) {
|
$uservar->{response_headers}->[$uservar->{nheaders}]->{response_line} = $header;
|
||||||
$value .= ', ' . $1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (defined($value)) {
|
return length($_[1]);
|
||||||
$self->{response_headers_parse}->{$header} = []
|
|
||||||
if (!defined($self->{response_headers_parse}->{$header}));
|
|
||||||
push @{$self->{response_headers_parse}->{$header}}, $value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
sub request {
|
sub request {
|
||||||
@ -334,7 +329,10 @@ sub request {
|
|||||||
$self->set_extra_curl_opt(%options);
|
$self->set_extra_curl_opt(%options);
|
||||||
|
|
||||||
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_FILE'), parameter => \$self->{response_body});
|
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_FILE'), parameter => \$self->{response_body});
|
||||||
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HEADERDATA'), parameter => \$self->{response_headers});
|
$self->{nheaders} = 0;
|
||||||
|
$self->{response_headers} = [{}];
|
||||||
|
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HEADERDATA'), parameter => $self);
|
||||||
|
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_HEADERFUNCTION'), parameter => \&cb_get_header);
|
||||||
|
|
||||||
eval {
|
eval {
|
||||||
$self->{curl_easy}->perform();
|
$self->{curl_easy}->perform();
|
||||||
@ -345,7 +343,6 @@ sub request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$self->{response_code} = $self->{curl_easy}->getinfo($self->{constant_cb}->(name => 'CURLINFO_RESPONSE_CODE'));
|
$self->{response_code} = $self->{curl_easy}->getinfo($self->{constant_cb}->(name => 'CURLINFO_RESPONSE_CODE'));
|
||||||
$self->parse_headers();
|
|
||||||
|
|
||||||
# Check response
|
# Check response
|
||||||
my $status = 'ok';
|
my $status = 'ok';
|
||||||
@ -384,12 +381,20 @@ sub request {
|
|||||||
return $self->{response_body};
|
return $self->{response_body};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub get_first_header {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
return undef
|
||||||
|
if (!defined($self->{response_headers}->[0]->{ lc($options{name}) }));
|
||||||
|
return wantarray ? @{$self->{response_headers}->[0]->{ lc($options{name}) }} : $self->{response_headers}->[0]->{ lc($options{name}) }->[0];
|
||||||
|
}
|
||||||
|
|
||||||
sub get_header {
|
sub get_header {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
return undef
|
return undef
|
||||||
if (!defined($self->{response_headers_parse}->{ lc($options{name}) }));
|
if (!defined($self->{response_headers}->[-1]->{ lc($options{name}) }));
|
||||||
return wantarray ? @{$self->{response_headers_parse}->{ lc($options{name}) }} : $self->{response_headers_parse}->{ lc($options{name}) }->[0];
|
return wantarray ? @{$self->{response_headers}->[-1]->{ lc($options{name}) }} : $self->{response_headers}->[-1]->{ lc($options{name}) }->[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
sub get_code {
|
sub get_code {
|
||||||
|
@ -238,6 +238,18 @@ sub request {
|
|||||||
return $self->{response}->content;
|
return $self->{response}->content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub get_first_header {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
my @redirects = $self->{response}->redirects();
|
||||||
|
|
||||||
|
return
|
||||||
|
defined($redirects[0]) ?
|
||||||
|
$redirects[0]->headers()->header($options{name}) :
|
||||||
|
$self->{headers}->header($options{name})
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
sub get_header {
|
sub get_header {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
@ -177,6 +177,12 @@ sub request {
|
|||||||
return $self->{'backend_' . $self->{http_backend}}->request(request => $request_options);
|
return $self->{'backend_' . $self->{http_backend}}->request(request => $request_options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub get_first_header {
|
||||||
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
return $self->{'backend_' . $self->{http_backend}}->get_first_header(%options);
|
||||||
|
}
|
||||||
|
|
||||||
sub get_header {
|
sub get_header {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user