line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::Response::Encoding; |
2
|
2
|
|
|
2
|
|
694747
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
77
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
1820
|
|
4
|
|
|
|
|
|
|
our $VERSION = sprintf "%d.%02d", q$Revision: 0.6 $ =~ /(\d+)/g; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
sub HTTP::Response::charset { |
7
|
9
|
|
|
9
|
1
|
389004
|
my $self = shift; |
8
|
9
|
100
|
|
|
|
56
|
return $self->{__charset} if exists $self->{__charset}; |
9
|
5
|
50
|
|
|
|
41
|
if ($self->can('content_charset')){ |
10
|
|
|
|
|
|
|
# To suppress: |
11
|
|
|
|
|
|
|
# Parsing of undecoded UTF-8 will give garbage when decoding entities |
12
|
5
|
|
|
0
|
|
44
|
local $SIG{__WARN__} = sub {}; |
|
0
|
|
|
|
|
0
|
|
13
|
5
|
|
|
|
|
28
|
my $charset = $self->content_charset; |
14
|
5
|
|
|
|
|
42062
|
$self->{__charset} = $charset; |
15
|
5
|
|
|
|
|
73
|
return $charset; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
|
|
0
|
my $content_type = $self->headers->header('Content-Type'); |
19
|
0
|
0
|
|
|
|
0
|
return unless $content_type; |
20
|
0
|
|
|
|
|
0
|
$content_type =~ /charset=([A-Za-z0-9_\-]+)/io; |
21
|
0
|
|
0
|
|
|
0
|
$self->{__charset} = $1 || undef; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub HTTP::Response::encoder { |
25
|
6
|
|
|
6
|
0
|
62
|
require Encode; |
26
|
6
|
|
|
|
|
14
|
my $self = shift; |
27
|
6
|
100
|
|
|
|
35
|
return $self->{__encoder} if exists $self->{__encoder}; |
28
|
5
|
50
|
|
|
|
18
|
my $charset = $self->charset or return; |
29
|
5
|
|
|
|
|
23
|
my $enc = Encode::find_encoding($charset); |
30
|
5
|
|
|
|
|
2692
|
$self->{__encoder} = $enc; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub HTTP::Response::encoding { |
34
|
6
|
50
|
|
6
|
0
|
7154
|
my $enc = shift->encoder or return; |
35
|
6
|
|
|
|
|
41
|
$enc->name; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 NAME |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
HTTP::Response::Encoding - Adds encoding() to HTTP::Response |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 VERSION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
$Id: Encoding.pm,v 0.6 2009/07/28 21:25:25 dankogai Exp dankogai $ |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=head1 SYNOPSIS |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use LWP::UserAgent; |
51
|
|
|
|
|
|
|
use HTTP::Response::Encoding; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
54
|
|
|
|
|
|
|
my $res = $ua->get("http://www.example.com/"); |
55
|
|
|
|
|
|
|
warn $res->encoding; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 EXPORT |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Nothing. |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 METHODS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
This module adds the following methods to L objects. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=over 2 |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=item C<< $res->charset >> |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Tells the charset I in the C header. |
70
|
|
|
|
|
|
|
Note that the presence of the charset does not guarantee if the |
71
|
|
|
|
|
|
|
response content is decodable via Encode. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
To normalize this, you should try |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
$res->encoder->mime_name; # with Encode 2.21 or above |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
or |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
use I18N::Charset; |
80
|
|
|
|
|
|
|
# ... |
81
|
|
|
|
|
|
|
mime_charset_name($res->encoding); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=item C<< $res->encoder >> |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Returns the corresponding encoder object or undef if it can't. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=item C<< $res->encoding >> |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
Tells the content encoding in the canonical name in L. |
90
|
|
|
|
|
|
|
Returns undef if it can't. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
For most cases, you are more likely to successfully find encoding |
93
|
|
|
|
|
|
|
after GET than HEAD. HTTP::Response is smart enough to parse |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
But you need the content to let HTTP::Response parse it. |
98
|
|
|
|
|
|
|
If you don't want to retrieve the whole content but interested in its |
99
|
|
|
|
|
|
|
encoding, try something like below; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
my $req = HTTP::Request->new(GET => $uri); |
102
|
|
|
|
|
|
|
$req->headers->header(Range => "bytes=0-4095"); # just 1st 4k |
103
|
|
|
|
|
|
|
my $res = $ua->request($req); |
104
|
|
|
|
|
|
|
warn $res->encoding; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item C<< $res->decoded_content >> |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Discontinued since HTTP::Message already has this method. |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
See L for details. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=back |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 INSTALLATION |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
To install this module, run the following commands: |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
perl Makefile.PL |
119
|
|
|
|
|
|
|
make |
120
|
|
|
|
|
|
|
make test |
121
|
|
|
|
|
|
|
make install |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=head1 AUTHOR |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
Dan Kogai, C<< >> |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=head1 BUGS |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
130
|
|
|
|
|
|
|
C, or through the web interface at |
131
|
|
|
|
|
|
|
L. |
132
|
|
|
|
|
|
|
I will be notified, and then you'll automatically be notified of progress on |
133
|
|
|
|
|
|
|
your bug as I make changes. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head1 SUPPORT |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
You can find documentation for this module with the perldoc command. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
perldoc HTTP::Response::Encoding |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
You can also look for information at: |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=over 4 |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
L |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item * CPAN Ratings |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
L |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item * RT: CPAN's request tracker |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
L |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item * Search CPAN |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
L |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=back |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
GAAS for L. |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
MIYAGAWA for suggestions. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 COPYRIGHT & LICENSE |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Copyright 2007 Dan Kogai, all rights reserved. |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
174
|
|
|
|
|
|
|
under the same terms as Perl itself. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
1; # End of HTTP::Response::Encoding |