blib/lib/URI/AnyService.pm

filter: Statementstmt 96.1% 75 / 78 Branchbran 73.6% 28 / 38 Conditioncond 50.0% 4 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 84.7% 122 / 144 CC 23 SCAR 19.2
CC
23
Cov
86%
_load_services
11.7
_scheme
9.3
new
5.0
CRAP 6.9
↑ Summary
1 package URI::AnyService;
2  
3 # ABSTRACT: Simple /etc/services-driven URI objects
4 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 our $VERSION = 'v0.90.0'; # VERSION
6  
7 2 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ### XXX: Is this abusing a private, undocumented class from an external module? Yes.
12 ###
13 ### Are the risks of copying entire functions wholesale, and having those functions get changed from
14 ### underneath us, far greater than the risk of this load-bearing and intertwined package name getting
15 ### renamed? Also yes.
16  
17 2 ▶use parent 'URI::_server';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #pod =head1 SYNOPSIS
23 #pod
24 #pod use URI;
25 #pod use URI::AnyService;
26 #pod
27 #pod my $uri = URI->new('smtp://example.com');
28 #pod say $uri->has_recognized_scheme; # !!0
29 #pod say $uri->port; # ''
30 #pod
31 #pod my $uas = URI::AnyService->new('smtp://example.com');
32 #pod say $uas->has_recognized_scheme; # !!1
33 #pod say $uas->port; # 25
34 #pod
35 #pod =head1 DESCRIPTION
36 #pod
37 #pod This module creates simple L<URI>-based objects for protocols that it doesn't yet have defined.
38 #pod It works just like L<URI>, except it just supports the L<generic servers methods|URL/SERVER METHODS>,
39 #pod and not anything scheme-specific. In other words, most of the methods that C<http>/C<https>
40 #pod objects support.
41 #pod
42 #pod Port numbers are looked up from a C</etc/services> file. If you don't have a C</etc/services>
43 #pod file, one will be provided for you, but there's no guarantee that it will be up-to-date or apply
44 #pod to your specific protocol.
45 #pod
46 #pod This allows you to use a URL like C<smtp://example.com> without having to set a default port in
47 #pod the URL.
48 #pod
49 #pod =head1 GLOBAL VARIABLES
50 #pod
51 #pod =head2 %SERVICE_PORTS
52 #pod
53 #pod This is a hash of schemes/protocols to port numbers. It is loaded from the C</etc/services> file,
54 #pod as soon as you C<use> this module.
55 #pod
56 #pod Most of the time, you shouldn't mess with this. But, if there is some new protocol that you need
57 #pod to add that happens to not exist in your C</etc/services> files, you can do so with:
58 #pod
59 #pod $URI::AnyService::SERVICE_PORTS{'my-protocol'} = 1234;
60 #pod
61 #pod This variable is not exportable, so you need to use the full package syntax.
62 #pod
63 #pod =cut
64  
65 2 our %SERVICE_PORTS;
66  
67 #pod =head1 USE OPTIONS
68 #pod
69 #pod =head2 :InternalServicesData
70 #pod
71 #pod use URI::AnyService ':InternalServicesData';
72 #pod
73 #pod This will force the internal services data to be loaded, even if the C</etc/services> file is
74 #pod available. This is useful for testing, or if you know that the C</etc/services> files on your
75 #pod servers are not consistent.
76 #pod
77 #pod Please note that there are no guarantees that the internal data is up-to-date or accurate. It's
78 #pod mostly used as a "last resort" fallback.
79 #pod
80 #pod =cut
81  
82 2 my $found_services_in;
83 2 my $force_internal_services_data = 0;
84  
85 1 ▶sub _load_services {
Subroutine _load_services: called CC=10 SCAR=24.6
86 1 my $fh;
87 # XXX: Windows support not tested. This is just the usual location where it's expected to be.
88 1 ▶ my $services_file = $^O eq 'MSWin32' ? 'C:\Windows\System32\drivers\etc\services' : '/etc/services';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
89 1 ▶ if (!$force_internal_services_data && -f -r $services_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $force_internal_services_data and -f -r $services_file) { } 0 1 1
Condition33%1/3
not $force_internal_services_data and -f -r $services_file
!ll&&!rl&&r
100
MC/DC0%0/2
not $force_internal_services_data and -f -r $services_file
not $force_internal_services_data -f -r $services_file
Truth table33%1/3
not(A) and B
A= not $force_internal_services_dataB= -f -r $services_file
ABresult
0X0
100
111
90 0 ▶ open $fh, '<', $services_file or Carp::croak("Can't open /etc/services: $!");
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $services_file or Carp::croak("Can't open /etc/services: $!") 0 0 0
91 0 $found_services_in = $services_file;
92 }
93 else {
94 1 $fh = \*DATA;
95 1 $found_services_in = '<DATA>';
96 }
97  
98 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 373 1 374
99 373 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/u 1 372 373
100 372 $line =~ s/#.*$//;
101 372 ▶ next unless ($line // '') =~ /^$URI::scheme_re\s+/;
Branch: 1 branch
BranchTrueFalseTotal
unless ($line // "") =~ /^$URI::scheme_re\s+/u 2 370 372
Condition50%1/2
$line // ""
l!l
3720
MC/DC0%0/1
$line // ""
$line
Truth table50%1/2
A
A= $line
Aresult
11
00
102  
103 370 my ($service, $port_proto, @other_services) = split /\s+/, $line;
104 370 my ($port, $proto) = split m!/!, $port_proto;
105 370 $SERVICE_PORTS{$service} = $port;
106 370 ▶ $SERVICE_PORTS{@other_services} = $port if @other_services;
Branch: 1 branch
BranchTrueFalseTotal
if @other_services 71 299 370
107 }
108 1 close $fh;
109 }
110  
111 1 ▶sub import {
Subroutine import: called CC=2 SCAR=7.0
112 1 my ($class, @opts) = @_;
113 1 ▶ $force_internal_services_data = 1 if grep { $_ eq ':InternalServicesData' } @opts;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq ":InternalServicesData";} @opts 1 0 1
114 1 _load_services();
115 }
116  
117 #pod =head1 CONSTRUCTOR
118 #pod
119 #pod URI::AnyService->new($str);
120 #pod URI::AnyService->new($str, $scheme);
121 #pod
122 #pod This constructor is very similiar to L<URI's version|URI/CONSTRUCTORS>, including the
123 #pod somewhat-legacy two parameter form. In most cases, you should just use the whole URL string.
124 #pod
125 #pod A scheme that was defined in the services file is required.
126 #pod
127 #pod =cut
128  
129 # Less /o bugs, more modern usage, still using the URI scheme RE
130 2 my $scheme_re = qr/^($URI::scheme_re):/;
131 2 my $scheme_re_sans_colon = qr/^($URI::scheme_re)/;
132  
133 ### XXX: Most of this code for `new` and `_scheme` were copied from URI, but irrelevant parts
134 ### have been removed and optimized.
135  
136 25 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
137 25 my ($class, $uri, $scheme) = @_;
138  
139 25 ▶ $uri = defined $uri ? "$uri" : ""; # stringify
Branch: 1 branch
BranchTrueFalseTotal
defined $uri ? : 25 0 25
140  
141 # Get rid of potential wrapping
142 25 $uri =~ s/^<(?:URL:)?(.*)>$/$1/;
143 25 $uri =~ s/^"(.*)"$/$1/;
144 25 $uri =~ s/^\s+//;
145 25 $uri =~ s/\s+$//;
146  
147 # We ARE the implementor class
148 25 ▶ $scheme = $1 if $uri =~ s/$scheme_re//;
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ s/$scheme_re//u 22 3 25
149 25 ▶ Carp::croak("No scheme defined in URI: $uri") unless $scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $scheme 2 23 25
150 23 ▶ Carp::croak("Scheme '$scheme' not found in $found_services_in") unless $SERVICE_PORTS{lc $scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless $SERVICE_PORTS{lc $scheme} 1 22 23
151  
152 # Find all funny characters and encode the bytes
153 22 $uri = URI->_uric_escape($uri);
154 22 $uri = "$scheme:$uri";
155 22 my $self = bless \$uri, $class;
156 22 $self;
157 }
158  
159 0 ▶sub _no_scheme_ok { 0 }
Subroutine _no_scheme_ok: not called CC=1 SCAR=6.9
160  
161 33 ▶sub _scheme {
Subroutine _scheme: called CC=9 SCAR=22.3
162 33 my $self = shift;
163  
164 33 ▶ unless (@_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 30 3 33
165 30 ▶ return undef unless $$self =~ $scheme_re;
Branch: 1 branch
BranchTrueFalseTotal
unless $$self =~ /$scheme_re/u 0 30 30
166 30 return $1;
167 }
168  
169 # URI supports this weird setting of the scheme to create a new URI object, which is fine,
170 # but it should call us, not URI->new.
171 3 my $old;
172 3 my $new = shift;
173 3 ▶ if (defined $new && length $new) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new and length $new) { } 2 1 3
Condition66%2/3
defined $new and length $new
!ll&&!rl&&r
012
MC/DC50%1/2
defined $new and length $new
defined $new length $new
Truth table66%2/3
A and B
A= defined $newB= length $new
ABresult
0X0
100
111
174 # Error checks before we mutate $self
175 2 ▶ Carp::croak("Bad scheme '$new'") unless $new =~ /$scheme_re_sans_colon$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $new =~ /$scheme_re_sans_colon$/u 0 2 2
176 2 ▶ Carp::croak("Scheme '$new' not found in $found_services_in") unless $SERVICE_PORTS{lc $new};
Branch: 1 branch
BranchTrueFalseTotal
unless $SERVICE_PORTS{lc $new} 1 1 2
177  
178 1 ▶ $old = $1 if $$self =~ s/$scheme_re//;
Branch: 1 branch
BranchTrueFalseTotal
if $$self =~ s/$scheme_re//u 1 0 1
179 1 my $newself = __PACKAGE__->new("$new:$$self");
180 1 $$self = $$newself;
181 1 bless $self, ref($newself);
182 }
183 else {
184 1 ▶ $old = $1 if $$self =~ $scheme_re;
Branch: 1 branch
BranchTrueFalseTotal
if $$self =~ /$scheme_re/u 1 0 1
185 }
186  
187 2 return $old;
188 }
189  
190 18 ▶sub default_port {
Subroutine default_port: called CC=1 SCAR=0.0
Pod documented
191 18 my ($self) = @_;
192 18 return $SERVICE_PORTS{$self->scheme};
193 }
194  
195 2 1;
196  
197 =pod
198  
199 =encoding UTF-8
200  
201 =head1 NAME
202  
203 URI::AnyService - Simple /etc/services-driven URI objects
204  
205 =head1 VERSION
206  
207 version v0.90.0
208  
209 =head1 SYNOPSIS
210  
211 use URI;
212 use URI::AnyService;
213  
214 my $uri = URI->new('smtp://example.com');
215 say $uri->has_recognized_scheme; # !!0
216 say $uri->port; # ''
217  
218 my $uas = URI::AnyService->new('smtp://example.com');
219 say $uas->has_recognized_scheme; # !!1
220 say $uas->port; # 25
221  
222 =head1 DESCRIPTION
223  
224 This module creates simple L<URI>-based objects for protocols that it doesn't yet have defined.
225 It works just like L<URI>, except it just supports the L<generic servers methods|URL/SERVER METHODS>,
226 and not anything scheme-specific. In other words, most of the methods that C<http>/C<https>
227 objects support.
228  
229 Port numbers are looked up from a C</etc/services> file. If you don't have a C</etc/services>
230 file, one will be provided for you, but there's no guarantee that it will be up-to-date or apply
231 to your specific protocol.
232  
233 This allows you to use a URL like C<smtp://example.com> without having to set a default port in
234 the URL.
235  
236 =head1 GLOBAL VARIABLES
237  
238 =head2 %SERVICE_PORTS
239  
240 This is a hash of schemes/protocols to port numbers. It is loaded from the C</etc/services> file,
241 as soon as you C<use> this module.
242  
243 Most of the time, you shouldn't mess with this. But, if there is some new protocol that you need
244 to add that happens to not exist in your C</etc/services> files, you can do so with:
245  
246 $URI::AnyService::SERVICE_PORTS{'my-protocol'} = 1234;
247  
248 This variable is not exportable, so you need to use the full package syntax.
249  
250 =head1 USE OPTIONS
251  
252 =head2 :InternalServicesData
253  
254 use URI::AnyService ':InternalServicesData';
255  
256 This will force the internal services data to be loaded, even if the C</etc/services> file is
257 available. This is useful for testing, or if you know that the C</etc/services> files on your
258 servers are not consistent.
259  
260 Please note that there are no guarantees that the internal data is up-to-date or accurate. It's
261 mostly used as a "last resort" fallback.
262  
263 =head1 CONSTRUCTOR
264  
265 URI::AnyService->new($str);
266 URI::AnyService->new($str, $scheme);
267  
268 This constructor is very similiar to L<URI's version|URI/CONSTRUCTORS>, including the
269 somewhat-legacy two parameter form. In most cases, you should just use the whole URL string.
270  
271 A scheme that was defined in the services file is required.
272  
273 =head1 AUTHOR
274  
275 Grant Street Group <developers@grantstreet.com>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 This software is Copyright (c) 2025 by Grant Street Group.
280  
281 This is free software, licensed under:
282  
283 The Artistic License 2.0 (GPL Compatible)
284  
285 =cut
286  
287 __DATA__
↑ Summary