| 1 | package Zuzu::Module::URL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | our $VERSION = '0.007002'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use URI (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use URI::Escape qw( uri_escape_utf8 uri_unescape ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use URI::Template (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Zuzu::Error; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Zuzu::Util::NativeHelpers qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | native_function | ||||||||||||||||||||||||||||
| 16 | perl_to_zuzu | ||||||||||||||||||||||||||||
| 17 | zuzu_to_perl | ||||||||||||||||||||||||||||
| 18 | 2 | ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | ▶ | sub _str { | ||||||||||||||||||||||||||
|
Subroutine
_str: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 21 | 0 | my ( $value, $default ) = @_; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | ▶ | return $default if not defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 0 | return "$value"; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | sub _hash_from { | ||||||||||||||||||||||||||
|
Subroutine
_hash_from: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 28 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | ▶ | return {} if not defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | my $perl = zuzu_to_perl( $value ); | |||||||||||||||||||||||||||
| 32 | 0 | ▶ | return {} if ref($perl) ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | return $perl; | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # RFC 6570 expression grammar: optional operator, then a comma list of | ||||||||||||||||||||||||||||
| 37 | # varspecs (varname with optional :N prefix or * explode modifier). | ||||||||||||||||||||||||||||
| 38 | 2 | my $TEMPLATE_VARCHAR = qr/(?:[A-Za-z0-9_]|%[0-9A-Fa-f]{2})/; | |||||||||||||||||||||||||||
| 39 | 2 | my $TEMPLATE_VARSPEC = | |||||||||||||||||||||||||||
| 40 | qr/$TEMPLATE_VARCHAR(?:\.?$TEMPLATE_VARCHAR)*(?::[1-9][0-9]{0,3}|\*)?/; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | sub _die_template { | ||||||||||||||||||||||||||
|
Subroutine
_die_template: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 0 | my ( $source ) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | die Zuzu::Error->new_runtime( | |||||||||||||||||||||||||||
| 46 | message => "invalid URL template: $source", | ||||||||||||||||||||||||||||
| 47 | file => '<std/net/url>', | ||||||||||||||||||||||||||||
| 48 | line => 0, | ||||||||||||||||||||||||||||
| 49 | ); | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub _validate_template { | ||||||||||||||||||||||||||
|
Subroutine
_validate_template: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||
| 53 | 0 | my ( $source, $data ) = @_; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | my $rest = $source; | |||||||||||||||||||||||||||
| 56 | 0 | ▶ | while ( length $rest ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 0 | my $open = index( $rest, '{' ); | |||||||||||||||||||||||||||
| 58 | 0 | my $close = index( $rest, '}' ); | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | last if $open < 0 and $close < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$open < 0 and $close < 0
MC/DC0%0/2
$open < 0 and $close < 0
$open < 0
$close < 0
Truth table0%0/3
A and B
A= $open < 0B= $close < 0
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | _die_template( $source ) if $open < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | _die_template( $source ) if $close < 0 or $close < $open; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$close < 0 or $close < $open
MC/DC0%0/2
$close < 0 or $close < $open
$close < 0
$close < $open
Truth table0%0/3
A or B
A= $close < 0B= $close < $open
| |||||||||||||||||||||||||||||
| 62 | 0 | my $expression = substr( $rest, $open + 1, $close - $open - 1 ); | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | _die_template( $source ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | if $expression !~ /\A[+#.\/;?&]?$TEMPLATE_VARSPEC(?:,$TEMPLATE_VARSPEC)*\z/; | ||||||||||||||||||||||||||||
| 65 | # The :N prefix modifier only applies to string values; using it | ||||||||||||||||||||||||||||
| 66 | # with a list or associative value is an expansion failure. | ||||||||||||||||||||||||||||
| 67 | 0 | ( my $specs = $expression ) =~ s/\A[+#.\/;?&]//; | |||||||||||||||||||||||||||
| 68 | 0 | for my $spec ( split /,/, $specs ) { | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | if ( $spec =~ /\A(.*):[1-9][0-9]{0,3}\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | _die_template( $source ) if ref $data->{$1}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 0 | $rest = substr( $rest, $close + 1 ); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | sub _template_value { | ||||||||||||||||||||||||||
|
Subroutine
_template_value: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 78 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | return undef if not defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | if ( blessed($value) and $value->isa('Zuzu::Value::Boolean') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Zuzu::Value::Boolean")
builtin::blessed($value)
$value->isa("Zuzu::Value::Boolean")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= $value->isa("Zuzu::Value::Boolean")
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $value->value ? 'true' : 'false'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( ref($value) eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | # RFC 6570: a zero-member list is undefined. | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return undef if not @{$value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | return [ map { _str( $_, '' ) } @{$value} ]; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | if ( ref($value) eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | # RFC 6570: a zero-member associative array is undefined. | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | return undef if not keys %{$value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | return { map { $_ => _str( $value->{$_}, '' ) } keys %{$value} }; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 0 | return _str( $value, '' ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | sub _parse_url { | ||||||||||||||||||||||||||
|
Subroutine
_parse_url: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 98 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 0 | my $text = _str( $value, '' ); | |||||||||||||||||||||||||||
| 101 | 0 | my $uri = URI->new( $text ); | |||||||||||||||||||||||||||
| 102 | 0 | my %query = $uri->query_form; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | return { | ||||||||||||||||||||||||||||
| 105 | 0 | url => $uri->as_string, | |||||||||||||||||||||||||||
| 106 | scheme => scalar $uri->scheme, | ||||||||||||||||||||||||||||
| 107 | authority => scalar $uri->authority, | ||||||||||||||||||||||||||||
| 108 | userinfo => scalar $uri->userinfo, | ||||||||||||||||||||||||||||
| 109 | host => scalar $uri->host, | ||||||||||||||||||||||||||||
| 110 | port => scalar $uri->port, | ||||||||||||||||||||||||||||
| 111 | path => scalar $uri->path, | ||||||||||||||||||||||||||||
| 112 | query => scalar $uri->query, | ||||||||||||||||||||||||||||
| 113 | fragment => scalar $uri->fragment, | ||||||||||||||||||||||||||||
| 114 | query_params => \%query, | ||||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | sub _fill_template { | ||||||||||||||||||||||||||
|
Subroutine
_fill_template: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 119 | 0 | my ( $template, $values ) = @_; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my $source = _str( $template, '' ); | |||||||||||||||||||||||||||
| 122 | 0 | my $raw = _hash_from( $values ); | |||||||||||||||||||||||||||
| 123 | 0 | my %data; | |||||||||||||||||||||||||||
| 124 | 0 | for my $key ( keys %{$raw} ) { | |||||||||||||||||||||||||||
| 125 | 0 | my $converted = _template_value( $raw->{$key} ); | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | $data{$key} = $converted if defined $converted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | 0 | _validate_template( $source, \%data ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | return URI::Template->new( $source )->process_to_string( %data ); | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 8 | ▶ | sub IMPORT { | ||||||||||||||||||||||||||
|
Subroutine
IMPORT: called CC=1 SCAR=1.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 134 | 8 | my ( $class, $runtime ) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | my $escape_fn = native_function( | ||||||||||||||||||||||||||||
| 137 | name => 'escape', | ||||||||||||||||||||||||||||
| 138 | 0 | ▶ | native => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 139 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||
| 140 | 0 | return uri_escape_utf8( _str( $value, '' ) ); | |||||||||||||||||||||||||||
| 141 | }, | ||||||||||||||||||||||||||||
| 142 | 8 | ); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | my $unescape_fn = native_function( | ||||||||||||||||||||||||||||
| 145 | name => 'unescape', | ||||||||||||||||||||||||||||
| 146 | 0 | ▶ | native => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 147 | 0 | my ( $value ) = @_; | |||||||||||||||||||||||||||
| 148 | 0 | return uri_unescape( _str( $value, '' ) ); | |||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | 8 | ); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | my $parse_fn = native_function( | ||||||||||||||||||||||||||||
| 153 | name => 'parse', | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | native => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 155 | 0 | my ( $url ) = @_; | |||||||||||||||||||||||||||
| 156 | 0 | return perl_to_zuzu( _parse_url( $url ) ); | |||||||||||||||||||||||||||
| 157 | }, | ||||||||||||||||||||||||||||
| 158 | 8 | ); | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | my $fill_template_fn = native_function( | ||||||||||||||||||||||||||||
| 161 | name => 'fill_template', | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | native => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 163 | 0 | my ( $template, $values ) = @_; | |||||||||||||||||||||||||||
| 164 | 0 | return _fill_template( $template, $values ); | |||||||||||||||||||||||||||
| 165 | }, | ||||||||||||||||||||||||||||
| 166 | 8 | ); | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | return { | ||||||||||||||||||||||||||||
| 169 | 8 | escape => $escape_fn, | |||||||||||||||||||||||||||
| 170 | unescape => $unescape_fn, | ||||||||||||||||||||||||||||
| 171 | parse => $parse_fn, | ||||||||||||||||||||||||||||
| 172 | fill_template => $fill_template_fn, | ||||||||||||||||||||||||||||
| 173 | }; | ||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | 1; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =pod | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head1 NAME | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Zuzu::Module::URL - std/net/url bindings for ZuzuScript. | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | Implements the C<std/net/url> module and exports four functions: | ||||||||||||||||||||||||||||
| 187 | C<escape>, C<unescape>, C<parse>, and C<fill_template>. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head1 COPYRIGHT AND LICENCE | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | B<< Zuzu::Module::URL >> is copyright Toby Inkster. | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | It is free software; you may redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 194 | the terms of either the Artistic License 1.0 or the GNU General Public | ||||||||||||||||||||||||||||
| 195 | License version 2. | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | =cut | ||||||||||||||||||||||||||||