- Timestamp:
- 07/20/08 18:43:42 (4 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/openid2/perl/Net-OpenID-Consumer/lib/Net/OpenID/Yadis.pm
r136 r138 15 15 16 16 use constant { 17 YR_HEAD => 0,18 17 YR_GET => 1, 19 18 YR_XRDS => 2, … … 21 20 22 21 use fields ( 23 'cache', # the Cache object sent to URI::Fetch24 '_ua', # Custom LWP::UserAgent instance to use25 22 'last_errcode', # last error code we got 26 23 'last_errtext', # last error code we got 27 24 'debug', # debug flag or codeblock 25 'consumer', # consumer object 28 26 'identity_url', # URL to be identified 29 27 'xrd_url', # URL of XRD file … … 36 34 my %opts = @_; 37 35 38 $self->ua ( delete $opts{ua} ); 39 $self->cache ( delete $opts{cache} ); 36 $self->consumer(delete($opts{consumer})); 40 37 41 38 $self->{debug} = delete $opts{debug}; … … 46 43 } 47 44 48 sub cache { &_getset; } 45 sub consumer { &_getset; } 46 49 47 sub identity_url { &_getset; } 50 48 sub xrd_url { &_getset; } 51 49 sub xrd_objects { _pack_array(&_getset); } 52 sub _ua { &_getset; }53 50 sub _getset { 54 51 my $self = shift; … … 112 109 } 113 110 114 sub ua {115 my $self = shift;116 my $ua = shift if @_;117 Carp::croak("Too many parameters") if @_;118 119 if (($ua) || (!$self->{_ua})) {120 $self->{_ua} = Net::OpenID::Yadis::UA->new($ua);121 }122 123 $self->{_ua}->{'ua'};124 }125 126 111 sub _get_contents { 127 112 my $self = shift; 128 129 113 my ($url, $final_url_ref, $content_ref, $headers_ref) = @_; 130 $final_url_ref ||= do { my $dummy; \$dummy; }; 131 132 my $ures = URI::Fetch->fetch($url,133 UserAgent => $self->_ua,134 Cache => $self->_ua->force_head ? undef : $self->cache,135 ContentAlterHook => sub {my $htmlref = shift;$$htmlref =~ s/<body\b.*//is;}, 136 )137 or return $self->_fail("url_fetch_error", "Error fetching URL: " . URI::Fetch->errstr); 138 139 if ($ures->status == URI::Fetch::URI_GONE()) {140 return $self->_fail("url_gone");141 }142 143 my $res = $ures->http_response;144 145 $$final_url_ref = $res->request->uri->as_string;146 $res->headers->scan(sub{$headers_ref->{lc($_[0])} ||= $_[1];});147 $$content_ref = $ures->content;148 149 return 1;114 115 my $alter_hook = sub { 116 my $htmlref = shift; 117 $$htmlref =~ s/<body\b.*//is; 118 }; 119 120 my $res = Net::OpenID::URIFetch->fetch($url, $self->consumer, $alter_hook); 121 122 if ($res) { 123 $$final_url_ref = $res->final_uri; 124 my $headers = $res->headers; 125 foreach my $k (keys %$headers) { 126 $headers_ref->{$k} ||= $headers->{$k}; 127 } 128 $$content_ref = $res->content; 129 return 1; 130 } 131 else { 132 return undef; 133 } 150 134 } 151 135 … … 153 137 my $self = shift; 154 138 my $url = shift or return $self->_fail("empty_url"); 155 my $count = shift || YR_ HEAD; # $count = YR_HEAD:HEAD request YR_GET:GET request YR_XRDS:XRDS request139 my $count = shift || YR_GET; 156 140 Carp::croak("Too many parameters") if @_; 157 141 … … 164 148 my %headers; 165 149 166 $self->_ua->force_head(1) if ($count == YR_HEAD);167 168 150 my $xrd; 169 151 $self->_get_contents($url, \$final_url, \$xrd, \%headers) or return; … … 173 155 my $doc_url; 174 156 if (($doc_url = $headers{'x-yadis-location'} || $headers{'x-xrds-location'}) && ($count < YR_XRDS)) { 175 return $self->discover($doc_url, YR_XRDS);176 } elsif ($headers{'content-type'} eq 'application/xrds+xml') {177 return $self->discover($final_url,YR_XRDS) if ((!$xrd) && ($count == YR_HEAD));157 return $self->discover($doc_url, YR_XRDS); 158 } 159 elsif ($headers{'content-type'} eq 'application/xrds+xml') { 178 160 $self->xrd_url($final_url); 179 161 return $self->parse_xrd($xrd); 180 162 } 181 182 return $count == YR_HEAD ? $self->discover($final_url,YR_GET) : $self->_fail($count == YR_GET ? "no_yadis_document" :"too_many_hops"); 163 else { 164 return $self->_fail($count == YR_GET ? "no_yadis_document" : "too_many_hops"); 165 } 183 166 } 184 167 … … 225 208 my $code_ref; 226 209 my $protocol = undef; 227 210 228 211 Carp::croak("You haven't called the discover method yet") unless $self->xrd_objects; 229 212 … … 258 241 } 259 242 260 package Net::OpenID::Yadis::UA;261 262 # This module is decolation module to LWP::UserAgent.263 # This add application/xrds+xml HTTP header and GET method to request object used in URI::Fetch.264 265 use strict;266 use warnings;267 use LWP::UserAgent;268 use vars qw($AUTOLOAD $lwpclass);269 270 BEGIN {271 eval "use LWPx::ParanoidAgent;";272 $lwpclass = $@ ? "LWP::UserAgent" : "LWPx::ParanoidAgent";273 }274 275 sub new {276 my $class = shift;277 my $ua = shift;278 unless ($ua) {279 $ua = $lwpclass->new;280 $ua->timeout(10);281 }282 bless {ua => $ua,force_head => 0},$class;283 }284 285 sub request {286 my $self = shift;287 my $req = shift;288 $req->header('Accept' => 'application/xrds+xml');289 $req->method($self->force_head ? "HEAD" : "GET");290 $self->force_head(0);291 $self->{'ua'}->request($req);292 }293 294 sub force_head {295 $_[0]->{'force_head'} = $_[1] if defined($_[1]);296 $_[0]->{'force_head'};297 }298 299 sub AUTOLOAD {300 my $self = shift;301 return if $AUTOLOAD =~ /::DESTROY$/;302 $AUTOLOAD =~ s/.*:://;303 $self->{'ua'}->$AUTOLOAD(@_);304 }305 306 243 1; 307 244 __END__ … … 316 253 317 254 my $disc = Net::OpenID::Yadis->new( 318 ua => $ua, # LWP::UserAgent (or similar) object 319 cache => $cache # Cache object 320 ); 255 consumer => $consumer, # Net::OpenID::Consumer object 256 ); 321 257 322 258 my $xrd = $disc->discover("http://id.example.com/") or Carp::croak($disc->err); … … 351 287 but was forked and simplified for inclusion in the core OpenID Consumer package. 352 288 289 This simplified version is tailored for the needs of Net::OpenID::Consumer; for other 290 uses, L<Net::Yadis::Discovery> is probably a better choice. 291 353 292 =head1 CONSTRUCTOR 354 293 … … 359 298 my $disc = Net::OpenID::Yadis->new([ %opts ]); 360 299 361 You can set the C< ua> and C<cache> in the constructor. See the corresponding362 method description sbelow.300 You can set the C<consumer> in the constructor. See the corresponding 301 method description below. 363 302 364 303 =back … … 370 309 =over 4 371 310 372 =item C<YR_HEAD>373 374 If you set this value to option argument of discover method, module check Yadis375 URL start from HTTP HEAD request.376 377 311 =item C<YR_GET> 378 312 379 If you set this, module check Yadis URL start from HTTP GET request. 313 If you set this, module check Yadis URL start from HTTP GET request. This is the default. 380 314 381 315 =item C<YR_XRDS> 382 316 383 If you set this, this module consider Yadis URL as Yadis Resource Descriptor 384 URL. 385 If not so, error returns. 317 If you set this, this module consider Yadis URL as Yadis Resource Descriptor URL. 318 If not so, an error is returned. 386 319 387 320 =back … … 391 324 =over 4 392 325 393 =item $disc->B<ua>($user_agent) 394 395 =item $disc->B<ua> 396 397 Getter/setter for the LWP::UserAgent (or subclass) instance which will 398 be used when web donwloads are needed. It's highly recommended that 399 you use LWPx::ParanoidAgent, or at least read its documentation so 400 you're aware of why you should care. 401 402 =item $disc->B<cache>($cache) 403 404 =item $disc->B<cache> 405 406 Getter/setter for the optional (but recommended!) cache instance you 407 want to use for storing fetched parts of pages. 408 409 The $cache object can be anything that has a -E<gt>get($key) and 410 -E<gt>set($key,$value) methods. See L<URI::Fetch> for more 411 information. This cache object is just passed to L<URI::Fetch> 412 directly. 326 =item $disc->B<consumer>($consumer) 327 328 =item $disc->B<consumer> 329 330 Get or set the Net::OpenID::Consumer object that this object is associated with. 413 331 414 332 =item $disc->B<discover>($url,[$request_method]) … … 525 443 L<Net::OpenID::Yadis::Service> 526 444 445 L<Net::OpenID::Consumer> 446 527 447 =head1 AUTHORS 528 448
