enh(http): backends - unify client timeout error (#3366)

This commit is contained in:
qgarnier 2022-01-03 16:43:28 +01:00 committed by GitHub
parent 36e2ec2d0e
commit 56339e6b98
2 changed files with 32 additions and 10 deletions

View File

@ -104,6 +104,7 @@ my $http_code_explained = {
415 => 'Unsupported Media Type',
416 => 'Requested Range Not Satisfiable',
417 => 'Expectation Failed',
450 => 'Timeout reached', # custom code
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
@ -400,15 +401,26 @@ sub request {
$self->curl_setopt(option => $self->{constant_cb}->(name => 'CURLOPT_CERTINFO'), parameter => 1);
}
$self->{response_code} = undef;
eval {
$self->{curl_easy}->perform();
};
if ($@) {
$self->{output}->add_option_msg(short_msg => 'curl perform error : ' . $@);
$self->{output}->option_exit();
if (ref($@) eq "Net::Curl::Easy::Code") {
my $num = $@;
if ($num == $self->{constant_cb}->(name => 'CURLE_OPERATION_TIMEDOUT')) {
$self->{response_code} = 450;
}
}
if (!defined($self->{response_code})) {
$self->{output}->add_option_msg(short_msg => 'curl perform error : ' . $@);
$self->{output}->option_exit();
}
}
$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'))
if (!defined($self->{response_code}));
# Check response
my $status = 'ok';

View File

@ -255,17 +255,28 @@ sub request {
$self->{response} = $self->{ua}->request($req);
$self->{response_code} = $self->{response}->code();
$self->{response_message} = $self->{response}->message();
$self->{headers} = $self->{response}->headers();
if ($self->{response_code} == 500) {
my $client_warning = $self->get_header(name => 'Client-Warning');
if (defined($client_warning) && $client_warning eq 'Internal response') {
$self->{response_code} = 450;
$self->{response_message} = 'Timeout reached';
}
}
# Check response
my $status = 'ok';
my $code = $self->{response}->code();
if (defined($request_options->{critical_status}) && $request_options->{critical_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{critical_status}, values => { code => $code })) {
$self->{output}->test_eval(test => $request_options->{critical_status}, values => { code => $self->{response_code} })) {
$status = 'critical';
} elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{warning_status}, values => { code => $code })) {
$self->{output}->test_eval(test => $request_options->{warning_status}, values => { code => $self->{response_code} })) {
$status = 'warning';
} elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' &&
$self->{output}->test_eval(test => $request_options->{unknown_status}, values => { code => $code })) {
$self->{output}->test_eval(test => $request_options->{unknown_status}, values => { code => $self->{response_code} })) {
$status = 'unknown';
}
@ -284,7 +295,6 @@ sub request {
$self->{output}->exit();
}
$self->{headers} = $self->{response}->headers();
return $self->{response}->content;
}
@ -327,13 +337,13 @@ sub get_header {
sub get_code {
my ($self, %options) = @_;
return $self->{response}->code();
return $self->{response_code};
}
sub get_message {
my ($self, %options) = @_;
return $self->{response}->message();
return $self->{response_message};
}
sub get_certificate {