X-Set-Cake instead of cookie.
[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     warn "in MyApache2::CopyCookie (f=$f)";
14
15     # If the server generated a new cookie, make it available in a
16     # header other than the magic "Cookie" that clients can't read.
17     my $ho = $f->r->headers_out;
18     my $cookie = $ho->get('Set-Cookie');
19     if (defined $cookie && $cookie ne "") {
20         $ho->set('X-Set-Cake', $cookie);
21     }
22
23     # If the client sent an existing cookie as X-Cake, but didn't
24     # set Cookie, copy the former to the latter.
25     my $hi = $f->r->headers_in;
26     $cookie = $hi->get('Cookie');
27     if (!defined $cookie || $cookie eq "") {
28         $cookie = $hi->get('X-Cake');
29         warn "copying X-Cake '$cookie' to Cookie";
30         $hi->set('Cookie', $cookie);
31     }
32
33     while ($f->read(my $buffer, BUFF_LEN)) {
34         $f->print($buffer);
35     }
36
37     return Apache2::Const::OK;
38 }
39
40 1;