|
| 1 | +use v6.c; # required for correct execution |
| 2 | + |
| 3 | +my $RFC3986_unreserved := rx/<[0..9A..Za..z\-.~_]>/; |
| 4 | +my $RFC3986_reserved := rx/<[:/?#\[\]@!$&'()*+,;=]>/; |
| 5 | + |
| 6 | +# for uri_decode |
| 7 | +my %escapes = (^256).map: { |
| 8 | + sprintf("%%%02X", $_) => chr($_) |
| 9 | + unless chr($_) ~~ /$RFC3986_unreserved/; |
| 10 | +} |
| 11 | + |
| 12 | +my sub enc (Str:D $m) { |
| 13 | + $m.encode.list.map(*.fmt('%%%02X')).join |
| 14 | +} |
| 15 | + |
| 16 | +sub uri_encode (Str:D $text) is export { |
| 17 | + $text.comb.map({ |
| 18 | + if $RFC3986_unreserved || $RFC3986_reserved { |
| 19 | + $_ |
| 20 | + } |
| 21 | + else { |
| 22 | + &enc($_) |
| 23 | + } |
| 24 | + }).join |
| 25 | +} |
| 26 | + |
| 27 | +sub uri_encode_component (Str:D $text) is export { |
| 28 | + $text.comb.map({ |
| 29 | + if $RFC3986_unreserved { |
| 30 | + $_ |
| 31 | + } |
| 32 | + else { |
| 33 | + enc($_) |
| 34 | + } |
| 35 | + }).join |
| 36 | +} |
| 37 | + |
| 38 | +my sub dec ($m) { |
| 39 | + Buf.new($m<bit>.list.map({:16($_.Str)})).decode |
| 40 | +} |
| 41 | + |
| 42 | +sub uri_decode (Str:D $text) is export { |
| 43 | + $text.subst(/[\%$<bit>=[<[0..9A..Fa..f]>** 2]]+/, &dec, :g) |
| 44 | +} |
| 45 | + |
| 46 | +sub uri_decode_component (Str:D $text) is export { |
| 47 | + $text.subst(/[\%$<bit>=[<[0..9A..Fa..f]>** 2]]+/, &dec, :g) |
| 48 | +} |
| 49 | + |
| 50 | +=begin pod |
| 51 | +
|
| 52 | +=head1 NAME |
| 53 | +
|
| 54 | +URI::Encode - a Raku module for encoding / decoding URIs |
| 55 | +
|
| 56 | +=head1 SYNOPSIS |
| 57 | +
|
| 58 | +=begin code :lang<raku> |
| 59 | +
|
| 60 | +use URI::Encode; |
| 61 | +
|
| 62 | +# for encoding whole URIs, ignores reserved chars: :/?#[]@!$&'()*+,;= |
| 63 | +my $encoded_uri = uri_encode('http://www.example.com/?name=john doe&age=54'); |
| 64 | +
|
| 65 | +# encode every reserved char |
| 66 | +my $encoded_uri_component = uri_encode_component('some text/to encode+ safely'); |
| 67 | +
|
| 68 | +# remove percent encoding |
| 69 | +my $decoded_uri = uri_decode('http://www.example.com/?name=john%20doe&age=54'); |
| 70 | +
|
| 71 | +# provided for symmetry, is the same as uri_decode() |
| 72 | +my $decoded_component = uri_decode_component('some%20text%2Fto%20%2B%20safely'); |
| 73 | +
|
| 74 | +=end code |
| 75 | +
|
| 76 | +=head1 DESCRIPTION |
| 77 | +
|
| 78 | +URI::Encode is a module that exports four subroutines: |
| 79 | +
|
| 80 | +=item uri_encode - encode a whole URI |
| 81 | +=item uri_encode_component - encode every reserved char |
| 82 | +=item uri_decode - decode a whole URI |
| 83 | +=item uri_decode_component - decode every reserved char |
| 84 | +
|
| 85 | +=head1 SEE ALSo |
| 86 | +
|
| 87 | +L<C<URI>|https://raku.land/github:raku-community-modules/URI> is another |
| 88 | +implementation that covers this area, including encoding and decoding of |
| 89 | +URIs. |
| 90 | +
|
| 91 | +=head1 AUTHOR |
| 92 | +
|
| 93 | +David Farrell |
| 94 | +
|
| 95 | +=head1 COPYRIGHT AND LICENSE |
| 96 | +
|
| 97 | +Copyright 2014 - 2015 David Farrell |
| 98 | +
|
| 99 | +Copyright 2016 - 2024 Raku Community |
| 100 | +
|
| 101 | +This library is free software; you can redistribute it and/or modify it under the FreeBSD license. |
| 102 | +
|
| 103 | +=end pod |
| 104 | + |
| 105 | +# vim: expandtab shiftwidth=4 |
0 commit comments