File Coverage

blib/lib/URI/URL.pm
Criterion Covered Total %
statement 89 93 95.7
branch 21 28 75.0
condition 11 18 61.1
subroutine 32 34 94.1
pod 3 27 11.1
total 156 200 78.0


line stmt bran cond sub pod time code
1             package URI::URL;
2              
3 6     6   286124 use strict;
  6         50  
  6         182  
4 6     6   36 use warnings;
  6         16  
  6         195  
5              
6 6     6   3307 use parent 'URI::WithBase';
  6         1950  
  6         34  
7              
8             our $VERSION = '5.21';
9              
10             # Provide as much as possible of the old URI::URL interface for backwards
11             # compatibility...
12              
13 6     6   443 use Exporter 5.57 'import';
  6         121  
  6         480  
14             our @EXPORT = qw(url);
15              
16             # Easy to use constructor
17 7     7 0 244 sub url ($;$) { URI::URL->new(@_); }
18              
19 6     6   40 use URI::Escape qw(uri_unescape);
  6         15  
  6         9752  
20              
21             sub new
22             {
23 255     255 1 48471 my $class = shift;
24 255         703 my $self = $class->SUPER::new(@_);
25 255         1036 $self->[0] = $self->[0]->canonical;
26 255         763 $self;
27             }
28              
29             sub newlocal
30             {
31 6     6 0 202 my $class = shift;
32 6         113 require URI::file;
33 6         103 bless [URI::file->new_abs(shift)], $class;
34             }
35              
36             {package URI::_foreign;
37             sub _init # hope it is not defined
38             {
39 8     8   20 my $class = shift;
40 8 50       23 die "Unknown URI::URL scheme $_[1]:" if $URI::URL::STRICT;
41 8         31 $class->SUPER::_init(@_);
42             }
43             }
44              
45             sub strict
46             {
47 2     2 0 1211 my $old = $URI::URL::STRICT;
48 2 50       9 $URI::URL::STRICT = shift if @_;
49 2         3 $old;
50             }
51              
52             sub print_on
53             {
54 0     0 0 0 my $self = shift;
55 0         0 require Data::Dumper;
56 0         0 print STDERR Data::Dumper::Dumper($self);
57             }
58              
59             sub _try
60             {
61 42     42   66 my $self = shift;
62 42         51 my $method = shift;
63 42         47 scalar(eval { $self->$method(@_) });
  42         122  
64             }
65              
66             sub crack
67             {
68             # should be overridden by subclasses
69 6     6 0 10 my $self = shift;
70 6         35 (scalar($self->scheme),
71             $self->_try("user"),
72             $self->_try("password"),
73             $self->_try("host"),
74             $self->_try("port"),
75             $self->_try("path"),
76             $self->_try("params"),
77             $self->_try("query"),
78             scalar($self->fragment),
79             )
80             }
81              
82             sub full_path
83             {
84 8     8 0 2415 my $self = shift;
85 8         44 my $path = $self->path_query;
86 8 50       29 $path = "/" unless length $path;
87 8         56 $path;
88             }
89              
90             sub netloc
91             {
92 16     16 0 1294 shift->authority(@_);
93             }
94              
95             sub epath
96             {
97 33     33 0 761 my $path = shift->SUPER::path(@_);
98 33         87 $path =~ s/;.*//;
99 33         59 $path;
100             }
101              
102             sub eparams
103             {
104 12     12 0 612 my $self = shift;
105 12         56 my @p = $self->path_segments;
106 11 100       46 return undef unless ref($p[-1]);
107 3         44 @p = @{$p[-1]};
  3         39  
108 3         10 shift @p;
109 3         22 join(";", @p);
110             }
111              
112 10     10 0 2428 sub params { shift->eparams(@_); }
113              
114             sub path {
115 28     28 0 6376 my $self = shift;
116 28         73 my $old = $self->epath(@_);
117 28 100       68 return unless defined wantarray;
118 23 50 33     120 return '/' if !defined($old) || !length($old);
119 23 100 66     401 Carp::croak("Path components contain '/' (you must call epath)")
120             if $old =~ /%2[fF]/ and !@_;
121 22 100 100     89 $old = "/$old" if $old !~ m|^/| && defined $self->netloc;
122 22         67 return uri_unescape($old);
123             }
124              
125             sub path_components {
126 4     4 0 38 shift->path_segments(@_);
127             }
128              
129             sub query {
130 18     18 0 4933 my $self = shift;
131 18         85 my $old = $self->equery(@_);
132 18 100 100     81 if (defined(wantarray) && defined($old)) {
133 7 100       41 if ($old =~ /%(?:26|2[bB]|3[dD])/) { # contains escaped '=' '&' or '+'
134 1         2 my $mess;
135 1         3 for ($old) {
136 1 50 33     3 $mess = "Query contains both '+' and '%2B'"
137             if /\+/ && /%2[bB]/;
138 1 50 33     10 $mess = "Form query contains escaped '=' or '&'"
139             if /=/ && /%(?:3[dD]|26)/;
140             }
141 1 50       4 if ($mess) {
142 1         72 Carp::croak("$mess (you must call equery)");
143             }
144             }
145             # Now it should be safe to unescape the string without losing
146             # information
147 6         16 return uri_unescape($old);
148             }
149 11         53 undef;
150              
151             }
152              
153             sub abs
154             {
155 80     80 1 310 my $self = shift;
156 80         109 my $base = shift;
157 80         107 my $allow_scheme = shift;
158 80 100       163 $allow_scheme = $URI::URL::ABS_ALLOW_RELATIVE_SCHEME
159             unless defined $allow_scheme;
160 80         122 local $URI::ABS_ALLOW_RELATIVE_SCHEME = $allow_scheme;
161 80         105 local $URI::ABS_REMOTE_LEADING_DOTS = $URI::URL::ABS_REMOTE_LEADING_DOTS;
162 80         249 $self->SUPER::abs($base);
163             }
164              
165 7     7 0 2311 sub frag { shift->fragment(@_); }
166 5     5 0 34 sub keywords { shift->query_keywords(@_); }
167              
168             # file:
169 1     1 0 40 sub local_path { shift->file; }
170 9     9 0 69 sub unix_path { shift->file("unix"); }
171 2     2 0 63 sub dos_path { shift->file("dos"); }
172 3     3 0 25 sub mac_path { shift->file("mac"); }
173 0     0 0 0 sub vms_path { shift->file("vms"); }
174              
175             # mailto:
176 1     1 0 631 sub address { shift->to(@_); }
177 1     1 0 672 sub encoded822addr { shift->to(@_); }
178 1     1 1 7 sub URI::mailto::authority { shift->to(@_); } # make 'netloc' method work
179              
180             # news:
181 1     1 0 618 sub groupart { shift->_group(@_); }
182 3     3 0 33 sub article { shift->message(@_); }
183              
184             1;
185              
186             __END__