1 |
wakaba |
1.1 |
package Message::CGI::Util; |
2 |
|
|
use strict; |
3 |
|
|
|
4 |
|
|
use Exporter; |
5 |
|
|
push our @ISA, 'Exporter'; |
6 |
|
|
|
7 |
|
|
our @EXPORT_OK = qw/ |
8 |
|
|
htescape |
9 |
|
|
percent_encode percent_encode_na |
10 |
|
|
percent_decode |
11 |
|
|
get_absolute_url |
12 |
|
|
/; |
13 |
|
|
|
14 |
|
|
require Encode; |
15 |
|
|
|
16 |
|
|
sub htescape ($) { |
17 |
|
|
my $s = shift; |
18 |
|
|
$s =~ s/&/&/g; |
19 |
|
|
$s =~ s/</</g; |
20 |
|
|
$s =~ s/"/"/g; |
21 |
|
|
return $s; |
22 |
|
|
} # htescape |
23 |
|
|
|
24 |
|
|
sub percent_encode ($) { |
25 |
|
|
my $s = Encode::encode ('utf8', $_[0]); |
26 |
|
|
$s =~ s/([^A-Za-z0-9_~-])/sprintf '%%%02X', ord $1/ges; |
27 |
|
|
return $s; |
28 |
|
|
} # percent_encode |
29 |
|
|
|
30 |
|
|
sub percent_encode_na ($) { |
31 |
|
|
my $s = Encode::encode ('utf8', $_[0]); |
32 |
|
|
$s =~ s/([^\x00-\x7F])/sprintf '%%%02X', ord $1/ges; |
33 |
|
|
return $s; |
34 |
|
|
} # percent_encode_na |
35 |
|
|
|
36 |
|
|
sub percent_decode ($) { # input should be a byte string. |
37 |
|
|
my $s = shift; |
38 |
|
|
$s =~ s/%([0-9A-Fa-f]{2})/pack 'C', hex $1/ge; |
39 |
|
|
return Encode::decode ('utf-8', $s); # non-UTF-8 octet converted to \xHH |
40 |
|
|
} # percent_decode |
41 |
|
|
|
42 |
|
|
sub get_absolute_url ($$) { |
43 |
|
|
require Message::DOM::DOMImplementation; |
44 |
|
|
return Message::DOM::DOMImplementation->create_uri_reference ($_[0]) |
45 |
|
|
->get_absolute_reference ($_[1]) |
46 |
|
|
->get_uri_reference |
47 |
|
|
->uri_reference; |
48 |
|
|
} # get_absolute_url |
49 |
|
|
|
50 |
|
|
1; |