Newer Older
74 lines | 1.413kb
add files
Yuki Kimoto authored on 2014-03-26
1
package Mojo::Cookie::Request;
2
use Mojo::Base 'Mojo::Cookie';
3

            
4
use Mojo::Util qw(quote split_header);
5

            
6
sub parse {
7
  my ($self, $str) = @_;
8

            
9
  my @cookies;
10
  my @pairs = map {@$_} @{split_header($str // '')};
11
  while (@pairs) {
12
    my ($name, $value) = (shift @pairs, shift @pairs);
13
    next if $name =~ /^\$/;
14
    push @cookies, $self->new(name => $name, value => $value // '');
15
  }
16

            
17
  return \@cookies;
18
}
19

            
20
sub to_string {
21
  my $self = shift;
22
  return '' unless length(my $name = $self->name // '');
23
  my $value = $self->value // '';
24
  return join '=', $name, $value =~ /[,;" ]/ ? quote($value) : $value;
25
}
26

            
27
1;
28

            
29
=encoding utf8
30

            
31
=head1 NAME
32

            
33
Mojo::Cookie::Request - HTTP request cookie
34

            
35
=head1 SYNOPSIS
36

            
37
  use Mojo::Cookie::Request;
38

            
39
  my $cookie = Mojo::Cookie::Request->new;
40
  $cookie->name('foo');
41
  $cookie->value('bar');
42
  say "$cookie";
43

            
44
=head1 DESCRIPTION
45

            
46
L<Mojo::Cookie::Request> is a container for HTTP request cookies as described
47
in RFC 6265.
48

            
49
=head1 ATTRIBUTES
50

            
51
L<Mojo::Cookie::Request> inherits all attributes from L<Mojo::Cookie>.
52

            
53
=head1 METHODS
54

            
55
L<Mojo::Cookie::Request> inherits all methods from L<Mojo::Cookie> and
56
implements the following new ones.
57

            
58
=head2 parse
59

            
60
  my $cookies = Mojo::Cookie::Request->parse('f=b; g=a');
61

            
62
Parse cookies.
63

            
64
=head2 to_string
65

            
66
  my $str = $cookie->to_string;
67

            
68
Render cookie.
69

            
70
=head1 SEE ALSO
71

            
72
L<Mojolicious>, L<Mojolicious::Guides>, L<http://mojolicio.us>.
73

            
74
=cut