| 1 | #======================================================================== | ||
| 2 | # | ||
| 3 | # Badger::Codec::URL | ||
| 4 | # | ||
| 5 | # DESCRIPTION | ||
| 6 | # Codec module for URL encoding/decoding | ||
| 7 | # | ||
| 8 | # AUTHOR | ||
| 9 | # Andy Wardley <abw@wardley.org> | ||
| 10 | # | ||
| 11 | #======================================================================== | ||
| 12 | |||
| 13 | package Badger::Codec::URL; | ||
| 14 | |||
| 15 | use Badger::Class | ||
| 16 | version => 0.01, | ||
| 17 | base => 'Badger::Codec::URI'; | ||
| 18 | |||
| 19 | # cache of escaped characters is shared with Badger::Codec::URI | ||
| 20 | our $URI_ESCAPES = $Badger::Codec::URI::URI_ESCAPES; | ||
| 21 | |||
| 22 | sub encode_url { | ||
| 23 | my $url = shift; | ||
| 24 | |||
| 25 | utf8::encode($url) | ||
| 26 | if $] >= 5.008 && utf8::is_utf8($url); | ||
| 27 | |||
| 28 | $URI_ESCAPES ||= { | ||
| 29 | map { ( chr($_), sprintf("%%%02X", $_) ) } | ||
| 30 | (0..255) | ||
| 31 | }; | ||
| 32 | |||
| 33 | # the different between the URL and URI encoding is that URL does | ||
| 34 | # not escape any of: ; / ? : @ & = + $ | ||
| 35 | $url =~ s/([^;\/?:@&=+\$,A-Za-z0-9\-_.!~*'()])/$URI_ESCAPES->{$1}/eg; | ||
| 36 | $url; | ||
| 37 | } | ||
| 38 | |||
| 39 | *decode_url = \&Badger::Codec::URI::decode_uri; | ||
| 40 | |||
| 41 | sub encode { | ||
| 42 | shift; | ||
| 43 | goto &encode_url; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub decode { | ||
| 47 | shift; | ||
| 48 | goto &decode_url; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub encoder { | ||
| 52 | \&encode_url; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub decoder { | ||
| 56 | \&decode_url; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | 1; | ||
| 61 | |||
| 62 | |||
| 63 | __END__ |