File Coverage

blib/lib/URI/ImpliedBase.pm
Criterion Covered Total %
statement 36 36 100.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package URI::ImpliedBase;
2 8     8   190270 use strict;
  8         19  
  8         327  
3 8     8   43 use Cwd;
  8         14  
  8         494  
4 8     8   9212 use URI;
  8         70611  
  8         274  
5              
6             BEGIN {
7 8     8   63 use Exporter ();
  8         16  
  8         167  
8 8     8   41 use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  8         13  
  8         992  
9 8     8   19 $VERSION = 0.08;
10 8         158 @ISA = qw (Exporter URI);
11             #Give a hoot don't pollute, do not export more than needed by default
12 8         20 @EXPORT = qw ();
13 8         12 @EXPORT_OK = qw ();
14 8         2646 %EXPORT_TAGS = ();
15             }
16              
17              
18             =head1 NAME
19              
20             URI::ImpliedBase - magically force all URIs to be absolute
21              
22             =head1 SYNOPSIS
23              
24             use URI::ImpliedBase;
25              
26             # Set the base to search.cpan.org
27             $u = URI::ImpliedBase->new("http://search.cpan.org");
28              
29             $v = URI::ImpliedBase->new('subdir')
30             print $v->as_string; # prints http://search.cpan.org/subdir
31              
32             # No default now
33             URI::ImpliedBase->clear();
34              
35             # Force current working directory to be the URI
36             $w = URI::ImpliedBase->new("../wsdl/test.wsdl");
37             print $w->as_string; # prints (e.g.) file:///Users/joe/wsdl/test.wsdl
38              
39             =head1 DESCRIPTION
40              
41             This module is a drop-in replacement for URI. It wraps the new() method with
42             some extra code which automatically captures either the base of the supplied
43             URI (if it is absolute), or supplies the current base to Cnew_abs()>
44             (if it is relative). If the current base is unset when a relative URI is
45             supplied, the current working directory is used to build a "file:" URI and
46             this is saved as the current base.
47              
48             You can force a new base at any time by calling Cclear()>.
49              
50             =head1 USAGE
51              
52             See the X section for typical usage.
53              
54             =head1 NOTES
55              
56             Each time you call URI::ImpliedBase->new(), URI::ImpliedBase checks the scheme
57             of the supplied URL against @URI::ImpliedBase::accepted_schemes. If the
58             scheme of the new URI is in the list of accepted schemes, we update the base.
59              
60             The initial set of schemes which update the base are 'http' and 'https'. You
61             may update the list of schemes by altering @URI::ImpliedBase::accepted_schemes.
62              
63             =head1 BUGS
64              
65             Whether or not the current directory stuff works for a non-UNIX OS is currently
66             unknown.
67              
68             The base is stored internally at the moment; this may be problematic for
69             multi-threaded code.
70              
71             =head1 SUPPORT
72              
73             Contact the author for support on an informal basis. No guarantee of response
74             in a timely fashion.
75              
76             =head1 AUTHOR
77              
78             Joe McMahon
79             mcmahon@ibiblio.org
80             http://ibiblio.org/mcmahon
81              
82             =head1 COPYRIGHT
83              
84             This program is free software; you can redistribute
85             it and/or modify it under the same terms as Perl itself.
86              
87             The full text of the license can be found in the
88             LICENSE file included with this module.
89              
90              
91             =head1 SEE ALSO
92              
93             perl(1), C URI.
94              
95             =head1 CLASS METHODS
96              
97             =head2 new
98              
99             The new method uses Cnew()> to convert the incoming string into a
100             URI object. It extracts the scheme and path if it can, and saves them as
101             the new default base.
102              
103             If there is no scheme, but there is a path, and there's no existing default
104             base,C guesses that the path is a reference to the local
105             filesystem relative to the current working directory. It saves the current
106             working directory as the base and C as the scheme, then uses these to
107             build an absolute C URI and returns it.
108              
109             If there's no scheme, and there is a path, and there is a default base,
110             C uses the default base to convert the path to an absolute
111             URI.
112              
113             The base is stored in a package lexical, C<$current_base>. This may be a
114             problem for multithreaded code, or code under C or C;
115             this code has I been tested in these environments.
116              
117             =cut
118              
119             our $current_base = "";
120             our @accepted_schemes = qw(http https);
121              
122             =head1 METHODS
123              
124             =head2 new
125              
126             Accepts a URI and figures out what the proper base is for it.
127              
128             If the scheme is defined, we can just save the current URI as
129             the base. If there's a path but no scheme, we have to determine
130             the proper base: if the base has already been determined by a
131             previous call, then we use that. Otherwise we figure out the
132             current working directory and use that.
133              
134             =cut
135              
136             sub new {
137 16     16 1 1882 my ($class, $uri_string) = @_;
138 16         30 my $result;
139              
140 16         103 my $probe_uri = URI->new($uri_string);
141 16 100 100     72014 if ($probe_uri->scheme and
  12 100       909  
142             grep {$_ eq $probe_uri->scheme} @accepted_schemes) {
143             # New base. Save it.
144 5         109 $current_base = $probe_uri->as_string;
145 5         32 $result = $probe_uri;
146             }
147             elsif ($probe_uri->path) {
148             # Path but no scheme. Assume relative.
149 10 100       726 if ($current_base) {
150             # Use the current base to construct an absoute URI.
151 5         22 $result = URI->new_abs($uri_string, $current_base);
152             }
153             else {
154             # Relative, but no current base. Use the current working directory.
155 5         22 $uri_string =~ s{^./}{};
156 5         69 $result = URI->new("file://" . getcwd() . "/" . $uri_string);
157 5         7056 $current_base = $result->as_string;
158             }
159             }
160             else {
161             # A scheme-only URI? Let URI bounce it.
162 1         109 $result = $probe_uri;
163             }
164 16         1911 $result;
165             }
166              
167             =head2 current_base
168              
169             Returns the currently-derived base URI.
170              
171             =cut
172              
173             sub current_base {
174 11     11 1 1082 $current_base;
175             }
176              
177             =head2 clear
178              
179             Deletes the current implied base.
180              
181             =cut
182              
183             sub clear {
184 1     1 1 505 $current_base = "";
185             }
186              
187              
188             1; #this line is important and will help the module return a true value
189             __END__