1 | package Mojo::Cookie::Response;
|
---|
2 | use Mojo::Base 'Mojo::Cookie';
|
---|
3 |
|
---|
4 | use Mojo::Date;
|
---|
5 | use Mojo::Util qw(quote split_cookie_header);
|
---|
6 |
|
---|
7 | has [qw(domain expires host_only httponly max_age path secure)];
|
---|
8 |
|
---|
9 | my %ATTRS = map { $_ => 1 } qw(domain expires httponly max-age path secure);
|
---|
10 |
|
---|
11 | sub parse {
|
---|
12 | my ($self, $str) = @_;
|
---|
13 |
|
---|
14 | my @cookies;
|
---|
15 | my $tree = split_cookie_header $str // '';
|
---|
16 | while (my $pairs = shift @$tree) {
|
---|
17 | my ($name, $value) = splice @$pairs, 0, 2;
|
---|
18 | push @cookies, $self->new(name => $name, value => $value // '');
|
---|
19 |
|
---|
20 | while (my ($name, $value) = splice @$pairs, 0, 2) {
|
---|
21 | next unless $ATTRS{my $attr = lc $name};
|
---|
22 | $value =~ s/^\.// if $attr eq 'domain' && defined $value;
|
---|
23 | $value = Mojo::Date->new($value // '')->epoch if $attr eq 'expires';
|
---|
24 | $value = 1 if $attr eq 'secure' || $attr eq 'httponly';
|
---|
25 | $cookies[-1]{$attr eq 'max-age' ? 'max_age' : $attr} = $value;
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | return \@cookies;
|
---|
30 | }
|
---|
31 |
|
---|
32 | sub to_string {
|
---|
33 | my $self = shift;
|
---|
34 |
|
---|
35 | # Name and value
|
---|
36 | return '' unless length(my $name = $self->name // '');
|
---|
37 | my $value = $self->value // '';
|
---|
38 | my $cookie = join '=', $name, $value =~ /[,;" ]/ ? quote $value : $value;
|
---|
39 |
|
---|
40 | # "expires"
|
---|
41 | my $expires = $self->expires;
|
---|
42 | $cookie .= '; expires=' . Mojo::Date->new($expires) if defined $expires;
|
---|
43 |
|
---|
44 | # "domain"
|
---|
45 | if (my $domain = $self->domain) { $cookie .= "; domain=$domain" }
|
---|
46 |
|
---|
47 | # "path"
|
---|
48 | if (my $path = $self->path) { $cookie .= "; path=$path" }
|
---|
49 |
|
---|
50 | # "secure"
|
---|
51 | $cookie .= "; secure" if $self->secure;
|
---|
52 |
|
---|
53 | # "HttpOnly"
|
---|
54 | $cookie .= "; HttpOnly" if $self->httponly;
|
---|
55 |
|
---|
56 | # "Max-Age"
|
---|
57 | if (defined(my $max = $self->max_age)) { $cookie .= "; Max-Age=$max" }
|
---|
58 |
|
---|
59 | return $cookie;
|
---|
60 | }
|
---|
61 |
|
---|
62 | 1;
|
---|
63 |
|
---|
64 | =encoding utf8
|
---|
65 |
|
---|
66 | =head1 NAME
|
---|
67 |
|
---|
68 | Mojo::Cookie::Response - HTTP response cookie
|
---|
69 |
|
---|
70 | =head1 SYNOPSIS
|
---|
71 |
|
---|
72 | use Mojo::Cookie::Response;
|
---|
73 |
|
---|
74 | my $cookie = Mojo::Cookie::Response->new;
|
---|
75 | $cookie->name('foo');
|
---|
76 | $cookie->value('bar');
|
---|
77 | say "$cookie";
|
---|
78 |
|
---|
79 | =head1 DESCRIPTION
|
---|
80 |
|
---|
81 | L<Mojo::Cookie::Response> is a container for HTTP response cookies, based on
|
---|
82 | L<RFC 6265|http://tools.ietf.org/html/rfc6265>.
|
---|
83 |
|
---|
84 | =head1 ATTRIBUTES
|
---|
85 |
|
---|
86 | L<Mojo::Cookie::Response> inherits all attributes from L<Mojo::Cookie> and
|
---|
87 | implements the following new ones.
|
---|
88 |
|
---|
89 | =head2 domain
|
---|
90 |
|
---|
91 | my $domain = $cookie->domain;
|
---|
92 | $cookie = $cookie->domain('localhost');
|
---|
93 |
|
---|
94 | Cookie domain.
|
---|
95 |
|
---|
96 | =head2 expires
|
---|
97 |
|
---|
98 | my $expires = $cookie->expires;
|
---|
99 | $cookie = $cookie->expires(time + 60);
|
---|
100 |
|
---|
101 | Expiration for cookie.
|
---|
102 |
|
---|
103 | =head2 host_only
|
---|
104 |
|
---|
105 | my $bool = $cookie->host_only;
|
---|
106 | $cookie = $cookie->host_only($bool);
|
---|
107 |
|
---|
108 | Host-only flag, indicating that the canonicalized request-host is identical to
|
---|
109 | the cookie's L</"domain">.
|
---|
110 |
|
---|
111 | =head2 httponly
|
---|
112 |
|
---|
113 | my $bool = $cookie->httponly;
|
---|
114 | $cookie = $cookie->httponly($bool);
|
---|
115 |
|
---|
116 | HttpOnly flag, which can prevent client-side scripts from accessing this
|
---|
117 | cookie.
|
---|
118 |
|
---|
119 | =head2 max_age
|
---|
120 |
|
---|
121 | my $max_age = $cookie->max_age;
|
---|
122 | $cookie = $cookie->max_age(60);
|
---|
123 |
|
---|
124 | Max age for cookie.
|
---|
125 |
|
---|
126 | =head2 path
|
---|
127 |
|
---|
128 | my $path = $cookie->path;
|
---|
129 | $cookie = $cookie->path('/test');
|
---|
130 |
|
---|
131 | Cookie path.
|
---|
132 |
|
---|
133 | =head2 secure
|
---|
134 |
|
---|
135 | my $bool = $cookie->secure;
|
---|
136 | $cookie = $cookie->secure($bool);
|
---|
137 |
|
---|
138 | Secure flag, which instructs browsers to only send this cookie over HTTPS
|
---|
139 | connections.
|
---|
140 |
|
---|
141 | =head1 METHODS
|
---|
142 |
|
---|
143 | L<Mojo::Cookie::Response> inherits all methods from L<Mojo::Cookie> and
|
---|
144 | implements the following new ones.
|
---|
145 |
|
---|
146 | =head2 parse
|
---|
147 |
|
---|
148 | my $cookies = Mojo::Cookie::Response->parse('f=b; path=/');
|
---|
149 |
|
---|
150 | Parse cookies.
|
---|
151 |
|
---|
152 | =head2 to_string
|
---|
153 |
|
---|
154 | my $str = $cookie->to_string;
|
---|
155 |
|
---|
156 | Render cookie.
|
---|
157 |
|
---|
158 | =head1 SEE ALSO
|
---|
159 |
|
---|
160 | L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
|
---|
161 |
|
---|
162 | =cut
|
---|