X-Set-Cake instead of cookie.
[mkws-moved-to-github.git] / etc / mod_perl / MyApache2 / CopyCookie.pm
index 2cdf24f..86e9b69 100644 (file)
@@ -6,11 +6,34 @@ use APR::Table ();
 
 use Apache2::Const -compile => qw(OK);
 
+use constant BUFF_LEN => 1024;
+
 sub handler {
     my $f = shift;
+    warn "in MyApache2::CopyCookie (f=$f)";
+
+    # If the server generated a new cookie, make it available in a
+    # header other than the magic "Cookie" that clients can't read.
+    my $ho = $f->r->headers_out;
+    my $cookie = $ho->get('Set-Cookie');
+    if (defined $cookie && $cookie ne "") {
+       $ho->set('X-Set-Cake', $cookie);
+    }
+
+    # If the client sent an existing cookie as X-Cake, but didn't
+    # set Cookie, copy the former to the latter.
+    my $hi = $f->r->headers_in;
+    $cookie = $hi->get('Cookie');
+    if (!defined $cookie || $cookie eq "") {
+       $cookie = $hi->get('X-Cake');
+       warn "copying X-Cake '$cookie' to Cookie";
+       $hi->set('Cookie', $cookie);
+    }
+
+    while ($f->read(my $buffer, BUFF_LEN)) {
+       $f->print($buffer);
+    }
 
-    my $cookie = $f->r->headers_out->get('Set-Cookie');
-    $f->r->headers_out->set('X-Set-Cookie', $cookie);
     return Apache2::Const::OK;
 }