File Coverage

blib/lib/HTTP/Headers/ActionPack.pm
Criterion Covered Total %
statement 43 49 87.7
branch 14 22 63.6
condition 4 9 44.4
subroutine 13 16 81.2
pod 6 8 75.0
total 80 104 76.9


line stmt bran cond sub pod time code
1             package HTTP::Headers::ActionPack;
2             BEGIN {
3 10     10   500749 $HTTP::Headers::ActionPack::AUTHORITY = 'cpan:STEVAN';
4             }
5             {
6             $HTTP::Headers::ActionPack::VERSION = '0.09';
7             }
8             # ABSTRACT: HTTP Action, Adventure and Excitement
9              
10 10     10   91 use strict;
  10         25  
  10         311  
11 10     10   53 use warnings;
  10         20  
  10         461  
12              
13 10     10   61 use Scalar::Util qw[ blessed ];
  10         20  
  10         1383  
14 10     10   57 use Carp qw[ confess ];
  10         19  
  10         488  
15 10     10   8666 use Module::Runtime qw[ use_module ];
  10         18121  
  10         98  
16              
17             my @DEFAULT_CLASSES = qw[
18             HTTP::Headers::ActionPack::AcceptCharset
19             HTTP::Headers::ActionPack::AcceptLanguage
20             HTTP::Headers::ActionPack::AuthenticationInfo
21             HTTP::Headers::ActionPack::Authorization
22             HTTP::Headers::ActionPack::Authorization::Basic
23             HTTP::Headers::ActionPack::Authorization::Digest
24             HTTP::Headers::ActionPack::DateHeader
25             HTTP::Headers::ActionPack::LinkHeader
26             HTTP::Headers::ActionPack::LinkList
27             HTTP::Headers::ActionPack::MediaType
28             HTTP::Headers::ActionPack::MediaTypeList
29             HTTP::Headers::ActionPack::PriorityList
30             HTTP::Headers::ActionPack::WWWAuthenticate
31             ];
32              
33             my %DEFAULT_MAPPINGS = (
34             'link' => 'HTTP::Headers::ActionPack::LinkList',
35             'content-type' => 'HTTP::Headers::ActionPack::MediaType',
36             'accept' => 'HTTP::Headers::ActionPack::MediaTypeList',
37             'accept-charset' => 'HTTP::Headers::ActionPack::AcceptCharset',
38             'accept-encoding' => 'HTTP::Headers::ActionPack::PriorityList',
39             'accept-language' => 'HTTP::Headers::ActionPack::AcceptLanguage',
40             'date' => 'HTTP::Headers::ActionPack::DateHeader',
41             'client-date' => 'HTTP::Headers::ActionPack::DateHeader', # added by LWP
42             'expires' => 'HTTP::Headers::ActionPack::DateHeader',
43             'last-modified' => 'HTTP::Headers::ActionPack::DateHeader',
44             'if-unmodified-since' => 'HTTP::Headers::ActionPack::DateHeader',
45             'if-modified-since' => 'HTTP::Headers::ActionPack::DateHeader',
46             'www-authenticate' => 'HTTP::Headers::ActionPack::WWWAuthenticate',
47             'authentication-info' => 'HTTP::Headers::ActionPack::AuthenticationInfo',
48             'authorization' => 'HTTP::Headers::ActionPack::Authorization',
49             );
50              
51             sub new {
52 9     9 1 1647 my $class = shift;
53 9         32 my %additional = @_;
54 9         165 my %mappings = ( %DEFAULT_MAPPINGS, %additional );
55 9         46 my %classes = map { $_ => undef } ( @DEFAULT_CLASSES, values %additional );
  117         400  
56              
57 9         97 bless {
58             mappings => \%mappings,
59             classes => \%classes
60             } => $class;
61             }
62              
63 0     0 0 0 sub mappings { (shift)->{'mappings'} }
64 0     0 1 0 sub classes { keys %{ (shift)->{'classes'} } }
  0         0  
65              
66             sub has_mapping {
67 0     0 0 0 my ($self, $header_name) = @_;
68 0 0       0 exists $self->{'mappings'}->{ lc $header_name } ? 1 : 0
69             }
70              
71             sub get_content_negotiator {
72 4     4 1 26 use_module('HTTP::Headers::ActionPack::ContentNegotiation')->new( shift );
73             }
74              
75             sub create {
76 98     98 1 1317 my ($self, $class_name, $args) = @_;
77              
78 98 50       568 my $class = exists $self->{'classes'}->{ $class_name }
    50          
79             ? $class_name
80             : exists $self->{'classes'}->{ __PACKAGE__ . '::' . $class_name }
81             ? __PACKAGE__ . '::' . $class_name
82             : undef;
83              
84 98 50       226 (defined $class)
85             || confess "Could not find class '$class_name' (or 'HTTP::Headers::ActionPack::$class_name')";
86              
87 98 100       367 ref $args
88             ? use_module( $class )->new( @$args )
89             : use_module( $class )->new_from_string( $args );
90             }
91              
92             sub create_header {
93 17     17 1 4225 my ($self, $header_name, $header_value) = @_;
94              
95 17         84 my $class = $self->{'mappings'}->{ lc $header_name };
96              
97 17 50       64 (defined $class)
98             || confess "Could not find mapping for '$header_name'";
99              
100 17 100       483 ref $header_value
101             ? use_module( $class )->new( @$header_value )
102             : use_module( $class )->new_from_string( $header_value );
103             }
104              
105             sub inflate {
106 4     4 1 16121 my $self = shift;
107 4 100       45 return $self->_inflate_http_headers( @_ )
108             if $_[0]->isa('HTTP::Headers');
109 1 0 33     13 return $self->_inflate_generic_request( @_ )
      33        
110             if $_[0]->isa('HTTP::Request')
111             || $_[0]->isa('Plack::Request')
112             || $_[0]->isa('Web::Request');
113 0         0 confess "I don't know how to inflate '$_[0]'";
114             }
115              
116             sub _inflate_http_headers {
117 4     4   13 my ($self, $http_headers) = @_;
118 4         7 foreach my $header ( keys %{ $self->{'mappings'} } ) {
  4         35  
119 60 100       1787 if ( my $old = $http_headers->header( $header ) ) {
120 8 100 66     232 $http_headers->header( $header => $self->create_header( $header, $old ) )
121             unless blessed $old && $old->isa('HTTP::Headers::ActionPack::Core::Base');
122             }
123             }
124 4         130 return $http_headers;
125             }
126              
127             sub _inflate_generic_request {
128 1     1   2 my ($self, $request) = @_;
129 1         10 $self->_inflate_http_headers( $request->headers );
130 1         6 return $request;
131             }
132              
133             1;
134              
135             __END__