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 be5b649e57
commit aedde490cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 10 deletions

View File

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

View File

@ -255,17 +255,28 @@ sub request {
$self->{response} = $self->{ua}->request($req); $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 # Check response
my $status = 'ok'; my $status = 'ok';
my $code = $self->{response}->code();
if (defined($request_options->{critical_status}) && $request_options->{critical_status} ne '' && 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'; $status = 'critical';
} elsif (defined($request_options->{warning_status}) && $request_options->{warning_status} ne '' && } 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'; $status = 'warning';
} elsif (defined($request_options->{unknown_status}) && $request_options->{unknown_status} ne '' && } 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'; $status = 'unknown';
} }
@ -284,7 +295,6 @@ sub request {
$self->{output}->exit(); $self->{output}->exit();
} }
$self->{headers} = $self->{response}->headers();
return $self->{response}->content; return $self->{response}->content;
} }
@ -327,13 +337,13 @@ sub get_header {
sub get_code { sub get_code {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{response}->code(); return $self->{response_code};
} }
sub get_message { sub get_message {
my ($self, %options) = @_; my ($self, %options) = @_;
return $self->{response}->message(); return $self->{response_message};
} }
sub get_certificate { sub get_certificate {