lib/Dancer/Cookie.pm

filter: Statementstmt 95.9% 71 / 74 Branchbran 90.4% 38 / 42 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 94.7% 127 / 134 CC 23 SCAR 17.3
CC
23
Cov
94%
to_header
8.0
init
7.0
_parse_duration
6.1
CRAP 5.7
« lib/Dancer/Request.pm ↑ Summary lib/Dancer/Engine.pm »
1 package Dancer::Cookie;
2 219 our $AUTHORITY = 'cpan:SUKRIA';
3 #ABSTRACT: class representing cookies
4 219 $Dancer::Cookie::VERSION = '1.3522';
5 219 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 219 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 219 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 219 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 219 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 219 __PACKAGE__->attributes( qw/name expires domain path same_site secure http_only/ );
13  
14 88 ▶sub init {
Subroutine init: called CC=7 SCAR=19.5
Pod documented
15 88 my ($self, %args) = @_;
16 88 $self->value($args{value});
17 88 ▶ if (my $time = $self->expires) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $time = $self->expires) 16 72 88
18 # First, normalize things like +2h to # of seconds
19 16 ▶ $time = _parse_duration($time) if $time !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $time =~ /^\d+$/ 8 8 16
20  
21 # Then translate to a gmt string, if it isn't one already
22 16 ▶ $time = _epoch_to_gmtstring($time) if $time =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $time =~ /^\d+$/ 14 2 16
23  
24 16 $self->expires($time);
25 }
26 88 ▶ $self->path('/') unless defined $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->path 87 1 88
27  
28 # If we have a same_site attribute, ensure it's sane:
29 88 ▶ if (my $same_site = $self->same_site) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $same_site = $self->same_site) 35 53 88
30 35 ▶ if ($same_site !~ m{^(Strict|Lax|None)$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $same_site =~ /^(Strict|Lax|None)$/i) { } 0 35 35
31 0 Carp::croak(
32 "Invalid same_site value '$same_site'"
33 . " - must be 'Strict', 'Lax' or 'None', see RFC6265bis"
34 );
35 } else {
36 # Normalise case
37 35 $self->same_site(ucfirst lc $same_site);
38 }
39 }
40 }
41  
42 37 ▶sub to_header {
Subroutine to_header: called CC=8 SCAR=20.8
Pod documented
43 37 my $self = shift;
44 37 my $header = '';
45  
46 37 my $value = join('&', map {uri_escape($_)} $self->value);
47 37 ▶ my $no_httponly = defined( $self->http_only ) && $self->http_only == 0;
Condition100%3/3
defined $self->http_only && $self->http_only == 0
!ll&&!rl&&r
25102
MC/DC100%2/2
defined $self->http_only && $self->http_only == 0
defined $self->http_only $self->http_only == 0
Truth table100%3/3
A && B
A= defined $self->http_onlyB= $self->http_only == 0
ABresult
0X0
100
111
48  
49 37 my $name = $self->name;
50 37 $name =~ s/[=,; \t\r\n\013\014]//mg;
51  
52 37 my @headers = $name . '=' . $value;
53 37 ▶ push @headers, "path=" . $self->path if $self->path;
Branch: 1 branch
BranchTrueFalseTotal
if $self->path 37 0 37
54 37 ▶ push @headers, "expires=" . $self->expires if $self->expires;
Branch: 1 branch
BranchTrueFalseTotal
if $self->expires 16 21 37
55 37 ▶ push @headers, "domain=" . $self->domain if $self->domain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->domain 1 36 37
56 37 ▶ push @headers, "Secure" if $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->secure 1 36 37
57 37 ▶ push @headers, "SameSite=" . $self->same_site if $self->same_site;
Branch: 1 branch
BranchTrueFalseTotal
if $self->same_site 10 27 37
58 37 ▶ push @headers, 'HttpOnly' unless $no_httponly;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_httponly 35 2 37
59  
60 37 return join '; ', @headers;
61 }
62  
63 484 ▶sub value {
Subroutine value: called CC=5 SCAR=16.1
Pod documented
64 484 my ( $self, $value ) = @_;
65 484 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 88 396 484
66 88 ▶ my @values =
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 21 67 88
ref $value eq 'HASH' ? : 1 66 67
67 ref $value eq 'ARRAY' ? @$value
68 : ref $value eq 'HASH' ? %$value
69 : ($value);
70 88 $self->{'value'} = [@values];
71 }
72 484 ▶ return wantarray ? @{ $self->{'value'} } : $self->{'value'}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 181 303 484
73 }
74  
75 14 ▶sub _epoch_to_gmtstring {
Subroutine _epoch_to_gmtstring: called CC=1 SCAR=0.0
76 14 my ($epoch) = @_;
77  
78 14 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($epoch);
79 14 my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
80 14 my @days = qw(Sun Mon Tue Wed Thu Fri Sat);
81  
82 14 return sprintf "%s, %02d-%s-%d %02d:%02d:%02d GMT",
83 $days[$wday],
84 $mday,
85 $months[$mon],
86 ($year + 1900),
87 $hour, $min, $sec;
88 }
89  
90 # This map is taken from Cache and Cache::Cache
91 # map of expiration formats to their respective time in seconds
92 219 my %Units = ( map(($_, 1), qw(s second seconds sec secs)),
93 map(($_, 60), qw(m minute minutes min mins)),
94 map(($_, 60*60), qw(h hr hour hours)),
95 map(($_, 60*60*24), qw(d day days)),
96 map(($_, 60*60*24*7), qw(w week weeks)),
97 map(($_, 60*60*24*30), qw(M month months)),
98 map(($_, 60*60*24*365), qw(y year years)) );
99  
100 # This code is taken from Time::Duration::Parse, except if it isn't
101 # understood it just passes it through and it adds the current time.
102 8 ▶sub _parse_duration {
Subroutine _parse_duration: called CC=6 SCAR=18.1
103 8 my $timespec = shift;
104 8 my $orig_timespec = $timespec;
105  
106 # Treat a plain number as a number of seconds (and parse it later)
107 8 ▶ if ($timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/) 0 8 8
108 0 $timespec = "$1s";
109 }
110  
111 # Convert hh:mm(:ss)? to something we understand
112 8 $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g;
113 8 $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g;
114  
115 8 my $duration = 0;
116 8 ▶ while ($timespec =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) 10 8 18
117 10 my($amount, $unit) = ($1, $2);
118 10 ▶ $unit = lc($unit) unless length($unit) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $unit == 1 8 2 10
119  
120 10 ▶ if (my $value = $Units{$unit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $Units{$unit}) { } 10 0 10
121 10 $amount =~ s/,/./;
122 10 $duration += $amount * $value;
123 } else {
124 0 return $orig_timespec;
125 }
126 }
127  
128 8 ▶ if ($timespec =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timespec =~ /\S/) 2 6 8
129 2 return $orig_timespec;
130 }
131  
132 6 return sprintf "%.0f", $duration + time;
133 }
134  
135  
136 219 1;
137  
138 __END__
« lib/Dancer/Request.pm ↑ Summary lib/Dancer/Engine.pm »