| 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
| |||||||||||||||||||||
| 18 | # First, normalize things like +2h to # of seconds | ||||||||||||||||||||
| 19 | 16 | ▶ | $time = _parse_duration($time) if $time !~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | 16 | $self->expires($time); | |||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | 88 | ▶ | $self->path('/') unless defined $self->path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 30 | 35 | ▶ | if ($same_site !~ m{^(Strict|Lax|None)$}i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 54 | 37 | ▶ | push @headers, "expires=" . $self->expires if $self->expires; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 37 | ▶ | push @headers, "domain=" . $self->domain if $self->domain; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 37 | ▶ | push @headers, "Secure" if $self->secure; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 37 | ▶ | push @headers, "SameSite=" . $self->same_site if $self->same_site; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | 37 | ▶ | push @headers, 'HttpOnly' unless $no_httponly; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 66 | 88 | ▶ | my @values = | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 117 | 10 | my($amount, $unit) = ($1, $2); | |||||||||||||||||||
| 118 | 10 | ▶ | $unit = lc($unit) unless length($unit) == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 10 | ▶ | if (my $value = $Units{$unit}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 129 | 2 | return $orig_timespec; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 6 | return sprintf "%.0f", $duration + time; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 219 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||