Easy/Headers.pm

filter: Statementstmt 87.6% 57 / 65 Branchbran 41.6% 5 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 79.5% 78 / 98 CC 12 SCAR 16.3
CC
12
Cov
79%
compose
9.4
require_basic_auth
6.0
new
2.1
CRAP 5.1
« Easy/Util.pm ↑ Summary Easy/Session.pm »
1 package CGI::Easy::Headers;
2 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 'v2.0.1';
9  
10 2 ▶use CGI::Easy::Util qw( date_http make_cookie );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 11 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
14 11 my ($class, $opt) = @_;
15 my $self = {
16 'Status' => '200 OK',
17 'Content-Type' => 'text/html; charset=utf-8',
18 'Date' => q{},
19 'Set-Cookie' => [],
20 0 ▶ $opt ? %{$opt} : (),
Branch: 1 branch
BranchTrueFalseTotal
$opt ? : 0 11 11
21 };
22 11 return bless $self, $class;
23 }
24  
25 11 ▶sub add_cookie {
Subroutine add_cookie: called CC=1 SCAR=0.0
Pod documented
26 11 my ($self, @cookies) = @_;
27 11 push @{ $self->{'Set-Cookie'} }, @cookies;
28 11 return;
29 }
30  
31 1 ▶sub redirect {
Subroutine redirect: called CC=2 SCAR=7.0
Pod documented
32 1 my ($self, $url, $status) = @_;
33 1 $self->{'Location'} = $url;
34 1 ▶ if (!defined $status) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $status) 1 0 1
35 1 $status = '302 Found';
36 }
37 1 $self->{'Status'} = $status;
38 1 return;
39 }
40  
41 0 ▶sub require_basic_auth {
Subroutine require_basic_auth: not called CC=2 SCAR=17.9
Pod documented
42 0 my ($self, $realm) = @_;
43 0 ▶ if (!defined $realm) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $realm) 0 0 0
44 0 $realm = q{};
45 }
46 0 $self->{'WWW-Authenticate'} = "Basic realm=\"$realm\"";
47 0 $self->{'Status'} = '401 Authorization Required';
48 0 return;
49 }
50  
51 6 ▶sub compose {
Subroutine compose: called CC=9 SCAR=22.4
Pod documented
52 6 my ($self) = @_;
53 6 my %h = %{$self};
54 6 for my $header (keys %h) {
55 26 my $expect = join q{-}, map {ucfirst lc} split /-/xms, $header;
56 26 $expect =~ s/\bEtag\b/ETag/xms;
57 26 $expect =~ s/\bWww\b/WWW/xms;
58 26 $expect =~ s/\bMd5\b/MD5/xms;
59 26 ▶ if ($header ne $expect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header ne $expect) 0 26 26
60 0 croak "Bad header name '$header' (should be '$expect')";
61 }
62 }
63  
64 6 my $s = sprintf "Status: %s\r\n", delete $h{'Status'};
65 6 $s .= sprintf "Content-Type: %s\r\n", delete $h{'Content-Type'};
66 6 my $date = delete $h{'Date'};
67 6 ▶ if (defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date) 6 0 6
68 6 ▶ $s .= sprintf "Date: %s\r\n", $date || date_http(time);
Condition33%1/3
$date || date_http(time)
l!l&&r!l&&!r
060
MC/DC0%0/2
$date || date_http(time)
$date date_http(time)
Truth table33%1/3
A || B
A= $dateB= date_http(time)
ABresult
1X1
011
000
69 }
70 6 for my $cookie (@{ delete $h{'Set-Cookie'} }) {
71 12 $s .= make_cookie($cookie);
72 }
73 6 for my $header (keys %h) {
74 2 ▶ if (!ref $h{$header}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $h{$header}) 2 0 2
75 2 $h{$header} = [ $h{$header} ];
76 }
77 2 for my $value (@{ $h{$header} }) {
78 2 $s .= sprintf "%s: %s\r\n", $header, $value;
79 }
80 }
81  
82 6 return $s . "\r\n";
83 }
84  
85  
86 2 1; # Magic true value required at end of module
87 __END__
« Easy/Util.pm ↑ Summary Easy/Session.pm »