File Coverage

blib/lib/APR/HTTP/Headers/Compat.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package APR::HTTP::Headers::Compat;
2              
3 2     2   24369 use warnings;
  2         3  
  2         56  
4 2     2   9 use strict;
  2         3  
  2         55  
5              
6 2     2   9 use Carp;
  2         7  
  2         171  
7 2     2   1355 use APR::HTTP::Headers::Compat::MagicHash;
  0            
  0            
8              
9             use base qw( HTTP::Headers );
10              
11             =head1 NAME
12              
13             APR::HTTP::Headers::Compat - Make an APR::Table look like an HTTP::Headers
14              
15             =head1 VERSION
16              
17             This document describes APR::HTTP::Headers::Compat version 0.02
18              
19             =cut
20              
21             our $VERSION = '0.02';
22              
23             =head1 SYNOPSIS
24              
25             use APR::HTTP::Headers::Compat;
26              
27             # We're running under mod_perl2...
28             my $hdrs = APR::HTTP::Headers::Compat->new( $r->headers_out );
29              
30             # Now we can treat $hdrs as if it was an HTTP::Headers
31             $hdrs->header( 'Content-Type' => 'text/plain' );
32              
33             =head1 DESCRIPTION
34              
35             Under mod_perl HTTP headers are stashed in L objects.
36             Sometimes you will encounter code (such as L) that
37             needs an L. This module wraps an C in a
38             subclass of C so that it can be used wherever an
39             C is expected.
40              
41             Synchronisation is bi-directional; changes via the C
42             interface are reflected immediately in the underlying C and
43             direct changes to the table show up immediately in the wrapper.
44              
45             =head1 INTERFACE
46              
47             Unless otherwise stated below all methods are inherited from
48             C.
49              
50             =head2 C<< new >>
51              
52             Create a new wrapper around an existing C.
53              
54             # Normally you'll be given the table - we're creating one here for the
55             # sake of the example
56             my $table = APR::Table::make( APR::Pool->new, 1 );
57              
58             # Wrap the table so it can be used as an HTTP::Headers instance
59             my $h = APR::HTTP::Headers::Compat->new( $table );
60              
61             Optionally header initialisers may be passed:
62              
63             my $h = APR::HTTP::Headers::Compat->new( $table,
64             'Content-type' => 'text/plain'
65             );
66              
67             =cut
68              
69             sub new {
70             my ( $class, $table ) = ( shift, shift );
71             my %self = %{ $class->SUPER::new( @_ ) };
72             tie %self, 'APR::HTTP::Headers::Compat::MagicHash', $table, %self;
73             return bless \%self, $class;
74             }
75              
76             sub _magic { tied %{ shift() } }
77              
78             =head2 C<< clone >>
79              
80             Clone this object. The clone is a regular L object rather
81             than an C.
82              
83             =cut
84              
85             sub clone { bless { %{ shift() } }, 'HTTP::Headers' }
86              
87             =head2 C<< table >>
88              
89             Get the underlying L object. Changes made in either the
90             table or the wrapper are reflected immediately in the other.
91              
92             =cut
93              
94             sub table { shift->_magic->table }
95              
96             =head2 C<< remove_content_headers >>
97              
98             This will remove all the header fields used to describe the content of a
99             message. All header field names prefixed with Content- falls into this
100             category, as well as Allow, Expires and Last-Modified. RFC 2616 denote
101             these fields as Entity Header Fields.
102              
103             The return value is a new C object that contains the
104             removed headers only. Note that the returned object is I an
105             C.
106              
107             =cut
108              
109             sub remove_content_headers {
110             my $self = shift;
111              
112             return $self->SUPER::remove_content_headers( @_ )
113             unless defined wantarray;
114              
115             # This gets nasty. We downbless ourself to be an HTTP::Headers so that
116             # when HTTP::Headers->remove_content_headers does
117             #
118             # my $c = ref( $self )->new
119             #
120             # it creates a new HTTP::Headers instead of attempting to create a
121             # new APR::HTTP::Headers::Compat.
122              
123             my $class = ref $self;
124             bless $self, 'HTTP::Headers';
125            
126             # Calls SUPER::remove_content_headers due to rebless
127             my $other = $self->remove_content_headers( @_ );
128             bless $self, $class;
129              
130             # Return a non-magic HTTP::Headers
131             return $other;
132             }
133              
134             1;
135             __END__