source: perl/modules/Facebook/lib/URI/Encode.pm @ bbd0cf1

release-1.10release-1.9
Last change on this file since bbd0cf1 was bbd0cf1, checked in by Edward Z. Yang <ezyang@mit.edu>, 13 years ago
Bundle Ouch, AnyEvent::HTTP, and URI::Encode from CPAN Those dependencies of Facebook::Graph are not in Debian and are each single files, so we may as well just bundle them. Also update list of bundled libraries in README and drop their licenses (or the closest thing they have resembling one) into COPYING.
  • Property mode set to 100644
File size: 5.4 KB
Line 
1package URI::Encode;
2
3use 5.008001;
4use warnings;
5use strict;
6use Carp;
7use Encode qw();
8
9our $VERSION = 0.03;
10
11## Exporter
12use base qw(Exporter);
13our @EXPORT_OK = qw(uri_encode uri_decode);
14
15## OOP Intrerface
16
17# Constructor
18sub new {
19    my $class = shift;
20    my $self = bless {}, $class;
21    return $self;
22}
23
24# Encode
25sub 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
52sub 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
67sub uri_encode {
68    my ( $url, $flag ) = @_;
69    my $uri = URI::Encode->new();
70    return $uri->encode( $url, $flag );
71}
72
73# Decode
74sub uri_decode {
75    my ($url) = @_;
76    my $uri = URI::Encode->new();
77    return $uri->decode($url);
78}
79
80## Done
811;
82__END__
83
84=pod
85
86=head1 NAME
87
88URI::Encode - Simple URI Encoding/Decoding
89
90=head1 VERSION
91
92This 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
109This modules provides simple URI (Percent) encoding/decoding. This module
110borrows from L<URI::Escape>, but 'tinier'
111
112=head1 METHODS
113
114=head2 new()
115
116Creates a new object, no argumetns are required
117
118        my $encoder = URI::Encode->new();
119
120=head2 encode($url, $including_reserved)
121
122This method encodes the URL provided. The method does not encode any
123L</"Reserved Characters"> unless C<$including_reserved> is true. The $url
124provided 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
131This method decodes a 'percent' encoded URL. If you had encoded the URL using
132this module (or any other method), chances are that the URL was converted to
133UTF-8 before 'percent' encoding. Be sure to check the format and convert back
134if required.
135
136        $uri->decode("http%3A%2F%2Fperl.com%2Ffoo%20bar"); # "http://perl.com/foo bar"
137
138=head1 EXPORTED FUNCTIONS
139
140The following functions are exported upon request. This provides a non-OOP
141interface
142
143=head2 uri_encode($url, $including_reserved)
144
145See L</encode($url, $including_reserved)>
146
147=head2 uri_decode($url)
148
149See L</decode($url)>
150
151=head1 CHARACTER CLASSES
152
153=head2 Reserved Characters
154
155The following characters are considered as reserved (L<RFC
1563986|http://tools.ietf.org/html/rfc3986>). They will be encoded only if requested.
157
158         ! * ' ( ) ; : @ & = + $ , / ? % # [ ]
159
160=head2 Unreserved Characters
161
162The 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
171L<Encode>
172
173=head1 ACKNOWLEDGEMENTS
174
175Gisle Aas for L<URI::Escape>
176
177David Nicol for L<Tie::UrlEncoder>
178
179=head1 SEE ALSO
180
181L<RFC 3986|http://tools.ietf.org/html/rfc3986>
182
183L<URI::Escape>
184
185L<URI::Escape::XS>
186
187L<URI::Escape::JavaScript>
188
189L<Tie::UrlEncoder>
190
191=head1 BUGS AND LIMITATIONS
192
193No bugs have been reported.
194
195Please report any bugs or feature requests to C<bug-uri-encode@rt.cpan.org>, or
196through the web interface at L<http://rt.cpan.org>.
197
198=head1 AUTHOR
199
200Mithun Ayachit  C<< <mithun@cpan.org> >>
201
202=head1 LICENCE AND COPYRIGHT
203
204Copyright (c) 2010, Mithun Ayachit C<< <mithun@cpan.org> >>. All rights
205reserved.
206
207This module is free software; you can redistribute it and/or modify it under
208the same terms as Perl itself. See L<perlartistic>.
209
210=head1 DISCLAIMER OF WARRANTY
211
212BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE
213SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE
214STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE
215SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
216INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
217FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
218PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE,
219YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
220
221IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
222COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE
223SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES,
224INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
225OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO
226LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR
227THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE),
228EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
229DAMAGES.
230
Note: See TracBrowser for help on using the repository browser.