File Coverage

blib/lib/XRI/Resolution/Lite.pm
Criterion Covered Total %
statement 22 24 91.6
branch n/a
condition n/a
subroutine 8 8 100.0
pod n/a
total 30 32 93.7


line stmt bran cond sub pod time code
1             package XRI::Resolution::Lite;
2              
3 2     2   1119 use strict;
  2         4  
  2         87  
4 2     2   11 use warnings;
  2         5  
  2         196  
5 2     2   3631 use parent qw(Class::Accessor::Fast);
  2         1118  
  2         11  
6              
7             __PACKAGE__->mk_accessors(qw/resolver ua parser/);
8              
9 2     2   18408 use Carp;
  2         5  
  2         709  
10 2     2   2418 use HTTP::Request;
  2         111996  
  2         125  
11 2     2   2584 use LWP::UserAgent;
  2         55896  
  2         77  
12 2     2   26 use URI;
  2         3  
  2         43  
13 2     2   2014 use XML::LibXML;
  0            
  0            
14              
15             =head1 NAME
16              
17             XRI::Resolution::Lite - The Lightweight client module for XRI Resolution
18              
19             =head1 VERSION
20              
21             version 0.04
22              
23             =cut
24              
25             our $VERSION = '0.04';
26              
27             my %param_map = (
28             format => '_xrd_r',
29             type => '_xrd_t',
30             media => '_xrd_m',
31             );
32              
33             =head1 SYNOPSIS
34              
35             use XML::LibXML::XPathContext;
36             use XRI::Resolution::Lite;
37              
38             my $r = XRI::Resolution::Lite->new;
39             my $xrds = $r->resolve('=zigorou'); ### XML::LibXML::Document
40             my $ctx = XML::LibXML::XPathContext->new($xrds);
41             my @services = $ctx->findnodes('//Service');
42              
43             =head1 METHODS
44              
45             =head2 new
46              
47             =over 2
48              
49             =item $args
50              
51             This param must be HASH reference. Available 2 fields.
52              
53             =over 2
54              
55             =item ua
56              
57             (Optional) L object or its inheritance.
58              
59             =item resolver
60              
61             (Optional) URI string of XRI Proxy Resolver.
62             If this param is omitted, using XRI Global Proxy Resolver, "http://xri.net/", as resolver.
63              
64             =back
65              
66             =back
67              
68             =cut
69              
70             sub new {
71             my ( $class, $args ) = @_;
72              
73             $args ||= +{};
74             $args = +{
75             ua => $args->{ua} || LWP::UserAgent->new,
76             resolver => ( $args->{resolver} )
77             ? ( UNIVERSAL::isa( $args->{resolver}, 'URI' )
78             ? $args->{resolver}
79             : URI->new( $args->{resolver} ) )
80             : URI->new('http://xri.net/'),
81             parser => XML::LibXML->new,
82             };
83              
84             my $self = $class->SUPER::new($args);
85             return $self;
86             }
87              
88             =head2 resolve($qxri, \%params, \%media_flags)
89              
90             When type parameter is substituted "application/xrds+xml" or "application/xrd+xml", the result would be returned as L object.
91             Substituted "text/uri-list" to type parameter, the result would be returned as url list ARRAY or ARRAYREF.
92              
93             =over 2
94              
95             =item $qxri
96              
97             Query XRI string. For example :
98              
99             =zigorou
100             @linksafe
101             @id*zigorou
102              
103             =item $params
104              
105             This param must be HASH reference. Available 3 fields.
106             See Section 3.3 of XRI Resolution 2.0.
107             L
108              
109             =over 2
110              
111             =item format
112              
113             Resolution Output Format. This param would be '_xrd_r' query parameter.
114              
115             =item type
116              
117             Service Type. This param would be '_xrd_t' query parameter.
118              
119             =item media
120              
121             Service Media Type. This param would be '_xrd_m' query parameter.
122              
123             =back
124              
125             =item $media_flags
126              
127             If you want to specify flag on or off, then substitute to 1 as true, 0 as false.
128              
129             =over 2
130              
131             =item https
132              
133             Specifies use of HTTPS trusted resolution. default value is 0.
134              
135             =item saml
136              
137             Specifies use of SAML trusted resolution. default value is 0.
138              
139             =item refs
140              
141             Specifies whether Refs should be followed during resolution (by default they are followed), default value is 1.
142              
143             =item sep
144              
145             Specifies whether service endpoint selection should be performed. default value is 0.
146              
147             =item nodefault_t
148              
149             Specifies whether a default match on a Type service endpoint selection element is allowed. default value is 1.
150              
151             =item nodefault_p
152              
153             Specifies whether a default match on a Path service endpoint selection element is allowed. default value is 1.
154              
155             =item nodefault_m
156              
157             Specifies whether a default match on a MediaType service endpoint selection element is allowed. default value is 1.
158              
159             =item uric
160              
161             Specifies whether a resolver should automatically construct service endpoint URIs. default value is 0.
162              
163             =item cid
164              
165             Specifies whether automatic canonical ID verification should performed. default value is 1
166              
167             =back
168              
169             =back
170              
171             =cut
172              
173             sub resolve {
174             my ( $self, $qxri, $params, $media_flags ) = @_;
175              
176             $params ||= {};
177             $media_flags ||= {};
178              
179             $qxri =~ s|^xri://||; ### normalize
180              
181             my %query = ();
182             %query = (
183             _xrd_r => 'application/xrds+xml',
184             map { ( $param_map{$_}, $params->{$_} ) } keys %$params
185             );
186              
187             my %flags = (
188             https => 0,
189             saml => 0,
190             refs => 1,
191             sep => 0,
192             nodefault_t => 1,
193             nodefault_p => 1,
194             nodefault_m => 1,
195             uric => 0,
196             cid => 1,
197             );
198              
199             $query{'_xrd_r'} .=
200             ';' . join ';' => map { $_->[0] . '=' . $_->[1] ? 'true' : 'false' }
201             map { [ $_, $media_flags->{$_} || $flags{$_} ] }
202             keys %flags;
203              
204             my $hxri = $self->resolver->clone;
205             $hxri->path($qxri);
206             $hxri->query_form(%query);
207              
208             my $req = HTTP::Request->new( GET => $hxri );
209             $req->header( Accept => $params->{type} || 'application/xrds+xml' );
210              
211             my ( $res, $e );
212              
213             eval { $res = $self->ua->request($req); };
214             if ( $e = $@ ) {
215             $@ = undef;
216             croak($e);
217             }
218              
219             croak( $res->status_line ) unless ( $res->is_success ); ### HTTP error
220             croak( $res->content )
221             if ( $res->header('Content-Type') =~ m#^text/plain# )
222             ; ### Invalid Content-Type
223              
224             unless ( defined $params->{format} && $params->{format} eq 'text/uri-list' )
225             { ## XRDS or XRD format
226             my $doc = $self->parser->parse_string( $res->content );
227             return $doc;
228             }
229             else { ## URL List format
230             my @url_list = split "\n" => $res->content;
231             wantarray ? @url_list : \@url_list;
232             }
233             }
234              
235             =head1 SEE ALSO
236              
237             =over 2
238              
239             =item http://docs.oasis-open.org/xri/xri-resolution/2.0/specs/cd03/xri-resolution-V2.0-cd-03.html
240              
241             There are XRI Resolution spec in OASIS.
242              
243             =back
244              
245             =head1 AUTHOR
246              
247             Toru Yamaguchi, C<< >>
248              
249             =head1 BUGS
250              
251             Please report any bugs or feature requests to
252             C, or through the web interface at
253             L. I will be notified, and then you'll automatically be
254             notified of progress on your bug as I make changes.
255              
256             =head1 COPYRIGHT & LICENSE
257              
258             Copyright 2008 Toru Yamaguchi, All Rights Reserved.
259              
260             This program is free software; you can redistribute it and/or modify it
261             under the same terms as Perl itself.
262              
263             =cut
264              
265             1; # End of XRI::Resolution::Lite