line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Net::FreeDB2::Connection::HTTP; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# Copyright 2002, Vincenzo Zocca. |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
# See LICENSE section for usage and distribution rights. |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require 5.005_62; |
8
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
33
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
10
|
1
|
|
|
1
|
|
842
|
use Error qw (:try); |
|
1
|
|
|
|
|
7223
|
|
|
1
|
|
|
|
|
5
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Exporter; |
13
|
1
|
|
|
1
|
|
1192
|
use AutoLoader qw(AUTOLOAD); |
|
1
|
|
|
|
|
1741
|
|
|
1
|
|
|
|
|
8
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
#our @ISA = qw(Net::FreeDB2::Connection Exporter); |
16
|
1
|
|
|
1
|
|
40
|
use base qw (Net::FreeDB2::Connection Exporter); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
622
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
19
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
20
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# This allows declaration use Net::FreeDB2::Connection::HTTP ':all'; |
23
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
24
|
|
|
|
|
|
|
# will save memory. |
25
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
) ] ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our @EXPORT = qw( |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
our ( $VERSION ) = '$Revision: 0.8.2.3 $ ' =~ /\$Revision:\s+([^\s]+)/; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub new { |
37
|
|
|
|
|
|
|
# Call constructor of super class |
38
|
0
|
|
|
0
|
1
|
|
my $self = &Net::FreeDB2::Connection::new (@_); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# Shift out this class specification |
41
|
0
|
|
|
|
|
|
shift; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# Return object |
44
|
0
|
|
|
|
|
|
return ($self); |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub _initialize { |
48
|
0
|
|
|
0
|
|
|
my $self = shift; |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
# Get options |
51
|
0
|
|
0
|
|
|
|
my $opt = shift || {}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# Set freedb_cgi |
54
|
0
|
|
0
|
|
|
|
$self->setFreeDBCgi ($opt->{freedb_cgi} || '~cddb/cddb.cgi'); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# Initialize super class |
57
|
0
|
|
|
|
|
|
return ($self->SUPER::_initialize ($opt)); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
0
|
1
|
|
sub hello { |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
sub lscat { |
64
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# Send command and wait for reply |
67
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ('?cmd=cddb+lscat', { |
68
|
|
|
|
|
|
|
210 => 1, |
69
|
|
|
|
|
|
|
}); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Parse the result |
72
|
0
|
|
|
|
|
|
my @content = split (/[\n\r]+/, ${$content_ref}); |
|
0
|
|
|
|
|
|
|
73
|
0
|
|
|
|
|
|
my $head = shift (@content); |
74
|
0
|
|
|
|
|
|
my @cat = (); |
75
|
0
|
|
|
|
|
|
foreach my $cat (@content) { |
76
|
0
|
0
|
|
|
|
|
last if ($cat eq '.'); |
77
|
0
|
|
|
|
|
|
push (@cat, $cat); |
78
|
|
|
|
|
|
|
} |
79
|
0
|
|
|
|
|
|
return (@cat); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub query { |
83
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
84
|
0
|
|
|
|
|
|
my $entity = shift; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# Send command and wait for reply |
87
|
0
|
|
|
|
|
|
my $query = $entity->mkQuery (); |
88
|
0
|
|
|
|
|
|
$query =~ s/\s+/+/g; |
89
|
0
|
|
|
|
|
|
my $cmd = '?cmd=cddb+query+' . $query; |
90
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ($cmd, { |
91
|
|
|
|
|
|
|
200 => 1, |
92
|
|
|
|
|
|
|
211 => 1, |
93
|
|
|
|
|
|
|
202 => 1, |
94
|
|
|
|
|
|
|
403 => 1, |
95
|
|
|
|
|
|
|
409 => 1, |
96
|
|
|
|
|
|
|
}); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# Parse the result |
99
|
1
|
|
|
1
|
|
624
|
use Net::FreeDB2::Response::Query; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
332
|
|
100
|
0
|
|
|
|
|
|
return Net::FreeDB2::Response::Query->new ({ |
101
|
|
|
|
|
|
|
content_ref => $content_ref, |
102
|
|
|
|
|
|
|
}); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub read { |
106
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
107
|
0
|
|
|
|
|
|
my $match = shift; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# Send command and wait for reply |
110
|
0
|
|
|
|
|
|
my $cmd = '?cmd=cddb+read+' . $match->getCateg () . '+' . $match->getDiscid (); |
111
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ($cmd, { |
112
|
|
|
|
|
|
|
200 => 1, |
113
|
|
|
|
|
|
|
211 => 1, |
114
|
|
|
|
|
|
|
202 => 1, |
115
|
|
|
|
|
|
|
403 => 1, |
116
|
|
|
|
|
|
|
409 => 1, |
117
|
|
|
|
|
|
|
}); |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Parse the result |
120
|
1
|
|
|
1
|
|
589
|
use Net::FreeDB2::Response::Read; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
649
|
|
121
|
0
|
|
|
|
|
|
return Net::FreeDB2::Response::Read->new ({ |
122
|
|
|
|
|
|
|
content_ref => $content_ref, |
123
|
|
|
|
|
|
|
}); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub write { |
127
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::write, command not supported under HTTP."); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub log { |
131
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::log, to be implemented."); |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub motd { |
135
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# Send command and wait for reply |
138
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ('?cmd=motd', { |
139
|
|
|
|
|
|
|
210 => 1, |
140
|
|
|
|
|
|
|
401 => 1, |
141
|
|
|
|
|
|
|
}); |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# Parse the result |
144
|
0
|
|
|
|
|
|
my @content = split (/[\n\r]+/, ${$content_ref}); |
|
0
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
|
my $head = shift (@content); |
146
|
0
|
|
|
|
|
|
my @motd = (); |
147
|
0
|
|
|
|
|
|
foreach my $motd (@content) { |
148
|
0
|
0
|
|
|
|
|
last if ($motd eq '.'); |
149
|
0
|
|
|
|
|
|
push (@motd, $motd); |
150
|
|
|
|
|
|
|
} |
151
|
0
|
|
|
|
|
|
return (@motd); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub discid { |
155
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
156
|
0
|
|
|
|
|
|
my $entity = shift; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Send command and wait for reply |
159
|
0
|
|
|
|
|
|
my $discid = $entity->mkQuery (); |
160
|
0
|
|
|
|
|
|
$discid =~ s/^\s*\S+\s+//; |
161
|
0
|
|
|
|
|
|
$discid =~ s/\s+/+/g; |
162
|
0
|
|
|
|
|
|
my $cmd = '?cmd=discid+' . $discid; |
163
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ($cmd, { |
164
|
|
|
|
|
|
|
200 => 1, |
165
|
|
|
|
|
|
|
500 => 1, |
166
|
|
|
|
|
|
|
}); |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
# Parse the result |
169
|
0
|
|
|
|
|
|
my @content = split (/[\n\r]+/, ${$content_ref}); |
|
0
|
|
|
|
|
|
|
170
|
0
|
|
|
|
|
|
my $head = shift (@content); |
171
|
0
|
|
|
|
|
|
my ($code) = $head =~ /^\s*(\d{3})\s+/; |
172
|
0
|
0
|
0
|
|
|
|
$code == 200 || $code == 500 || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::discid, unknown code '$code' returned."); |
173
|
0
|
0
|
|
|
|
|
$code == 500 && throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::discid, Command Syntax error."); |
174
|
0
|
|
|
|
|
|
my @head = split (/\s+/, $head); |
175
|
0
|
|
|
|
|
|
return ($head[4]); |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
sub proto { |
179
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::proto, to be implemented."); |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub sites { |
183
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
# Send command and wait for reply |
186
|
0
|
|
|
|
|
|
my $content_ref = $self->waitCommandReply ('?cmd=sites', { |
187
|
|
|
|
|
|
|
200 => 1, |
188
|
|
|
|
|
|
|
500 => 1, |
189
|
|
|
|
|
|
|
}); |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
# Parse the result |
192
|
1
|
|
|
1
|
|
630
|
use Net::FreeDB2::Response::Sites; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
195
|
|
193
|
0
|
|
|
|
|
|
return Net::FreeDB2::Response::Sites->new ({ |
194
|
|
|
|
|
|
|
content_ref => $content_ref, |
195
|
|
|
|
|
|
|
}); |
196
|
|
|
|
|
|
|
} |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub stat { |
199
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::stat, to be implemented."); |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub ver { |
203
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::ver, to be implemented."); |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
sub update { |
207
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::update, to be implemented."); |
208
|
|
|
|
|
|
|
} |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
sub whom { |
211
|
0
|
|
|
0
|
1
|
|
throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::whom, to be implemented."); |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub connect { |
215
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
# Make connection through user agent |
218
|
1
|
|
|
1
|
|
1149
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
96846
|
|
|
1
|
|
|
|
|
185
|
|
219
|
0
|
|
|
|
|
|
my $connection = LWP::UserAgent->new (); |
220
|
0
|
0
|
|
|
|
|
defined ($connection) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::connect, Failed to instanciate an \'LWP::UserAgent\' object.'); |
221
|
0
|
|
|
|
|
|
$self->setConnection ($connection); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# Set proxy if required |
224
|
0
|
0
|
|
|
|
|
defined ($self->getProxyHost ()) || return; |
225
|
0
|
|
0
|
|
|
|
my $url = 'http://' . $self->getProxyHost() . ':' . ($self->getProxyPort () || 8080); |
226
|
0
|
|
|
|
|
|
$connection->proxy ('http', $url); |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub waitCommandReply { |
230
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
231
|
0
|
|
|
|
|
|
my $cmd = shift; |
232
|
0
|
|
|
|
|
|
my $rx = shift; |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
# Check if connection is defined |
235
|
0
|
0
|
|
|
|
|
defined ($self->getConnection ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, no connection available.'); |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
# Make url |
238
|
0
|
|
|
|
|
|
my $url = $self->mkUrlBase (); |
239
|
0
|
|
|
|
|
|
$url .= $cmd . $self->mkHello (); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Make request |
242
|
1
|
|
|
1
|
|
13
|
use HTTP::Request; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
549
|
|
243
|
0
|
|
|
|
|
|
my $request = HTTP::Request->new (GET => $url); |
244
|
0
|
0
|
|
|
|
|
defined ($request) || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to make HTTP::Request object out of url '$url'."); |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
# Set proxy authorization if required |
247
|
0
|
0
|
0
|
|
|
|
if ($self->getProxyHost () && $self->getProxyUser ()) { |
248
|
0
|
|
|
|
|
|
$request->proxy_authorization_basic ($self->getProxyUser (), $self->getProxyPasswd ()); |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
# Execute the request through the connection |
252
|
0
|
|
|
|
|
|
my $response = $self->getConnection ()->simple_request ($request); |
253
|
0
|
0
|
|
|
|
|
$response->is_success() || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to execute request for url '$url'."); |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Return the content reference |
257
|
0
|
|
|
|
|
|
return ($response->content_ref ()); |
258
|
|
|
|
|
|
|
} |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sub mkHello { |
261
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
262
|
|
|
|
|
|
|
|
263
|
0
|
0
|
|
|
|
|
defined ($self->getClientName ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_name\' not set.'); |
264
|
0
|
0
|
|
|
|
|
defined ($self->getClientVersion ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_version\' not set.'); |
265
|
0
|
0
|
|
|
|
|
defined ($self->getClientHost ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_host\' not set.'); |
266
|
0
|
0
|
|
|
|
|
defined ($self->getClientUser ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_user\' not set.'); |
267
|
|
|
|
|
|
|
|
268
|
0
|
|
|
|
|
|
return ('&hello=' . join ('+', |
269
|
|
|
|
|
|
|
$self->getClientUser (), |
270
|
|
|
|
|
|
|
$self->getClientHost (), |
271
|
|
|
|
|
|
|
$self->getClientName (), |
272
|
|
|
|
|
|
|
$self->getClientVersion (), |
273
|
|
|
|
|
|
|
) . |
274
|
|
|
|
|
|
|
'&proto=1' |
275
|
|
|
|
|
|
|
); |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
sub mkUrlBase { |
279
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
280
|
|
|
|
|
|
|
|
281
|
0
|
0
|
|
|
|
|
defined ($self->getFreeDBHost ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, \'freedb_host\' not set.'); |
282
|
0
|
0
|
|
|
|
|
defined ($self->getFreeDBCgi ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, \'freedb_cgi\' not set.'); |
283
|
0
|
|
|
|
|
|
my $url = 'http://' . $self->getFreeDBHost (); |
284
|
0
|
0
|
|
|
|
|
$url .= ':' . $self->getFreeDBPort () if ($self->getFreeDBPort ()); |
285
|
0
|
|
|
|
|
|
$url .= '/' . $self->getFreeDBCgi (); |
286
|
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
sub setFreeDBCgi { |
289
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
# Set freedb/cddb url |
292
|
0
|
|
|
|
|
|
$self->{Net_FreeDB2_Connection_HTTP}{freedb_cgi} = shift; |
293
|
|
|
|
|
|
|
} |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
sub getFreeDBCgi { |
296
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
# Return freedb/cddb url |
299
|
0
|
|
|
|
|
|
return ($self->{Net_FreeDB2_Connection_HTTP}{freedb_cgi}); |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
1; |
303
|
|
|
|
|
|
|
__END__ |