line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::Resource; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21401
|
use 5.008006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
34
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
5
|
1
|
|
|
1
|
|
6
|
use vars qw( $VERSION ); |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
58
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
51
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.01'; |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
7884
|
use Data::Dumper; |
|
1
|
|
|
|
|
14263
|
|
|
1
|
|
|
|
|
205
|
|
11
|
|
|
|
|
|
|
|
12
|
1
|
|
|
1
|
|
775
|
use FCGI; |
|
1
|
|
|
|
|
1389
|
|
|
1
|
|
|
|
|
23
|
|
13
|
1
|
|
|
1
|
|
1329
|
use IO::Handle; |
|
1
|
|
|
|
|
7961
|
|
|
1
|
|
|
|
|
57
|
|
14
|
1
|
|
|
1
|
|
8471
|
use CGI qw(:standard); # convenience functions |
|
1
|
|
|
|
|
22639
|
|
|
1
|
|
|
|
|
9
|
|
15
|
1
|
|
|
1
|
|
3864
|
use HTTP::Status; |
|
1
|
|
|
|
|
3216
|
|
|
1
|
|
|
|
|
280
|
|
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
383
|
use XML::Dumper; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
XML::Dumper::dtd; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use JSON; |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
my %formats = ( |
23
|
|
|
|
|
|
|
xml => \&pl2xml, |
24
|
|
|
|
|
|
|
json => sub { objToJson( $_[0], {pretty => 1, indent => 4} ) }, |
25
|
|
|
|
|
|
|
browser => \&browserprint, |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $TTL = 60 * 60; # time-to-live is 1 hr by default |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub run { |
31
|
|
|
|
|
|
|
my $package = bless {}, shift; |
32
|
|
|
|
|
|
|
my $STARTTIME = time; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# install the pretty printer from the subclass, if it exists |
35
|
|
|
|
|
|
|
if( my $printer = $package->can("browserprint") ){ |
36
|
|
|
|
|
|
|
$formats{browser} = $printer; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# how much time could a ttl live if a ttl could live time? |
40
|
|
|
|
|
|
|
$TTL = $package->ttl if $package->can("ttl"); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
my $request = FCGI::Request; |
43
|
|
|
|
|
|
|
while ( $request->Accept >= 0 ) { |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
my $method = $ENV{REQUEST_METHOD}; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
if ( my $handler = $package->can($method) ) { |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
my %query = map { split /=/ } split /;/, lc $ENV{QUERY_STRING}; |
50
|
|
|
|
|
|
|
my( $status, $obj ) = $handler->(\%query); |
51
|
|
|
|
|
|
|
return_result( $status, $obj ); |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
|
|
|
|
|
|
return_result( RC_NOT_IMPLEMENTED ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# Time to leave? |
59
|
|
|
|
|
|
|
exit if ( time - $STARTTIME ) > $TTL; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub return_result { |
64
|
|
|
|
|
|
|
my ( $status, $obj ) = @_; |
65
|
|
|
|
|
|
|
my $status_msg = join " ", $status, status_message($status); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if ( is_error($status) or !defined $obj ) { |
68
|
|
|
|
|
|
|
print header( -status => $status_msg ); |
69
|
|
|
|
|
|
|
return; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my %query = map { split /=/ } split /;/, lc $ENV{QUERY_STRING}; |
73
|
|
|
|
|
|
|
my $formatter = $formats{json}; |
74
|
|
|
|
|
|
|
$formatter = $formats{ $query{format} } |
75
|
|
|
|
|
|
|
if (exists $query{format} |
76
|
|
|
|
|
|
|
and exists $formats{ $query{format} }); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
print header( -status => $status_msg ); |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
print ref $obj ? $formatter->($obj) : $obj; |
81
|
|
|
|
|
|
|
return; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Format the data structure for browser viewing. This is an incredibly |
86
|
|
|
|
|
|
|
# stupid prettyprint. It simply places an html break at all newlines. |
87
|
|
|
|
|
|
|
sub browserprint { |
88
|
|
|
|
|
|
|
my $obj = shift; |
89
|
|
|
|
|
|
|
my $dumped = Dumper $obj; |
90
|
|
|
|
|
|
|
substr($dumped, 0, 7) = ''; |
91
|
|
|
|
|
|
|
$dumped =~ s/\n/ \n/g; |
92
|
|
|
|
|
|
|
return start_html("Pretty-printed Data Structure") . $dumped . end_html; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
__END__ |