File Coverage

blib/lib/AnyEvent/HTTPD/CookiePatch.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package AnyEvent::HTTPD::CookiePatch;
2              
3 1     1   29006 use 5.006;
  1         4  
  1         69  
4 1     1   7 use strict;
  1         2  
  1         43  
5 1     1   6 use warnings FATAL => 'all';
  1         14  
  1         62  
6              
7             =head1 NAME
8              
9             AnyEvent::HTTPD::CookiePatch -
10             Patch of AnyEvent::HTTPD for cookie support
11              
12             =head1 VERSION
13              
14             Version 0.01
15              
16             =cut
17              
18 1     1   1011 use version;
  1         2032  
  1         7  
19             our $VERSION = 'v0.1.0';
20              
21              
22             =head1 SYNOPSIS
23              
24             # by module injection
25             use AnyEvent::HTTPD::CookiePatch qw(inject);
26              
27             # or by inheritance
28             use AnyEvent::HTTPD::CookiePatch;
29             my $httpd = AnyEvent::HTTPD->new( request_class => 'AnyEvent::HTTPD::CookiePatch' );
30              
31             # and then in your handler
32             sub {
33             my($httpd, $req) = @_;
34              
35             # get cookie
36             my $cookie_a = $req->cookie('a');
37              
38             # set cookie
39             $req->cookie('a', 'a_value');
40             # or with other cookie parameters
41             $req->cookie('b', 'b_value', 10*60, '/', '.example.com');
42              
43             # then add the cookie header when respond
44             $req->respond(200, 'OK', {
45             ...
46             'set-cookie' => $req->{_set_cookie},
47             ...
48             }, "html...");
49             }
50              
51             =head1 METHODS
52              
53             =head2 $value = $req->cookie($name)
54              
55             Get the cookie
56              
57             =head2 $req->cookie($name, $value[, $max_age[, $path[, $domain]]])
58              
59             Set the cookie
60              
61             =head2 $req->{_set_cookie}
62              
63             The response header field Set-Cookie's value
64              
65             =cut
66              
67 1     1   526 use AnyEvent::HTTPD::SendMultiHeaderPatch;
  0            
  0            
68             our @ISA = 'AnyEvent::HTTPD::Request';
69              
70             sub cookie {
71             my($req, $name, $value, $max_age, $path, $domain) = @_;
72             if( defined $value ) { # set cookie
73             my $fragment = "$name=$value";
74             $fragment .= "; Max-Age=$max_age" if( defined $max_age );
75             $fragment .= "; Path=$path" if( defined $path );
76             $fragment .= "; Domain=$domain" if( defined $domain );
77             if( exists $req->{_set_cookie} ) {
78             $req->{_set_cookie} .= "\0$fragment";
79             }
80             else {
81             $req->{_set_cookie} = $fragment;
82             }
83             }
84             else { # get cookie
85             if( !$req->{_cookie} ) {
86             my $cookie_header = $req->headers->{cookie};
87             my %cookie;
88             while( $cookie_header =~ / *([^ =]+)\s*=\s*([^ ;]*)\s*;?/g ) {
89             $cookie{$1} = $2;
90             }
91             $req->{_cookie} = \%cookie;
92             }
93             return $req->{_cookie}{$name};
94             }
95             }
96              
97             sub import {
98             if( grep { $_ eq 'inject' } @_ ) {
99             *AnyEvent::HTTPD::Request::cookie = \&cookie;
100             }
101             }
102              
103             =head1 CAVEATS
104              
105             This module use module L (a hack)
106             for sending multiple Set-Cookie response header.
107              
108             =head1 AUTHOR
109              
110             Cindy Wang (CindyLinz)
111              
112             =head1 ACKNOWLEDGEMENTS
113              
114             =head1 LICENSE AND COPYRIGHT
115              
116             Copyright 2013 Cindy Wang (CindyLinz).
117              
118             This program is free software; you can redistribute it and/or modify it
119             under the terms of the the Artistic License (2.0). You may obtain a
120             copy of the full license at:
121              
122             L
123              
124             Any use, modification, and distribution of the Standard or Modified
125             Versions is governed by this Artistic License. By using, modifying or
126             distributing the Package, you accept this license. Do not use, modify,
127             or distribute the Package, if you do not accept this license.
128              
129             If your Modified Version has been derived from a Modified Version made
130             by someone other than you, you are nevertheless required to ensure that
131             your Modified Version complies with the requirements of this license.
132              
133             This license does not grant you the right to use any trademark, service
134             mark, tradename, or logo of the Copyright Holder.
135              
136             This license includes the non-exclusive, worldwide, free-of-charge
137             patent license to make, have made, use, offer to sell, sell, import and
138             otherwise transfer the Package with respect to any patent claims
139             licensable by the Copyright Holder that are necessarily infringed by the
140             Package. If you institute patent litigation (including a cross-claim or
141             counterclaim) against any party alleging that the Package constitutes
142             direct or contributory patent infringement, then this Artistic License
143             to you shall terminate on the date that such litigation is filed.
144              
145             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
146             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
147             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
148             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
149             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
150             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
151             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
152             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
153              
154              
155             =cut
156              
157             1; # End of AnyEvent::HTTPD::CookiePatch