3dbade1b866a4d8471cdd29e99d56e8e4c721959
[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     $ho->set('X-Set-Cookie', $cookie);
20
21     # If the client sent an existing cookie as X-Cookie, but didn't
22     # set Cookie, copy the former to the latter.
23     my $hi = $f->r->headers_in;
24     $cookie = $hi->get('Cookie');
25     if (!defined $cookie || $cookie eq "") {
26         $cookie = $hi->get('X-Cookie');
27         warn "copying X-Cookie '$cookie' to Cookie";
28         $hi->set('Cookie', $cookie);
29     }
30
31     while ($f->read(my $buffer, BUFF_LEN)) {
32         $f->print($buffer);
33     }
34
35     return Apache2::Const::OK;
36 }
37
38 1;