lib/URI/Title.pm

filter: Statementstmt 65.8% 85 / 129 Branchbran 31.4% 17 / 54 Conditioncond 36.0% 9 / 25 MC/DCmcdc 6.6% 1 / 15 Subroutinesub 93.7% 15 / 16 Podpod 100% 1 / 1 totaltotal 53.3% 128 / 240 SCAR 52.6
CC
39
Cov
53%
_get_end
72.0
title
50.8
_get_limited
15.2
CRAP 193.3
« blib/lib/URI/Title/HTML.pm ↑ Summary lib/URI/Title/MP3.pm »
1 package URI::Title;
2 5 $URI::Title::VERSION = '1.902';
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 our @EXPORT_OK = qw( title );
9  
10 5 ▶use Module::Pluggable (search_path => ['URI::Title'], require => 1 );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use File::Type;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub _ua {
19 4 ▶ my $ua = LWP::UserAgent->new;
Subroutine _ua: called CC=1 SCAR=0.0
20 4 $ua->agent("URI::Title/$URI::Title::VERSION");
21 4 $ua->timeout(20);
22 4 $ua->default_header('Accept-Encoding' => 'gzip');
23 4 return $ua;
24 }
25  
26 sub _get_limited {
27 3 ▶ my $url = shift;
Subroutine _get_limited: called CC=11 SCAR=27.2
28 3 ▶ my $size = shift || 32*1024;
Condition50%1/2
shift() || 32768
l!l
03
MC/DC0%0/1
shift() || 32768
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
29 3 my $ua = _ua();
30 3 $ua->max_size($size);
31 3 my $req = HTTP::Request->new(GET => $url);
32 3 $req->header( Range => "bytes=0-$size" );
33 3 $req->header( "Accept-Encoding" => "" ); # vox sends invalid gzipped data?
34 3 my $res = eval { $ua->request($req) };
35 3 ▶ return unless $res; # useragent explodes for non-valid uris
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 3 3
36  
37 # some servers don't like the Range header. If we
38 # get an odd 4xx response that isn't 404, just try getting
39 # the full thing. This may be a little impolite.
40 3 ▶ return _get_all($url) if $res->code >= 400 and $res->code < 500 and $res->code != 404;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code >= 400 and $res->code < 500 and $res->code != 404 1 2 3
Condition66%4/6
$res->code >= 400 and $res->code < 500 and $res->code != 404
!ll&&!rl&&r
201
$res->code >= 400 and $res->code < 500
!ll&&!rl&&r
201
MC/DC33%1/3
$res->code >= 400 and $res->code < 500 and $res->code != 404
$res->code >= 400 $res->code < 500 $res->code != 404
Truth table50%2/4
A and B and C
A= $res->code >= 400B= $res->code < 500C= $res->code != 404
ABCresult
0XX0
10X0
1100
1111
41 2 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 2 2
42 2 ▶ if (!wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 2 2
43 0 ▶ return $res->decoded_content || $res->content;
Condition0%0/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
000
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table0%0/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
44 }
45 2 my $cset = "iso-8859-1"; # default;
46 2 my $ct = $res->header("Content-type");
47 2 ▶ if ($ct =~ /charset\s*=\>?\s*\"?([\w-]+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /charset\s*=\>?\s*\"?([\w-]+)/i) 0 2 2
48 0 $cset = lc($1);
49 #warn "Got charset $cset from URI headers\n";
50 }
51 2 ▶ return ($res->decoded_content || $res->content, $cset);
Condition33%1/3
$res->decoded_content || $res->content
l!l&&r!l&&!r
200
MC/DC0%0/2
$res->decoded_content || $res->content
$res->decoded_content $res->content
Truth table33%1/3
A || B
A= $res->decoded_contentB= $res->content
ABresult
1X1
011
000
52 }
53  
54 sub _get_end {
55 0 ▶ my $url = shift;
Subroutine _get_end: not called CC=8 SCAR=42.8
56 0 ▶ my $size = shift || 16*1024;
Condition0%0/2
shift() || 16384
l!l
00
MC/DC0%0/1
shift() || 16384
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
57  
58 0 my $ua = _ua();
59  
60 0 my $request = HTTP::Request->new(HEAD => $url);
61 0 my $response = $ua->request($request);
62 0 ▶ return unless $response; # useragent explodes for non-valid uris
Branch: 1 branch
BranchTrueFalseTotal
unless $response 0 0 0
63 0 my $length = $response->header('Content-Length');
64  
65 0 ▶ return unless $length; # We can't get the length, and we're _not_
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 0 0
66 # going to get the whole thing.
67  
68 0 my $start = $length - $size;
69  
70 0 $ua->max_size($size);
71  
72 0 my $req = HTTP::Request->new(GET => $url);
73 0 $req->header( Range => "bytes=$start-$length" );
74 0 my $res = $ua->request($req);
75 0 ▶ return unless $res; # useragent explodes for non-valid uris
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
76  
77 0 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
78 0 ▶ return $res->decoded_content unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
79 0 my $cset = "iso-8859-1"; # default;
80 0 my $ct = $res->header("Content-type");
81 0 ▶ if ($ct =~ /charset=\"?(.*)\"?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /charset=\"?(.*)\"?$/) 0 0 0
82 0 $cset = $1;
83 }
84 0 return ($res->decoded_content, $cset);
85 }
86  
87 sub _get_all {
88 1 ▶ my $url = shift;
Subroutine _get_all: called CC=4 SCAR=21.2
89 1 my $ua = _ua();
90 1 my $req = HTTP::Request->new(GET => $url);
91 1 my $res = $ua->request($req);
92 1 ▶ return unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 1 0 1
93 0 ▶ return $res->decoded_content unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
94 0 my $cset = "iso-8859-1"; # default;
95 0 my $ct = $res->header("Content-type");
96 0 ▶ if ($ct =~ /charset=\"?(.*)\"?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct =~ /charset=\"?(.*)\"?$/) 0 0 0
97 0 $cset = $1;
98 }
99 0 return ($res->decoded_content, $cset);
100 }
101  
102 # cache
103 5 our $HANDLERS;
104 sub _handlers {
105 2 ▶ my @plugins = plugins();
Subroutine _handlers: called CC=4 SCAR=13.9
106 2 ▶ return $HANDLERS if $HANDLERS;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Title::HANDLERS 0 2 2
107 2 for my $plugin (@plugins) {
108 8 for my $type ($plugin->types) {
109 18 $HANDLERS->{$type} = $plugin;
110 }
111 }
112 2 return $HANDLERS;
113 }
114  
115 sub title {
116 3 ▶ my $param = shift;
Subroutine title: called CC=16 SCAR=39.3
Pod documented
117 3 my $data;
118 my $url;
119 3 my $type;
120 3 my $cset = "iso-8859-1"; # default
121  
122 # we can be passed a hashref. Keys are url, or data.
123 3 ▶ if (ref($param)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param) { } 0 3 3
124 0 ▶ if ($param->{data}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($param->{'data'}) { } 0 0 0
elsif ($param->{'url'}) { } 0 0 0
125 0 $data = $param->{data};
126 0 ▶ $data = $$data if ref($data); # we can be passed a ref to the data
Branch: 1 branch
BranchTrueFalseTotal
if ref $data 0 0 0
127 } elsif ($param->{url}) {
128 0 $url = $param->{url};
129 } else {
130 5 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
131 0 croak("Expected a single parameter, or an 'url' or 'data' key");
132 }
133  
134 # otherwise, assume we're passed an url
135 } else {
136 3 $url = $param;
137 }
138  
139 3 ▶ if (!$url and !$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url or $data) 0 3 3
Condition33%1/3
$url or $data
l!l&&r!l&&!r
300
MC/DC0%0/2
$url or $data
$url $data
Truth table33%1/3
A or B
A= $urlB= $data
ABresult
1X1
011
000
140 0 warn "Need at least an url or data";
141 0 return;
142 }
143  
144 # If we don't have data, we will have an url, so try to get data.
145 3 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 3 0 3
146 # url might be a filename
147 3 ▶ if (-e $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $url) { } 0 3 3
148 0 local $/ = undef;
149 0 ▶ unless (open DATA, $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open DATA, $url) 0 0 0
150 0 warn "$url looks like a file and isn't";
151 0 return;
152 }
153 0 $data = <DATA>;
154 0 close DATA;
155
156 # If not, assume it's an url
157 } else {
158 # special case for itms
159 3 ▶ if ($url =~ s/^itms:/http:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s/^itms:/http:/) { } 0 3 3
160 0 $type = "itms";
161 0 $data = 1; # we don't need it, fake it.
162  
163 } else {
164 3 $url =~ s{#!}{?_escaped_fragment_=};
165  
166 3 ($data, $cset) = _get_limited($url);
167 }
168 }
169 }
170 3 ▶ if (!$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 1 2 3
171 #warn "Can't get content for $url";
172 1 return;
173 }
174  
175 2 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 2 2
176  
177 2 ▶ $type ||= File::Type->new->checktype_contents($data);
Condition33%1/3
$type ||= "File::Type"->new->checktype_contents($data)
l!l&&r!l&&!r
020
MC/DC0%0/2
$type ||= "File::Type"->new->checktype_contents($data)
$type "File::Type"->new->checktype_contents($data)
Truth table33%1/3
A ||= B
A= $typeB= "File::Type"->new->checktype_contents($data)
ABresult
1X1
011
000
178  
179 2 my $handlers = _handlers();
180 my $handler = $handlers->{$type} || $handlers->{default}
181 2 ▶ or return;
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $handlers->{$type} || $handlers->{"default"} or return 0 2 2
Condition33%1/3
$handlers->{$type} || $handlers->{'default'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$handlers->{$type} || $handlers->{'default'}
$handlers->{$type} $handlers->{'default'}
Truth table33%1/3
A || B
A= $handlers->{$type}B= $handlers->{'default'}
ABresult
1X1
011
000
182  
183 2 return $handler->title($url, $data, $type, $cset);
184 }
185  
186 5 1;
187  
188 __END__
« blib/lib/URI/Title/HTML.pm ↑ Summary lib/URI/Title/MP3.pm »