blib/lib/Net/FreeDB2/Connection/HTTP.pm

filter: Statementstmt 27.5% 38 / 138 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 31.2% 10 / 32 Podpod 95.2% 20 / 21 totaltotal 27.8% 68 / 244 CC 25 SCAR 25.1
CC
25
Cov
21%
waitCommandReply
31.3
mkHello
30.0
discid
20.0
CRAP 12.3
« blib/lib/Net/FreeDB2/Site.pm ↑ Summary blib/lib/Net/FreeDB2/Connection.pm »
1 package Net::FreeDB2::Connection::HTTP;
2  
3 # Copyright 2002, Vincenzo Zocca.
4  
5 # See LICENSE section for usage and distribution rights.
6  
7 1 require 5.005_62;
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Error qw (:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 require Exporter;
13 1 ▶use AutoLoader qw(AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #our @ISA = qw(Net::FreeDB2::Connection Exporter);
16 1 ▶use base qw (Net::FreeDB2::Connection Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
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 1 our %EXPORT_TAGS = ( 'all' => [ qw(
26
27 ) ] );
28  
29 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
30  
31 1 our @EXPORT = qw(
32
33 );
34 1 our ( $VERSION ) = '$Revision: 0.8.2.3 $ ' =~ /\$Revision:\s+([^\s]+)/;
35  
36 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
37 # Call constructor of super class
38 0 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 0 ▶sub _initialize {
Subroutine _initialize: not called CC=3 SCAR=24.8
48 0 my $self = shift;
49  
50 # Get options
51 0 ▶ my $opt = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
52  
53 # Set freedb_cgi
54 0 ▶ $self->setFreeDBCgi ($opt->{freedb_cgi} || '~cddb/cddb.cgi');
Condition0%0/2
$opt->{'freedb_cgi'} || '~cddb/cddb.cgi'
l!l
00
MC/DC0%0/1
$opt->{'freedb_cgi'} || '~cddb/cddb.cgi'
$opt->{'freedb_cgi'}
Truth table0%0/2
A
A= $opt->{'freedb_cgi'}
Aresult
11
00
55  
56 # Initialize super class
57 0 return ($self->SUPER::_initialize ($opt));
58 }
59  
60 0 ▶sub hello {
Subroutine hello: not called CC=1 SCAR=0.0
Pod documented
61 }
62  
63 0 ▶sub lscat {
Subroutine lscat: not called CC=3 SCAR=24.8
Pod documented
64 0 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});
73 0 my $head = shift (@content);
74 0 my @cat = ();
75 0 foreach my $cat (@content) {
76 0 ▶ last if ($cat eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $cat eq "." 0 0 0
77 0 push (@cat, $cat);
78 }
79 0 return (@cat);
80 }
81  
82 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=2.9
Pod documented
83 0 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 ▶ use Net::FreeDB2::Response::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 0 return Net::FreeDB2::Response::Query->new ({
101 content_ref => $content_ref,
102 });
103 }
104  
105 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=2.2
Pod documented
106 0 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 ▶ use Net::FreeDB2::Response::Read;
Subroutine BEGIN: called CC=1 SCAR=0.0
121 0 return Net::FreeDB2::Response::Read->new ({
122 content_ref => $content_ref,
123 });
124 }
125  
126 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
127 0 throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::write, command not supported under HTTP.");
128 }
129  
130 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod documented
131 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::log, to be implemented.");
132 }
133  
134 0 ▶sub motd {
Subroutine motd: not called CC=3 SCAR=24.8
Pod documented
135 0 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});
145 0 my $head = shift (@content);
146 0 my @motd = ();
147 0 foreach my $motd (@content) {
148 0 ▶ last if ($motd eq '.');
Branch: 1 branch
BranchTrueFalseTotal
if $motd eq "." 0 0 0
149 0 push (@motd, $motd);
150 }
151 0 return (@motd);
152 }
153  
154 0 ▶sub discid {
Subroutine discid: not called CC=4 SCAR=30.0
Pod documented
155 0 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});
170 0 my $head = shift (@content);
171 0 my ($code) = $head =~ /^\s*(\d{3})\s+/;
172 0 ▶ $code == 200 || $code == 500 || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::discid, unknown code '$code' returned.");
Branch: 1 branch
BranchTrueFalseTotal
$code == 200 or $code == 500 or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::discid, unknown code '${code}' returned.") 0 0 0
Condition0%0/3
$code == 200 or $code == 500
l!l&&r!l&&!r
000
MC/DC0%0/2
$code == 200 or $code == 500
$code == 200 $code == 500
Truth table0%0/3
A or B
A= $code == 200B= $code == 500
ABresult
1X1
011
000
173 0 ▶ $code == 500 && throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::discid, Command Syntax error.");
Branch: 1 branch
BranchTrueFalseTotal
$code == 500 and "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::discid, Command Syntax error.") 0 0 0
174 0 my @head = split (/\s+/, $head);
175 0 return ($head[4]);
176 }
177  
178 0 ▶sub proto {
Subroutine proto: not called CC=1 SCAR=6.9
Pod documented
179 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::proto, to be implemented.");
180 }
181  
182 0 ▶sub sites {
Subroutine sites: not called CC=1 SCAR=1.2
Pod documented
183 0 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 ▶ use Net::FreeDB2::Response::Sites;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 0 return Net::FreeDB2::Response::Sites->new ({
194 content_ref => $content_ref,
195 });
196 }
197  
198 0 ▶sub stat {
Subroutine stat: not called CC=1 SCAR=6.9
Pod documented
199 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::stat, to be implemented.");
200 }
201  
202 0 ▶sub ver {
Subroutine ver: not called CC=1 SCAR=6.9
Pod documented
203 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::ver, to be implemented.");
204 }
205  
206 0 ▶sub update {
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
207 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::update, to be implemented.");
208 }
209  
210 0 ▶sub whom {
Subroutine whom: not called CC=1 SCAR=6.9
Pod documented
211 0 throw Error::Simple ("ERROR: Net::FreeDB2::HTTP::whom, to be implemented.");
212 }
213  
214 0 ▶sub connect {
Subroutine connect: not called CC=4 SCAR=25.3
Pod documented
215 0 my $self = shift;
216  
217 # Make connection through user agent
218 1 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 my $connection = LWP::UserAgent->new ();
220 0 ▶ defined ($connection) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::connect, Failed to instanciate an \'LWP::UserAgent\' object.');
Branch: 1 branch
BranchTrueFalseTotal
defined $connection or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::connect, Failed to instanciate an 'LWP::UserAgent' object.") 0 0 0
221 0 $self->setConnection ($connection);
222  
223 # Set proxy if required
224 0 ▶ defined ($self->getProxyHost ()) || return;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getProxyHost or return 0 0 0
225 0 ▶ my $url = 'http://' . $self->getProxyHost() . ':' . ($self->getProxyPort () || 8080);
Condition0%0/2
$self->getProxyPort || 8080
l!l
00
MC/DC0%0/1
$self->getProxyPort || 8080
$self->getProxyPort
Truth table0%0/2
A
A= $self->getProxyPort
Aresult
11
00
226 0 $connection->proxy ('http', $url);
227 }
228  
229 0 ▶sub waitCommandReply {
Subroutine waitCommandReply: not called CC=6 SCAR=34.4
Pod undocumented
230 0 my $self = shift;
231 0 my $cmd = shift;
232 0 my $rx = shift;
233  
234 # Check if connection is defined
235 0 ▶ defined ($self->getConnection ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, no connection available.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getConnection or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, no connection available.") 0 0 0
236  
237 # Make url
238 0 my $url = $self->mkUrlBase ();
239 0 $url .= $cmd . $self->mkHello ();
240  
241 # Make request
242 1 ▶ use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
243 0 my $request = HTTP::Request->new (GET => $url);
244 0 ▶ defined ($request) || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to make HTTP::Request object out of url '$url'.");
Branch: 1 branch
BranchTrueFalseTotal
defined $request or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to make HTTP::Request object out of url '${url}'.") 0 0 0
245  
246 # Set proxy authorization if required
247 0 ▶ if ($self->getProxyHost () && $self->getProxyUser ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getProxyHost and $self->getProxyUser) 0 0 0
Condition0%0/3
$self->getProxyHost and $self->getProxyUser
!ll&&!rl&&r
000
MC/DC0%0/2
$self->getProxyHost and $self->getProxyUser
$self->getProxyHost $self->getProxyUser
Truth table0%0/3
A and B
A= $self->getProxyHostB= $self->getProxyUser
ABresult
0X0
100
111
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 ▶ $response->is_success() || throw Error::Simple ("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to execute request for url '$url'.");
Branch: 1 branch
BranchTrueFalseTotal
$response->is_success or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::waitCommandReply, failed to execute request for url '${url}'.") 0 0 0
254  
255  
256 # Return the content reference
257 0 return ($response->content_ref ());
258 }
259  
260 0 ▶sub mkHello {
Subroutine mkHello: not called CC=5 SCAR=34.0
Pod documented
261 0 my $self = shift;
262  
263 0 ▶ defined ($self->getClientName ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_name\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientName or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkHello, 'client_name' not set.") 0 0 0
264 0 ▶ defined ($self->getClientVersion ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_version\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientVersion or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkHello, 'client_version' not set.") 0 0 0
265 0 ▶ defined ($self->getClientHost ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_host\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientHost or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkHello, 'client_host' not set.") 0 0 0
266 0 ▶ defined ($self->getClientUser ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkHello, \'client_user\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getClientUser or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkHello, 'client_user' not set.") 0 0 0
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 0 ▶sub mkUrlBase {
Subroutine mkUrlBase: not called CC=4 SCAR=30.0
Pod documented
279 0 my $self = shift;
280  
281 0 ▶ defined ($self->getFreeDBHost ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, \'freedb_host\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getFreeDBHost or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, 'freedb_host' not set.") 0 0 0
282 0 ▶ defined ($self->getFreeDBCgi ()) || throw Error::Simple ('ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, \'freedb_cgi\' not set.');
Branch: 1 branch
BranchTrueFalseTotal
defined $self->getFreeDBCgi or "Error::Simple"->throw("ERROR: Net::FreeDB2::Connection::HTTP::mkUrlBase, 'freedb_cgi' not set.") 0 0 0
283 0 my $url = 'http://' . $self->getFreeDBHost ();
284 0 ▶ $url .= ':' . $self->getFreeDBPort () if ($self->getFreeDBPort ());
Branch: 1 branch
BranchTrueFalseTotal
if $self->getFreeDBPort 0 0 0
285 0 $url .= '/' . $self->getFreeDBCgi ();
286 }
287  
288 0 ▶sub setFreeDBCgi {
Subroutine setFreeDBCgi: not called CC=1 SCAR=6.9
Pod documented
289 0 my $self = shift;
290  
291 # Set freedb/cddb url
292 0 $self->{Net_FreeDB2_Connection_HTTP}{freedb_cgi} = shift;
293 }
294  
295 0 ▶sub getFreeDBCgi {
Subroutine getFreeDBCgi: not called CC=1 SCAR=6.9
Pod documented
296 0 my $self = shift;
297  
298 # Return freedb/cddb url
299 0 return ($self->{Net_FreeDB2_Connection_HTTP}{freedb_cgi});
300 }
301  
302 1 1;
303 __END__
« blib/lib/Net/FreeDB2/Site.pm ↑ Summary blib/lib/Net/FreeDB2/Connection.pm »