|
1 | 1 | package HTTP::Message;
|
2 | 2 |
|
3 |
| -# $Id: Message.pm,v 1.51 2004/11/30 11:37:26 gisle Exp $ |
| 3 | +# $Id: Message.pm,v 1.52 2004/11/30 12:00:22 gisle Exp $ |
4 | 4 |
|
5 | 5 | use strict;
|
6 | 6 | use vars qw($VERSION $AUTOLOAD);
|
7 |
| -$VERSION = sprintf("%d.%02d", q$Revision: 1.51 $ =~ /(\d+)\.(\d+)/); |
| 7 | +$VERSION = sprintf("%d.%02d", q$Revision: 1.52 $ =~ /(\d+)\.(\d+)/); |
8 | 8 |
|
9 | 9 | require HTTP::Headers;
|
10 | 10 | require Carp;
|
@@ -159,64 +159,71 @@ sub content_ref
|
159 | 159 | sub decoded_content
|
160 | 160 | {
|
161 | 161 | my($self, %opt) = @_;
|
| 162 | + my $content_ref; |
162 | 163 |
|
163 |
| - require HTTP::Headers::Util; |
164 |
| - my($ct, %ct_param); |
165 |
| - if (my @ct = HTTP::Headers::Util::split_header_words($self->header("Content-Type"))) { |
166 |
| - ($ct, undef, %ct_param) = @{$ct[-1]}; |
167 |
| - $ct = lc($ct); |
| 164 | + eval { |
168 | 165 |
|
169 |
| - Carp::croak("Can't decode multipart content") |
170 |
| - if $ct =~ m,^multipart/,; |
171 |
| - } |
| 166 | + require HTTP::Headers::Util; |
| 167 | + my($ct, %ct_param); |
| 168 | + if (my @ct = HTTP::Headers::Util::split_header_words($self->header("Content-Type"))) { |
| 169 | + ($ct, undef, %ct_param) = @{$ct[-1]}; |
| 170 | + $ct = lc($ct); |
172 | 171 |
|
173 |
| - my $content_ref = $self->content_ref; |
174 |
| - Carp::croak("Can't decode ref content") if ref($content_ref) ne "SCALAR"; |
175 |
| - |
176 |
| - if (my $h = $self->header("Content-Encoding")) { |
177 |
| - $h =~ s/^\s+//; |
178 |
| - $h =~ s/\s+$//; |
179 |
| - for my $ce (reverse split(/\s*,\s*/, lc($h))) { |
180 |
| - next unless $ce || $ce eq "identity"; |
181 |
| - if ($ce eq "gzip" || $ce eq "x-gzip") { |
182 |
| - require Compress::Zlib; |
183 |
| - $content_ref = \Compress::Zlib::memGunzip($$content_ref); |
184 |
| - Carp::croak("Can't gunzip content") unless defined $$content_ref; |
185 |
| - } |
186 |
| - elsif ($ce eq "x-bzip2") { |
187 |
| - require Compress::Bzip2; |
188 |
| - $content_ref = Compress::Bzip2::decompress($$content_ref); |
189 |
| - Carp::croak("Can't bunzip content") unless defined $$content_ref; |
190 |
| - } |
191 |
| - elsif ($ce eq "deflate") { |
192 |
| - require Compress::Zlib; |
193 |
| - $content_ref = \Compress::Zlib::uncompress($$content_ref); |
194 |
| - Carp::croak("Can't inflate content") unless defined $$content_ref; |
195 |
| - } |
196 |
| - elsif ($ce eq "compress" || $ce eq "x-compress") { |
197 |
| - Carp::croak("Can't uncompress content"); |
198 |
| - } |
199 |
| - elsif ($ce eq "base64") { # not really C-T-E, but should be harmless |
200 |
| - require MIME::Base64; |
201 |
| - $content_ref = \MIME::Base64::decode($$content_ref); |
202 |
| - } |
203 |
| - elsif ($ce eq "quoted-printable") { # not really C-T-E, but should be harmless |
204 |
| - require MIME::QuotedPrint; |
205 |
| - $content_ref = \MIME::QuotedPrint::decode($$content_ref); |
206 |
| - } |
207 |
| - else { |
208 |
| - Carp::croak("Don't know how to decode Content-Encoding '$ce'"); |
| 172 | + die "Can't decode multipart content" if $ct =~ m,^multipart/,; |
| 173 | + } |
| 174 | + |
| 175 | + $content_ref = $self->content_ref; |
| 176 | + die "Can't decode ref content" if ref($content_ref) ne "SCALAR"; |
| 177 | + |
| 178 | + if (my $h = $self->header("Content-Encoding")) { |
| 179 | + $h =~ s/^\s+//; |
| 180 | + $h =~ s/\s+$//; |
| 181 | + for my $ce (reverse split(/\s*,\s*/, lc($h))) { |
| 182 | + next unless $ce || $ce eq "identity"; |
| 183 | + if ($ce eq "gzip" || $ce eq "x-gzip") { |
| 184 | + require Compress::Zlib; |
| 185 | + $content_ref = \Compress::Zlib::memGunzip($$content_ref); |
| 186 | + die "Can't gunzip content" unless defined $$content_ref; |
| 187 | + } |
| 188 | + elsif ($ce eq "x-bzip2") { |
| 189 | + require Compress::Bzip2; |
| 190 | + $content_ref = Compress::Bzip2::decompress($$content_ref); |
| 191 | + die "Can't bunzip content" unless defined $$content_ref; |
| 192 | + } |
| 193 | + elsif ($ce eq "deflate") { |
| 194 | + require Compress::Zlib; |
| 195 | + $content_ref = \Compress::Zlib::uncompress($$content_ref); |
| 196 | + die "Can't inflate content" unless defined $$content_ref; |
| 197 | + } |
| 198 | + elsif ($ce eq "compress" || $ce eq "x-compress") { |
| 199 | + die "Can't uncompress content"; |
| 200 | + } |
| 201 | + elsif ($ce eq "base64") { # not really C-T-E, but should be harmless |
| 202 | + require MIME::Base64; |
| 203 | + $content_ref = \MIME::Base64::decode($$content_ref); |
| 204 | + } |
| 205 | + elsif ($ce eq "quoted-printable") { # not really C-T-E, but should be harmless |
| 206 | + require MIME::QuotedPrint; |
| 207 | + $content_ref = \MIME::QuotedPrint::decode($$content_ref); |
| 208 | + } |
| 209 | + else { |
| 210 | + die "Don't know how to decode Content-Encoding '$ce'"; |
| 211 | + } |
209 | 212 | }
|
210 | 213 | }
|
211 |
| - } |
212 | 214 |
|
213 |
| - if ($ct && $ct =~ m,^text/,,) { |
214 |
| - my $charset = $opt{charset} || $ct_param{charset} || $opt{default_charset} || "ISO-8859-1"; |
215 |
| - $charset = lc($charset); |
216 |
| - if ($charset ne "none") { |
217 |
| - require Encode; |
218 |
| - $content_ref = \Encode::decode($charset, $$content_ref, Encode::FB_CROAK()); |
| 215 | + if ($ct && $ct =~ m,^text/,,) { |
| 216 | + my $charset = $opt{charset} || $ct_param{charset} || $opt{default_charset} || "ISO-8859-1"; |
| 217 | + $charset = lc($charset); |
| 218 | + if ($charset ne "none") { |
| 219 | + require Encode; |
| 220 | + $content_ref = \Encode::decode($charset, $$content_ref, Encode::FB_CROAK()); |
| 221 | + } |
219 | 222 | }
|
| 223 | + }; |
| 224 | + if ($@) { |
| 225 | + Carp::croak($@) if $opt{raise_error}; |
| 226 | + return undef; |
220 | 227 | }
|
221 | 228 |
|
222 | 229 | return $opt{ref} ? $content_ref : $$content_ref;
|
@@ -537,6 +544,13 @@ C<none> can used to suppress decoding of the charset.
|
537 | 544 |
|
538 | 545 | This override the default charset of "ISO-8859-1".
|
539 | 546 |
|
| 547 | +=item C<raise_error> |
| 548 | +
|
| 549 | +If TRUE then raise an exception if not able to decode content. Reason |
| 550 | +might be that the specified C<Content-Encoding> or C<charset> is not |
| 551 | +supported. If this option is FALSE, then decode_content() will return |
| 552 | +C<undef> on errors, but will still set $@. |
| 553 | +
|
540 | 554 | =item C<ref>
|
541 | 555 |
|
542 | 556 | If TRUE then a reference to decoded content is returned. This might
|
|
0 commit comments