blib/lib/XML/Atom/Server.pm untested

filter: Statementstmt 0.0% 0 / 198 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 37 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 351 SCAR 69.6
CC
32
Cov
0%
run
20.0
path_info
20.0
request_method
20.0
CRAP 1056.0
1 # $Id$
2  
3 package XML::Atom::Server;
4 use strict;
5  
6 use XML::Atom;
7 use base qw( XML::Atom::ErrorHandler );
8 use MIME::Base64 qw( encode_base64 decode_base64 );
9 use Digest::SHA1 qw( sha1 );
10 use XML::Atom::Util qw( first encode_xml textValue );
11 use XML::Atom::Entry;
12  
13 use constant NS_SOAP => 'http://schemas.xmlsoap.org/soap/envelope/';
14 use constant NS_WSSE => 'http://schemas.xmlsoap.org/ws/2002/07/secext';
15 use constant NS_WSU => 'http://schemas.xmlsoap.org/ws/2002/07/utility';
16  
17 sub handler ($$) {
18 my $class = shift;
19 my($r) = @_;
20 require Apache::Constants;
21 if (lc($r->dir_config('Filter') || '') eq 'on') {
22 $r = $r->filter_register;
23 }
24 my $server = $class->new or die $class->errstr;
25 $server->{apache} = $r;
26 $server->run;
27 return Apache::Constants::OK();
28 }
29  
30 sub new {
31 my $class = shift;
32 my $server = bless { }, $class;
33 $server->init(@_) or return $class->error($server->errstr);
34 $server;
35 }
36  
37 sub init {
38 my $server = shift;
39 $server->{param} = {};
40 unless ($ENV{MOD_PERL}) {
41 require CGI;
42 $server->{cgi} = CGI->new({});
43 }
44 $server;
45 }
46  
47 sub run {
48 my $server = shift;
49 (my $pi = $server->path_info) =~ s!^/!!;
50 my @args = split /\//, $pi;
51 for my $arg (@args) {
52 my($k, $v) = split /=/, $arg, 2;
53 $server->request_param($k, $v);
54 }
55 if (my $action = $server->request_header('SOAPAction')) {
56 $server->{is_soap} = 1;
57 $action =~ s/"//g;
58 my($method) = $action =~ m!/([^/]+)$!;
59 $server->request_method($method);
60 }
61 my $out;
62 eval {
63 defined($out = $server->handle_request) or die $server->errstr;
64 if (defined $out && $server->{is_soap}) {
65 $out =~ s!^(<\?xml.*?\?>)!!;
66 $out = <<SOAP;
67 $1
68 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
69 <soap:Body>$out</soap:Body>
70 </soap:Envelope>
71 SOAP
72 }
73 };
74 if ($@) {
75 $out = $server->show_error($@);
76 }
77 $server->send_http_header;
78 $server->print($out);
79 1;
80 }
81  
82 sub handle_request;
83 sub password_for_user;
84  
85 sub uri {
86 my $server = shift;
87 $ENV{MOD_PERL} ? $server->{apache}->uri : $server->{cgi}->url;
88 }
89  
90 sub path_info {
91 my $server = shift;
92 return $server->{__path_info} if exists $server->{__path_info};
93 my $path_info;
94 if ($ENV{MOD_PERL}) {
95 ## mod_perl often leaves part of the script name (Location)
96 ## in the path info, for some reason. This should remove it.
97 $path_info = $server->{apache}->path_info;
98 if ($path_info) {
99 my($script_last) = $server->{apache}->location =~ m!/([^/]+)$!;
100 $path_info =~ s!^/$script_last!!;
101 }
102 } else {
103 $path_info = $server->{cgi}->path_info;
104 }
105 $server->{__path_info} = $path_info;
106 }
107  
108 sub request_header {
109 my $server = shift;
110 my($key) = @_;
111 if ($ENV{MOD_PERL}) {
112 return $server->{apache}->header_in($key);
113 } else {
114 ($key = uc($key)) =~ tr/-/_/;
115 return $ENV{'HTTP_' . $key};
116 }
117 }
118  
119 sub request_method {
120 my $server = shift;
121 if (@_) {
122 $server->{request_method} = shift;
123 } elsif (!exists $server->{request_method}) {
124 $server->{request_method} =
125 $ENV{MOD_PERL} ? $server->{apache}->method : $ENV{REQUEST_METHOD};
126 }
127 $server->{request_method};
128 }
129  
130 sub request_content {
131 my $server = shift;
132 unless (exists $server->{request_content}) {
133 if ($ENV{MOD_PERL}) {
134 ## Read from $server->{apache}
135 my $r = $server->{apache};
136 my $len = $server->request_header('Content-length');
137 $r->read($server->{request_content}, $len);
138 } else {
139 ## Read from STDIN
140 my $len = $ENV{CONTENT_LENGTH} || 0;
141 read STDIN, $server->{request_content}, $len;
142 }
143 }
144 $server->{request_content};
145 }
146  
147 sub request_param {
148 my $server = shift;
149 my $k = shift;
150 $server->{param}{$k} = shift if @_;
151 $server->{param}{$k};
152 }
153  
154 sub response_header {
155 my $server = shift;
156 my($key, $val) = @_;
157 if ($ENV{MOD_PERL}) {
158 $server->{apache}->header_out($key, $val);
159 } else {
160 unless ($key =~ /^-/) {
161 ($key = lc($key)) =~ tr/-/_/;
162 $key = '-' . $key;
163 }
164 $server->{cgi_headers}{$key} = $val;
165 }
166 }
167  
168 sub response_code {
169 my $server = shift;
170 $server->{response_code} = shift if @_;
171 $server->{response_code};
172 }
173  
174 sub response_content_type {
175 my $server = shift;
176 $server->{response_content_type} = shift if @_;
177 $server->{response_content_type};
178 }
179  
180 sub send_http_header {
181 my $server = shift;
182 my $type = $server->response_content_type || 'application/x.atom+xml';
183 if ($ENV{MOD_PERL}) {
184 $server->{apache}->status($server->response_code || 200);
185 $server->{apache}->send_http_header($type);
186 } else {
187 $server->{cgi_headers}{-status} = $server->response_code || 200;
188 $server->{cgi_headers}{-type} = $type;
189 print $server->{cgi}->header(%{ $server->{cgi_headers} });
190 }
191 }
192  
193 sub print {
194 my $server = shift;
195 if ($ENV{MOD_PERL}) {
196 $server->{apache}->print(@_);
197 } else {
198 CORE::print(@_);
199 }
200 }
201  
202 sub error {
203 my $server = shift;
204 my($code, $msg) = @_;
205 $server->response_code($code) if ref($server);
206 return $server->SUPER::error($msg);
207 }
208  
209 sub show_error {
210 my $server = shift;
211 my($err) = @_;
212 chomp($err = encode_xml($err));
213 if ($server->{is_soap}) {
214 my $code = $server->response_code;
215 if ($code >= 400) {
216 $server->response_code(500);
217 }
218 return <<FAULT;
219 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
220 <soap:Body>
221 <soap:Fault>
222 <faultcode>$code</faultcode>
223 <faultstring>$err</faultstring>
224 </soap:Fault>
225 </soap:Body>
226 </soap:Envelope>
227 FAULT
228 } else {
229 return <<ERR;
230 <?xml version="1.0" encoding="UTF-8"?>
231 <error>$err</error>
232 ERR
233 }
234 }
235  
236 sub get_auth_info {
237 my $server = shift;
238 my %param;
239 if ($server->{is_soap}) {
240 my $xml = $server->xml_body;
241 my $auth = first($xml, NS_WSSE, 'UsernameToken');
242 $param{Username} = textValue($auth, NS_WSSE, 'Username');
243 $param{PasswordDigest} = textValue($auth, NS_WSSE, 'Password');
244 $param{Nonce} = textValue($auth, NS_WSSE, 'Nonce');
245 $param{Created} = textValue($auth, NS_WSSE, 'Created');
246 } else {
247 my $req = $server->request_header('X-WSSE')
248 or return $server->auth_failure(401, 'X-WSSE authentication required');
249 $req =~ s/^(?:WSSE|UsernameToken) //;
250 for my $i (split /,\s*/, $req) {
251 my($k, $v) = split /=/, $i, 2;
252 $v =~ s/^"//;
253 $v =~ s/"$//;
254 $param{$k} = $v;
255 }
256 }
257 \%param;
258 }
259  
260 sub authenticate {
261 my $server = shift;
262 my $auth = $server->get_auth_info or return;
263 for my $f (qw( Username PasswordDigest Nonce Created )) {
264 return $server->auth_failure(400, "X-WSSE requires $f")
265 unless $auth->{$f};
266 }
267 my $password = $server->password_for_user($auth->{Username});
268 defined($password) or return $server->auth_failure(403, 'Invalid login');
269 my $expected = encode_base64(sha1(
270 decode_base64($auth->{Nonce}) . $auth->{Created} . $password
271 ), '');
272 return $server->auth_failure(403, 'Invalid login')
273 unless $expected eq $auth->{PasswordDigest};
274 return 1;
275 }
276  
277 sub auth_failure {
278 my $server = shift;
279 $server->response_header('WWW-Authenticate', 'WSSE profile="UsernameToken"');
280 return $server->error(@_);
281 }
282  
283 sub xml_body {
284 my $server = shift;
285 unless (exists $server->{xml_body}) {
286 if (LIBXML) {
287 my $parser = $server->libxml_parser;
288 $server->{xml_body} =
289 $parser->parse_string($server->request_content);
290 } else {
291 $server->{xml_body} =
292 XML::XPath->new(xml => $server->request_content);
293 }
294 }
295 $server->{xml_body};
296 }
297  
298 sub atom_body {
299 my $server = shift;
300 my $atom;
301 if ($server->{is_soap}) {
302 my $xml = $server->xml_body;
303 $atom = XML::Atom::Entry->new(Doc => first($xml, NS_SOAP, 'Body'))
304 or return $server->error(500, XML::Atom::Entry->errstr);
305 } else {
306 $atom = XML::Atom::Entry->new(Stream => \$server->request_content)
307 or return $server->error(500, XML::Atom::Entry->errstr);
308 }
309 $atom;
310 }
311  
312 sub libxml_parser { XML::Atom->libxml_parser }
313  
314 1;
315 __END__
316  
317 =head1 NAME
318  
319 XML::Atom::Server - A server for the Atom API
320  
321 =head1 SYNOPSIS
322  
323 package My::Server;
324 use base qw( XML::Atom::Server );
325 sub handle_request {
326 my $server = shift;
327 $server->authenticate or return;
328 my $method = $server->request_method;
329 if ($method eq 'POST') {
330 return $server->new_post;
331 }
332 ...
333 }
334  
335 my %Passwords;
336 sub password_for_user {
337 my $server = shift;
338 my($username) = @_;
339 $Passwords{$username};
340 }
341  
342 sub new_post {
343 my $server = shift;
344 my $entry = $server->atom_body or return;
345 ## $entry is an XML::Atom::Entry object.
346 ## ... Save the new entry ...
347 }
348  
349 package main;
350 my $server = My::Server->new;
351 $server->run;
352  
353 =head1 DESCRIPTION
354  
355 I<XML::Atom::Server> provides a base class for Atom API servers. It handles
356 all core server processing, both the SOAP and REST formats of the protocol,
357 and WSSE authentication. It can also run as either a mod_perl handler or as
358 part of a CGI program.
359  
360 It does not provide functions specific to any particular implementation,
361 such as posting an entry, retrieving a list of entries, deleting an entry, etc.
362 Implementations should subclass I<XML::Atom::Server>, overriding the
363 I<handle_request> method, and handle all functions such as this themselves.
364  
365 =head1 SUBCLASSING
366  
367 =head2 Request Handling
368  
369 Subclasses of I<XML::Atom::Server> must override the I<handle_request>
370 method to perform all request processing. The implementation must set all
371 response headers, including the response code and any relevant HTTP headers,
372 and should return a scalar representing the response body to be sent back
373 to the client.
374  
375 For example:
376  
377 sub handle_request {
378 my $server = shift;
379 my $method = $server->request_method;
380 if ($method eq 'POST') {
381 return $server->new_post;
382 }
383 ## ... handle GET, PUT, etc
384 }
385
386 sub new_post {
387 my $server = shift;
388 my $entry = $server->atom_body or return;
389 my $id = save_this_entry($entry); ## Implementation-specific
390 $server->response_header(Location => $server->uri . '/entry_id=' . $id);
391 $server->response_code(201);
392 $server->response_content_type('application/x.atom+xml');
393 return serialize_entry($entry); ## Implementation-specific
394 }
395  
396 =head2 Authentication
397  
398 Servers that require authentication for posting or retrieving entries or
399 feeds should override the I<password_for_user> method. Given a username
400 (from the WSSE header), I<password_for_user> should return that user's
401 password in plaintext. This will then be combined with the nonce and the
402 creation time to generate the digest, which will be compared with the
403 digest sent in the WSSE header. If the supplied username doesn't exist in
404 your user database or alike, just return C<undef>.
405  
406 For example:
407  
408 my %Passwords = ( foo => 'bar' ); ## The password for "foo" is "bar".
409 sub password_for_user {
410 my $server = shift;
411 my($username) = @_;
412 $Passwords{$username};
413 }
414  
415 =head1 METHODS
416  
417 I<XML::Atom::Server> provides a variety of methods to be used by subclasses
418 for retrieving headers, content, and other request information, and for
419 setting the same on the response.
420  
421 =head2 Client Request Parameters
422  
423 =over 4
424  
425 =item * $server->uri
426  
427 Returns the URI of the Atom server implementation.
428  
429 =item * $server->request_method
430  
431 Returns the name of the request method sent to the server from the client
432 (for example, C<GET>, C<POST>, etc). Note that if the client sent the
433 request in a SOAP envelope, the method is obtained from the I<SOAPAction>
434 HTTP header.
435  
436 =item * $server->request_header($header)
437  
438 Retrieves the value of the HTTP request header I<$header>.
439  
440 =item * $server->request_content
441  
442 Returns a scalar containing the contents of a POST or PUT request from the
443 client.
444  
445 =item * $server->request_param($param)
446  
447 I<XML::Atom::Server> automatically parses the PATH_INFO sent in the request
448 and breaks it up into key-value pairs. This can be used to pass parameters.
449 For example, in the URI
450  
451 http://localhost/atom-server/entry_id=1
452  
453 the I<entry_id> parameter would be set to C<1>.
454  
455 I<request_param> returns the value of the value of the parameter I<$param>.
456  
457 =back
458  
459 =head2 Setting up the Response
460  
461 =over 4
462  
463 =item * $server->response_header($header, $value)
464  
465 Sets the value of the HTTP response header I<$header> to I<$value>.
466  
467 =item * $server->response_code([ $code ])
468  
469 Returns the current response code to be sent back to the client, and if
470 I<$code> is given, sets the response code.
471  
472 =item * $server->response_content_type([ $type ])
473  
474 Returns the current I<Content-Type> header to be sent back to the client, and
475 I<$type> is given, sets the value for that header.
476  
477 =back
478  
479 =head2 Processing the Request
480  
481 =over 4
482  
483 =item * $server->authenticate
484  
485 Attempts to authenticate the request based on the authentication
486 information present in the request (currently just WSSE). This will call
487 the I<password_for_user> method in the subclass to obtain the cleartext
488 password for the username given in the request.
489  
490 =item * $server->atom_body
491  
492 Returns an I<XML::Atom::Entry> object containing the entry sent in the
493 request.
494  
495 =back
496  
497 =head1 USAGE
498  
499 Once you have defined your server subclass, you can set it up either as a
500 CGI program or as a mod_perl handler.
501  
502 A simple CGI program would look something like this:
503  
504 #!/usr/bin/perl -w
505 use strict;
506  
507 use My::Server;
508 my $server = My::Server->new;
509 $server->run;
510  
511 A simple mod_perl handler configuration would look something like this:
512  
513 PerlModule My::Server
514 <Location /atom-server>
515 SetHandler perl-script
516 PerlHandler My::Server
517 </Location>
518  
519 =head1 ERROR HANDLING
520  
521 If you wish to return an error from I<handle_request>, you can use the
522 built-in I<error> method:
523  
524 sub handle_request {
525 my $server = shift;
526 ...
527 return $server->error(500, "Something went wrong");
528 }
529  
530 This will be returned to the client with a response code of 500 and an
531 error string of C<Something went wrong>. Errors are automatically
532 serialized into SOAP faults if the incoming request is enclosed in a SOAP
533 envelope.
534  
535 =head1 AUTHOR & COPYRIGHT
536  
537 Please see the I<XML::Atom> manpage for author, copyright, and license
538 information.
539  
540 =cut