File Coverage

blib/lib/Net/HTTP2/Response.pm
Criterion Covered Total %
statement 28 31 90.3
branch 9 10 90.0
condition 0 3 0.0
subroutine 6 8 75.0
pod 4 5 80.0
total 47 57 82.4


line stmt bran cond sub pod time code
1             package Net::HTTP2::Response;
2              
3 1     1   7 use strict;
  1         2  
  1         26  
4 1     1   4 use warnings;
  1         2  
  1         31  
5              
6             =encoding utf-8
7              
8             =head1 NAME
9              
10             Net::HTTP2::Response - HTTP/2 Response
11              
12             =head1 DESCRIPTION
13              
14             This class represents an HTTP/2 response.
15              
16             =cut
17              
18             #----------------------------------------------------------------------
19              
20             use constant {
21 1         322 _HEADERS_AR => 0,
22             _DATA => 1,
23             _STATUS => 2,
24             _HEADERS_HR => 3,
25 1     1   5 };
  1         1  
26              
27             #----------------------------------------------------------------------
28              
29             =head1 METHODS
30              
31             =cut
32              
33             # Not called publicly.
34             sub new {
35 2     2 0 7 my ($class, $headers_ar, $data) = @_;
36              
37 2         15 return bless [$headers_ar, $data], $class;
38             }
39              
40             =head2 $str = I->content()
41              
42             Returns the response payload, or undef if the payload was
43             delivered to an C handler. (See L’s
44             C method.)
45              
46             =cut
47              
48 0     0 1 0 sub content { $_[0][ _DATA ] }
49              
50             =head2 $num = I->status()
51              
52             Returns the (numeric) HTTP statis.
53              
54             (NB: HTTP/2 doesn’t have response status strings as HTTP/1 has.)
55              
56             =cut
57              
58             sub status {
59 2 50   2 1 19 $_[0]->headers() if !defined $_[0][ _STATUS ];
60 2         6 return $_[0][ _STATUS ];
61             }
62              
63             =head2 $yn = I->success()
64              
65             Returns a boolean that indicates whether the response indicates
66             success.
67              
68             =cut
69              
70             sub success {
71 0     0 1 0 my $status = $_[0]->status();
72              
73 0   0     0 return ($status >= 200) && ($status <= 299);
74             }
75              
76             =head2 $hr = I->headers()
77              
78             Returns a hash reference similar to that in
79             L’s method of the same name.
80              
81             =cut
82              
83             sub headers {
84 3 100   3 1 8 if (!$_[0][ _HEADERS_HR ]) {
85 2         5 my $headers_ar = $_[0][ _HEADERS_AR ];
86              
87 2         3 my %headers;
88 2         6 $_[0][ _HEADERS_HR ] = \%headers;
89              
90 2         10 for (my $h=0; $h < @$headers_ar; $h+=2) {
91 28         35 my ($name, $value) = @{$headers_ar}[ $h, $h+1 ];
  28         42  
92              
93 28 100       54 if ($name eq ':status') {
    100          
94 2         8 $_[0][ _STATUS ] = $value;
95             }
96             elsif (exists $headers{$name}) {
97 2 100       7 if ('ARRAY' eq ref $headers{$name}) {
98 1         2 push @{$headers{$name}}, $value;
  1         4  
99             }
100             else {
101 1         5 $headers{$name} = [ $headers{$name}, $value ];
102             }
103             }
104             else {
105 24         54 $headers{$name} = $value;
106             }
107             }
108             }
109              
110 3         10 return $_[0][ _HEADERS_HR ];
111             }
112              
113             1;
114