This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
/
Encode.pm
332 lines (233 loc) · 7.8 KB
/
Encode.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package URI::Encode;
#######################
# LOAD MODULES
#######################
use strict;
use warnings FATAL => 'all';
use 5.008001;
use Encode qw();
use Carp qw(croak carp);
#######################
# VERSION
#######################
our $VERSION = '0.07';
#######################
# EXPORT
#######################
use base qw(Exporter);
our (@EXPORT_OK);
@EXPORT_OK = qw(uri_encode uri_decode);
#######################
# SETTINGS
#######################
# Reserved characters
my $reserved_re
= qr{([^a-zA-Z0-9\-\_\.\~\!\*\'\(\)\;\:\@\&\=\+\$\,\/\?\#\[\]\%])}x;
# Un-reserved characters
my $unreserved_re = qr{([^a-zA-Z0-9\Q-_.~\E\%])}x;
# Encoded character set
my $encoded_chars = qr{%([a-fA-F0-9]{2})}x;
#######################
# CONSTRUCTOR
#######################
sub new {
my ( $class, @in ) = @_;
# Check Input
my $input = {
# this module, unlike URI::Escape,
# does not encode reserved characters
encode_reserved => 0,
# Allow Double encoding?
# defaults to YES
double_encode => 1,
};
if ( ref $in[0] eq 'HASH' ) { $input = $in[0]; }
else { $input = {@in}; }
# Encoding Map
$input->{enc_map}
= { ( map { chr($_) => sprintf( "%%%02X", $_ ) } ( 0 ... 255 ) ) };
# Decoding Map
$input->{dec_map}
= { ( map { sprintf( "%02X", $_ ) => chr($_) } ( 0 ... 255 ) ) };
# Return
my $self = bless $input, $class;
return $self;
} ## end sub new
#######################
# ENCODE
#######################
sub encode {
my ( $self, $data, $options ) = @_;
# Check for data
# Allow to be '0'
return unless defined $data;
my $enc_res = $self->{encode_reserved};
my $double_encode = $self->{double_encode};
if ( defined $options ) {
if ( ref $options eq 'HASH' ) {
$enc_res = $options->{encode_reserved}
if exists $options->{encode_reserved};
$double_encode = $options->{double_encode}
if exists $options->{double_encode};
} ## end if ( ref $options eq 'HASH')
else {
$enc_res = $options;
}
} ## end if ( defined $options )
# UTF-8 encode
$data = Encode::encode( 'utf-8-strict', $data );
# Encode a literal '%'
if ($double_encode) { $data =~ s{\%}{$self->_get_encoded_char($1)}gex; }
else { $data =~ s{(\%)}{$self->_encode_literal_percent($1, $')}gex; }
# Percent Encode
if ($enc_res) {
$data =~ s{$unreserved_re}{$self->_get_encoded_char($1)}gex;
}
else {
$data =~ s{$reserved_re}{$self->_get_encoded_char($1)}gex;
}
# Done
return $data;
} ## end sub encode
#######################
# DECODE
#######################
sub decode {
my ( $self, $data ) = @_;
# Check for data
# Allow to be '0'
return unless defined $data;
# Percent Decode
$data =~ s{$encoded_chars}{ $self->_get_decoded_char($1) }gex;
return $data;
} ## end sub decode
#######################
# EXPORTED FUNCTIONS
#######################
# Encoder
sub uri_encode { return __PACKAGE__->new()->encode(@_); }
# Decoder
sub uri_decode { return __PACKAGE__->new()->decode(@_); }
#######################
# INTERNAL
#######################
sub _get_encoded_char {
my ( $self, $char ) = @_;
return $self->{enc_map}->{$char} if exists $self->{enc_map}->{$char};
return $char;
}
sub _encode_literal_percent {
my ( $self, $char, $post ) = @_;
return $self->_get_encoded_char($char) if not defined $post;
if ( $post =~ m{^([a-fA-F0-9]{2})}x ) {
return $self->_get_encoded_char($char)
unless exists $self->{dec_map}->{$1};
return $char;
}
return $self->_get_encoded_char($char);
} ## end sub _encode_literal_percent
sub _get_decoded_char {
my ( $self, $char ) = @_;
return $self->{dec_map}->{$char} if exists $self->{dec_map}->{$char};
return $char;
}
#######################
1;
__END__
#######################
# POD SECTION
#######################
=pod
=head1 NAME
URI::Encode - Simple percent Encoding/Decoding
=head1 SYNOPSIS
# OOP Interface
use URI::Encode;
my $uri = URI::Encode->new({encode_reserved =>0});
my $encoded = $uri->encode($data);
my $decoded = $uri->decode($encoded);
# Functional
use URI::Encode qw(uri_encode uri_decode);
my $encoded = uri_encode($data);
my $decoded = uri_decode($encoded);
=head1 DESCRIPTION
This modules provides simple URI (Percent) encoding/decoding
The main purpose of this module (at least for me) was to provide an easy method
to encode strings (mainly URLs) into a format which can be pasted into a plain
text emails, and that those links are 'click-able' by the person reading that
email. This can be accomplished by NOT encoding the reserved characters.
This module can also be useful when using L<HTTP::Tiny> to ensure the URLs are
properly escaped.
If you are looking for speed and want to encode reserved characters, use
L<URI::Escape::XS>
See L<this
script|https://github.com/mithun/perl-uri-encode/raw/master/.author/benchmark.pl>
for a comparison on encoding results and performance.
=head1 METHODS
=head2 new()
Creates a new object, no arguments are required
my $encoder = URI::Encode->new(\%options);
The following options can be passed to the constructor
=over
=item encode_reserved
my $encoder = URI::Encode->new({encode_reserved => 0});
If true, L</"Reserved Characters"> are also encoded. Defaults to false.
=item double_encode
my $encoder = URI::Encode->new({double_encode => 1});
If true, characters that are already percent-encoded will not be encoded again.
Defaults to true.
my $encoder = URI::Encode->new({double_encode => 0});
print $encoder->encode('http://perl.com/foo%20bar'); # prints http://perl.com/foo%20bar
=back
=head2 C<encode($url, $including_reserved)>
This method encodes the URL provided. The method does not encode any
L</"Reserved Characters"> unless C<$including_reserved> is true or set in the
constructor. The $url provided is first converted into UTF-8 before percent
encoding.
$uri->encode("http://perl.com/foo bar"); # http://perl.com/foo%20bar
$uri->encode("http://perl.com/foo bar", 1); # http%3A%2F%2Fperl.com%2Ffoo%20bar
=head2 C<decode($url)>
This method decodes a 'percent' encoded URL. If you had encoded the URL using
this module (or any other method), chances are that the URL was converted to
UTF-8 before 'percent' encoding. Be sure to check the format and convert back
if required.
$uri->decode("http%3A%2F%2Fperl.com%2Ffoo%20bar"); # "http://perl.com/foo bar"
=head1 EXPORTED FUNCTIONS
The following functions are exported upon request. This provides a non-OOP
interface
=head2 C<uri_encode($url, \%options)>
=head2 C<uri_decode($url)>
=head1 CHARACTER CLASSES
=head2 Reserved Characters
The following characters are considered as reserved (L<RFC
3986|http://tools.ietf.org/html/rfc3986>). They will be encoded only if
requested.
! * ' ( ) ; : @ & = + $ , / ? # [ ]
=head2 Unreserved Characters
The following characters are considered as Unreserved. They will not be encoded
a-z
A-Z
0-9
- _ . ~
=head1 DEPENDENCIES
L<Encode>
=head1 ACKNOWLEDGEMENTS
Gisle Aas for L<URI::Escape>
David Nicol for L<Tie::UrlEncoder>
=head1 SEE ALSO
L<RFC 3986|http://tools.ietf.org/html/rfc3986>
L<URI::Escape>
L<URI::Escape::XS>
L<URI::Escape::JavaScript>
L<Tie::UrlEncoder>
=head1 BUGS AND LIMITATIONS
Please report any bugs or feature requests to C<bug-uri-encode@rt.cpan.org>, or
through the web interface at
L<http://rt.cpan.org/Public/Dist/Display.html?Name=URI-Encode>
=head1 AUTHOR
Mithun Ayachit C<mithun@cpan.org>
=head1 LICENSE AND COPYRIGHT
Copyright (c) 2012, Mithun Ayachit. All rights reserved.
This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>.
=cut