File Coverage

blib/lib/Furl/Headers.pm
Criterion Covered Total %
statement 43 62 69.3
branch 9 14 64.2
condition n/a
subroutine 10 20 50.0
pod 15 16 93.7
total 77 112 68.7


line stmt bran cond sub pod time code
1             package Furl::Headers;
2 13     13   96326 use strict;
  13         25  
  13         534  
3 13     13   60 use warnings;
  13         23  
  13         728  
4 13     13   449 use utf8;
  13         286  
  13         96  
5 13     13   329 use Carp ();
  13         17  
  13         12918  
6              
7             sub new {
8 7     7 1 16 my ($class, $headers) = @_; # $headers is HashRef or ArrayRef
9 7         29 my $self = {};
10 7 100       34 if (ref $headers eq 'ARRAY') {
    50          
11 1         2 my @h = @$headers; # copy
12 1         4 while (my ($k, $v) = splice @h, 0, 2) {
13 2         2 push @{$self->{lc $k}}, $v;
  2         9  
14             }
15             }
16             elsif(ref $headers eq 'HASH') {
17 6         27 while (my ($k, $v) = each %$headers) {
18 24 100       24 push @{$self->{$k}}, ref($v) eq 'ARRAY' ? @$v : $v;
  24         92  
19             }
20             }
21             else {
22 0         0 Carp::confess($class . ': $headers must be an ARRAY or HASH reference');
23             }
24              
25 7         85 bless $self, $class;
26             }
27              
28             sub header {
29 6     6 1 1887 my ($self, $key, $new) = @_;
30 6 50       14 if ($new) { # setter
31 0 0       0 $new = [$new] unless ref $new;
32 0         0 $self->{lc $key} = $new;
33 0         0 return;
34             } else {
35 6         15 my $val = $self->{lc $key};
36 6 50       12 return unless $val;
37 6 100       40 return wantarray ? @$val : join(", ", @$val);
38             }
39             }
40              
41             sub remove_header {
42 0     0 1 0 my ($self, $key) = @_;
43 0         0 delete $self->{lc $key};
44             }
45              
46             sub flatten {
47 3     3 1 32 my $self = shift;
48 3         29 my @ret;
49 3         13 while (my ($k, $v) = each %$self) {
50 13         15 for my $e (@$v) {
51 13         31 push @ret, $k, $e;
52             }
53             }
54 3         15 return @ret;
55             }
56              
57             sub keys :method {
58 0     0 1 0 my $self = shift;
59 0         0 keys %$self;
60             }
61 0     0 1 0 sub header_field_names { shift->keys }
62              
63             sub as_string {
64 2     2 1 26 my $self = shift;
65 2         2 my $ret = '';
66 2         9 for my $k (sort keys %$self) {
67 8         7 for my $e (@{$self->{$k}}) {
  8         46  
68 8         16 $ret .= "$k: $e\015\012";
69             }
70             }
71 2         6 return $ret;
72             }
73              
74             sub as_http_headers {
75 0     0 0 0 my ($self, $key) = @_;
76 0         0 require HTTP::Headers;
77 0         0 return HTTP::Headers->new($self->flatten);
78             }
79              
80             # shortcut for popular headers.
81 0     0 1 0 sub referer { [ shift->header( 'Referer' => @_ ) ]->[0] }
82 0     0 1 0 sub expires { [ shift->header( 'Expires' => @_ ) ]->[0] }
83 0     0 1 0 sub last_modified { [ shift->header( 'Last-Modified' => @_ ) ]->[0] }
84 0     0 1 0 sub if_modified_since { [ shift->header( 'If-Modified-Since' => @_ ) ]->[0] }
85 2     2 1 14 sub content_type { [ shift->header( 'Content-Type' => @_ ) ]->[0] }
86 2     2 1 16 sub content_length { [ shift->header( 'Content-Length' => @_ ) ]->[0] }
87 0     0 1   sub content_encoding { [ shift->header( 'Content-Encoding' => @_ ) ]->[0] }
88              
89             sub clone {
90 0     0 1   require Storable;
91 0           Storable::dclone($_[0]);
92             }
93              
94             1;
95             __END__