[bbd0cf1] | 1 | package URI::Encode; |
---|
| 2 | |
---|
| 3 | use 5.008001; |
---|
| 4 | use warnings; |
---|
| 5 | use strict; |
---|
| 6 | use Carp; |
---|
| 7 | use Encode qw(); |
---|
| 8 | |
---|
| 9 | our $VERSION = 0.03; |
---|
| 10 | |
---|
| 11 | ## Exporter |
---|
| 12 | use base qw(Exporter); |
---|
| 13 | our @EXPORT_OK = qw(uri_encode uri_decode); |
---|
| 14 | |
---|
| 15 | ## OOP Intrerface |
---|
| 16 | |
---|
| 17 | # Constructor |
---|
| 18 | sub new { |
---|
| 19 | my $class = shift; |
---|
| 20 | my $self = bless {}, $class; |
---|
| 21 | return $self; |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | # Encode |
---|
| 25 | sub encode { |
---|
| 26 | my ( $self, $url, $encode_reserved ) = @_; |
---|
| 27 | return unless $url; |
---|
| 28 | |
---|
| 29 | # Encode URL into UTF-8 |
---|
| 30 | $url = Encode::encode( 'utf-8-strict', $url ); |
---|
| 31 | |
---|
| 32 | # Create character map |
---|
| 33 | my %map = map { chr($_) => sprintf( "%%%02X", $_ ) } ( 0 ... 255 ); |
---|
| 34 | |
---|
| 35 | # Create Regex |
---|
| 36 | my $reserved = |
---|
| 37 | qr{([^a-zA-Z0-9\-\_\.\~\!\*\'\(\)\;\:\@\&\=\+\$\,\/\?\%\#\[\]])}x; |
---|
| 38 | my $unreserved = qr{([^a-zA-Z0-9\Q-_.~\E])}x; |
---|
| 39 | |
---|
| 40 | # Percent Encode URL |
---|
| 41 | if ($encode_reserved) { |
---|
| 42 | $url =~ s/$unreserved/$map{$1}/gx; |
---|
| 43 | } |
---|
| 44 | else { |
---|
| 45 | $url =~ s/$reserved/$map{$1}/gx; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | return $url; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | # Decode |
---|
| 52 | sub decode { |
---|
| 53 | my ( $shift, $url ) = @_; |
---|
| 54 | return unless $url; |
---|
| 55 | |
---|
| 56 | # Character map |
---|
| 57 | my %map = map { sprintf( "%02X", $_ ) => chr($_) } ( 0 ... 255 ); |
---|
| 58 | |
---|
| 59 | # Decode percent encoding |
---|
| 60 | $url =~ s/%([a-fA-F0-9]{2})/$map{$1}/gx; |
---|
| 61 | return $url; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | ## Traditional Interface |
---|
| 65 | |
---|
| 66 | # Encode |
---|
| 67 | sub uri_encode { |
---|
| 68 | my ( $url, $flag ) = @_; |
---|
| 69 | my $uri = URI::Encode->new(); |
---|
| 70 | return $uri->encode( $url, $flag ); |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | # Decode |
---|
| 74 | sub uri_decode { |
---|
| 75 | my ($url) = @_; |
---|
| 76 | my $uri = URI::Encode->new(); |
---|
| 77 | return $uri->decode($url); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | ## Done |
---|
| 81 | 1; |
---|
| 82 | __END__ |
---|
| 83 | |
---|
| 84 | =pod |
---|
| 85 | |
---|
| 86 | =head1 NAME |
---|
| 87 | |
---|
| 88 | URI::Encode - Simple URI Encoding/Decoding |
---|
| 89 | |
---|
| 90 | =head1 VERSION |
---|
| 91 | |
---|
| 92 | This document describes URI::Encode version 0.03 |
---|
| 93 | |
---|
| 94 | =head1 SYNOPSIS |
---|
| 95 | |
---|
| 96 | ## OO Interface |
---|
| 97 | use URI::Encode; |
---|
| 98 | my $uri = URI::Encode->new(); |
---|
| 99 | my $encoded = $uri->encode($url); |
---|
| 100 | my $decoded = $uri->decode($encoded); |
---|
| 101 | |
---|
| 102 | ## Using exported functions |
---|
| 103 | use URI::Encode qw(uri_encode uri_decode); |
---|
| 104 | my $encoded = uri_encode($url); |
---|
| 105 | my $decoded = uri_decode($url); |
---|
| 106 | |
---|
| 107 | =head1 DESCRIPTION |
---|
| 108 | |
---|
| 109 | This modules provides simple URI (Percent) encoding/decoding. This module |
---|
| 110 | borrows from L<URI::Escape>, but 'tinier' |
---|
| 111 | |
---|
| 112 | =head1 METHODS |
---|
| 113 | |
---|
| 114 | =head2 new() |
---|
| 115 | |
---|
| 116 | Creates a new object, no argumetns are required |
---|
| 117 | |
---|
| 118 | my $encoder = URI::Encode->new(); |
---|
| 119 | |
---|
| 120 | =head2 encode($url, $including_reserved) |
---|
| 121 | |
---|
| 122 | This method encodes the URL provided. The method does not encode any |
---|
| 123 | L</"Reserved Characters"> unless C<$including_reserved> is true. The $url |
---|
| 124 | provided is first converted into UTF-8 before percent encoding. |
---|
| 125 | |
---|
| 126 | $uri->encode("http://perl.com/foo bar"); # http://perl.com/foo%20bar |
---|
| 127 | $uri->encode("http://perl.com/foo bar", 1); # http%3A%2F%2Fperl.com%2Ffoo%20bar |
---|
| 128 | |
---|
| 129 | =head2 decode($url) |
---|
| 130 | |
---|
| 131 | This method decodes a 'percent' encoded URL. If you had encoded the URL using |
---|
| 132 | this module (or any other method), chances are that the URL was converted to |
---|
| 133 | UTF-8 before 'percent' encoding. Be sure to check the format and convert back |
---|
| 134 | if required. |
---|
| 135 | |
---|
| 136 | $uri->decode("http%3A%2F%2Fperl.com%2Ffoo%20bar"); # "http://perl.com/foo bar" |
---|
| 137 | |
---|
| 138 | =head1 EXPORTED FUNCTIONS |
---|
| 139 | |
---|
| 140 | The following functions are exported upon request. This provides a non-OOP |
---|
| 141 | interface |
---|
| 142 | |
---|
| 143 | =head2 uri_encode($url, $including_reserved) |
---|
| 144 | |
---|
| 145 | See L</encode($url, $including_reserved)> |
---|
| 146 | |
---|
| 147 | =head2 uri_decode($url) |
---|
| 148 | |
---|
| 149 | See L</decode($url)> |
---|
| 150 | |
---|
| 151 | =head1 CHARACTER CLASSES |
---|
| 152 | |
---|
| 153 | =head2 Reserved Characters |
---|
| 154 | |
---|
| 155 | The following characters are considered as reserved (L<RFC |
---|
| 156 | 3986|http://tools.ietf.org/html/rfc3986>). They will be encoded only if requested. |
---|
| 157 | |
---|
| 158 | ! * ' ( ) ; : @ & = + $ , / ? % # [ ] |
---|
| 159 | |
---|
| 160 | =head2 Unreserved Characters |
---|
| 161 | |
---|
| 162 | The following characters are considered as Unreserved. They will not be encoded |
---|
| 163 | |
---|
| 164 | a-z |
---|
| 165 | A-Z |
---|
| 166 | 0-9 |
---|
| 167 | - _ . ~ |
---|
| 168 | |
---|
| 169 | =head1 DEPENDENCIES |
---|
| 170 | |
---|
| 171 | L<Encode> |
---|
| 172 | |
---|
| 173 | =head1 ACKNOWLEDGEMENTS |
---|
| 174 | |
---|
| 175 | Gisle Aas for L<URI::Escape> |
---|
| 176 | |
---|
| 177 | David Nicol for L<Tie::UrlEncoder> |
---|
| 178 | |
---|
| 179 | =head1 SEE ALSO |
---|
| 180 | |
---|
| 181 | L<RFC 3986|http://tools.ietf.org/html/rfc3986> |
---|
| 182 | |
---|
| 183 | L<URI::Escape> |
---|
| 184 | |
---|
| 185 | L<URI::Escape::XS> |
---|
| 186 | |
---|
| 187 | L<URI::Escape::JavaScript> |
---|
| 188 | |
---|
| 189 | L<Tie::UrlEncoder> |
---|
| 190 | |
---|
| 191 | =head1 BUGS AND LIMITATIONS |
---|
| 192 | |
---|
| 193 | No bugs have been reported. |
---|
| 194 | |
---|
| 195 | Please report any bugs or feature requests to C<bug-uri-encode@rt.cpan.org>, or |
---|
| 196 | through the web interface at L<http://rt.cpan.org>. |
---|
| 197 | |
---|
| 198 | =head1 AUTHOR |
---|
| 199 | |
---|
| 200 | Mithun Ayachit C<< <mithun@cpan.org> >> |
---|
| 201 | |
---|
| 202 | =head1 LICENCE AND COPYRIGHT |
---|
| 203 | |
---|
| 204 | Copyright (c) 2010, Mithun Ayachit C<< <mithun@cpan.org> >>. All rights |
---|
| 205 | reserved. |
---|
| 206 | |
---|
| 207 | This module is free software; you can redistribute it and/or modify it under |
---|
| 208 | the same terms as Perl itself. See L<perlartistic>. |
---|
| 209 | |
---|
| 210 | =head1 DISCLAIMER OF WARRANTY |
---|
| 211 | |
---|
| 212 | BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE |
---|
| 213 | SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE |
---|
| 214 | STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE |
---|
| 215 | SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, |
---|
| 216 | INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
---|
| 217 | FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND |
---|
| 218 | PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, |
---|
| 219 | YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION. |
---|
| 220 | |
---|
| 221 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY |
---|
| 222 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE |
---|
| 223 | SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, |
---|
| 224 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING |
---|
| 225 | OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO |
---|
| 226 | LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR |
---|
| 227 | THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), |
---|
| 228 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
---|
| 229 | DAMAGES. |
---|
| 230 | |
---|