Skip to content

Commit

Permalink
updated cookie implementation for RFC 6265 and in turn deprecated Moj…
Browse files Browse the repository at this point in the history
…o::Cookie->version, Mojo::Cookie::Response->comment and Mojo::Cookie::Response->port
  • Loading branch information
kraih committed Dec 28, 2011
1 parent 510caa0 commit ce9204d
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 354 deletions.
3 changes: 3 additions & 0 deletions Changes
@@ -1,6 +1,9 @@
This file documents the revision history for Perl extension Mojolicious.

2.41 2011-12-28 00:00:00
- Deprecated Mojo::Cookie->version.
- Deprecated Mojo::Cookie::Response->comment and
Mojo::Cookie::Response->port.
- Removed experimental status from group feature of
Mojolicious::Lite.
- Removed experimental status from Mojo::Collection.
Expand Down
12 changes: 4 additions & 8 deletions lib/Mojo/Cookie.pm
Expand Up @@ -8,7 +8,7 @@ use overload
use Carp 'croak';
use Mojo::Util 'unquote';

has [qw/name path value version/];
has [qw/name path value/];

my $COOKIE_SEPARATOR_RE = qr/^\s*\,\s*/;
my $NAME_RE = qr/
Expand All @@ -34,6 +34,9 @@ my $VALUE_RE = qr/
# but he is not a porn star."
sub to_string { croak 'Method "to_string" not implemented by subclass' }

# DEPRECATED in Leaf Fluttering In Wind!
sub version { warn "Mojo::Cookie->version is DEPRECATED!\n" }

