add first-header method in http backend

This commit is contained in:
garnier-quentin 2019-03-25 13:12:36 +01:00
parent 4c44743620
commit 36c5feb616
4 changed files with 53 additions and 27 deletions

View File

@ -161,6 +161,9 @@ sub manage_selection {
my $timing0 = [gettimeofday];
my $webcontent = $self->{http}->request();
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() };

View File

@ -238,31 +238,26 @@ sub set_extra_curl_opt {
}
}
sub parse_headers {
my ($self, %options) = @_;
$self->{response_headers_parse} = {};
my ($header, $value);
foreach (split /\n/, $self->{response_headers}) {
s/\r//g;
if (/^(\S(?:.*?))\s*:\s*(.*)/) {
if (defined($value)) {
$self->{response_headers_parse}->{$header} = []
if (!defined($self->{response_headers_parse}->{$header}));
push @{$self->{response_headers_parse}->{$header}}, $value;
}
$header = lc($1);
$value = $2;
} elsif (/^\s+(.*)/) {
$value .= ', ' . $1;
sub cb_get_header {
my ($easy, $header, $uservar) = @_;
$header =~ s/[\r\n]//g;
if ($header =~ /^[\r\n]*$/) {
$uservar->{nheaders}++;
} else {
$uservar->{response_headers}->[$uservar->{nheaders}] = {}
if (!defined($uservar->{response_headers}->[$uservar->{nheaders}]));
if ($header =~ /^(\S(?:.*?))\s*:\s*(.*)/) {
my $header_name = lc($1);
$uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name} = []
if (!defined($uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name}));
push @{$uservar->{response_headers}->[$uservar->{nheaders}]->{$header_name}}, $2;
} else {
$uservar->{response_headers}->[$uservar->{nheaders}]->{response_line} = $header;
}
}
if (defined($value)) {
$self->{response_headers_parse}->{$header} = []
if (!defined($self->{response_headers_parse}->{$header}));
push @{$self->{response_headers_parse}->{$header}}, $value;
}
return length($_[1]);
}
sub request {
@ -334,8 +329,11 @@ sub request {
$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_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 {
$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->parse_headers();
# Check response
my $status = 'ok';
@ -384,12 +381,20 @@ sub request {
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 {
my ($self, %options) = @_;
return undef
if (!defined($self->{response_headers_parse}->{ lc($options{name}) }));
return wantarray ? @{$self->{response_headers_parse}->{ lc($options{name}) }} : $self->{response_headers_parse}->{ lc($options{name}) }->[0];
if (!defined($self->{response_headers}->[-1]->{ lc($options{name}) }));
return wantarray ? @{$self->{response_headers}->[-1]->{ lc($options{name}) }} : $self->{response_headers}->[-1]->{ lc($options{name}) }->[0];
}
sub get_code {

View File

@ -238,6 +238,18 @@ sub request {
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 {
my ($self, %options) = @_;

View File

@ -177,6 +177,12 @@ sub request {
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 {
my ($self, %options) = @_;