line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Plugin::WebService; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
27470
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
43
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
6
|
use base qw(Template::Plugin); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
984
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
6133
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
53
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
$VERSION = '0.16'; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
506
|
use CGI::Ex; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Carp qw(confess); |
14
|
|
|
|
|
|
|
use CGI::Cookie; |
15
|
|
|
|
|
|
|
use Storable qw(thaw); |
16
|
|
|
|
|
|
|
use WWW::Mechanize; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub new { |
19
|
|
|
|
|
|
|
my $class = shift; |
20
|
|
|
|
|
|
|
my $context = shift; |
21
|
|
|
|
|
|
|
bless { _CONTEXT => $context, }, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub load { |
25
|
|
|
|
|
|
|
my ($class, $context) = @_; |
26
|
|
|
|
|
|
|
return $class; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub URLEncode { |
30
|
|
|
|
|
|
|
my $arg = shift; |
31
|
|
|
|
|
|
|
my ($ref, $return) = ref($arg) ? ($arg, 0) : (\$arg, 1); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
$$ref =~ s/([^\w\.\ -])/sprintf("%%%02X",ord($1))/eg; |
34
|
|
|
|
|
|
|
$$ref =~ tr/\ /+/; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
return $return ? $$ref : ''; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub get_outserial { |
40
|
|
|
|
|
|
|
my $self = shift; |
41
|
|
|
|
|
|
|
my $url = shift; |
42
|
|
|
|
|
|
|
my $form = shift; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my $outserial_key = $self->outserial_key; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $outserial = 'json'; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
if($form->{$outserial_key}) { |
49
|
|
|
|
|
|
|
$outserial = $form->{$outserial_key}; |
50
|
|
|
|
|
|
|
} elsif($url =~ /\b$outserial_key=(\w+)/) { |
51
|
|
|
|
|
|
|
$outserial = $1; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
return $outserial; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub make_form { |
58
|
|
|
|
|
|
|
return '' if !@_; |
59
|
|
|
|
|
|
|
my ($hash, $keys); |
60
|
|
|
|
|
|
|
if (ref $_[0]) { |
61
|
|
|
|
|
|
|
$hash = shift; |
62
|
|
|
|
|
|
|
$keys = shift() if @_ && ref $_[0]; |
63
|
|
|
|
|
|
|
} else { |
64
|
|
|
|
|
|
|
$hash = {@_}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
$keys ||= [ sort keys %$hash ]; |
67
|
|
|
|
|
|
|
my $str = ""; |
68
|
|
|
|
|
|
|
foreach my $key (@$keys) { |
69
|
|
|
|
|
|
|
$hash->{$key} = "" if !exists($hash->{$key}); |
70
|
|
|
|
|
|
|
my $ref = ref($hash->{$key}); |
71
|
|
|
|
|
|
|
next if $ref && $ref eq 'HASH'; |
72
|
|
|
|
|
|
|
my $array = ($ref eq 'ARRAY') ? $hash->{$key} : [ $hash->{$key} ]; |
73
|
|
|
|
|
|
|
foreach my $val (@$array) { |
74
|
|
|
|
|
|
|
my $ref2 = ref($val); |
75
|
|
|
|
|
|
|
next if $ref2 && $ref2 eq 'HASH'; |
76
|
|
|
|
|
|
|
my $array2 = ($ref2 eq 'ARRAY') ? $val : [$val]; |
77
|
|
|
|
|
|
|
foreach (@$array2) { |
78
|
|
|
|
|
|
|
$str .= URLEncode($key) . "=" . URLEncode($_ . '') . "&"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
chop $str; |
83
|
|
|
|
|
|
|
return $str; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub content_cleanup { |
87
|
|
|
|
|
|
|
my $self = shift; |
88
|
|
|
|
|
|
|
my $content_ref = shift; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub default_host { |
92
|
|
|
|
|
|
|
return '127.0.0.1'; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub outserial_key { |
96
|
|
|
|
|
|
|
return 'outserial'; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub webservice_call { |
100
|
|
|
|
|
|
|
my $self = shift; |
101
|
|
|
|
|
|
|
my $url = shift || confess 'need a url'; |
102
|
|
|
|
|
|
|
my $form = shift || {}; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
confess 'form needs to be a hash ref' unless(UNIVERSAL::isa($form, 'HASH')); |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $host; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
if($url =~ m@^https?://([^/]+)@) { |
109
|
|
|
|
|
|
|
$host = $1; |
110
|
|
|
|
|
|
|
} else { |
111
|
|
|
|
|
|
|
$host = $self->default_host; |
112
|
|
|
|
|
|
|
$url = "http://$host$url"; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
if (scalar keys %$form) { |
116
|
|
|
|
|
|
|
$url .= ($url =~ /\?/) ? '&' : '?'; |
117
|
|
|
|
|
|
|
$url .= make_form($form); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $mech = WWW::Mechanize->new; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
my %cookies = fetch CGI::Cookie; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
my $content; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
if(%cookies && scalar keys %cookies) { |
127
|
|
|
|
|
|
|
require HTTP::Cookies; |
128
|
|
|
|
|
|
|
require WWW::Mechanize; |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
my $cj = HTTP::Cookies->new(); |
131
|
|
|
|
|
|
|
foreach my $cookie_key (keys %cookies) { |
132
|
|
|
|
|
|
|
$cj->set_cookie(0, $cookie_key, $cookies{$cookie_key}->value, '/', $host); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
$mech = WWW::Mechanize->new(cookie_jar => $cj); |
135
|
|
|
|
|
|
|
$content = $mech->get($url)->content; |
136
|
|
|
|
|
|
|
} else { |
137
|
|
|
|
|
|
|
require LWP::Simple; |
138
|
|
|
|
|
|
|
$content = LWP::Simple::get($url); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$self->content_cleanup(\$content); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
my $obj; |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
my $outserial = $self->get_outserial($url, $form); |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
if($outserial eq 'storable') { |
148
|
|
|
|
|
|
|
require Storable; |
149
|
|
|
|
|
|
|
$obj = Storable::thaw($content); |
150
|
|
|
|
|
|
|
} elsif($outserial eq 'xml') { |
151
|
|
|
|
|
|
|
require XML::Simple; |
152
|
|
|
|
|
|
|
$obj = XML::Simple::XMLin($content); |
153
|
|
|
|
|
|
|
} elsif($outserial eq 'yaml') { |
154
|
|
|
|
|
|
|
require YAML; |
155
|
|
|
|
|
|
|
$obj = YAML::Load($content); |
156
|
|
|
|
|
|
|
} else { |
157
|
|
|
|
|
|
|
require JSON; |
158
|
|
|
|
|
|
|
$obj = JSON::from_json($content); |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
return $obj; |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
__END__ |