Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.6 KiB

  1. package URI::Heuristic;
  2. # $Id: Heuristic.pm,v 4.12 2001/01/09 20:44:54 gisle Exp $
  3. =head1 NAME
  4. uf_uristr - Expand URI using heuristics
  5. =head1 SYNOPSIS
  6. use URI::Heuristic qw(uf_uristr);
  7. $u = uf_uristr("perl"); # http://www.perl.com
  8. $u = uf_uristr("www.sol.no/sol"); # http://www.sol.no/sol
  9. $u = uf_uristr("aas"); # http://www.aas.no
  10. $u = uf_uristr("ftp.funet.fi"); # ftp://ftp.funet.fi
  11. $u = uf_uristr("/etc/passwd"); # file:/etc/passwd
  12. =head1 DESCRIPTION
  13. This module provides functions that expand strings into real absolute
  14. URIs using some builtin heuristics. Strings that already represent
  15. absolute URIs (i.e. start with a C<scheme:> part) are never modified
  16. and are returned unchanged. The main use of these functions are to
  17. allow abbreviated URIs similar to what many web browsers allow for URIs
  18. typed in by the user.
  19. The following functions are provided:
  20. =over 4
  21. =item uf_uristr($str)
  22. The uf_uristr() function will try to make the string passed as argument
  23. into a proper absolute URI string. The "uf_" prefix stands for "User
  24. Friendly". Under MacOS, it assumes that any string with a common URL
  25. scheme (http, ftp, etc.) is a URL rather than a local path. So don't name
  26. your volumes after common URL schemes and expect uf_uristr() to construct
  27. valid file: URL's on those volumes for you, because it won't.
  28. =item uf_uri($str)
  29. This functions work the same way as uf_uristr() but it will
  30. return a C<URI> object.
  31. =back
  32. =head1 ENVIRONMENT
  33. If the hostname portion of a URI does not contain any dots, then
  34. certain qualified guesses will be made. These guesses are governed be
  35. the following two environment variables.
  36. =over 10
  37. =item COUNTRY
  38. This is the two letter country code (ISO 3166) for your location. If
  39. the domain name of your host ends with two letters, then it is taken
  40. to be the default country. See also L<Locale::Country>.
  41. =item URL_GUESS_PATTERN
  42. Contain a space separated list of URL patterns to try. The string
  43. "ACME" is for some reason used as a placeholder for the host name in
  44. the URL provided. Example:
  45. URL_GUESS_PATTERN="www.ACME.no www.ACME.se www.ACME.com"
  46. export URL_GUESS_PATTERN
  47. Specifying URL_GUESS_PATTERN disables any guessing rules based on
  48. country. An empty URL_GUESS_PATTERN disables any guessing that
  49. involves host name lookups.
  50. =back
  51. =head1 COPYRIGHT
  52. Copyright 1997-1998, Gisle Aas
  53. This library is free software; you can redistribute it and/or
  54. modify it under the same terms as Perl itself.
  55. =cut
  56. use strict;
  57. use vars qw(@EXPORT_OK $VERSION $MY_COUNTRY %LOCAL_GUESSING $DEBUG);
  58. require Exporter;
  59. *import = \&Exporter::import;
  60. @EXPORT_OK = qw(uf_uri uf_uristr uf_url uf_urlstr);
  61. $VERSION = sprintf("%d.%02d", q$Revision: 4.12 $ =~ /(\d+)\.(\d+)/);
  62. eval {
  63. require Net::Domain;
  64. my $fqdn = Net::Domain::hostfqdn();
  65. $MY_COUNTRY = lc($1) if $fqdn =~ /\.([a-zA-Z]{2})$/;
  66. # Some other heuristics to guess country? Perhaps looking
  67. # at some environment variable (LANG, LC_ALL, ???)
  68. $MY_COUNTRY = $ENV{COUNTRY} if exists $ENV{COUNTRY};
  69. };
  70. %LOCAL_GUESSING =
  71. (
  72. 'us' => [qw(www.ACME.gov www.ACME.mil)],
  73. 'uk' => [qw(www.ACME.co.uk www.ACME.org.uk www.ACME.ac.uk)],
  74. 'au' => [qw(www.ACME.com.au www.ACME.org.au www.ACME.edu.au)],
  75. 'il' => [qw(www.ACME.co.il www.ACME.org.il www.ACME.net.il)],
  76. # send corrections and new entries to <[email protected]>
  77. );
  78. sub uf_uristr ($)
  79. {
  80. local($_) = @_;
  81. print STDERR "uf_uristr: resolving $_\n" if $DEBUG;
  82. return unless defined;
  83. s/^\s+//;
  84. s/\s+$//;
  85. if (/^(www|web|home)\./) {
  86. $_ = "http://$_";
  87. } elsif (/^(ftp|gopher|news|wais|http|https)\./) {
  88. $_ = "$1://$_";
  89. } elsif ($^O ne "MacOS" &&
  90. (m,^/, || # absolute file name
  91. m,^\.\.?/, || # relative file name
  92. m,^[a-zA-Z]:[/\\],) # dosish file name
  93. )
  94. {
  95. $_ = "file:$_";
  96. } elsif ($^O eq "MacOS" && m/:/) {
  97. # potential MacOS file name
  98. unless (m/^(ftp|gopher|news|wais|http|https|mailto):/) {
  99. require URI::file;
  100. my $a = URI::file->new($_)->as_string;
  101. $_ = ($a =~ m/^file:/) ? $a : "file:$a";
  102. }
  103. } elsif (/^\w+([\.\-]\w+)*\@(\w+\.)+\w{2,3}$/) {
  104. $_ = "mailto:$_";
  105. } elsif (!/^[.+\-\w]+:/) { # no scheme specified
  106. if (s/^([-\w]+(?:\.[-\w]+)*)([\/:\?\#]|$)/$2/) {
  107. my $host = $1;
  108. if ($host !~ /\./ && $host ne "localhost") {
  109. my @guess;
  110. if (exists $ENV{URL_GUESS_PATTERN}) {
  111. @guess = map { s/\bACME\b/$host/; $_ }
  112. split(' ', $ENV{URL_GUESS_PATTERN});
  113. } else {
  114. if ($MY_COUNTRY) {
  115. my $special = $LOCAL_GUESSING{$MY_COUNTRY};
  116. if ($special) {
  117. my @special = @$special;
  118. push(@guess, map { s/\bACME\b/$host/; $_ }
  119. @special);
  120. } else {
  121. push(@guess, "www.$host.$MY_COUNTRY");
  122. }
  123. }
  124. push(@guess, map "www.$host.$_",
  125. "com", "org", "net", "edu", "int");
  126. }
  127. my $guess;
  128. for $guess (@guess) {
  129. print STDERR "uf_uristr: gethostbyname('$guess')..."
  130. if $DEBUG;
  131. if (gethostbyname($guess)) {
  132. print STDERR "yes\n" if $DEBUG;
  133. $host = $guess;
  134. last;
  135. }
  136. print STDERR "no\n" if $DEBUG;
  137. }
  138. }
  139. $_ = "http://$host$_";
  140. } else {
  141. # pure junk, just return it unchanged...
  142. }
  143. }
  144. print STDERR "uf_uristr: ==> $_\n" if $DEBUG;
  145. $_;
  146. }
  147. sub uf_uri ($)
  148. {
  149. require URI;
  150. URI->new(uf_uristr($_[0]));
  151. }
  152. # legacy
  153. *uf_urlstr = \*uf_uristr;
  154. sub uf_url ($)
  155. {
  156. require URI::URL;
  157. URI::URL->new(uf_uristr($_[0]));
  158. }
  159. 1;