/usr/share/perl5/XML/Atom/Client.pm is in libxml-atom-perl 0.41-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | # $Id$
package XML::Atom::Client;
use strict;
use XML::Atom;
use base qw( XML::Atom::ErrorHandler );
use LWP::UserAgent;
use XML::Atom::Entry;
use XML::Atom::Feed;
use XML::Atom::Util qw( first textValue );
use Digest::SHA qw( sha1 );
use MIME::Base64 qw( encode_base64 );
use DateTime;
use constant NS_SOAP => 'http://schemas.xmlsoap.org/soap/envelope/';
sub new {
my $class = shift;
my $client = bless { }, $class;
$client->init(@_) or return $class->error($client->errstr);
$client;
}
sub init {
my $client = shift;
my %param = @_;
$client->{ua} = LWP::UserAgent::AtomClient->new($client);
$client->{ua}->agent('XML::Atom/' . XML::Atom->VERSION);
$client->{ua}->parse_head(0);
$client;
}
sub username {
my $client = shift;
$client->{username} = shift if @_;
$client->{username};
}
sub password {
my $client = shift;
$client->{password} = shift if @_;
$client->{password};
}
sub use_soap {
my $client = shift;
$client->{use_soap} = shift if @_;
$client->{use_soap};
}
sub auth_digest {
my $client = shift;
$client->{auth_digest} = shift if @_;
$client->{auth_digest};
}
sub getEntry {
my $client = shift;
my($url) = @_;
my $req = HTTP::Request->new(GET => $url);
my $res = $client->make_request($req);
return $client->error("Error on GET $url: " . $res->status_line)
unless $res->code == 200;
XML::Atom::Entry->new(Stream => \$res->content);
}
sub createEntry {
my $client = shift;
my($uri, $entry) = @_;
return $client->error("Must pass a PostURI before posting")
unless $uri;
my $req = HTTP::Request->new(POST => $uri);
$req->content_type($entry->content_type);
my $xml = $entry->as_xml;
_utf8_off($xml);
$req->content_length(length $xml);
$req->content($xml);
my $res = $client->make_request($req);
return $client->error("Error on POST $uri: " . $res->status_line)
unless $res->code == 201;
$res->header('Location') || 1;
}
sub updateEntry {
my $client = shift;
my($url, $entry) = @_;
my $req = HTTP::Request->new(PUT => $url);
$req->content_type($entry->content_type);
my $xml = $entry->as_xml;
_utf8_off($xml);
$req->content_length(length $xml);
$req->content($xml);
my $res = $client->make_request($req);
return $client->error("Error on PUT $url: " . $res->status_line)
unless $res->code == 200;
1;
}
sub deleteEntry {
my $client = shift;
my($url) = @_;
my $req = HTTP::Request->new(DELETE => $url);
my $res = $client->make_request($req);
return $client->error("Error on DELETE $url: " . $res->status_line)
unless $res->code == 200;
1;
}
sub getFeed {
my $client = shift;
my($uri) = @_;
return $client->error("Must pass a FeedURI before retrieving feed")
unless $uri;
my $req = HTTP::Request->new(GET => $uri);
my $res = $client->make_request($req);
return $client->error("Error on GET $uri: " . $res->status_line)
unless $res->code == 200;
my $feed = XML::Atom::Feed->new(Stream => \$res->content)
or return $client->error(XML::Atom::Feed->errstr);
$feed;
}
sub make_request {
my $client = shift;
my($req) = @_;
$client->munge_request($req);
my $res = $client->{ua}->request($req);
$client->munge_response($res);
$client->{response} = $res;
$res;
}
sub munge_request {
my $client = shift;
my($req) = @_;
$req->header(
Accept => 'application/atom+xml, application/x.atom+xml, application/xml, text/xml, */*',
);
my $nonce = $client->make_nonce;
my $nonce_enc = encode_base64($nonce, '');
my $now = DateTime->now->iso8601 . 'Z';
my $digest = encode_base64(sha1($nonce . $now . ($client->password || '')), '');
if ($client->use_soap) {
my $xml = $req->content || '';
$xml =~ s!^(<\?xml.*?\?>)!!;
my $method = $req->method;
$xml = ($1 || '') . <<SOAP;
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility"
xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/07/secext">
<soap:Header>
<wsse:Security>
<wsse:UsernameToken>
<wsse:Username>@{[ $client->username || '' ]}</wsse:Username>
<wsse:Password Type="wsse:PasswordDigest">$digest</wsse:Password>
<wsse:Nonce>$nonce_enc</wsse:Nonce>
<wsu:Created>$now</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soap:Body>
<$method xmlns="http://schemas.xmlsoap.org/wsdl/http/">
$xml
</$method>
</soap:Body>
</soap:Envelope>
SOAP
$req->content($xml);
$req->content_length(length $xml);
$req->header('SOAPAction', 'http://schemas.xmlsoap.org/wsdl/http/' . $method);
$req->method('POST');
$req->content_type('text/xml');
} else {
if ($client->username) {
$req->header('X-WSSE', sprintf
qq(UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"),
$client->username || '', $digest, $nonce_enc, $now);
$req->header('Authorization', 'WSSE profile="UsernameToken"');
}
}
}
sub munge_response {
my $client = shift;
my($res) = @_;
if ($client->use_soap && (my $xml = $res->content)) {
my $doc;
if (LIBXML) {
my $parser = $client->libxml_parser;
$doc = $parser->parse_string($xml);
} else {
my $xp = XML::XPath->new(xml => $xml);
$doc = ($xp->find('/')->get_nodelist)[0];
}
my $body = first($doc, NS_SOAP, 'Body');
if (my $fault = first($body, NS_SOAP, 'Fault')) {
$res->code(textValue($fault, undef, 'faultcode'));
$res->message(textValue($fault, undef, 'faultstring'));
$res->content('');
$res->content_length(0);
} else {
$xml = join '', map $_->toString(LIBXML ? 1 : 0),
LIBXML ? $body->childNodes : $body->getChildNodes;
$res->content($xml);
$res->content_length(1);
}
}
}
sub make_nonce { sha1(sha1(time() . {} . rand() . $$)) }
sub _utf8_off {
if ($] >= 5.008) {
require Encode;
Encode::_utf8_off($_[0]);
}
}
sub libxml_parser { XML::Atom->libxml_parser }
package LWP::UserAgent::AtomClient;
use strict;
use Scalar::Util;
use base qw( LWP::UserAgent );
my %ClientOf;
sub new {
my($class, $client) = @_;
my $ua = $class->SUPER::new;
$ClientOf{$ua} = $client;
Scalar::Util::weaken($ClientOf{$ua});
$ua;
}
sub get_basic_credentials {
my($ua, $realm, $url, $proxy) = @_;
my $client = $ClientOf{$ua} or die "Cannot find $ua";
return $client->username, $client->password;
}
sub DESTROY {
my $self = shift;
delete $ClientOf{$self};
}
1;
__END__
=head1 NAME
XML::Atom::Client - A client for the Atom API
=head1 SYNOPSIS
use XML::Atom::Client;
use XML::Atom::Entry;
my $api = XML::Atom::Client->new;
$api->username('Melody');
$api->password('Nelson');
my $entry = XML::Atom::Entry->new;
$entry->title('New Post');
$entry->content('Content of my post.');
my $EditURI = $api->createEntry($PostURI, $entry);
my $feed = $api->getFeed($FeedURI);
my @entries = $feed->entries;
my $entry = $api->getEntry($EditURI);
=head1 DESCRIPTION
I<XML::Atom::Client> implements a client for the Atom API described at
I<http://bitworking.org/projects/atom/draft-gregorio-09.html>, with the
authentication scheme described at
I<http://www.intertwingly.net/wiki/pie/DifferentlyAbledClients>.
B<NOTE:> the API, and particularly the authentication scheme, are still
in flux.
=head1 USAGE
=head2 XML::Atom::Client->new(%param)
=head2 $api->use_soap([ 0 | 1 ])
I<XML::Atom::Client> supports both the REST and SOAP-wrapper versions of the
Atom API. By default, the REST version of the API will be used, but you can
turn on the SOAP wrapper--for example, if you need to connect to a server
that supports only the SOAP wrapper--by calling I<use_soap> with a value of
C<1>:
$api->use_soap(1);
If called without arguments, returns the current value of the flag.
=head2 $api->username([ $username ])
If called with an argument, sets the username for login to I<$username>.
Returns the current username that will be used when logging in to the
Atom server.
=head2 $api->password([ $password ])
If called with an argument, sets the password for login to I<$password>.
Returns the current password that will be used when logging in to the
Atom server.
=head2 $api->createEntry($PostURI, $entry)
Creates a new entry.
I<$entry> must be an I<XML::Atom::Entry> object.
=head2 $api->getEntry($EditURI)
Retrieves the entry with the given URL I<$EditURI>.
Returns an I<XML::Atom::Entry> object.
=head2 $api->updateEntry($EditURI, $entry)
Updates the entry at URL I<$EditURI> with the entry I<$entry>, which must be
an I<XML::Atom::Entry> object.
Returns true on success, false otherwise.
=head2 $api->deleteEntry($EditURI)
Deletes the entry at URL I<$EditURI>.
=head2 $api->getFeed($FeedURI)
Retrieves the feed at I<$FeedURI>.
Returns an I<XML::Atom::Feed> object representing the feed returned
from the server.
=head2 ERROR HANDLING
Methods return C<undef> on error, and the error message can be retrieved
using the I<errstr> method.
=head1 AUTHOR & COPYRIGHT
Please see the I<XML::Atom> manpage for author, copyright, and license
information.
=cut
|