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 7     7   446062 use strict;
  7         11  
  7         262  
4 7     7   33 use warnings;
  7         12  
  7         479  
5              
6 7     7   2197 use parent 'URI::WithBase';
  7         1308  
  7         39  
7              
8             our $VERSION = '5.34';
9              
10             # Provide as much as possible of the old URI::URL interface for backwards
11             # compatibility...
12              
13 7     7   529 use Exporter 5.57 'import';
  7         121  
  7         556  
14             our @EXPORT = qw(url);
15              
16             # Easy to use constructor
17 7     7 0 652900 sub url ($;$) { URI::URL->new(@_); }
18              
19 7     7   43 use URI::Escape qw(uri_unescape);
  7         11  
  7         10949  
20              
21             sub new
22             {
23 255     255 1 1014022 my $class = shift;
24 255         1042 my $self = $class->SUPER::new(@_);
25 255         993 $self->[0] = $self->[0]->canonical;
26 255         755 $self;
27             }
28              
29             sub newlocal
30             {
31 6     6 0 89 my $class = shift;
32 6         55 require URI::file;
33 6         61 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   12 my $class = shift;
40 8 50       14 die "Unknown URI::URL scheme $_[1]:" if $URI::URL::STRICT;
41 8         29 $class->SUPER::_init(@_);
42             }
43             }
44              
45             sub strict
46             {
47 2     2 0 1784 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   41 my $self = shift;
62 42         46 my $method = shift;
63 42         31 scalar(eval { $self->$method(@_) });
  42         83  
64             }
65              
66             sub crack
67             {
68             # should be overridden by subclasses
69 6     6 0 5 my $self = shift;
70 6         29 (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 3413 my $self = shift;
85 8         52 my $path = $self->path_query;
86 8 50       17 $path = "/" unless length $path;
87 8         36 $path;
88             }
89              
90             sub netloc
91             {
92 16     16 0 1610 shift->authority(@_);
93             }
94              
95             sub epath
96             {
97 33     33 0 1062 my $path = shift->SUPER::path(@_);
98 33         75 $path =~ s/;.*//;
99 33         60 $path;
100             }
101              
102             sub eparams
103             {
104 12     12 0 852 my $self = shift;
105 12         75 my @p = $self->path_segments;
106 11 100       50 return undef unless ref($p[-1]);
107 3         4 @p = @{$p[-1]};
  3         33  
108 3         9 shift @p;
109 3         16 join(";", @p);
110             }
111              
112 10     10 0 2105 sub params { shift->eparams(@_); }
113              
114             sub path {
115 28     28 0 10844 my $self = shift;
116 28         89 my $old = $self->epath(@_);
117 28 100       69 return unless defined wantarray;
118 23 50 33     96 return '/' if !defined($old) || !length($old);
119 23 100 66     391 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         60 return uri_unescape($old);
123             }
124              
125             sub path_components {
126 4     4 0 42 shift->path_segments(@_);
127             }
128              
129             sub query {
130 18     18 0 6670 my $self = shift;
131 18         89 my $old = $self->equery(@_);
132 18 100 100     84 if (defined(wantarray) && defined($old)) {
133 7 100       39 if ($old =~ /%(?:26|2[bB]|3[dD])/) { # contains escaped '=' '&' or '+'
134 1         2 my $mess;
135 1         3 for ($old) {
136 1 50 33     6 $mess = "Query contains both '+' and '%2B'"
137             if /\+/ && /%2[bB]/;
138 1 50 33     13 $mess = "Form query contains escaped '=' or '&'"
139             if /=/ && /%(?:3[dD]|26)/;
140             }
141 1 50       4 if ($mess) {
142 1         117 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         18 return uri_unescape($old);
148             }
149 11         40 undef;
150              
151             }
152              
153             sub abs
154             {
155 80     80 1 248 my $self = shift;
156 80         102 my $base = shift;
157 80         89 my $allow_scheme = shift;
158 80 100       164 $allow_scheme = $URI::URL::ABS_ALLOW_RELATIVE_SCHEME
159             unless defined $allow_scheme;
160 80         125 local $URI::ABS_ALLOW_RELATIVE_SCHEME = $allow_scheme;
161 80         137 local $URI::ABS_REMOTE_LEADING_DOTS = $URI::URL::ABS_REMOTE_LEADING_DOTS;
162 80         210 $self->SUPER::abs($base);
163             }
164              
165 7     7 0 4039 sub frag { shift->fragment(@_); }
166 5     5 0 46 sub keywords { shift->query_keywords(@_); }
167              
168             # file:
169 1     1 0 23 sub local_path { shift->file; }
170 9     9 0 46 sub unix_path { shift->file("unix"); }
171 2     2 0 25 sub dos_path { shift->file("dos"); }
172 3     3 0 18 sub mac_path { shift->file("mac"); }
173 0     0 0 0 sub vms_path { shift->file("vms"); }
174              
175             # mailto:
176 1     1 0 1529 sub address { shift->to(@_); }
177 1     1 0 22 sub encoded822addr { shift->to(@_); }
178 1     1 1 5 sub URI::mailto::authority { shift->to(@_); } # make 'netloc' method work
179              
180             # news:
181 1     1 0 1105 sub groupart { shift->_group(@_); }
182 3     3 0 49 sub article { shift->message(@_); }
183              
184             1;
185              
186             __END__