3e29647b559845b6e2d2a621e01fe7061b5d2e1f
[mkws-moved-to-github.git] / etc / mod_perl / MyApache2 / CopyCookie.pm
1 package MyApache2::CopyCookie;
2
3 use Apache2::Filter ();
4 use Apache2::RequestRec ();
5 use APR::Table ();
6
7 use Apache2::Const -compile => qw(OK);
8
9 use constant BUFF_LEN => 1024;
10
11 sub handler {
12     my $f = shift;
13
14     # If the server generated a new cookie, make it available in a
15     # header other than the magic "Cookie" that clients can't read.
16     my $ho = $f->r->headers_out;
17     my $cookie = $ho->get('Set-Cookie');
18     if (defined $cookie && $cookie ne "") {
19         $ho->set('X-Set-Cake', $cookie);
20     }
21
22     # If the client sent an existing cookie as X-Cake, but didn't
23     # set Cookie, copy the former to the latter.
24     my $hi = $f->r->headers_in;
25     $cookie = $hi->get('Cookie');
26     if (!defined $cookie || $cookie eq "") {
27         $cookie = $hi->get('X-Cake');
28         if (defined $cookie && $cookie ne "") {
29             warn "copying X-Cake '$cookie' to Cookie";
30             $hi->set('Cookie', $cookie);
31         }
32     }
33
34     while ($f->read(my $buffer, BUFF_LEN)) {
35         $f->print($buffer);
36     }
37
38     return Apache2::Const::OK;
39 }
40
41 1;