| 1 | package Cookie::Baker; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base qw/Exporter/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=16.5
| |||||||||||||||||||||||||||||
| 10 | 3 | our $VERSION = "0.13"; | |||||||||||||||||||||||||||
| 11 | 3 | our @EXPORT = qw/bake_cookie crush_cookie/; | |||||||||||||||||||||||||||
| 12 | 3 | my $use_pp = $ENV{COOKIE_BAKER_PP}; | |||||||||||||||||||||||||||
| 13 | 3 | ▶ | if (!$use_pp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 14 | 3 | eval { | |||||||||||||||||||||||||||
| 15 | 3 | require Cookie::Baker::XS; | |||||||||||||||||||||||||||
| 16 | 0 | ▶ | if ( $Cookie::Baker::XS::VERSION < $VERSION ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 0 | warn "Cookie::Baker::XS $VERSION is require. fallback to PP version"; | |||||||||||||||||||||||||||
| 18 | 0 | die; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | }; | ||||||||||||||||||||||||||||
| 21 | 3 | $use_pp = !!$@; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 3 | ▶ | if ($use_pp) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 3 | *crush_cookie = \&pp_crush_cookie; | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | else { | ||||||||||||||||||||||||||||
| 27 | 0 | *crush_cookie = \&Cookie::Baker::XS::crush_cookie; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 6 | ▶ | sub valid_path { | ||||||||||||||||||||||||||
|
Subroutine
valid_path: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 6 | my $path = shift; | |||||||||||||||||||||||||||
| 34 | 6 | for my $s (split //, $path) { | |||||||||||||||||||||||||||
| 35 | 8 | my $hex = hex(ord($s)); | |||||||||||||||||||||||||||
| 36 | 8 | ▶ | return 0 if 0x20 > $hex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 8 | ▶ | return 0 if $hex >= 0x7f; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 7 | ▶ | return 0 if $hex == 0x3b; # 0x3b is ';' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 5 | return 1; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 35 | ▶ | sub bake_cookie { | ||||||||||||||||||||||||||
|
Subroutine
bake_cookie: called CC=22 SCAR=30.9
Pod
documented
| |||||||||||||||||||||||||||||
| 44 | 35 | my ($name,$val) = @_; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 35 | ▶ | return '' unless defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 35 | ▶ | my %args = ref $val ? %{$val} : (value => $val); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 35 | ▶ | if ($args{partitioned}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | # enforce SameSite=None; and secure; on CHIPS (Cookies Having Independent Partitioned State) | ||||||||||||||||||||||||||||
| 50 | 2 | $args{samesite} = 'none'; | |||||||||||||||||||||||||||
| 51 | 2 | $args{secure} = 1; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # validation for domain, path and max-age | ||||||||||||||||||||||||||||
| 55 | 35 | ▶ | if (exists $args{domain} && $args{domain} !~ /^[a-zA-Z0-9\.\-]+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"domain"} and not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
MC/DC100%2/2
exists $args{"domain"} and not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
exists $args{"domain"}
not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
Truth table100%3/3
A and not(B)
A= exists $args{"domain"}B= not $args{"domain"} =~ /^[a-zA-Z0-9\.\-]+$/
| |||||||||||||||||||||||||||||
| 56 | 1 | warn "Invalid domain: $args{domain}"; | |||||||||||||||||||||||||||
| 57 | 1 | delete $args{domain}; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 35 | ▶ | if (exists $args{path} && !valid_path($args{path})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"path"} and not valid_path($args{"path"})
MC/DC100%2/2
exists $args{"path"} and not valid_path($args{"path"})
exists $args{"path"}
not valid_path($args{"path"})
Truth table100%3/3
A and not(B)
A= exists $args{"path"}B= not valid_path($args{"path"})
| |||||||||||||||||||||||||||||
| 60 | 1 | warn "Invalid path: $args{path}"; | |||||||||||||||||||||||||||
| 61 | 1 | delete $args{path}; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | 35 | ▶ | if (exists $args{"max-age"} && $args{"max-age"} !~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"max-age"} and not $args{"max-age"} =~ /^\d+$/
MC/DC100%2/2
exists $args{"max-age"} and not $args{"max-age"} =~ /^\d+$/
exists $args{"max-age"}
not $args{"max-age"} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= exists $args{"max-age"}B= not $args{"max-age"} =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 64 | 1 | warn "Invalid max-age: " . $args{"max-age"}; | |||||||||||||||||||||||||||
| 65 | 1 | delete $args{"max-age"}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 35 | ▶ | $name = URI::Escape::uri_escape($name) if $name =~ m![^a-zA-Z\-\._~]!; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 35 | my $cookie = "$name=" . URI::Escape::uri_escape($args{value}) . '; '; | |||||||||||||||||||||||||||
| 70 | 35 | ▶ | $cookie .= 'domain=' . $args{domain} . '; ' if $args{domain}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 35 | ▶ | $cookie .= 'path='. $args{path} . '; ' if $args{path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 35 | ▶ | $cookie .= 'expires=' . _date($args{expires}) . '; ' if exists $args{expires} && defined $args{expires}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"expires"} and defined $args{"expires"}
MC/DC100%2/2
exists $args{"expires"} and defined $args{"expires"}
exists $args{"expires"}
defined $args{"expires"}
Truth table100%3/3
A and B
A= exists $args{"expires"}B= defined $args{"expires"}
| |||||||||||||||||||||||||||||
| 73 | 35 | ▶ | $cookie .= 'max-age=' . $args{"max-age"} . '; ' if exists $args{"max-age"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 35 | ▶ | if (exists $args{samesite} && $args{samesite} =~ m/\A(?:lax|strict|none)\Z/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $args{"samesite"} and $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
MC/DC100%2/2
exists $args{"samesite"} and $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
exists $args{"samesite"}
$args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
Truth table100%3/3
A and B
A= exists $args{"samesite"}B= $args{"samesite"} =~ /\A(?:lax|strict|none)\Z/i
| |||||||||||||||||||||||||||||
| 75 | 6 | $cookie .= 'SameSite=' . ucfirst(lc($args{samesite})) . '; '; | |||||||||||||||||||||||||||
| 76 | # secure flag must be set when SameSite=None | ||||||||||||||||||||||||||||
| 77 | 6 | ▶ | $args{secure} = 1 if $cookie =~ m/SameSite=None; /; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 35 | ▶ | $cookie .= 'secure; ' if $args{secure}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 35 | ▶ | $cookie .= 'HttpOnly; ' if $args{httponly}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 35 | ▶ | $cookie .= 'Partitioned; ' if $args{partitioned}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 35 | substr($cookie,-2,2,''); | |||||||||||||||||||||||||||
| 84 | 35 | $cookie; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 3 | my @MON = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); | |||||||||||||||||||||||||||
| 88 | 3 | my @WDAY = qw( Sun Mon Tue Wed Thu Fri Sat ); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 3 | my %term = ( | |||||||||||||||||||||||||||
| 91 | 's' => 1, | ||||||||||||||||||||||||||||
| 92 | 'm' => 60, | ||||||||||||||||||||||||||||
| 93 | 'h' => 3600, | ||||||||||||||||||||||||||||
| 94 | 'd' => 86400, | ||||||||||||||||||||||||||||
| 95 | 'M' => 86400 * 30, | ||||||||||||||||||||||||||||
| 96 | 'y' => 86400 * 365, | ||||||||||||||||||||||||||||
| 97 | ); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 13 | ▶ | sub _date { | ||||||||||||||||||||||||||
|
Subroutine
_date: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 100 | 13 | my $expires = shift; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 13 | my $expires_at; | |||||||||||||||||||||||||||
| 103 | 13 | ▶ | if ($expires =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 104 | # all numbers -> epoch date | ||||||||||||||||||||||||||||
| 105 | 2 | $expires_at = $expires; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | elsif ( $expires =~ /^([-+]?(?:\d+|\d*\.\d*))([smhdMy]?)/ ) { | ||||||||||||||||||||||||||||
| 108 | 3 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 109 | 9 | ▶ | my $offset = ($term{$2} || 1) * $1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$term{$2} || 1
MC/DC100%1/1
$term{$2} || 1
$term{$2}
Truth table100%2/2
A
A= $term{$2}
| |||||||||||||||||||||||||||||
| 110 | 9 | $expires_at = time + $offset; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | elsif ( $expires eq 'now' ) { | ||||||||||||||||||||||||||||
| 113 | 1 | $expires_at = time; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | else { | ||||||||||||||||||||||||||||
| 116 | 1 | return $expires; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | 12 | my($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($expires_at); | |||||||||||||||||||||||||||
| 119 | 12 | $year += 1900; | |||||||||||||||||||||||||||
| 120 | # (cookies use '-' as date separator, HTTP uses ' ') | ||||||||||||||||||||||||||||
| 121 | 12 | return sprintf("%s, %02d-%s-%04d %02d:%02d:%02d GMT", | |||||||||||||||||||||||||||
| 122 | $WDAY[$wday], $mday, $MON[$mon], $year, $hour, $min, $sec); | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 52 | ▶ | sub pp_crush_cookie { | ||||||||||||||||||||||||||
|
Subroutine
pp_crush_cookie: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 126 | 52 | my $cookie_string = shift; | |||||||||||||||||||||||||||
| 127 | 52 | my $raw_key = shift; | |||||||||||||||||||||||||||
| 128 | 52 | ▶ | return {} unless $cookie_string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 48 | my %results; | |||||||||||||||||||||||||||
| 130 | 48 | my @pairs = grep m/=/, split /; ?/, $cookie_string; | |||||||||||||||||||||||||||
| 131 | 48 | for my $pair ( @pairs ) { | |||||||||||||||||||||||||||
| 132 | # trim leading trailing whitespace | ||||||||||||||||||||||||||||
| 133 | 138 | $pair =~ s/^\s+//; $pair =~ s/\s+$//; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 138 | my ($key, $value) = split( "=", $pair, 2 ); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 138 | ▶ | if (!$raw_key) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 116 | $key = URI::Escape::uri_unescape($key); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Values can be quoted | ||||||||||||||||||||||||||||
| 142 | 138 | ▶ | $value = "" unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 138 | $value =~ s/\A"(.*)"\z/$1/; | |||||||||||||||||||||||||||
| 144 | 138 | $value = URI::Escape::uri_unescape($value); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # Take the first one like CGI.pm or rack do | ||||||||||||||||||||||||||||
| 147 | 138 | ▶ | $results{$key} = $value unless exists $results{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 48 | return \%results; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 3 | 1; | |||||||||||||||||||||||||||
| 153 | __END__ | ||||||||||||||||||||||||||||