File Coverage

blib/lib/Data/Decode/Encode/HTTP/Response.pm
Criterion Covered Total %
statement 21 44 47.7
branch 0 8 0.0
condition 0 5 0.0
subroutine 7 9 77.7
pod 2 2 100.0
total 30 68 44.1


line stmt bran cond sub pod time code
1             # $Id: /mirror/perl/Data-Decode/trunk/lib/Data/Decode/Encode/HTTP/Response.pm 8763 2007-11-06T09:42:32.814221Z daisuke $
2             #
3             # Copyright (c) 2007 Daisuke Maki
4             # All rights reserved.
5              
6             package Data::Decode::Encode::HTTP::Response;
7 2     2   3374 use strict;
  2         6  
  2         238  
8 2     2   13 use warnings;
  2         4  
  2         96  
9 2     2   10 use base qw(Class::Accessor::Fast);
  2         22  
  2         175  
10 2     2   11 use Data::Decode::Exception;
  2         3  
  2         39  
11 2     2   1878 use Encode();
  2         20846  
  2         65  
12 2     2   1121 use Data::Decode::Util qw(try_decode pick_encoding);
  2         6  
  2         152  
13 2     2   1723 use HTTP::Response::Encoding;
  2         1334  
  2         1031  
14              
15             __PACKAGE__->mk_accessors($_) for qw(_parser);
16              
17             sub decode
18             {
19 0     0 1   my ($self, $decoder, $string, $hints) = @_;
20              
21 0 0 0       if (! $hints->{response} || ! eval { $hints->{response}->isa('HTTP::Response') }) {
  0            
22 0           Data::Decode::Exception::Deferred->throw;
23             }
24 0           my $res = $hints->{response};
25              
26 0           my $decoded;
27             { # Attempt to decode from header information
28 0   0       my $encoding = pick_encoding(
  0            
29             $res->encoding,
30             ( ($res->header('Content-Type') || '') =~ /charset=([\w\-]+)/g),
31             );
32 0           $decoded = try_decode($encoding, $string);
33 0 0         return $decoded if $decoded;
34             }
35              
36             { # Attempt to decode from meta information
37 0           my $p = $self->parser();
  0            
38 0           my $encoding = pick_encoding(
39             $p->extract_encodings( $res->content )
40             );
41              
42 0           $decoded = try_decode($encoding, $string);
43 0 0         return $decoded if $decoded;
44             }
45              
46 0           Data::Decode::Exception::Deferred->throw;
47             }
48              
49             sub parser
50             {
51 0     0 1   my $self = shift;
52 0           my $parser = $self->_parser();
53 0 0         if (! $parser) {
54 0           require Data::Decode::Encode::HTTP::Response::Parser;
55 0           $parser = Data::Decode::Encode::HTTP::Response::Parser->new();
56 0           $self->_parser($parser);
57             }
58 0           return $parser;
59             }
60              
61             1;
62              
63             __END__