File Coverage

blib/lib/PGP/FindKey.pm
Criterion Covered Total %
statement 34 41 82.9
branch 3 6 50.0
condition 4 9 44.4
subroutine 7 9 77.7
pod 0 3 0.0
total 48 68 70.5


line stmt bran cond sub pod time code
1             package PGP::FindKey;
2              
3 1     1   723 use strict;
  1         2  
  1         40  
4 1     1   5 use vars qw($VERSION);
  1         2  
  1         65  
5              
6             $VERSION = '0.02';
7              
8 1     1   1267 use LWP::UserAgent;
  1         86783  
  1         42  
9 1     1   12 use HTTP::Request;
  1         2  
  1         26  
10 1     1   13 use URI::Escape;
  1         2  
  1         547  
11              
12             sub new {
13 1     1 0 108 my ($this, @args) = @_;
14 1         3 my $class = shift;
15 1         6 my $self = bless { @_ }, $class;
16 1         6 return $self->_init(@_);
17             }
18              
19              
20             sub _init {
21 1     1   4 my($self, %params) = @_;;
22              
23             # Caller can set:
24             # \- address: (mandatory)
25             # \- keyserver: (default to 'keyserver.pgp.com')
26             # \- path: (default to '/pks/lookup')
27             # \- command: (default to '?op=index&search=')
28            
29 1 50       5 return undef unless exists($params{address});
30 1   50     9 $self->{keyserver} ||= 'keyserver.pgp.com';
31 1   50     9 $self->{path} ||= '/pks/lookup';
32 1   50     7 $self->{command} ||= '?op=index&search=';
33 1   33     3 $self->{address} ||= uri_escape($params{address});
34              
35 1         6 my $query = "http://" . $self->{keyserver} . $self->{path} . $self->{command} . $self->{address};
36              
37 1         11 my $ua = LWP::UserAgent->new();
38            
39             # Check for *_proxy env vars. Use them if they're there.
40 1         3748 $ua->env_proxy();
41            
42             # Get the page.
43              
44 1         19905 my $req = new HTTP::Request('GET' => $query);
45 1         10882 my $res = $ua->request($req);
46 1 50       809349 unless($res->is_success){
47 0         0 warn(__PACKAGE__ . ":" . $res->status_line);
48 0         0 return undef;
49             }
50 1         21 my $page = $res->content;
51            
52             # Parse the response page. $count contains number of re matches.
53             # An example of the html response is:
54             #
55             # pub 1024/7C2F31DF\
56             # 2001/08/28 Chris J. Ball < 57             # =0x7C2F31DF">chris@void.printf.net>
58              
59 1         32 my $count =()= $page =~ m!pub \d{4}/(.{8}) \d{4}/\d{2}/\d{2} (.*) <!g;
60              
61             # We must only have two matches; one for keyid, one for name. Zero
62             # matches signifies a missed search, and more than two would signify
63             # multiple matches. The reason for giving up and returning undef in
64             # the latter case is explained POD-wards.
65            
66 1 50       83 return undef unless $count == 2;
67 0           $self->{_result} = $1;
68 0           $self->{_name} = $2;
69 0           return $self;
70             }
71              
72 0     0 0   sub name { return $_[0]->{_name} }
73 0     0 0   sub result { return $_[0]->{_result} }
74              
75             1;
76             __END__