LS/HTTP/Service.pm untested

filter: Statementstmt 0.0% 0 / 207 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 359 CC 34 SCAR 30.5
CC
34
Cov
0%
getAvailableServices
56.0
getMetadata
56.0
do_authority
30.0
CRAP 21.0
1 # =====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::HTTP::Service;
10  
11 use strict;
12 use warnings;
13  
14 use vars qw( $METHODS );
15  
16 use URI;
17 use HTTP::Status;
18 use HTTP::Response;
19  
20 use LS;
21 use LS::Service;
22  
23  
24 use base 'LS::Service';
25  
26  
27 #
28 # new( %options ) -
29 #
30 sub new {
31  
32 my $self = shift;
33  
34 unless (ref $self) {
35  
36 $self = $self->SUPER::new(@_);
37 }
38  
39 return $self;
40 }
41  
42  
43 sub dispatch_authority_to {
44  
45 my $self = shift->new;
46  
47 @_ ? ($self->{'_dispatch_authority_to'} = $_[0], return $self)
48 : return $self->{'_dispatch_authority_to'};
49 }
50  
51  
52 sub dispatch_metadata_to {
53  
54 my $self = shift->new;
55  
56 @_ ? ($self->{'_dispatch_metadata_to'} = $_[0], return $self)
57 : return $self->{'_dispatch_metadata_to'};
58 }
59  
60  
61 sub dispatch_data_to {
62  
63 my $self = shift->new;
64  
65 @_ ? ($self->{'_dispatch_data_to'} = $_[0], return $self)
66 : return $self->{'_dispatch_data_to'};
67 }
68  
69  
70 #
71 # Internal routines used to invoke various services
72 #
73  
74 sub do_authority {
75  
76 my ($self, $lsid) = @_;
77  
78 #
79 # Make sure we have an authority service hooked up to this thing
80 #
81 my $handler = $self->dispatch_authority_to();
82  
83 unless($handler && $handler->can('getAvailableServices')) {
84  
85 LS::HTTP::Fault->fault('Method Not Implemented')->to_http_response();
86  
87 return undef;
88 }
89  
90  
91 $lsid = new LS::ID($lsid);
92  
93 unless(UNIVERSAL::isa($lsid, 'LS::ID')) {
94  
95 LS::HTTP::Fault->fault('Invalid LSID')->to_http_response();
96  
97 return undef;
98 }
99  
100 my $services = $handler->getAvailableServices($lsid);
101  
102 unless(UNIVERSAL::isa($services, 'LS::Service::Response')) {
103  
104 if(UNIVERSAL::isa($services, 'LS::Service::Fault')) {
105  
106 bless $services, 'LS::HTTP::Fault';
107 $services->to_http_response();
108 }
109 else {
110  
111 LS::HTTP::Fault->fault('Internal Server Error')->to_http_response();
112 }
113  
114 return undef;
115 }
116  
117 my $response = HTTP::Response->new(200);
118  
119 $response->header('Content-Type'=> ($services->format() || 'application/xml') );
120 $response->header('Content-Length'=> length($services->response()));
121  
122 $response->content($services->response());
123  
124 $self->print_http($response);
125 }
126  
127  
128 sub do_metadata {
129  
130 my ($self, $params) = @_;
131  
132 my $handler = $self->dispatch_metadata_to();
133  
134 unless($handler && $handler->can('getMetadata')) {
135  
136 LS::HTTP::Fault->fault('Method Not Implemented')->to_http_response();
137  
138 return undef;
139 }
140  
141 $params =~ s/^\?//;
142 my @p = split /[&]/, $params;
143  
144 my %url_params;
145  
146 foreach (@p) {
147  
148 my ($option, $val) = split /[=]/;
149  
150 $url_params{$option} = URI::Escape::uri_unescape($val);
151 }
152  
153 my $metadata = $handler->getMetadata($url_params{'lsid'}, $url_params{'acceptedFormats'});
154  
155 unless(UNIVERSAL::isa($metadata, 'LS::Service::Response')) {
156  
157 if(UNIVERSAL::isa($metadata, 'LS::Service::Fault')) {
158  
159 bless $metadata, 'LS::HTTP::Fault';
160 $metadata->to_http_response();
161 }
162 else {
163  
164 LS::HTTP::Fault->fault('Internal Server Error')->to_http_response();
165 }
166  
167 return undef;
168 }
169  
170 my $response = HTTP::Response->new(200);
171  
172 $response->header('Content-Type'=> $metadata->format());
173 $response->header('Content-Length'=> length($metadata->response()));
174  
175 $response->content($metadata->response());
176  
177 $self->print_http($response);
178 }
179  
180  
181 sub do_data {
182  
183 my ($self, $params) = @_;
184  
185 my $handler = $self->dispatch_data_to();
186  
187 unless($handler && $handler->can('getData')) {
188  
189 LS::HTTP::Fault->fault('Method Not Implemented')->to_http_response();
190  
191 return undef;
192 }
193  
194 $params =~ s/^\?//;
195 my @p = split /[&]/, $params;
196  
197 my %url_params;
198  
199 foreach (@p) {
200  
201 my ($option, $val) = split /[=]/;
202  
203 $url_params{$option} = URI::Escape::uri_unescape($val);
204 }
205  
206 my $data = $handler->getData($url_params{'lsid'});
207  
208 unless(UNIVERSAL::isa($data, 'LS::Service::Response')) {
209  
210 if(UNIVERSAL::isa($data, 'LS::Service::Fault')) {
211  
212 bless $data, 'LS::HTTP::Fault';
213 $data->to_http_response();
214 }
215 else {
216  
217 LS::HTTP::Fault->fault('Internal Server Error')->to_http_response();
218 }
219  
220 return undef;
221 }
222  
223 my $response = HTTP::Response->new(200);
224  
225 $response->header('Content-Type'=> ($data->format || 'application/octet-stream'));
226 $response->header('Content-Length'=> length($data->response()));
227  
228 $response->content($data->response());
229  
230 $self->print_http($response);
231 }
232  
233 sub do_fault {
234  
235 my ($self, $msg) = @_;
236  
237 return LS::HTTP::Fault->fault($msg);
238 }
239  
240 sub handle {
241
242 my $self = shift;
243
244 }
245  
246 #
247 # FIXME: The authentication code is a mess, do something about it
248 #
249 sub do_auth {
250
251 my $self = shift;
252  
253 if($self->auth_type eq 'Basic') {
254  
255 $self->do_basic_auth();
256 }
257  
258 $self->auth_fail();
259
260 return undef;
261 }
262  
263 sub do_basic_auth {
264  
265 my $self = shift;
266  
267 #
268 # FIXME: A better authentication code base would be nice
269 #
270 # HTTP Basic Authentication
271 #
272 if($ENV{HTTP_CGI_AUTHORIZATION}) {
273
274 my $base64creds = (split(/ /, $ENV{HTTP_CGI_AUTHORIZATION}))[1];
275
276 $base64creds = MIME::Base64::decode_base64($base64creds);
277
278 my ($user, $pass) = split(/\:/, $base64creds);
279
280 return $self->auth_handler->($user, $pass);
281 }
282  
283 return undef;
284 }
285  
286 sub auth_fail {
287  
288 my $self = shift;
289
290 # All else fails, send the 401
291 my $resp = HTTP::Response->new(401);
292 $resp->content_type('text/html');
293
294 print STDOUT "WWW-Authenticate: Basic realm=\"LSID\"\n";
295 print STDOUT "Status: " . $resp->as_string() . "\n";
296 print STDOUT $resp->error_as_HTML();
297 }
298  
299 sub print_http {
300  
301 my ($self, $response) = @_;
302  
303 binmode(STDOUT);
304 print STDOUT "Status: " . $response->code() . ' ' . HTTP::Status::status_message($response->code()) . "\r\n";
305 print STDOUT $response->headers_as_string();
306 print STDOUT "\r\n";
307 print STDOUT $response->content() . "\r\n";
308 }
309  
310  
311  
312  
313 #
314 #
315 # LSID Resolution Service Methods
316 #
317 #
318  
319 sub getAvailableServices {
320  
321 my $self = shift;
322  
323 my $handler = $self->dispatch_authority_to;
324  
325 unless ($handler) {
326  
327 #die LS::HTTP::Fault->faultcode('Client')
328 #->faultstring('Unknown method')
329 #->errorcode(101)
330 #->description('A call was made to an unknown method getAvailableServices.');
331 }
332  
333 my $response;
334 my $method_name;
335
336 if ($handler->can('getAvailableServices')) {
337  
338 $method_name = 'getAvailableServices';
339 $response = $handler->getAvailableServices(@_);
340 }
341 else {
342  
343 #die LS::SOAP::Fault->faultcode('Server')
344 #->faultstring('Not implemented')
345 #->errorcode(501)
346 #->description('getAvailableServices is not implemented by this authority service.');
347 }
348  
349 unless( (UNIVERSAL::isa($response, 'LS::Service::Response') ||
350 UNIVERSAL::isa($response, 'LS::Service::Fault')) ) {
351  
352 #die LS::SOAP::Fault->faultcode('Server')
353 #->faultstring('Internal processing error')
354 #->errorcode(500)
355 #->description(
356 #$method_name . ' in package ' .
357 #(ref $handler ? ref $handler : $handler) .
358 #' did not return a scalar or an LS::SOAP::Response object.'
359 #);
360 }
361  
362 if(UNIVERSAL::isa($response, 'LS::Service::Fault')) {
363  
364 bless $response, 'LS::HTTP::Fault';
365 $response->to_http_response;
366  
367 return undef;
368 }
369  
370 my @ret;
371 if (ref $response eq 'LS::SOAP::Response') {
372  
373 if ($response->expires) {
374  
375 push(
376 @ret, undef
377 #SOAP::Header->name(expires => $response->expires)
378 #->prefix($METHOD_PREFIX)
379 #->uri($URI)
380 );
381 }
382 }
383
384 return @ret;
385 }
386  
387  
388 sub getMetadata {
389  
390 my $self = shift;
391 my $handler = $self->dispatch_metadata_to;
392  
393 unless ($handler) {
394  
395 #die LS::SOAP::Fault->faultcode('Client')
396 #->faultstring('Unknown method')
397 #->errorcode(101)
398 #->description('A call was made to an unknown method getMetadata.');
399 }
400  
401 unless ($handler->can('getMetadata')) {
402  
403 #die LS::SOAP::Fault->faultcode('Server')
404 #->faultstring('Not implemented')
405 #->errorcode(501)
406 #->description('getMetadata is not implemented by this metadata service.');
407 }
408  
409 my $response = $handler->getMetadata(@_);
410  
411 unless( (UNIVERSAL::isa($response, 'LS::Service::Response') ||
412 UNIVERSAL::isa($response, 'LS::Service::Fault')) ) {
413  
414 #die LS::SOAP::Fault->faultcode('Server')
415 #->faultstring('Internal processing error')
416 #->errorcode(500)
417 #->description(
418 #'getMetadata in package ' .
419 #(ref $handler ? ref $handler : $handler) .
420 #' did not return a scalar or an LS::SOAP::Response object.'
421 #);
422 }
423  
424 if(UNIVERSAL::isa($response, 'LS::Service::Fault')) {
425  
426 bless $response, 'LS::HTTP::Fault';
427 $response->to_http_response;
428  
429 return undef;
430 }
431  
432 my @ret;
433 if (ref $response eq 'LS::HTTP::Response') {
434 if ($response->expires) {
435 push(
436 @ret, undef
437 #SOAP::Header->name(expires => $response->expires)
438 #->prefix($METHOD_PREFIX)
439 #->uri($URI)
440 );
441 }
442 }
443  
444 return @ret;
445 }
446  
447  
448 sub getData {
449  
450 my $self = shift;
451  
452 my $handler = $self->dispatch_data_to;
453  
454 unless ($handler) {
455  
456 #die LS::SOAP::Fault->faultcode('Client')
457 #->faultstring('Unknown method')
458 #->errorcode(101)
459 #->description('A call was made to an unknown method getData.');
460 }
461  
462 unless ($handler->can('getData')) {
463  
464 #die LS::SOAP::Fault->faultcode('Server')
465 #->faultstring('Not implemented')
466 #->errorcode(501)
467 #->description('getData is not implemented by this data service.');
468 }
469  
470 my $data = $handler->getData(@_);
471  
472 unless( (UNIVERSAL::isa($data, 'LS::Service::Response') || UNIVERSAL::isa($data, 'LS::Service::Fault')) ) {
473  
474 #die LS::SOAP::Fault->faultcode('Server')
475 #->faultstring('Internal processing error')
476 #->errorcode(500)
477 #->description(
478 #'getData in package ' .
479 #(ref $handler ? ref $handler : $handler) .
480 #' did not return a scalar.'
481 #);
482 }
483  
484 if(UNIVERSAL::isa($data, 'LS::Service::Fault')) {
485  
486 bless $data, 'LS::HTTP::Fault';
487 $data->to_http_response;
488  
489 return undef;
490 }
491 }
492  
493  
494 #
495 # Faults for HTTP based authorities
496 #
497 package LS::HTTP::Fault;
498  
499 use strict;
500 use warnings;
501  
502  
503 use LS;
504 use LS::Service::Fault;
505  
506 use base 'LS::Service::Fault';
507  
508  
509 #
510 # new( %options ) -
511 #
512 sub new {
513  
514 my $self = shift;
515  
516 unless (ref $self) {
517  
518 $self = $self->SUPER::new(@_);
519 }
520  
521 return $self;
522 }
523  
524  
525 #
526 # to_http_response( ) -
527 #
528 sub to_http_response {
529  
530 my $self = shift;
531  
532 my $response = HTTP::Response->new(500);
533  
534 $response->content($self->message() . "\r\n\r\n" . $self->trace());
535  
536 $response->header('LSID-Error-Code'=> $self->code());
537 $response->header('Content-Length'=> length($response->content()));
538  
539 binmode(STDOUT);
540 print STDOUT "Status: " . $response->code() . ' ' . HTTP::Status::status_message($response->code()) . "\r\n";
541 print STDOUT $response->headers_as_string();
542 print STDOUT "\r\n";
543 print STDOUT $response->content() . "\r\n";
544 }
545  
546  
547 1;
548  
549  
550 __END__