URI/Heuristic.pm

filter: Statementstmt 91.9% 80 / 87 Branchbran 75.0% 48 / 64 Conditioncond 77.7% 14 / 18 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 85.7% 6 / 7 Podpod 50.0% 2 / 4 totaltotal 82.1% 156 / 190 CC 43 SCAR 33.6
CC
43
Cov
84%
uf_uristr
39.2
MY_COUNTRY
12.1
uf_uri
2.0
CRAP 28.7
↑ Summary URI/file/Mac.pm »
1 package URI::Heuristic;
2  
3 =head1 NAME
4  
5 URI::Heuristic - Expand URI using heuristics
6  
7 =head1 SYNOPSIS
8  
9 use URI::Heuristic qw(uf_uristr);
10 $u = uf_uristr("example"); # http://www.example.com
11 $u = uf_uristr("www.sol.no/sol"); # http://www.sol.no/sol
12 $u = uf_uristr("aas"); # http://www.aas.no
13 $u = uf_uristr("ftp.funet.fi"); # ftp://ftp.funet.fi
14 $u = uf_uristr("/etc/passwd"); # file:/etc/passwd
15  
16 =head1 DESCRIPTION
17  
18 This module provides functions that expand strings into real absolute
19 URIs using some built-in heuristics. Strings that already represent
20 absolute URIs (i.e. that start with a C<scheme:> part) are never modified
21 and are returned unchanged. The main use of these functions is to
22 allow abbreviated URIs similar to what many web browsers allow for URIs
23 typed in by the user.
24  
25 The following functions are provided:
26  
27 =over 4
28  
29 =item uf_uristr($str)
30  
31 Tries to make the argument string
32 into a proper absolute URI string. The "uf_" prefix stands for "User
33 Friendly". Under MacOS, it assumes that any string with a common URL
34 scheme (http, ftp, etc.) is a URL rather than a local path. So don't name
35 your volumes after common URL schemes and expect uf_uristr() to construct
36 valid file: URL's on those volumes for you, because it won't.
37  
38 =item uf_uri($str)
39  
40 Works the same way as uf_uristr() but
41 returns a C<URI> object.
42  
43 =back
44  
45 =head1 ENVIRONMENT
46  
47 If the hostname portion of a URI does not contain any dots, then
48 certain qualified guesses are made. These guesses are governed by
49 the following environment variables:
50  
51 =over 10
52  
53 =item COUNTRY
54  
55 The two-letter country code (ISO 3166) for your location. If
56 the domain name of your host ends with two letters, then it is taken
57 to be the default country. See also L<Locale::Country>.
58  
59 =item HTTP_ACCEPT_LANGUAGE, LC_ALL, LANG
60  
61 If COUNTRY is not set, these standard environment variables are
62 examined and country (not language) information possibly found in them
63 is used as the default country.
64  
65 =item URL_GUESS_PATTERN
66  
67 Contains a space-separated list of URL patterns to try. The string
68 "ACME" is for some reason used as a placeholder for the host name in
69 the URL provided. Example:
70  
71 URL_GUESS_PATTERN="www.ACME.no www.ACME.se www.ACME.com"
72 export URL_GUESS_PATTERN
73  
74 Specifying URL_GUESS_PATTERN disables any guessing rules based on
75 country. An empty URL_GUESS_PATTERN disables any guessing that
76 involves host name lookups.
77  
78 =back
79  
80 =head1 COPYRIGHT
81  
82 Copyright 1997-1998, Gisle Aas
83  
84 This library is free software; you can redistribute it and/or
85 modify it under the same terms as Perl itself.
86  
87 =cut
88  
89 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 1 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 1 our @EXPORT_OK = qw(uf_uri uf_uristr uf_url uf_urlstr);
94  
95 1 our $VERSION = '5.40';
96  
97 1 our ($MY_COUNTRY, $DEBUG);
98  
99 16 ▶sub MY_COUNTRY() {
Subroutine MY_COUNTRY: called CC=12 SCAR=24.9
Pod undocumented
100 16 for ($MY_COUNTRY) {
101 16 ▶ return $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 9 7 16
102  
103 # First try the environment.
104 7 $_ = $ENV{COUNTRY};
105 7 ▶ return $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 7 7
106  
107 # Try the country part of LC_ALL and LANG from environment
108 7 my @srcs = ($ENV{LC_ALL}, $ENV{LANG});
109  
110 # ...and HTTP_ACCEPT_LANGUAGE before those if present
111 7 ▶ if (my $httplang = $ENV{HTTP_ACCEPT_LANGUAGE}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $httplang = $ENV{"HTTP_ACCEPT_LANGUAGE"}) 3 4 7
112  
113 # TODO: q-value processing/ordering
114 3 for $httplang (split(/\s*,\s*/, $httplang)) {
115 3 ▶ if ($httplang =~ /^\s*([a-zA-Z]+)[_-]([a-zA-Z]{2})\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($httplang =~ /^\s*([a-zA-Z]+)[_-]([a-zA-Z]{2})\s*$/) 3 0 3
116 3 unshift(@srcs, "${1}_${2}");
117 3 last;
118 }
119 }
120 }
121 7 for (@srcs) {
122 9 ▶ next unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 9 9
123 9 ▶ return lc($1) if /^[a-zA-Z]+_([a-zA-Z]{2})(?:[.@]|$)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[a-zA-Z]+_([a-zA-Z]{2})(?:[.\@]|$)/ 5 4 9
124 }
125  
126 # Last bit of domain name. This may access the network.
127 2 require Net::Domain;
128 2 my $fqdn = Net::Domain::hostfqdn();
129 2 ▶ $_ = lc($1) if $fqdn =~ /\.([a-zA-Z]{2})$/;
Branch: 1 branch
BranchTrueFalseTotal
if $fqdn =~ /\.([a-zA-Z]{2})$/ 1 1 2
130 2 ▶ return $_ if defined;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 1 1 2
131  
132 # Give up. Defined but false.
133 1 return ($_ = 0);
134 }
135 }
136  
137 1 our %LOCAL_GUESSING = (
138 'us' => [qw(www.ACME.gov www.ACME.mil)],
139 'gb' => [qw(www.ACME.co.uk www.ACME.org.uk www.ACME.ac.uk)],
140 'au' => [qw(www.ACME.com.au www.ACME.org.au www.ACME.edu.au)],
141 'il' => [qw(www.ACME.co.il www.ACME.org.il www.ACME.net.il)],
142  
143 # send corrections and new entries to <gisle@aas.no>
144 );
145  
146 # Backwards compatibility; uk != United Kingdom in ISO 3166
147 1 $LOCAL_GUESSING{uk} = $LOCAL_GUESSING{gb};
148  
149  
150 28 ▶sub uf_uristr ($) {
Subroutine uf_uristr: called CC=32 SCAR=36.7
Pod documented
151 28 local ($_) = @_;
152 28 ▶ print STDERR "uf_uristr: resolving $_\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Heuristic::DEBUG 0 28 28
153 28 ▶ return unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 28 28
154  
155 28 s/^\s+//;
156 28 s/\s+$//;
157  
158 28 ▶ if (/^(www|web|home)[a-z0-9-]*(?:\.|$)/i) {
Branch: 7 branches
BranchTrueFalseTotal
if (/^(www|web|home)[a-z0-9-]*(?:\.|$)/i) { } 0 28 28
elsif (/^(ftp|gopher|news|wais|https|http)[a-z0-9-]*(?:\.|$)/i) { } 6 22 28
elsif (m[^//] or /^[\\][\\]/) { } 2 20 22
elsif ($^O ne 'MacOS' and m[^/] || m[^\.\.?/] || m(^[a-zA-Z]:[/\\])) { } 3 17 20
elsif ($^O eq 'MacOS' and /:/) { } 0 17 17
elsif (/^\w+([\.\-]\w+)*\@(\w+\.)+\w{2,3}$/) { } 2 15 17
elsif (not /^[a-zA-Z][a-zA-Z0-9.+\-]*:/) { } 12 3 15
159 0 $_ = "http://$_";
160  
161 }
162 elsif (/^(ftp|gopher|news|wais|https|http)[a-z0-9-]*(?:\.|$)/i) {
163 6 $_ = lc($1) . "://$_";
164  
165 }
166 ▶ elsif (m,^//, || m,^[\\][\\],) # UNC-like file name
Condition100%3/3
m[^//] or /^[\\][\\]/
l!l&&r!l&&!r
1120
MC/DC100%2/2
m[^//] or /^[\\][\\]/
m[^//] /^[\\][\\]/
Truth table100%3/3
A or B
A= m[^//]B= /^[\\][\\]/
ABresult
1X1
011
000
167 {
168 2 s{[\\]}{/}g;
169 2 $_ = "smb:$_";
170 }
171 ▶ elsif (
Condition88%8/9
$^O ne "MacOS" and m[^/] || m[^\.\.?/] || m(^[a-zA-Z]:[/\\])
!ll&&!rl&&r
0173
m[^/] or m[^\.\.?/] or m(^[a-zA-Z]:[/\\])
l!l&&r!l&&!r
2117
m[^/] or m[^\.\.?/]
l!l&&r!l&&!r
1118
MC/DC75%3/4
$^O ne "MacOS" and m[^/] || m[^\.\.?/] || m(^[a-zA-Z]:[/\\])
$^O ne "MacOS" m[^/] m[^\.\.?/] m(^[a-zA-Z]:[/\\])
Truth table80%4/5
A and B or C or D
A= $^O ne "MacOS"B= m[^/]C= m[^\.\.?/]D= m(^[a-zA-Z]:[/\\])
ABCDresult
0XXX0
10000
11XX1
101X1
10011
172 $^O ne "MacOS" && (
173 m,^/, || # absolute file name
174 m,^\.\.?/, || # relative file name
175 m,^[a-zA-Z]:[/\\],
176 ) # dosish file name
177 )
178 {
179 3 $_ = "file:$_";
180  
181 }
182 ▶ elsif ($^O eq "MacOS" && m/:/) {
Condition33%1/3
$^O eq "MacOS" and /:/
!ll&&!rl&&r
1700
MC/DC0%0/2
$^O eq "MacOS" and /:/
$^O eq "MacOS" /:/
Truth table33%1/3
A and B
A= $^O eq "MacOS"B= /:/
ABresult
0X0
100
111
183  
184 # potential MacOS file name
185 0 ▶ unless (m/^(ftp|gopher|news|wais|http|https|mailto):/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/^(ftp|gopher|news|wais|http|https|mailto):/) 0 0 0
186 0 require URI::file;
187 0 my $a = URI::file->new($_)->as_string;
188 0 ▶ $_ = ($a =~ m/^file:/) ? $a : "file:$a";
Branch: 1 branch
BranchTrueFalseTotal
$a =~ /^file:/ ? : 0 0 0
189 }
190 }
191 elsif (/^\w+([\.\-]\w+)*\@(\w+\.)+\w{2,3}$/) {
192 2 $_ = "mailto:$_";
193  
194 }
195 elsif (!/^[a-zA-Z][a-zA-Z0-9.+\-]*:/) { # no scheme specified
196 12 ▶ if (s/^([-\w]+(?:\.[-\w]+)*)([\/:\?\#]|$)/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if (s[^([-\w]+(?:\.[-\w]+)*)([/:\?\#]|$)][$2]) { } 12 0 12
197 12 my $host = $1;
198  
199 12 my $scheme = "http";
200 12 ▶ if (/^:(\d+)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^:(\d+)\b/) 3 9 12
201  
202 # Some more or less well known ports
203 3 ▶ if ($1 =~ /^[56789]?443$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 =~ /^[56789]?443$/) { } 1 2 3
elsif ($1 eq '21') { } 1 1 2
204 1 $scheme = "https";
205 }
206 elsif ($1 eq "21") {
207 1 $scheme = "ftp";
208 }
209 }
210  
211 12 ▶ if ($host !~ /\./ && $host ne "localhost") {
Branch: 1 branch
BranchTrueFalseTotal
if (not $host =~ /\./ and $host ne "localhost") 9 3 12
Condition66%2/3
not $host =~ /\./ and $host ne "localhost"
!ll&&!rl&&r
309
MC/DC50%1/2
not $host =~ /\./ and $host ne "localhost"
not $host =~ /\./ $host ne "localhost"
Truth table66%2/3
not(A) and B
A= not $host =~ /\./B= $host ne "localhost"
ABresult
0X0
100
111
212 9 my @guess;
213 9 ▶ if (exists $ENV{URL_GUESS_PATTERN}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ENV{'URL_GUESS_PATTERN'}) { } 2 7 9
214 2 @guess = map { s/\bACME\b/$host/; $_ }
215 2 split(' ', $ENV{URL_GUESS_PATTERN});
216 }
217 else {
218 7 ▶ if (MY_COUNTRY()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&MY_COUNTRY()) 6 1 7
219 6 my $special = $LOCAL_GUESSING{MY_COUNTRY()};
220 6 ▶ if ($special) {
Branch: 1 branch
BranchTrueFalseTotal
if ($special) { } 3 3 6
221 3 my @special = @$special;
222 push(@guess,
223 3 map { s/\bACME\b/$host/; $_ } @special);
224 }
225 else {
226 3 push(@guess, "www.$host." . MY_COUNTRY());
227 }
228 }
229 7 push(@guess,
230 map "www.$host.$_",
231 "com", "org", "net", "edu", "int");
232 }
233  
234  
235 9 my $guess;
236 9 for $guess (@guess) {
237 9 ▶ print STDERR "uf_uristr: gethostbyname('$guess.')..."
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Heuristic::DEBUG 0 9 9
238 if $DEBUG;
239 9 ▶ if (gethostbyname("$guess.")) {
Branch: 1 branch
BranchTrueFalseTotal
if (gethostbyname("$guess.")) 8 1 9
240 8 ▶ print STDERR "yes\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Heuristic::DEBUG 0 8 8
241 8 $host = $guess;
242 8 last;
243 }
244 1 ▶ print STDERR "no\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Heuristic::DEBUG 0 1 1
245 }
246 }
247 12 $_ = "$scheme://$host$_";
248  
249 }
250 else {
251 # pure junk, just return it unchanged...
252  
253 }
254 }
255 28 ▶ print STDERR "uf_uristr: ==> $_\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $URI::Heuristic::DEBUG 0 28 28
256  
257 28 $_;
258 }
259  
260 0 ▶sub uf_uri ($) {
Subroutine uf_uri: not called CC=1 SCAR=6.9
Pod documented
261 0 require URI;
262 0 URI->new(uf_uristr($_[0]));
263 }
264  
265 # legacy
266 1 *uf_urlstr = \*uf_uristr;
267  
268 5 ▶sub uf_url ($) {
Subroutine uf_url: called CC=1 SCAR=0.0
Pod undocumented
269 5 require URI::URL;
270 5 URI::URL->new(uf_uristr($_[0]));
271 }
272  
273 1 1;
↑ Summary URI/file/Mac.pm »