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

filter: Statementstmt 76.4% 136 / 178 Branchbran 55.5% 20 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 70.2% 26 / 37 Podpod 0.0% 0 / 19 totaltotal 64.9% 185 / 285 CC 25 SCAR 13.1
CC
25
Cov
71%
authenticate
9.1
new
6.7
handle_psgi
6.1
CRAP 3.7
↑ Summary
1 package XML::Atom::Server::PSGI;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Digest::SHA1 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Scope::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use XML::Atom::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use XML::Atom::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 use Class::Accessor::Lite
11 2 ▶ rw => [ qw(callbacks request response xml_parser) ],
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ;
13  
14 2 ▶use constant NS_SOAP => 'http://schemas.xmlsoap.org/soap/envelope/';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use constant NS_WSSE => 'http://schemas.xmlsoap.org/ws/2002/07/secext';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use constant NS_WSU => 'http://schemas.xmlsoap.org/ws/2002/07/utility';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 2 our $VERSION = "0.04";
20  
21 1 ▶sub psgi_app {
Subroutine psgi_app: called CC=1 SCAR=0.0
Pod undocumented
22 1 my $self = shift;
23 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 2 $self->handle_psgi(@_);
25 1 };
26 }
27  
28 # alias
29 2 *req = \&request;
30 2 *res = \&response;
31  
32 1 ▶sub new {
Subroutine new: called CC=5 SCAR=19.1
Pod undocumented
33 1 my $klass = shift;
34 my $self = bless {
35 0 ▶ (@_ == 1 && ref($_[0]) eq 'HASH' ? %{$_[0]} : @_),
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
36 }, $klass;
37 1 ▶ if (! $self->xml_parser()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->xml_parser) 1 0 1
38 1 $self->xml_parser(XML::Atom::Server::PSGI::XMLParser->new);
39 }
40 1 ▶ if (! $self->callbacks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->callbacks) 0 1 1
41 0 $self->callbacks({});
42 }
43 1 return $self;
44 }
45  
46 2 ▶sub handle_psgi {
Subroutine handle_psgi: called CC=6 SCAR=18.1
Pod undocumented
47 2 my ($self, $env) = @_;
48 2 my $req = Plack::Request->new($env);
49 2 my $res = $req->new_response(200);
50 2 $res->content_type('application/x.atom+xml');
51  
52 2 $self->request($req);
53 2 $self->response($res);
54 # Make sure these things are all cleaned up afterwards
55 2 ▶ my $guard = Scope::Guard->new(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 2 $self->request(undef);
57 2 $self->response(undef);
58 2 });
59  
60 2 $env->{'xml.atom.server.request_method'} = $req->method;
61  
62 # Process parameters in path_info
63 2 my $path_info = $req->path_info;
64 2 my $params = Hash::MultiValue->new;
65 2 $path_info =~ s/^\///;
66 2 foreach my $arg (split /\//, $path_info) {
67 6 my ($k, $v) = split /=/, $arg, 2;
68 6 $params->add($k, $v);
69 }
70 2 $env->{'xml.atom.server.request_params'} = $params;
71  
72 2 ▶ if (my $action = $req->header('SOAPAction')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $action = $req->header("SOAPAction")) 1 1 2
73 1 $env->{'xml.atom.server.is_soap'} = 1;
74 1 $action =~ s/"//g;
75 1 my ($method) = $action =~ m!/([^/]+)$!;
76 1 $env->{'xml.atom.server.request_method'} = $method;
77 }
78  
79 2 eval {
80 2 $self->_call('handle_request');
81 2 ▶ if ($self->is_soap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_soap) 1 1 2
82 1 my $body = $res->body;
83 1 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 1 0 1
84 1 $body =~ s!^(<\?xml.*?\?>)!!;
85 1 $body = <<SOAP;
86 $1
87 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
88 <soap:Body>$body</soap:Body>
89 </soap:Envelope>
90 SOAP
91 1 $res->body($body);
92 }
93 }
94 };
95 2 ▶ if (my $E = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $E = $@) 0 2 2
96 # Escape
97 0 $E =~ s/</&lt;/g;
98 0 $E =~ s/>/&gt;/g;
99 0 $res->code(500);
100 0 $res->body(<<EOXML);
101 <?xml version="1.0" encoding="UTF-8"?>
102 <error>$E</error>
103 EOXML
104 }
105  
106 2 return $res->finalize;
107 }
108  
109 # for compat
110 0 ▶sub request_param {
Subroutine request_param: not called CC=1 SCAR=6.9
Pod undocumented
111 0 shift->request_params->get(@_);
112 }
113  
114 2 ▶sub request_params {
Subroutine request_params: called CC=1 SCAR=0.0
Pod undocumented
115 2 return $_[0]->req->env->{'xml.atom.server.request_params'};
116 }
117  
118 # for compat
119 0 ▶sub request_content {
Subroutine request_content: not called CC=1 SCAR=6.9
Pod undocumented
120 0 shift->req->content;
121 }
122  
123 # for compat
124 0 ▶sub uri {
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
125 0 return $_[0]->req->uri;
126 }
127  
128 # for compat
129 0 ▶sub request_method {
Subroutine request_method: not called CC=1 SCAR=6.9
Pod undocumented
130 0 return $_[0]->req->env->{'xml.atom.server.request_method'};
131 }
132  
133 # for compat
134 0 ▶sub request_header {
Subroutine request_header: not called CC=1 SCAR=6.9
Pod undocumented
135 0 return $_[0]->req->header($_[1]);
136 }
137  
138 # for compat
139 0 ▶sub response_header {
Subroutine response_header: not called CC=1 SCAR=6.9
Pod undocumented
140 0 return shift->res->header(@_);
141 }
142  
143 # for compat
144 0 ▶sub response_content_type {
Subroutine response_content_type: not called CC=1 SCAR=6.9
Pod undocumented
145 0 return shift->res->content_type(@_);
146 }
147  
148 # for compat
149 0 ▶sub response_code {
Subroutine response_code: not called CC=1 SCAR=6.9
Pod undocumented
150 0 return shift->res->code(@_);
151 }
152  
153 # for compat
154 4 ▶sub is_soap {
Subroutine is_soap: called CC=1 SCAR=0.0
Pod undocumented
155 4 return $_[0]->req->env->{'xml.atom.server.is_soap'};
156 }
157  
158 4 ▶sub _call {
Subroutine _call: called CC=3 SCAR=12.7
159 4 my ($self, $name, @args) = @_;
160  
161 4 ▶ my $cb = $self->callbacks->{"on_$name"} ||
Condition33%1/3
$self->callbacks->{"on_$name"} || $self->can($name)
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->callbacks->{"on_$name"} || $self->can($name)
$self->callbacks->{"on_$name"} $self->can($name)
Truth table33%1/3
A || B
A= $self->callbacks->{"on_$name"}B= $self->can($name)
ABresult
1X1
011
000
162 $self->can($name);
163 4 ▶ if (! $cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 4 4
164 0 Carp::croak("no callback nor overridden method $name found");
165 }
166 4 return $cb->($self, @args);
167 }
168  
169 2 ▶sub get_auth_info {
Subroutine get_auth_info: called CC=5 SCAR=16.4
Pod undocumented
170 2 my $self = shift;
171  
172 2 my $req = $self->req;
173 2 my %param; # XXX Hash::MultiValue?
174 2 ▶ if ($self->is_soap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_soap) { } 1 1 2
175 1 my $xml = $self->xml_body;
176 1 my $auth = XML::Atom::Util::first($xml, NS_WSSE, 'UsernameToken');
177 1 $param{Username} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Username');
178 1 $param{PasswordDigest} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Password');
179 1 $param{Nonce} = XML::Atom::Util::textValue($auth, NS_WSSE, 'Nonce');
180 $param{Created} =
181 # Using XML::Atom::Client, Created comes with WSU namespace,
182 # but some the original code in XML::Atom::Server only looks
183 # at WSSE
184 1 ▶ XML::Atom::Util::textValue($auth, NS_WSSE, 'Created') ||
Condition33%1/3
XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/secext', 'Created') || XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/utility', 'Created')
l!l&&r!l&&!r
010
MC/DC0%0/2
XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/secext', 'Created') || XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/utility', 'Created')
XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/secext', 'Created') XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/utility', 'Created')
Truth table33%1/3
A || B
A= XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/secext', 'Created')B= XML::Atom::Util::textValue($auth, 'http://schemas.xmlsoap.org/ws/2002/07/utility', 'Created')
ABresult
1X1
011
000
185 XML::Atom::Util::textValue($auth, NS_WSU, 'Created');
186 } else {
187 1 my $wsse = $req->header('X-WSSE');
188 1 ▶ if (! $wsse) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wsse) 0 1 1
189 0 $self->auth_failure(401, 'X-WSSE authentication required');
190 0 return;
191 }
192  
193 1 $wsse =~ s/^(?:WSSE|UsernameToken) //;
194 1 for my $i (split /,\s*/, $wsse) {
195 4 my($k, $v) = split /=/, $i, 2;
196 4 $v =~ s/^"//;
197 4 $v =~ s/"$//;
198 4 $param{$k} = $v;
199 }
200 }
201 2 return \%param;
202 }
203  
204 2 ▶sub authenticate {
Subroutine authenticate: called CC=6 SCAR=22.0
Pod undocumented
205 2 my $self = shift;
206 2 my $auth = $self->get_auth_info;
207 2 ▶ if (! $auth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth) 0 2 2
208 0 return;
209 }
210  
211 2 for my $f (qw( Username PasswordDigest Nonce Created )) {
212 8 ▶ if (! $auth->{$f}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($auth->{$f}) 0 8 8
213 0 $self->auth_failure(400, "X-WSSE requires $f");
214 0 return;
215 }
216 }
217 2 my $password = $self->_call('password_for_user', $auth->{Username});
218 2 ▶ if (! defined $password) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $password) 0 2 2
219 0 $self->auth_failure(403, 'Invalid login');
220 0 return;
221 }
222  
223 my $expected = MIME::Base64::encode_base64(
224 Digest::SHA1::sha1(
225 MIME::Base64::decode_base64($auth->{Nonce}) .
226 $auth->{Created} .
227 2 $password
228 ),
229 '');
230 2 ▶ if ($expected ne $auth->{PasswordDigest}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expected ne $auth->{"PasswordDigest"}) 0 2 2
231 0 $self->auth_failure(403, 'Invalid login');
232 0 return;
233 }
234 2 return 1;
235 }
236  
237 0 ▶sub auth_failure {
Subroutine auth_failure: not called CC=1 SCAR=6.9
Pod undocumented
238 0 my ($self, $code, $reason) = @_;
239 0 my $res = $self->res;
240 0 $res->header('WWW-Authenticate', 'WSSE profile="UsernameToken"');
241 0 $self->error($code, $reason);
242 }
243  
244 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
245 0 my ($self, $code, $reason) = @_;
246 0 my $res = $self->res;
247 0 $res->code($code);
248 # XXX PSGI doesn't really give us a way to override the
249 # message portion of response status line, so shove it in
250 # X-Reason header
251 0 $res->header('X-Reason', $reason);
252 }
253  
254 1 ▶sub xml_body {
Subroutine xml_body: called CC=3 SCAR=11.3
Pod undocumented
255 1 my $self = shift;
256  
257 1 my $req = $self->req;
258 1 my $env = $req->env;
259 1 my $body = $env->{'xml.server.xml_body'};
260 1 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 0 1 1
261 0 return $body;
262 }
263  
264 1 $body = $self->xml_parser->parse_string($req->content);
265 1 ▶ if (defined $body) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) 1 0 1
266 1 $env->{'xml.server.xml_body'} = $body;
267 }
268 1 return $body;
269 }
270  
271 0 ▶sub atom_body {
Subroutine atom_body: not called CC=2 SCAR=17.9
Pod undocumented
272 0 my $self = shift;
273  
274 0 my $req = $self->req;
275 0 my $env = $req->env;
276 0 my $atom;
277 0 ▶ if ($self->is_soap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_soap) { } 0 0 0
278 0 my $xml = $self->xml_body;
279 0 $atom = XML::Atom::Entry->new(Doc => XML::Atom::Util::first($xml, NS_SOAP, 'Body'));
280 } else {
281 0 $atom = XML::Atom::Entry->new(Stream => \$req->content);
282 }
283 0 return $atom;
284 }
285  
286 package
287 XML::Atom::Server::PSGI::XMLParser;
288 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 use Class::Accessor::Lite
290 2 ▶ new => 1,
Subroutine BEGIN: called CC=1 SCAR=0.0
291 rw => [ qw(parser) ]
292 2 ;
293 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
294 2 if (! XML::Atom::LIBXML) {
295 require XML::XPath;
296 XML::XPath->import;
297 }
298 }
299  
300 1 ▶sub parse_string {
Subroutine parse_string: called CC=2 SCAR=7.0
301 1 my ($self, $string) = @_;
302  
303 1 if (XML::Atom::LIBXML) {
304 1 my $parser = $self->parser;
305 1 ▶ if (! $parser) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser) 1 0 1
306 1 $parser = XML::Atom->libxml_parser;
307 1 $self->parser($parser);
308 }
309 1 return $parser->parse_string($string);
310 } else {
311 return XML::XPath->new(xml => $string);
312 }
313 }
314  
315 2 1;
316 __END__
↑ Summary