blib/lib/MarpaX/RFC/RFC3986.pm

filter: Statementstmt 43.6% 41 / 94 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 28.0% 7 / 25 Podpod 33.3% 1 / 3 totaltotal 37.9% 49 / 129 CC 3 SCAR 8.5
CC
3
Cov
41%
BUILDARGS
6.0
is_absolute
6.0
BUILD
2.0
CRAP 2.3
↑ Summary
1 package MarpaX::RFC::RFC3986;
2 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Types::Standard -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use diagnostics;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Marpa::R2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Uniform Resource Identifier (URI): Generic Syntax - Marpa Parser
10  
11 # References: RFC 3986 URI http://tools.ietf.org/html/rfc3986
12 # RFC 6874 IPv6 Zone Identifiers http://tools.ietf.org/html/rfc6874
13 # RFC 7320 URI Design and Ownership http://tools.ietf.org/html/rfc7320 (does not affect the grammar)
14  
15 # AUTHORITY
16  
17 1 our $VERSION = '0.002'; # VERSION
18  
19  
20 1 our $DATA = do { local $/; <DATA>; };
21  
22 1 has value => (is => 'ro', isa => Str, required => 1 );
23  
24 1 ▶class_has grammar => (is => 'ro', isa => InstanceOf['Marpa::R2::Scanless:G'], default => sub { return Marpa::R2::Scanless::G->new({ source => \$DATA }) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 1 class_has bnf => (is => 'ro', isa => Str, default => $DATA );
27  
28 1 has scheme => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_scheme');
29 1 has authority => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_authority');
30 1 has path => (is => 'ro', isa => Str, default => '', writer => '_set_path'); # There is always a path in an URI
31 1 has query => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_query');
32 1 has fragment => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_fragment');
33  
34 1 has hier_part => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_hier_part');
35 1 has userinfo => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_userinfo');
36 1 has host => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_host');
37 1 has port => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_port');
38 1 has relative_part => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_relative_part');
39 1 has ip_literal => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ip_literal');
40 1 has zoneid => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_zoneid' );
41 1 has ipv4address => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_ipv4address');
42 1 has reg_name => (is => 'ro', isa => Str|Undef, default => undef, writer => '_set_reg_name');
43  
44 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
45 0 my ($class, @args) = @_;
46  
47 0 ▶ unshift(@args, 'value') if (@args % 2 == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 0 0 0
48  
49 0 return { @args };
50 };
51  
52 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ($self) = @_;
54 #
55 # This hack just to avoid recursivity: we do not want Marpa to
56 # call another new() but operate on our instance immediately
57 #
58 0 local $MarpaX::RFC::RFC3986::SELF = $self;
59 0 $self->grammar->parse(\$self->value, { ranking_method => 'high_rule_only' });
60  
61 0 return;
62 }
63  
64 0 ▶sub is_absolute {
Subroutine is_absolute: not called CC=2 SCAR=17.9
Pod documented
65 0 my ($self) = @_;
66 #
67 ## No need to reparse. An absolute URI is when scheme and hier_part are defined
68 #
69 0 ▶ return Str->check($self->scheme) && Str->check($self->hier_part);
Condition0%0/3
&Str()->check($self->scheme) && &Str()->check($self->hier_part)
!ll&&!rl&&r
000
MC/DC0%0/2
&Str()->check($self->scheme) && &Str()->check($self->hier_part)
&Str()->check($self->scheme) &Str()->check($self->hier_part)
Truth table0%0/3
A && B
A= &Str()->check($self->scheme)B= &Str()->check($self->hier_part)
ABresult
0X0
100
111
70 }
71  
72 #
73 # Grammar rules
74 #
75 0 ▶sub _marpa_concat { shift; return join('', @_); }
Subroutine _marpa_concat: not called CC=1 SCAR=6.9
76 0 ▶sub _marpa_scheme { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_scheme ($self->_marpa_concat(@_)); }
Subroutine _marpa_scheme: not called CC=1 SCAR=6.9
77 0 ▶sub _marpa_authority { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_authority ($self->_marpa_concat(@_)); }
Subroutine _marpa_authority: not called CC=1 SCAR=6.9
78 0 ▶sub _marpa_path { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_path ($self->_marpa_concat(@_)); }
Subroutine _marpa_path: not called CC=1 SCAR=6.9
79 0 ▶sub _marpa_query { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_query ($self->_marpa_concat(@_)); }
Subroutine _marpa_query: not called CC=1 SCAR=6.9
80 0 ▶sub _marpa_fragment { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_fragment ($self->_marpa_concat(@_)); }
Subroutine _marpa_fragment: not called CC=1 SCAR=6.9
81  
82 0 ▶sub _marpa_hier_part { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_hier_part ($self->_marpa_concat(@_)); }
Subroutine _marpa_hier_part: not called CC=1 SCAR=6.9
83 0 ▶sub _marpa_userinfo { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_userinfo ($self->_marpa_concat(@_)); }
Subroutine _marpa_userinfo: not called CC=1 SCAR=6.9
84 0 ▶sub _marpa_host { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_host ($self->_marpa_concat(@_)); }
Subroutine _marpa_host: not called CC=1 SCAR=6.9
85 0 ▶sub _marpa_port { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_port ($self->_marpa_concat(@_)); }
Subroutine _marpa_port: not called CC=1 SCAR=6.9
86 0 ▶sub _marpa_relative_part { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_relative_part ($self->_marpa_concat(@_)); }
Subroutine _marpa_relative_part: not called CC=1 SCAR=6.9
87 0 ▶sub _marpa_ip_literal { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_ip_literal ($self->_marpa_concat(@_)); }
Subroutine _marpa_ip_literal: not called CC=1 SCAR=6.9
88 0 ▶sub _marpa_zoneid { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_zoneid ($self->_marpa_concat(@_)); }
Subroutine _marpa_zoneid: not called CC=1 SCAR=6.9
89 0 ▶sub _marpa_ipv4address { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_ipv4address ($self->_marpa_concat(@_)); }
Subroutine _marpa_ipv4address: not called CC=1 SCAR=6.9
90 0 ▶sub _marpa_reg_name { shift; my $self = $MarpaX::RFC::RFC3986::SELF; return $self->_set_reg_name ($self->_marpa_concat(@_)); }
Subroutine _marpa_reg_name: not called CC=1 SCAR=6.9
91  
92 1 1;
93  
94 =pod
95  
96 =encoding UTF-8
97  
98 =head1 NAME
99  
100 MarpaX::RFC::RFC3986 - Uniform Resource Identifier (URI): Generic Syntax - Marpa Parser
101  
102 =head1 VERSION
103  
104 version 0.002
105  
106 =head1 SYNOPSIS
107  
108 use MarpaX::RFC::RFC3986;
109 use Try::Tiny;
110 use Data::Dumper;
111  
112 print Dumper(MarpaX::RFC::RFC3986->new('http://www.perl.org'));
113  
114 try {
115 print STDERR "\nThe following is an expected failure:\n";
116 MarpaX::RFC::RFC3986->new('http://invalid##');
117 } catch {
118 print STDERR "$_\n";
119 return;
120 }
121  
122 =head1 DESCRIPTION
123  
124 This module parses an URI reference as per RFC3986 STD 66, with RFC6874 update about IPv6 Zone Identifiers. It is not intended as a replacement of the URI module, but more for data validation using a strict grammar with good error reporting.
125  
126 =head1 URI DESCRIPTION
127  
128 Quoted from the RFC here is the overall structure of an URI that will help understand the meaning of the methods thereafter:
129  
130 foo://example.com:8042/over/there?name=ferret#nose
131 \_/ \______________/\_________/ \_________/ \__/
132 | | | | |
133 scheme authority path query fragment
134 | _____________________|__
135 / \ / \
136 urn:example:animal:ferret:nose
137  
138 The grammar is parsing both absolute URI and relative URI, the corresponding start rule being named a URI reference.
139  
140 An absolute URI has the following structure:
141  
142 URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
143  
144 while a relative URI is split into:
145  
146 relative-ref = relative-part [ "?" query ] [ "#" fragment ]
147  
148 Back to the overall structure, the authority is:
149  
150 authority = [ userinfo "@" ] host [ ":" port ]
151  
152 where the host can be an IP-literal with Zone information, and IPV4 address or a registered name:
153  
154 host = IP-literal / IPv4address / reg-name
155  
156 The Zone Identifier is an extension to original RFC3986, and is defined in RFC6874:
157  
158 IP-literal = "[" ( IPv6address / IPv6addrz / IPvFuture ) "]"
159  
160 ZoneID = 1*( unreserved / pct-encoded )
161  
162 IPv6addrz = IPv6address "%25" ZoneID
163  
164 =head1 CLASS METHODS
165  
166 =head2 MarpaX::RFC::RFC3986->new(@options --> InstanceOf['MarpaX::RFC::RFC3986'])
167  
168 Instantiate a new object. Usage is either C<MarpaX::RFC::RFC3986-E<gt>new(value =E<gt> $url)> or C<MarpaX::RFC::RFC3986-E<gt>new($url)>. This method will croak if the the C<$url> parameter cannot coerce to a string nor is a valid URI. The variable C<$self> is used below to refer to this object instance.
169  
170 =head2 MarpaX::RFC::RFC3986->grammar( --> InstanceOf['Marpa::R2::Scanless::G'])
171  
172 A Marpa::R2::Scanless::G instance, hosting the computed grammar. This is a class variable, i.e. works also with C<$self>.
173  
174 =head2 MarpaX::RFC::RFC3986->bnf( --> Str)
175  
176 The BNF grammar used to parse an URI. This is a class variable, i.e. works also with C<$self>.
177  
178 =head1 OBJECT METHODS
179  
180 =head2 $self->value( --> Str)
181  
182 The variable given in input to C<new()>.
183  
184 =head2 $self->scheme( --> Str|Undef)
185  
186 The URI scheme. Can be undefined.
187  
188 =head2 $self->authority( --> Str|Undef)
189  
190 The URI authority. Can be undefined.
191  
192 =head2 $self->path( --> Str)
193  
194 The URI path. Note that an URI always have a path, although it can be empty.
195  
196 =head2 $self->query( --> Str|Undef)
197  
198 The URI query. Can be undefined.
199  
200 =head2 $self->fragment( --> Str|Undef)
201  
202 The URI fragment. Can be undefined.
203  
204 =head2 $self->hier_part( --> Str|Undef)
205  
206 The URI hier part. Can be undefined.
207  
208 =head2 $self->userinfo( --> Str|Undef)
209  
210 The URI userinfo. Can be undefined.
211  
212 =head2 $self->host( --> Str|Undef)
213  
214 The URI host. Can be undefined.
215  
216 =head2 $self->port( --> Str|Undef)
217  
218 The URI port. Can be undefined.
219  
220 =head2 $self->relative_part( --> Str|Undef)
221  
222 The URI relative part. Can be undefined.
223  
224 =head2 $self->ip_literal( --> Str|Undef)
225  
226 The URI IP literal. Can be undefined.
227  
228 =head2 $self->zoneid( --> Str|Undef)
229  
230 The URI IP's zone id. Can be undefined.
231  
232 =head2 $self->ipv4address( --> Str|Undef)
233  
234 The URI IP Version 4 address. Can be undefined.
235  
236 =head2 $self->reg_name( --> Str|Undef)
237  
238 The URI registered name. Can be undefined.
239  
240 =head2 $self->is_absolute( --> Bool)
241  
242 Returns a true value if the URI is absolute, false otherwise.
243  
244 =head1 SEE ALSO
245  
246 L<Marpa::R2>
247  
248 L<URI>
249  
250 L<Data::Validate::URI>
251  
252 L<Uniform Resource Identifier (URI): Generic Syntax|http://tools.ietf.org/html/rfc3986>
253  
254 L<Representing IPv6 Zone Identifiers in Address Literals and Uniform Resource Identifiers|http://tools.ietf.org/html/rfc6874>
255  
256 L<URI Design and Ownership|http://tools.ietf.org/html/rfc7320>
257  
258 =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
259  
260 =head1 SUPPORT
261  
262 =head2 Bugs / Feature Requests
263  
264 Please report any bugs or feature requests through the issue tracker
265 at L<https://rt.cpan.org/Public/Dist/Display.html?Name=MarpaX-RFC-RFC3986>.
266 You will be notified automatically of any progress on your issue.
267  
268 =head2 Source Code
269  
270 This is open source software. The code repository is available for
271 public review and contribution under the terms of the license.
272  
273 L<https://github.com/jddurand/marpax-rfc-rfc3986>
274  
275 git clone git://github.com/jddurand/marpax-rfc-rfc3986.git
276  
277 =head1 AUTHOR
278  
279 Jean-Damien Durand <jeandamiendurand@free.fr>
280  
281 =head1 COPYRIGHT AND LICENSE
282  
283 This software is copyright (c) 2015 by Jean-Damien Durand.
284  
285 This is free software; you can redistribute it and/or modify it under
286 the same terms as the Perl 5 programming language system itself.
287  
288 =cut
289  
290 __DATA__
↑ Summary