blib/lib/URI/Title.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 27 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 233 SCAR 66.3
CC
27
Cov
0%
title
156.0
_get_end
56.0
_get_limited
42.0
CRAP 756.0
1 package URI::Title;
2 $URI::Title::VERSION = '1.902';
3 use 5.006;
4 use warnings;
5 use strict;
6  
7 use base qw(Exporter);
8 our @EXPORT_OK = qw( title );
9  
10 use Module::Pluggable (search_path => ['URI::Title'], require => 1 );
11 use File::Type;
12  
13 use LWP::UserAgent;
14 use HTTP::Request;
15 use HTTP::Response;
16  
17  
18 sub _ua {
19 my $ua = LWP::UserAgent->new;
20 $ua->agent("URI::Title/$URI::Title::VERSION");
21 $ua->timeout(20);
22 $ua->default_header('Accept-Encoding' => 'gzip');
23 return $ua;
24 }
25  
26 sub _get_limited {
27 my $url = shift;
28 my $size = shift || 32*1024;
29 my $ua = _ua();
30 $ua->max_size($size);
31 my $req = HTTP::Request->new(GET => $url);
32 $req->header( Range => "bytes=0-$size" );
33 $req->header( "Accept-Encoding" => "" ); # vox sends invalid gzipped data?
34 my $res = eval { $ua->request($req) };
35 return unless $res; # useragent explodes for non-valid uris
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 return _get_all($url) if $res->code >= 400 and $res->code < 500 and $res->code != 404;
41 return unless $res->is_success;
42 if (!wantarray) {
43 return $res->decoded_content || $res->content;
44 }
45 my $cset = "iso-8859-1"; # default;
46 my $ct = $res->header("Content-type");
47 if ($ct =~ /charset\s*=\>?\s*\"?([\w-]+)/i) {
48 $cset = lc($1);
49 #warn "Got charset $cset from URI headers\n";
50 }
51 return ($res->decoded_content || $res->content, $cset);
52 }
53  
54 sub _get_end {
55 my $url = shift;
56 my $size = shift || 16*1024;
57  
58 my $ua = _ua();
59  
60 my $request = HTTP::Request->new(HEAD => $url);
61 my $response = $ua->request($request);
62 return unless $response; # useragent explodes for non-valid uris
63 my $length = $response->header('Content-Length');
64  
65 return unless $length; # We can't get the length, and we're _not_
66 # going to get the whole thing.
67  
68 my $start = $length - $size;
69  
70 $ua->max_size($size);
71  
72 my $req = HTTP::Request->new(GET => $url);
73 $req->header( Range => "bytes=$start-$length" );
74 my $res = $ua->request($req);
75 return unless $res; # useragent explodes for non-valid uris
76  
77 return unless $res->is_success;
78 return $res->decoded_content unless wantarray;
79 my $cset = "iso-8859-1"; # default;
80 my $ct = $res->header("Content-type");
81 if ($ct =~ /charset=\"?(.*)\"?$/) {
82 $cset = $1;
83 }
84 return ($res->decoded_content, $cset);
85 }
86  
87 sub _get_all {
88 my $url = shift;
89 my $ua = _ua();
90 my $req = HTTP::Request->new(GET => $url);
91 my $res = $ua->request($req);
92 return unless $res->is_success;
93 return $res->decoded_content unless wantarray;
94 my $cset = "iso-8859-1"; # default;
95 my $ct = $res->header("Content-type");
96 if ($ct =~ /charset=\"?(.*)\"?$/) {
97 $cset = $1;
98 }
99 return ($res->decoded_content, $cset);
100 }
101  
102 # cache
103 our $HANDLERS;
104 sub _handlers {
105 my @plugins = plugins();
106 return $HANDLERS if $HANDLERS;
107 for my $plugin (@plugins) {
108 for my $type ($plugin->types) {
109 $HANDLERS->{$type} = $plugin;
110 }
111 }
112 return $HANDLERS;
113 }
114  
115 sub title {
116 my $param = shift;
117 my $data;
118 my $url;
119 my $type;
120 my $cset = "iso-8859-1"; # default
121  
122 # we can be passed a hashref. Keys are url, or data.
123 if (ref($param)) {
124 if ($param->{data}) {
125 $data = $param->{data};
126 $data = $$data if ref($data); # we can be passed a ref to the data
127 } elsif ($param->{url}) {
128 $url = $param->{url};
129 } else {
130 use Carp qw(croak);
131 croak("Expected a single parameter, or an 'url' or 'data' key");
132 }
133  
134 # otherwise, assume we're passed an url
135 } else {
136 $url = $param;
137 }
138  
139 if (!$url and !$data) {
140 warn "Need at least an url or data";
141 return;
142 }
143  
144 # If we don't have data, we will have an url, so try to get data.
145 if (!$data) {
146 # url might be a filename
147 if (-e $url) {
148 local $/ = undef;
149 unless (open DATA, $url) {
150 warn "$url looks like a file and isn't";
151 return;
152 }
153 $data = <DATA>;
154 close DATA;
155
156 # If not, assume it's an url
157 } else {
158 # special case for itms
159 if ($url =~ s/^itms:/http:/) {
160 $type = "itms";
161 $data = 1; # we don't need it, fake it.
162  
163 } else {
164 $url =~ s{#!}{?_escaped_fragment_=};
165  
166 ($data, $cset) = _get_limited($url);
167 }
168 }
169 }
170 if (!$data) {
171 #warn "Can't get content for $url";
172 return;
173 }
174  
175 return undef unless $data;
176  
177 $type ||= File::Type->new->checktype_contents($data);
178  
179 my $handlers = _handlers();
180 my $handler = $handlers->{$type} || $handlers->{default}
181 or return;
182  
183 return $handler->title($url, $data, $type, $cset);
184 }
185  
186 1;
187  
188 __END__