| 1 |
package Dancer2::Core::Cookie; |
| 2 |
# ABSTRACT: A cookie representing class |
| 3 |
$Dancer2::Core::Cookie::VERSION = '2.2.2'; |
| 4 |
use Moo; |
| 5 |
use URI::Escape; |
| 6 |
use Dancer2::Core::Types; |
| 7 |
use Dancer2::Core::Time; |
| 8 |
use Carp 'croak'; |
| 9 |
use Ref::Util qw< is_arrayref is_hashref >; |
| 10 |
use overload '""' => \&_get_value; |
| 11 |
|
| 12 |
BEGIN { |
| 13 |
my $try_xs = |
| 14 |
exists($ENV{PERL_HTTP_XSCOOKIES}) ? !!$ENV{PERL_HTTP_XSCOOKIES} : |
| 15 |
exists($ENV{PERL_ONLY}) ? !$ENV{PERL_ONLY} : |
| 16 |
1; |
| 17 |
|
| 18 |
my $use_xs = 0; |
| 19 |
$try_xs and eval { |
| 20 |
require HTTP::XSCookies; |
| 21 |
$use_xs++; |
| 22 |
}; |
| 23 |
if ( $use_xs ) { |
| 24 |
*to_header = \&xs_to_header; |
| 25 |
} |
| 26 |
else { |
| 27 |
*to_header = \&pp_to_header; |
| 28 |
} |
| 29 |
*_USE_XS = $use_xs ? sub () { !!1 } : sub () { !!0 }; |
| 30 |
} |
| 31 |
|
| 32 |
sub xs_to_header { |
| 33 |
my $self = shift; |
| 34 |
|
| 35 |
# HTTP::XSCookies can't handle multi-value cookies. |
| 36 |
return $self->pp_to_header(@_) if @{[ $self->value ]} > 1; |
| 37 |
|
| 38 |
return HTTP::XSCookies::bake_cookie( |
| 39 |
$self->name, |
| 40 |
{ value => $self->value, |
| 41 |
path => $self->path, |
| 42 |
domain => $self->domain, |
| 43 |
expires => $self->expires, |
| 44 |
httponly => $self->http_only, |
| 45 |
secure => $self->secure, |
| 46 |
samesite => $self->same_site, |
| 47 |
} |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
sub pp_to_header { |
| 52 |
my $self = shift; |
| 53 |
|
| 54 |
my $value = join( '&', map uri_escape($_), $self->value ); |
| 55 |
my $no_httponly = defined( $self->http_only ) && $self->http_only == 0; |
| 56 |
|
| 57 |
my @headers = $self->name . '=' . $value; |
| 58 |
push @headers, "Path=" . $self->path if $self->path; |
| 59 |
push @headers, "Expires=" . $self->expires if $self->expires; |
| 60 |
push @headers, "Domain=" . $self->domain if $self->domain; |
| 61 |
push @headers, "SameSite=" . $self->same_site if $self->same_site; |
| 62 |
push @headers, "Secure" if $self->secure; |
| 63 |
push @headers, 'HttpOnly' unless $no_httponly; |
| 64 |
|
| 65 |
return join '; ', @headers; |
| 66 |
} |
| 67 |
|
| 68 |
has value => ( |
| 69 |
is => 'rw', |
| 70 |
isa => ArrayRef, |
| 71 |
required => 0, |
| 72 |
coerce => sub { |
| 73 |
my $value = shift; |
| 74 |
my @values = |
| 75 |
is_arrayref($value) ? @$value |
| 76 |
: is_hashref($value) ? %$value |
| 77 |
: ($value); |
| 78 |
return [@values]; |
| 79 |
}, |
| 80 |
); |
| 81 |
|
| 82 |
around value => sub { |
| 83 |
my $orig = shift; |
| 84 |
my $self = shift; |
| 85 |
my $array = $orig->( $self, @_ ); |
| 86 |
return wantarray ? @$array : $array->[0]; |
| 87 |
}; |
| 88 |
|
| 89 |
sub values { |
| 90 |
my $self = shift; |
| 91 |
return @{ $self->{'value'} || [] }; |
| 92 |
} |
| 93 |
|
| 94 |
# this is only for overloading; need a real sub to refer to, as the Moose |
| 95 |
# attribute accessor won't be available at that point. |
| 96 |
sub _get_value { shift->value } |
| 97 |
|
| 98 |
has name => ( |
| 99 |
is => 'rw', |
| 100 |
isa => Str, |
| 101 |
required => 1, |
| 102 |
); |
| 103 |
|
| 104 |
has expires => ( |
| 105 |
is => 'rw', |
| 106 |
isa => Str, |
| 107 |
required => 0, |
| 108 |
coerce => sub { |
| 109 |
Dancer2::Core::Time->new( expression => $_[0] )->gmt_string; |
| 110 |
}, |
| 111 |
); |
| 112 |
|
| 113 |
has domain => ( |
| 114 |
is => 'rw', |
| 115 |
isa => Str, |
| 116 |
required => 0, |
| 117 |
); |
| 118 |
|
| 119 |
has path => ( |
| 120 |
is => 'rw', |
| 121 |
isa => Str, |
| 122 |
default => sub {'/'}, |
| 123 |
predicate => 1, |
| 124 |
); |
| 125 |
|
| 126 |
has secure => ( |
| 127 |
is => 'rw', |
| 128 |
isa => Bool, |
| 129 |
required => 0, |
| 130 |
default => sub {0}, |
| 131 |
); |
| 132 |
|
| 133 |
has http_only => ( |
| 134 |
is => 'rw', |
| 135 |
isa => Bool, |
| 136 |
required => 0, |
| 137 |
default => sub {1}, |
| 138 |
); |
| 139 |
|
| 140 |
has same_site => ( |
| 141 |
is => 'rw', |
| 142 |
isa => Enum[qw[Strict Lax None]], |
| 143 |
required => 0, |
| 144 |
); |
| 145 |
|
| 146 |
1; |
| 147 |
|
| 148 |
__END__ |
| 149 |
|
| 150 |
=pod |
| 151 |
|
| 152 |
=encoding UTF-8 |
| 153 |
|
| 154 |
=head1 NAME |
| 155 |
|
| 156 |
Dancer2::Core::Cookie - A cookie representing class |
| 157 |
|
| 158 |
=head1 VERSION |
| 159 |
|
| 160 |
version 2.2.2 |
| 161 |
|
| 162 |
=head1 SYNOPSIS |
| 163 |
|
| 164 |
use Dancer2::Core::Cookie; |
| 165 |
|
| 166 |
my $cookie = Dancer2::Core::Cookie->new( |
| 167 |
name => $cookie_name, value => $cookie_value |
| 168 |
); |
| 169 |
|
| 170 |
my $value = $cookie->value; |
| 171 |
|
| 172 |
print "$cookie"; # objects stringify to their value. |
| 173 |
|
| 174 |
=head1 DESCRIPTION |
| 175 |
|
| 176 |
Dancer2::Core::Cookie provides a HTTP cookie object to work with cookies. |
| 177 |
|
| 178 |
=head1 ATTRIBUTES |
| 179 |
|
| 180 |
=head2 value |
| 181 |
|
| 182 |
The cookie's value. |
| 183 |
|
| 184 |
(Note that cookie objects use overloading to stringify to their value, so if |
| 185 |
you say e.g. return "Hi, $cookie", you'll get the cookie's value there.) |
| 186 |
|
| 187 |
In list context, returns a list of potentially multiple values; in scalar |
| 188 |
context, returns just the first value. (So, if you expect a cookie to have |
| 189 |
multiple values, use list context.) |
| 190 |
|
| 191 |
=head2 name |
| 192 |
|
| 193 |
The cookie's name. |
| 194 |
|
| 195 |
=head2 expires |
| 196 |
|
| 197 |
The cookie's expiration date. There are several formats. |
| 198 |
|
| 199 |
Unix epoch time like 1288817656 to mean "Wed, 03-Nov-2010 20:54:16 GMT" |
| 200 |
|
| 201 |
It also supports a human readable offset from the current time such as "2 hours". |
| 202 |
See the documentation of L<Dancer2::Core::Time> for details of all supported |
| 203 |
formats. |
| 204 |
|
| 205 |
=head2 domain |
| 206 |
|
| 207 |
The cookie's domain. |
| 208 |
|
| 209 |
=head2 path |
| 210 |
|
| 211 |
The cookie's path. |
| 212 |
|
| 213 |
=head2 secure |
| 214 |
|
| 215 |
If true, it instructs the client to only serve the cookie over secure |
| 216 |
connections such as https. |
| 217 |
|
| 218 |
=head2 http_only |
| 219 |
|
| 220 |
By default, cookies are created with a property, named C<HttpOnly>, |
| 221 |
that can be used for security, forcing the cookie to be used only by |
| 222 |
the server (via HTTP) and not by any JavaScript code. |
| 223 |
|
| 224 |
If your cookie is meant to be used by some JavaScript code, set this |
| 225 |
attribute to 0. |
| 226 |
|
| 227 |
=head2 same_site |
| 228 |
|
| 229 |
Whether the cookie ought not to be sent along with cross-site requests. |
| 230 |
Valid values are C<Strict>, C<Lax>, or C<None>. Default is unset. |
| 231 |
Refer to |
| 232 |
L<RFC6265bis|https://tools.ietf.org/html/draft-ietf-httpbis-cookie-same-site> |
| 233 |
for further details regarding same-site context. |
| 234 |
|
| 235 |
=head1 METHODS |
| 236 |
|
| 237 |
=head2 my $cookie=Dancer2::Core::Cookie->new(%opts); |
| 238 |
|
| 239 |
Create a new Dancer2::Core::Cookie object. |
| 240 |
|
| 241 |
You can set any attribute described in the I<ATTRIBUTES> section above. |
| 242 |
|
| 243 |
=head2 my $header=$cookie->to_header(); |
| 244 |
|
| 245 |
Creates a proper HTTP cookie header from the content. |
| 246 |
|
| 247 |
=head2 values |
| 248 |
|
| 249 |
Returns all values associated with this cookie, always a list. (So in scalar |
| 250 |
context, you will get the count.) |
| 251 |
|
| 252 |
=head1 AUTHOR |
| 253 |
|
| 254 |
Dancer Core Developers |
| 255 |
|
| 256 |
=head1 COPYRIGHT AND LICENSE |
| 257 |
|
| 258 |
This software is copyright (c) 2026 by Alexis Sukrieh. |
| 259 |
|
| 260 |
This is free software; you can redistribute it and/or modify it under |
| 261 |
the same terms as the Perl 5 programming language system itself. |
| 262 |
|
| 263 |
=cut |