File Coverage

blib/lib/LWPx/ParanoidHandler.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 1 1 100.0
total 46 49 93.8


line stmt bran cond sub pod time code
1             package LWPx::ParanoidHandler;
2 3     3   157460 use strict;
  3         6  
  3         120  
3 3     3   88 use warnings;
  3         6  
  3         458  
4 3     3   99 use 5.008008;
  3         15  
  3         184  
5             our $VERSION = '0.07';
6 3     3   4159 use parent qw/Exporter/;
  3         2367  
  3         16  
7 3     3   4015 use Net::DNS::Paranoid;
  3         485804  
  3         901  
8              
9             our @EXPORT = qw/make_paranoid/;
10              
11             sub make_paranoid {
12 7     7 1 55219 my ($ua, $paranoid) = @_;
13 7         26 $ua->add_handler(request_send => _paranoid_handler($paranoid));
14             }
15              
16             sub _paranoid_handler {
17 7     7   18 my ($paranoid) = @_;
18 7   66     46 $paranoid ||= Net::DNS::Paranoid->new();
19              
20             sub {
21 19     19   957848 my ($request, $ua, $h) = @_;
22 19   33     132 $request->{_time_begin} ||= time();
23              
24 19         60 my $host = $request->uri->host;
25 19         775 my ($addrs, $errmsg) = $paranoid->resolve($host, $request->{_time_begin});
26 19 100       56241 if ($errmsg) {
27 16         108 my $err_res = HTTP::Response->new(403, "Unauthorized access to blocked host($errmsg)");
28 16         911 $err_res->request($request);
29 16         168 $err_res->header("Client-Warning" => "Internal response");
30 16         949 $err_res->header("Content-Type" => "text/plain");
31 16         665 $err_res->content("403 Unauthorized access to blocked host\n");
32 16         382 return $err_res;
33             }
34 3         19 return; # fallthrough
35             }
36 7         349 }
37              
38             1;
39             __END__