line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
## -*- Mode: CPerl -*- |
2
|
|
|
|
|
|
|
## File: DiaColloDB::Client::http.pm |
3
|
|
|
|
|
|
|
## Author: Bryan Jurish <moocow@cpan.org> |
4
|
|
|
|
|
|
|
## Description: collocation db: client: remote http server |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
package DiaColloDB::Client::http; |
7
|
2
|
|
|
2
|
|
16
|
use DiaColloDB::Client; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
58
|
|
8
|
2
|
|
|
2
|
|
12
|
use URI; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
36
|
|
9
|
2
|
|
|
2
|
|
2826
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
105348
|
|
|
2
|
|
|
|
|
86
|
|
10
|
2
|
|
|
2
|
|
26
|
use HTTP::Request; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
58
|
|
11
|
2
|
|
|
2
|
|
3056
|
use HTTP::Request::Common; ##-- for POST() |
|
2
|
|
|
|
|
4726
|
|
|
2
|
|
|
|
|
172
|
|
12
|
2
|
|
|
2
|
|
16
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2154
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
##============================================================================== |
15
|
|
|
|
|
|
|
## Globals & Constants |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
our @ISA = qw(DiaColloDB::Client); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
##============================================================================== |
20
|
|
|
|
|
|
|
## Constructors etc. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
## $cli = CLASS_OR_OBJECT->new(%args) |
23
|
|
|
|
|
|
|
## $cli = CLASS_OR_OBJECT->new($url, %args) |
24
|
|
|
|
|
|
|
## + %args, object structure: |
25
|
|
|
|
|
|
|
## ( |
26
|
|
|
|
|
|
|
## ##-- DiaColloDB::Client: options |
27
|
|
|
|
|
|
|
## url => $url, ##-- local url |
28
|
|
|
|
|
|
|
## ## |
29
|
|
|
|
|
|
|
## ##-- DiaColloDB::Client::http: options |
30
|
|
|
|
|
|
|
## user => $user, ##-- for LWP::UserAgent basic authentication |
31
|
|
|
|
|
|
|
## password => $password, ##-- for LWP::UserAgent basic authentication |
32
|
|
|
|
|
|
|
## logRequest => $log, ##-- log-level for HTTP requests (default:'debug') |
33
|
|
|
|
|
|
|
## ## |
34
|
|
|
|
|
|
|
## ##-- DiaColloDB::Client::http: guts |
35
|
|
|
|
|
|
|
## ua => $ua, ##-- underlying LWP::UserAgent |
36
|
|
|
|
|
|
|
## ) |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
## %defaults = $CLASS_OR_OBJ->defaults() |
39
|
|
|
|
|
|
|
## + called by new() |
40
|
|
|
|
|
|
|
sub defaults { |
41
|
|
|
|
|
|
|
return ( |
42
|
0
|
|
|
0
|
1
|
|
logRequest=>'debug', |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
##============================================================================== |
49
|
|
|
|
|
|
|
## I/O: open/close |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
## $cli_or_undef = $cli->open_http($http_url,%opts) |
52
|
|
|
|
|
|
|
## $cli_or_undef = $cli->open_http() |
53
|
|
|
|
|
|
|
## + opens a local file url |
54
|
|
|
|
|
|
|
## + may re-bless() $cli into an appropriate package |
55
|
|
|
|
|
|
|
## + OVERRIDE in subclasses supporting file urls |
56
|
|
|
|
|
|
|
sub open_http { |
57
|
0
|
|
|
0
|
1
|
|
my ($cli,$url,%opts) = @_; |
58
|
0
|
0
|
|
|
|
|
$cli = $cli->new() if (!ref($cli)); |
59
|
0
|
0
|
|
|
|
|
$cli->close() if ($cli->opened); |
60
|
0
|
|
0
|
|
|
|
$url //= $cli->{url}; |
61
|
0
|
|
|
|
|
|
my $uri = URI->new($url); |
62
|
0
|
0
|
|
|
|
|
if ((my $path=$uri->path) !~ m/profile/) { |
63
|
0
|
0
|
|
|
|
|
$path .= "/" if ($path !~ m{/$}); |
64
|
0
|
|
|
|
|
|
$path .= "profile.perl"; |
65
|
0
|
|
|
|
|
|
$uri->path($path); |
66
|
|
|
|
|
|
|
} |
67
|
0
|
0
|
|
|
|
|
if ($uri->query) { |
68
|
0
|
|
|
|
|
|
my %qf = $uri->query_form(); |
69
|
0
|
|
|
|
|
|
@$cli{keys %qf} = values %qf; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
$cli->{url} = $uri->as_string; |
72
|
0
|
|
0
|
|
|
|
$cli->{ua} //= LWP::UserAgent->new(); |
73
|
0
|
|
|
|
|
|
return $cli; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
## $cli_or_undef = $cli->close() |
77
|
|
|
|
|
|
|
## + default just returns $cli |
78
|
|
|
|
|
|
|
sub close { |
79
|
0
|
|
|
0
|
1
|
|
my $cli = shift; |
80
|
0
|
0
|
|
|
|
|
$cli->{db}->close() if ($cli->{db}); |
81
|
0
|
|
|
|
|
|
return $cli; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
## $bool = $cli->opened() |
85
|
|
|
|
|
|
|
## + default just checks for $cli->{url} |
86
|
|
|
|
|
|
|
sub opened { |
87
|
0
|
|
0
|
0
|
1
|
|
return ref($_[0]) && $_[0]{ua}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
##============================================================================== |
91
|
|
|
|
|
|
|
## Profiling |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
94
|
|
|
|
|
|
|
## Profiling: Generic: HTTP wrappers |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
## $obj_or_undef = $cli->jget($url,\%query_form,$class) |
97
|
|
|
|
|
|
|
## + wrapper for http json GET requests |
98
|
|
|
|
|
|
|
sub jget { |
99
|
0
|
|
|
0
|
0
|
|
my ($cli,$url,$form,$class) = @_; |
100
|
0
|
|
0
|
|
|
|
my $uri = URI->new($url // $cli->{url}); |
101
|
0
|
|
0
|
|
|
|
$uri->query_form( {%{$cli->{params}//{}}, %$form} ); |
|
0
|
|
|
|
|
|
|
102
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new('GET',"$uri"); |
103
|
0
|
0
|
0
|
|
|
|
$req->authorization_basic($cli->{user}, $cli->{password}) if (defined($cli->{user}) && defined($cli->{password})); |
104
|
0
|
|
|
|
|
|
$cli->vlog($cli->{logRequest}, "GET $uri"); |
105
|
0
|
|
|
|
|
|
my $rsp = $cli->{ua}->request($req); |
106
|
0
|
0
|
|
|
|
|
if (!$rsp->is_success) { |
107
|
0
|
|
|
|
|
|
$cli->{error} = $rsp->status_line; |
108
|
0
|
|
|
|
|
|
return undef; |
109
|
|
|
|
|
|
|
} |
110
|
0
|
|
|
|
|
|
my $cref = $rsp->content_ref; |
111
|
0
|
|
|
|
|
|
return $class->loadJsonString($cref,utf8=>!utf8::is_utf8($$cref)); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
## $obj_or_undef = $cli->jpost($url,\%query_form,$class) |
115
|
|
|
|
|
|
|
## + wrapper for json http POST requests |
116
|
|
|
|
|
|
|
sub jpost { |
117
|
0
|
|
|
0
|
0
|
|
my ($cli,$url,$form,$class) = @_; |
118
|
0
|
|
0
|
|
|
|
$url //= $cli->{url}; |
119
|
0
|
|
0
|
|
|
|
my $req = POST($url, Content => {%{$cli->{params}//{}}, %$form}); |
|
0
|
|
|
|
|
|
|
120
|
0
|
0
|
0
|
|
|
|
$req->authorization_basic($cli->{user}, $cli->{password}) if (defined($cli->{user}) && defined($cli->{password})); |
121
|
0
|
|
|
|
|
|
$cli->vlog($cli->{logRequest}, "POST $url"); |
122
|
|
|
|
|
|
|
#$cli->trace("REQUEST = ", $req->as_string); |
123
|
0
|
|
|
|
|
|
my $rsp = $cli->{ua}->request($req); |
124
|
0
|
0
|
|
|
|
|
if (!$rsp->is_success) { |
125
|
0
|
|
|
|
|
|
$cli->{error} = $rsp->status_line; |
126
|
0
|
|
|
|
|
|
return undef; |
127
|
|
|
|
|
|
|
} |
128
|
0
|
|
|
|
|
|
my $cref = $rsp->content_ref; |
129
|
0
|
|
|
|
|
|
return $class->loadJsonString($cref,utf8=>!utf8::is_utf8($$cref)); |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
133
|
|
|
|
|
|
|
## dbinfo |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
## \%info = $cli->dbinfo() |
136
|
|
|
|
|
|
|
## + adds 'url' key |
137
|
|
|
|
|
|
|
sub dbinfo { |
138
|
0
|
|
|
0
|
1
|
|
my $cli = shift; |
139
|
0
|
|
|
|
|
|
(my $url = $cli->{url}) =~ s{/profile.*$}{}; |
140
|
0
|
|
|
|
|
|
my $info = $cli->jget("$url/info.perl", {},'DiaColloDB::Persistent'); |
141
|
0
|
|
|
|
|
|
$info->{url} = "$url/"; |
142
|
0
|
|
|
|
|
|
return $info; |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
146
|
|
|
|
|
|
|
## Profiling: Generic |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
## $mprf_or_undef = $cli->profile($relation, %opts) |
149
|
|
|
|
|
|
|
## + get a relation profile for selected items as a DiaColloDB::Profile::Multi object |
150
|
|
|
|
|
|
|
## + %opts: as for DiaColloDB::profile() |
151
|
|
|
|
|
|
|
## + sets $cli->{error} on error |
152
|
|
|
|
|
|
|
sub profile { |
153
|
0
|
|
|
0
|
1
|
|
my ($cli,$rel,%opts) = @_; |
154
|
0
|
|
|
|
|
|
delete @opts{qw(alemma adate aslice blemma bdate bslice)}; |
155
|
0
|
|
|
|
|
|
return $cli->jget($cli->{url}, {profile=>$rel, %opts, format=>'json'},'DiaColloDB::Profile::Multi'); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
159
|
|
|
|
|
|
|
## Profiling: extend (pass-2 for multi-clients) |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
## $mprf = $cli->extend($relation, %opts) |
162
|
|
|
|
|
|
|
## + get an extension-profile for selected items as a DiaColloDB::Profile::Multi object |
163
|
|
|
|
|
|
|
## + %opts: as for DiaColloDB::extend() |
164
|
|
|
|
|
|
|
## + sets $cli->{error} on error |
165
|
|
|
|
|
|
|
sub extend { |
166
|
0
|
|
|
0
|
1
|
|
my ($cli,$rel,%opts) = @_; |
167
|
0
|
|
|
|
|
|
delete @opts{(qw(alemma adate aslice blemma bdate bslice), |
168
|
|
|
|
|
|
|
qw(eps score kbest cutoff global), |
169
|
|
|
|
|
|
|
qw(onepass), |
170
|
|
|
|
|
|
|
)}; |
171
|
0
|
|
|
|
|
|
return $cli->jpost($cli->{url}, {profile=>"extend-$rel", %opts, format=>'json'},'DiaColloDB::Profile::Multi'); |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
##-------------------------------------------------------------- |
175
|
|
|
|
|
|
|
## Profiling: Comparison (diff) |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
## $mprf = $cli->compare($relation, %opts) |
178
|
|
|
|
|
|
|
## + get a relation comparison profile for selected items as a DiaColloDB::Profile::MultiDiff object |
179
|
|
|
|
|
|
|
## + %opts: as for DiaColloDB::compare() |
180
|
|
|
|
|
|
|
sub compare { |
181
|
0
|
|
|
0
|
1
|
|
my ($cli,$rel,%opts) = @_; |
182
|
0
|
|
|
|
|
|
return $cli->jget($cli->{url}, {profile=>"d$rel", %opts, format=>'json'},'DiaColloDB::Profile::MultiDiff'); |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
##============================================================================== |
186
|
|
|
|
|
|
|
## Footer |
187
|
|
|
|
|
|
|
1; |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
__END__ |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
|