| 1 | package HTTP::Tiny::Paranoid; | ||
| 2 | $HTTP::Tiny::Paranoid::VERSION = '0.06'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: A safer HTTP::Tiny | ||
| 7 | |||
| 8 | use HTTP::Tiny 0.070; | ||
| 9 | use parent 'HTTP::Tiny'; | ||
| 10 | use Net::DNS::Paranoid; | ||
| 11 | use Class::Method::Modifiers; | ||
| 12 | |||
| 13 | my $dns = Net::DNS::Paranoid->new; | ||
| 14 | |||
| 15 | sub blocked_hosts { shift; $dns->blocked_hosts(@_) } | ||
| 16 | sub whitelisted_hosts { shift; $dns->whitelisted_hosts(@_) } | ||
| 17 | |||
| 18 | around _open_handle => sub { | ||
| 19 | my $next = shift; | ||
| 20 | my $self = shift; | ||
| 21 | |||
| 22 | my ($req, $scheme, $host, $port, $peer) = @_; | ||
| 23 | |||
| 24 | if ($peer) { | ||
| 25 | my ($ips, $error) = $dns->resolve($peer); | ||
| 26 | die "$peer: $error\n" if defined $error; | ||
| 27 | die "$peer: did not match resolved IP\n" unless @$ips && $ips->[0] eq $peer; | ||
| 28 | $self->$next($req, $scheme, $host, $port, $peer); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | my ($ips, $error) = $dns->resolve($host); | ||
| 32 | die "$host: $error\n" if defined $error; | ||
| 33 | die "$host: no IP address found\n" unless @$ips; | ||
| 34 | $self->$next($req, $scheme, $host, $port, $ips->[0]); | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | 1; | ||
| 39 | __END__ |