App-WebPerimeterScan
view release on metacpan or search on metacpan
lib/App/WebPerimeterScan/HTTP.pm view on Meta::CPAN
my (%headers, $last_name);
for my $line (@lines) {
# obs-fold: a continuation line belongs to the previous header.
if($line =~ /\A[ \t]+(.+)\z/ && defined $last_name) {
$headers{$last_name}[-1] .= " $1";
next;
}
# Copy the captures before the substitution below: a successful s///
# resets $2, which once lost the value of "Name : value" headers.
my ($name, $value) = $line =~ /\A([^:]+):[ \t]*(.*)\z/ or next;
($name = lc $name) =~ s/[ \t]+\z//;
push @{ $headers{$name} }, $value;
$last_name = $name;
}
return {
http_version => $http_version,
status => $status + 0,
reason => $reason,
headers => \%headers,
};
}
# _read_body
# Purpose: read the body the way the headers describe it.
# Entry: $buffer_ref holds bytes already read past the header block.
# Exit: arrayref [body, truncated flag].
# Side effects: socket reads; consumes $$buffer_ref.
sub _read_body :Private
{
my ($self, $sock, $deadline, $buffer_ref, $method, $parsed) = @_;
my $status = $parsed->{status};
my $headers = $parsed->{headers};
# HEAD, 1xx, 204 and 304 never have a body, whatever the headers say.
return [ '', 0 ] if $method eq 'HEAD' || $status < 200 || $NO_BODY_STATUS{$status};
my $encoding = lc join ',', @{ $headers->{'transfer-encoding'} || [] };
return $self->_read_chunked($sock, $deadline, $buffer_ref) if $encoding =~ /chunked/;
my $length = $headers->{'content-length'} ? $headers->{'content-length'}[0] : undef;
return $self->_read_until($sock, $deadline, $buffer_ref, $length)
if defined $length && $length =~ /\A\d+\z/;
return $self->_read_until($sock, $deadline, $buffer_ref, undef);
}
# _connect
# Purpose: open a TCP or TLS connection.
# Entry: $target from _target.
# Exit: the socket, or undef on failure ($! / SSL_ERROR set).
# Side effects: network connection.
sub _connect :Private
{
my ($self, $target) = @_;
my %common = (
PeerPort => $target->{port},
Timeout => $self->{timeout},
);
# Verification failures are reported by App::WebPerimeterScan::TLS;
# here the response is still wanted so the other checks can run.
return IO::Socket::SSL->new(
%common,
PeerHost => $target->{host},
SSL_hostname => $target->{host},
SSL_verify_mode => SSL_VERIFY_NONE,
) if $target->{scheme} eq 'https';
return IO::Socket::INET->new(%common, PeerAddr => $target->{host}, Proto => 'tcp');
}
# _throttle
# Purpose: keep at least 'delay' seconds between requests to one host.
# Entry: $host is the request's host.
# Exit: $self.
# Side effects: may sleep; records the time of this request.
sub _throttle :Private
{
my ($self, $host) = @_;
my $last = $self->{last}{$host};
if($self->{delay} > 0 && defined $last) {
my $wait = $last + $self->{delay} - time;
sleep($wait) if $wait > 0;
}
$self->{last}{$host} = time;
return $self;
}
# _read
# Purpose: one bounded read.
# Entry: $sock open; $deadline absolute.
# Exit: the bytes read ('' at EOF), or undef on timeout or error.
# Side effects: socket read.
sub _read :Private
{
my ($sock, $deadline) = @_;
my $wait = $deadline - time;
my $buffer;
# undef stands for "nothing more will come": timed out or failed.
if($wait > 0 && IO::Select->new($sock)->can_read($wait)) {
$buffer = '';
$buffer = undef unless defined sysread($sock, $buffer, $READ_SIZE);
}
return $buffer;
}
# _write_all
# Purpose: write a whole string, waiting for the socket as needed.
# Entry: $sock open; $deadline absolute.
# Exit: 1 when everything was written, 0 on timeout or error.
# Side effects: socket writes.
sub _write_all :Private
( run in 2.987 seconds using v1.01-cache-2.11-cpan-234fa220139 )