| 1 | package Dancer::Cookie; | ||||||||||||||||||||
| 2 | 208 | our $AUTHORITY = 'cpan:SUKRIA'; | |||||||||||||||||||
| 3 | #ABSTRACT: class representing cookies | ||||||||||||||||||||
| 4 | 208 | $Dancer::Cookie::VERSION = '1.3513'; | |||||||||||||||||||
| 5 | 208 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 208 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 208 | ▶ | use URI::Escape; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 208 | ▶ | use base 'Dancer::Object'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 208 | __PACKAGE__->attributes( qw/name expires domain path secure http_only/ ); | |||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | sub init { | ||||||||||||||||||||
| 14 | 86 | ▶ | my ($self, %args) = @_; | ||||||||||||||||||
|
Subroutine
init: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 15 | 86 | $self->value($args{value}); | |||||||||||||||||||
| 16 | 86 | ▶ | if (my $time = $self->expires) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 17 | # First, normalize things like +2h to # of seconds | ||||||||||||||||||||
| 18 | 16 | ▶ | $time = _parse_duration($time) if $time !~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | # Then translate to a gmt string, if it isn't one already | ||||||||||||||||||||
| 21 | 16 | ▶ | $time = _epoch_to_gmtstring($time) if $time =~ /^\d+$/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 16 | $self->expires($time); | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | 86 | ▶ | $self->path('/') unless defined $self->path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 26 | } | ||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | sub to_header { | ||||||||||||||||||||
| 29 | 37 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
to_header: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 30 | 37 | my $header = ''; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 37 | my $value = join('&', map {uri_escape($_)} $self->value); | |||||||||||||||||||
| 33 | 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
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 37 | my $name = $self->name; | |||||||||||||||||||
| 36 | 37 | $name =~ s/[=,; \t\r\n\013\014]//mg; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 37 | my @headers = $name . '=' . $value; | |||||||||||||||||||
| 39 | 37 | ▶ | push @headers, "path=" . $self->path if $self->path; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 40 | 37 | ▶ | push @headers, "expires=" . $self->expires if $self->expires; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 37 | ▶ | push @headers, "domain=" . $self->domain if $self->domain; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 42 | 37 | ▶ | push @headers, "Secure" if $self->secure; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 37 | ▶ | push @headers, 'HttpOnly' unless $no_httponly; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 37 | return join '; ', @headers; | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | sub value { | ||||||||||||||||||||
| 49 | 478 | ▶ | my ( $self, $value ) = @_; | ||||||||||||||||||
|
Subroutine
value: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||
| 50 | 478 | ▶ | if ( defined $value ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 51 | 86 | ▶ | my @values = | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 52 | ref $value eq 'ARRAY' ? @$value | ||||||||||||||||||||
| 53 | : ref $value eq 'HASH' ? %$value | ||||||||||||||||||||
| 54 | : ($value); | ||||||||||||||||||||
| 55 | 86 | $self->{'value'} = [@values]; | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | 478 | ▶ | return wantarray ? @{ $self->{'value'} } : $self->{'value'}->[0]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | sub _epoch_to_gmtstring { | ||||||||||||||||||||
| 61 | 14 | ▶ | my ($epoch) = @_; | ||||||||||||||||||
|
Subroutine
_epoch_to_gmtstring: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 14 | my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($epoch); | |||||||||||||||||||
| 64 | 14 | my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); | |||||||||||||||||||
| 65 | 14 | my @days = qw(Sun Mon Tue Wed Thu Fri Sat); | |||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 14 | return sprintf "%s, %02d-%s-%d %02d:%02d:%02d GMT", | |||||||||||||||||||
| 68 | $days[$wday], | ||||||||||||||||||||
| 69 | $mday, | ||||||||||||||||||||
| 70 | $months[$mon], | ||||||||||||||||||||
| 71 | ($year + 1900), | ||||||||||||||||||||
| 72 | $hour, $min, $sec; | ||||||||||||||||||||
| 73 | } | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # This map is taken from Cache and Cache::Cache | ||||||||||||||||||||
| 76 | # map of expiration formats to their respective time in seconds | ||||||||||||||||||||
| 77 | 208 | my %Units = ( map(($_, 1), qw(s second seconds sec secs)), | |||||||||||||||||||
| 78 | map(($_, 60), qw(m minute minutes min mins)), | ||||||||||||||||||||
| 79 | map(($_, 60*60), qw(h hr hour hours)), | ||||||||||||||||||||
| 80 | map(($_, 60*60*24), qw(d day days)), | ||||||||||||||||||||
| 81 | map(($_, 60*60*24*7), qw(w week weeks)), | ||||||||||||||||||||
| 82 | map(($_, 60*60*24*30), qw(M month months)), | ||||||||||||||||||||
| 83 | map(($_, 60*60*24*365), qw(y year years)) ); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | # This code is taken from Time::Duration::Parse, except if it isn't | ||||||||||||||||||||
| 86 | # understood it just passes it through and it adds the current time. | ||||||||||||||||||||
| 87 | sub _parse_duration { | ||||||||||||||||||||
| 88 | 8 | ▶ | my $timespec = shift; | ||||||||||||||||||
|
Subroutine
_parse_duration: called CC=6 SCAR=18.1
| |||||||||||||||||||||
| 89 | 8 | my $orig_timespec = $timespec; | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # Treat a plain number as a number of seconds (and parse it later) | ||||||||||||||||||||
| 92 | 8 | ▶ | if ($timespec =~ /^\s*([-+]?\d+(?:[.,]\d+)?)\s*$/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 0 | $timespec = "$1s"; | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | # Convert hh:mm(:ss)? to something we understand | ||||||||||||||||||||
| 97 | 8 | $timespec =~ s/\b(\d+):(\d\d):(\d\d)\b/$1h $2m $3s/g; | |||||||||||||||||||
| 98 | 8 | $timespec =~ s/\b(\d+):(\d\d)\b/$1h $2m/g; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 8 | my $duration = 0; | |||||||||||||||||||
| 101 | 8 | ▶ | while ($timespec =~ s/^\s*([-+]?\d+(?:[.,]\d+)?)\s*([a-zA-Z]+)(?:\s*(?:,|and)\s*)*//i) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 10 | my($amount, $unit) = ($1, $2); | |||||||||||||||||||
| 103 | 10 | ▶ | $unit = lc($unit) unless length($unit) == 1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 10 | ▶ | if (my $value = $Units{$unit}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 10 | $amount =~ s/,/./; | |||||||||||||||||||
| 107 | 10 | $duration += $amount * $value; | |||||||||||||||||||
| 108 | } else { | ||||||||||||||||||||
| 109 | 0 | return $orig_timespec; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | } | ||||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 8 | ▶ | if ($timespec =~ /\S/) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 2 | return $orig_timespec; | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 6 | return sprintf "%.0f", $duration + time; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | 208 | 1; | |||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | __END__ | ||||||||||||||||||||