lib/Regexp/Common/URI/RFC3986.pm

filter: Statementstmt 100% 87 / 87 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 91 / 91 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 ##----------------------------------------------------------------------------
2 ## Regexp patterns from RFC 3986 - ~/lib/Regexp/Common/URI/RFC3986.pm
3 ## Version 2025102001
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2025/10/20
7 ## Modified 2026/04/02
8 ## All rights reserved
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package Regexp::Common::URI::RFC3986;
15  
16 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 our $VERSION = '2025102001';
20  
21 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 our @ISA = qw /Exporter/;
23  
24  
25 1 my %vars;
26  
27 BEGIN
28 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 $vars{low} = [qw /$digit $upalpha $lowalpha $alpha $alphanum $hex $hexdig
30 $escaped $pct_encoded $mark $unreserved $sub_delims $reserved
31 $pchar $uric $urics $userinfo $userinfo_no_colon $uric_no_slash/];
32 1 $vars{parts} = [qw /$query $fragment $param $segment $segment_nz $segment_nz_nc
33 $path_abempty $path_absolute $path_noscheme $path_rootless
34 $path_empty $path_segments $ftp_segments $rel_segment
35 $abs_path $rel_path $path/];
36 1 $vars{connect} = [qw /$port $dec_octet $IPv4address $hextet $ls32 $IPv6address
37 $IPvFuture $IP_literal $toplabel $domainlabel $hostname
38 $host $hostport $server $reg_name $authority/];
39 1 $vars{URI} = [qw /$scheme $net_path $opaque_part $hier_part $relative_part
40 $relativeURI $absoluteURI $relative_ref $URI_reference/];
41 1 $vars{IDN} = [qw /$IDN_DOT $ACE $IDN_U_LABEL $IDN_HOST/];
42 }
43  
44 1 our @EXPORT = ();
45 1 our @EXPORT_OK = map { @$_ } values %vars;
46 1 our %EXPORT_TAGS = (%vars, ALL => [@EXPORT_OK]);
47  
48 # RFC3986, base definitions.
49 1 our $digit = '[0-9]';
50 1 our $upalpha = '[A-Z]';
51 1 our $lowalpha = '[a-z]';
52 1 our $alpha = '[a-zA-Z]'; # lowalpha | upalpha
53 1 our $alphanum = '[a-zA-Z0-9]'; # alpha | digit
54 1 our $hexdig = '[a-fA-F0-9]';
55 1 our $hex = $hexdig; # RFC2396 compatibility alias
56 1 our $pct_encoded = "(?:\%$hexdig$hexdig)"; # pct-encoded
57 1 our $escaped = $pct_encoded; # RFC2396 compatibility alias
58 # RFC3986 unreserved + sub-delims (ASCII)
59 1 our $unreserved = "[a-zA-Z0-9\\-_.~]"; # alphanum | mark
60 # %61-%7A, %41-%5A, %30-%39
61 # a - z A - Z 0 - 9
62 # %2D, %5F, %2E, %7E
63 # - _ . ~
64 1 our $sub_delims = "[!\\\$&'\\(\\)\\*\\+,;=]";
65 1 our $reserved = "[;/?:@&=+\$,]";
66 1 our $pchar = "(?:$unreserved|$pct_encoded|$sub_delims|[:\@])";
67 # unreserved | pct-encoded / sub-delims / ":" / "@"
68 # Compatibility aliases to RFC2396's uric/urics/uric_no_slash
69 1 our $mark = "[\\-_.!~*'()]"; # RFC2396 legacy
70 1 our $uric = "(?:[/?]|$pchar)"; # RFC2396 legacy superset
71 1 our $urics = "(?:(?:$uric)*)"; # RFC2396 legacy
72 1 our $uric_no_slash = "(?:$pchar|[?])"; # RFC2396 legacy
73  
74 # query / fragment = *( pchar / "/" / "?" )
75 1 our $query = "(?:(?:$pchar|[/?])*)";
76 1 our $fragment = "(?:(?:$pchar|[/?])*)";
77 1 our $param = "(?:(?:[a-zA-Z0-9\\-_.!~*'():\@&=+\$,]+|$escaped)*)";
78 # Path productions (RFC3986 §3.3)
79 1 our $segment = "(?:$pchar*)";
80 1 our $segment_nz = "(?:$pchar+)";
81 1 our $segment_nz_nc = "(?:(?:$unreserved|$pct_encoded|$sub_delims|@)+)";
82  
83 # RFC2396 names kept for compatibility
84 1 our $path_segments = "(?:$segment(?:/$segment)*)"; # RFC2396 legacy name
85 1 our $abs_path = "(?:/$path_segments)"; # RFC2396 legacy
86 1 our $ftp_segments = "(?:$param(?:/$param)*)"; # NOT from RFC 2396.
87 1 our $rel_segment = "(?:(?:[a-zA-Z0-9\\-_.!~*'();\@&=+\$,]*|$escaped)+)"; # RFC2396 legacy
88 1 our $rel_path = "(?:$rel_segment(?:$abs_path)?)"; # RFC2396 legacy
89 1 our $path = "(?:(?:$abs_path|$rel_path)?)"; # RFC2396 legacy
90  
91 # 3986 canonical forms
92 1 our $path_abempty = "(?:/$segment)*";
93 1 our $path_absolute = "(?:/(?:$segment_nz(?:/$segment)*)?)";
94 1 our $path_noscheme = "(?:$segment_nz_nc(?:/$segment)*)";
95 1 our $path_rootless = "(?:$segment_nz(?:/$segment)*)";
96 1 our $path_empty = "(?:)";
97  
98 # RFC3986 §3.2.2 Host / Authority
99 # IPv4
100 1 our $dec_octet = "(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])";
101 1 our $IPv4address = "(?:$dec_octet\\.$dec_octet\\.$dec_octet\\.$dec_octet)";
102  
103 # IPv6 (RFC3986 Appendix A)
104 1 our $hextet = "(?:(?:$hexdig){1,4})";
105 1 our $ls32 = "(?:$hextet:$hextet|$IPv4address)";
106  
107 # A faithful (and readable) IPv6address alternation set.
108 # Corresponds exactly to the 9 alternatives in RFC 3986 Appendix A:
109 # 1. 6( h16 ":" ) ls32
110 # 2. "::" 5( h16 ":" ) ls32
111 # 3. [ h16 ] "::" 4( h16 ":" ) ls32
112 # 4. [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
113 # 5. [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
114 # 6. [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
115 # 7. [ *4( h16 ":" ) h16 ] "::" ls32
116 # 8. [ *5( h16 ":" ) h16 ] "::" h16
117 # 9. [ *6( h16 ":" ) h16 ] "::"
118 1 our $IPv6address = "(?:" .
119 "(?:$hextet:){6}$ls32" . '|' .
120 "::(?:$hextet:){5}$ls32" . '|' .
121 "(?:$hextet)?::(?:$hextet:){4}$ls32" . '|' .
122 "(?:(?:$hextet:){0,2}$hextet)?::(?:$hextet:){3}$ls32" . '|' .
123 "(?:(?:$hextet:){0,3}$hextet)?::(?:$hextet:){2}$ls32" . '|' .
124 "(?:(?:$hextet:){0,4}$hextet)?::(?:$hextet:)$ls32" . '|' .
125 "(?:(?:$hextet:){0,5}$hextet)?::$ls32" . '|' .
126 "(?:(?:$hextet:){0,5}$hextet)?::$hextet" . '|' .
127 "(?:(?:$hextet:){0,6}$hextet)?::" .
128 ")";
129  
130 # IPvFuture = 'v' 1*HEXDIG '.' 1*( unreserved / sub-delims / ":" )
131 1 our $IPvFuture = "(?:v(?:$hexdig)+\\.(?:$unreserved|$sub_delims|:)+)";
132  
133 # IP-literal = "[" ( IPv6address / IPvFuture ) "]"
134 1 our $IP_literal = "(?:\\[(?:$IPv6address|$IPvFuture)\\])";
135  
136 # reg-name = *( unreserved / pct-encoded / sub-delims )
137 1 our $reg_name = "(?:(?:$unreserved|$pct_encoded|$sub_delims)*)";
138  
139 # host = IP-literal / IPv4address / reg-name
140 1 our $host = "(?:$IP_literal|$IPv4address|$reg_name)";
141 1 our $port = "(?:$digit*)";
142  
143 # userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
144 1 our $userinfo = "(?:(?:$unreserved|$pct_encoded|$sub_delims|:)*)";
145 1 our $userinfo_no_colon = "(?:(?:$unreserved|$pct_encoded|$sub_delims)*)";
146  
147 # authority = [ userinfo "@" ] host [ ":" port ]
148 1 our $authority = "(?:(?:$userinfo\@)?$host(?::$port)?)";
149  
150 # Legacy RFC2396 names kept (not used by RFC3986, but exported for compatibility)
151 1 our $toplabel = "(?:$alpha"."[-a-zA-Z0-9]*$alphanum|$alpha)";
152 1 our $domainlabel = "(?:(?:$alphanum"."[-a-zA-Z0-9]*)?$alphanum)";
153 1 our $hostname = "(?:(?:$domainlabel\[.])*$toplabel\[.]?)"; # RFC2396 legacy ASCII hostname
154 1 our $hostport = "(?:$host(?::$port)?)"; # RFC2396 legacy
155 1 our $server = "(?:(?:$userinfo\@)?$hostport)"; # RFC2396 legacy
156  
157 1 our $scheme = "(?:$alpha"."[a-zA-Z0-9+\\-.]*)";
158  
159 1 our $net_path = "(?://$authority$abs_path?)";
160 1 our $opaque_part = "(?:$uric_no_slash$urics)";
161 # hier-part = ("//" authority path-abempty) / path-absolute / path-rootless / path-empty
162 1 our $hier_part = "(?:(?://$authority$path_abempty)|$path_absolute|$path_rootless|$path_empty)";
163 # relative-part = ("//" authority path-abempty) / path-absolute / path-noscheme / path-empty
164 1 our $relative_part = "(?:(?://$authority$path_abempty)|$path_absolute|$path_noscheme|$path_empty)";
165 # absolute-URI = scheme ":" hier-part [ "?" query ]
166 1 our $absoluteURI = "(?:$scheme:$hier_part(?:\\?$query)?)";
167 # relative-ref = relative-part [ "?" query ] [ "#" fragment ]
168 1 our $relative_ref = "(?:$relative_part(?:\\?$query)?(?:\\#$fragment)?)";
169 # URI-reference = URI / relative-ref
170 # (where URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ])
171 1 our $URI_reference = "(?:(?:$absoluteURI(?:\\#$fragment)?|$relative_ref))";
172 # Legacy RFC2396 "relativeURI" (without fragment)
173 1 our $relativeURI = "(?:$relative_part(?:\\?$query)?)";
174  
175 # Optional Unicode/IDN helpers (non-normative)
176 # Accept "." and the IDNA dot equivalents
177 1 our $IDN_DOT = '[\.\x{3002}\x{FF0E}\x{FF61}]';
178  
179 # ACE punycode prefix (case-insensitive)
180 1 our $ACE = '(?i:xn--)';
181  
182 # Unicode IDN label with ACE hyphen rule (<=63 chars)
183 1 our $IDN_U_LABEL = join '',
184 '(?:',
185 '(?:', $ACE, '[\\p{L}\\p{N}\\p{M}\\p{Pc}-]{1,59})',
186 '|',
187 '(?![\\p{L}\\p{N}]{2}--)', # forbid "--" at pos 3-4 unless ACE
188 '[\\p{L}\\p{N}]',
189 '[\\p{L}\\p{N}\\p{M}\\p{Pc}-]{0,61}',
190 '(?<!-)',
191 ')';
192  
193 # Unicode IDN hostname: label *( DOT-equivalent label )
194 1 our $IDN_HOST = "(?:$IDN_U_LABEL(?:$IDN_DOT$IDN_U_LABEL)*)";
195  
196 1 1;
197 # NOTE: TODO
198 __END__
↑ Summary