sub _tokenize {
my ($self, $string) = @_;

Expand Down Expand Up @@ -114,13 +117,6 @@ Cookie path.
Cookie value.
=head2 C<version>
my $version = $cookie->version;
$cookie = $cookie->version(1);
Cookie version.
=head1 METHODS
L<Mojo::Cookie> inherits all methods from L<Mojo::Base> and implements the
Expand Down
33 changes: 3 additions & 30 deletions lib/Mojo/Cookie/Request.pm
Expand Up @@ -11,36 +11,28 @@ sub parse {

# Walk tree
my @cookies;
my $version = 0;
for my $knot ($self->_tokenize($string)) {
for my $token (@{$knot}) {
my ($name, $value) = @{$token};

# Path
if ($name =~ /^\$Path$/i) { $cookies[-1]->path($value) }

# Version
elsif ($name =~ /^\$Version$/i) { $version = $value }
# Garbage
elsif ($name =~ /^\$/) {next}

# Name and value
else {
push @cookies, Mojo::Cookie::Request->new;
$cookies[-1]->name($name);
$cookies[-1]->value($value //= '');
$cookies[-1]->version($version);
}
}
}

return \@cookies;
}

sub prefix {
my $self = shift;
my $version = $self->version || 1;
return "\$Version=$version";
}

sub to_string {
my $self = shift;

Expand All @@ -56,13 +48,6 @@ sub to_string {
return $cookie;
}

sub to_string_with_prefix {
my $self = shift;
my $prefix = $self->prefix;
my $cookie = $self->to_string;
return "$prefix; $cookie";
}

1;
__END__
Expand Down Expand Up @@ -94,28 +79,16 @@ implements the following new ones.
=head2 C<parse>
my $cookies = $cookie->parse('$Version=1; f=b; $Path=/');
my $cookies = $cookie->parse('f=b; $Path=/');
Parse cookies.
=head2 C<prefix>
my $prefix = $cookie->prefix;
Prefix for cookies.
=head2 C<to_string>
my $string = $cookie->to_string;
Render cookie.
=head2 C<to_string_with_prefix>
my $string = $cookie->to_string_with_prefix;
Render cookie with prefix.
=head1 SEE ALSO
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
Expand Down
45 changes: 15 additions & 30 deletions lib/Mojo/Cookie/Response.pm
Expand Up @@ -4,10 +4,12 @@ use Mojo::Base 'Mojo::Cookie';
use Mojo::Date;
use Mojo::Util 'quote';

has [qw/comment domain httponly max_age port secure/];
has [qw/domain httponly max_age secure/];

my $FIELD_RE =
qr/(Comment|Domain|expires|HttpOnly|Max-Age|Path|Port|Secure|Version)/msi;
my $FIELD_RE = qr/(Domain|expires|HttpOnly|Max-Age|Path|Secure)/msi;

# DEPRECATED in Leaf Fluttering In Wind!
sub comment { warn "Mojo::Cookie::Response->comment is DEPRECATED!\n" }

sub expires {
my ($self, $expires) = @_;
Expand All @@ -19,9 +21,8 @@ sub expires {
}

# Upgrade
return unless defined $self->{expires};
$self->{expires} = Mojo::Date->new($self->{expires})
unless ref $self->{expires};
if defined $self->{expires} && !ref $self->{expires};

return $self->{expires};
}
Expand Down Expand Up @@ -61,18 +62,20 @@ sub parse {
return \@cookies;
}

# DEPRECATED in Leaf Fluttering In Wind!
sub port { warn "Mojo::Cookie::Response->port is DEPRECATED!\n" }

sub to_string {
my $self = shift;

# Version
# Name and value
return '' unless $self->name;
my $cookie = $self->name;
my $value = $self->value;
if (defined $value) {
$cookie .= '=' . ($value =~ /[,;"]/ ? quote($value) : $value);
}
else { $cookie .= '=' }
$cookie .= sprintf "; Version=%d", ($self->version || 1);

# Domain
if (my $domain = $self->domain) { $cookie .= "; Domain=$domain" }
Expand All @@ -86,18 +89,12 @@ sub to_string {
# Expires
if (defined(my $e = $self->expires)) { $cookie .= "; expires=$e" }

# Port
if (my $port = $self->port) { $cookie .= qq/; Port="$port"/ }

# Secure
if (my $secure = $self->secure) { $cookie .= "; Secure" }

# HttpOnly
if (my $httponly = $self->httponly) { $cookie .= "; HttpOnly" }

# Comment
if (my $comment = $self->comment) { $cookie .= "; Comment=$comment" }

return $cookie;
}

Expand Down Expand Up @@ -126,13 +123,6 @@ L<Mojo::Cookie::Response> is a container for HTTP 1.1 response cookies.
L<Mojo::Cookie::Response> inherits all attributes from L<Mojo::Cookie> and
implements the followign new ones.
=head2 C<comment>
my $comment = $cookie->comment;
$cookie = $cookie->comment('test 123');
Cookie comment.
=head2 C<domain>
my $domain = $cookie->domain;
Expand All @@ -145,7 +135,8 @@ Cookie domain.
my $httponly = $cookie->httponly;
$cookie = $cookie->httponly(1);
HTTP only flag.
HttpOnly flag, which can prevent client side scripts from accessing this
cookie.
=head2 C<max_age>
Expand All @@ -154,19 +145,13 @@ HTTP only flag.
Max age for cookie in seconds.
=head2 C<port>
my $port = $cookie->port;
$cookie = $cookie->port('80 8080');
Cookie port.
=head2 C<secure>
my $secure = $cookie->secure;
$cookie = $cookie->secure(1);
Secure flag.
Secure flag, which instructs browsers to only send this cookie over HTTPS
connections.
=head1 METHODS
Expand All @@ -182,7 +167,7 @@ Expiration for cookie in seconds.
=head2 C<parse>
my $cookies = $cookie->parse('f=b; Version=1; Path=/');
my $cookies = $cookie->parse('f=b; Path=/');
Parse cookies.
Expand Down
11 changes: 4 additions & 7 deletions lib/Mojo/CookieJar.pm
Expand Up @@ -87,17 +87,14 @@ sub find {
push @new, $cookie;

# Taste cookie
my $port = $url->port || 80;
next if $cookie->port && $port != $cookie->port;
next if $cookie->secure && $url->scheme ne 'https';
my $cpath = $cookie->path;
push @found,
Mojo::Cookie::Request->new(
name => $cookie->name,
value => $cookie->value,
path => $cookie->path,
version => $cookie->version,
secure => $cookie->secure
name => $cookie->name,
value => $cookie->value,
path => $cookie->path,
secure => $cookie->secure
) if $path =~ /^$cpath/;
}

Expand Down
9 changes: 1 addition & 8 deletions lib/Mojo/Headers.pm
Expand Up @@ -13,7 +13,7 @@ my @HEADERS = (
qw/Expires Host If-Modified-Since Last-Modified Location/,
qw/Proxy-Authenticate Proxy-Authorization Range Sec-WebSocket-Accept/,
qw/Sec-WebSocket-Key Sec-WebSocket-Origin Sec-WebSocket-Protocol/,
qw/Sec-WebSocket-Version Server Set-Cookie Set-Cookie2 Status Trailer/,
qw/Sec-WebSocket-Version Server Set-Cookie Status Trailer/,
qw/Transfer-Encoding Upgrade User-Agent WWW-Authenticate X-Forwarded-For/,
);
{
Expand Down Expand Up @@ -531,13 +531,6 @@ Shortcut for the C<Server> header.
Shortcut for the C<Set-Cookie> header.
=head2 C<set_cookie2>
my $set_cookie2 = $headers->set_cookie2;
$headers = $headers->set_cookie2('f=b; Version=1; Path=/');
Shortcut for the C<Set-Cookie2> header.
=head2 C<status>
my $status = $headers->status;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojo/Message/Request.pm
Expand Up @@ -44,7 +44,7 @@ sub cookies {
my $cookies = shift;
$cookies = Mojo::Cookie::Request->new($cookies)
if ref $cookies eq 'HASH';
$cookies = $cookies->to_string_with_prefix;
$cookies = $cookies->to_string;
for my $cookie (@_) {
$cookie = Mojo::Cookie::Request->new($cookie)
if ref $cookie eq 'HASH';
Expand Down
9 changes: 2 additions & 7 deletions lib/Mojo/Message/Response.pm
Expand Up @@ -91,15 +91,10 @@ sub cookies {
return $self;
}

# Set-Cookie2
# Parse cookies
my @cookies;
my $headers = $self->headers;
push @cookies, @{Mojo::Cookie::Response->parse($_)}
for $headers->set_cookie2;

# Set-Cookie
push @cookies, @{Mojo::Cookie::Response->parse($_)}
for $headers->set_cookie;
for $self->headers->set_cookie;

return \@cookies;
}
Expand Down
5 changes: 1 addition & 4 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -487,10 +487,7 @@ sub _test_server {
my ($self, $scheme) = @_;

# Fresh start
if ($scheme) {
delete $self->{port};
delete $self->{server};
}
delete $self->{port} if $scheme;

# Start test server
unless ($self->{port}) {
Expand Down

0 comments on commit ce9204d

Please sign in to comment.