#!/usr/local/bin/perl

# 1998.03.01 j[A
# 1998.11.08 IE4.0ŐɐݒłȂɑΏ
# 2004.06.11 jCB^OɏCB

#
# Cookie̒l𓾂
#
&getCookie();
$time = $COOKIE{'KAISUU'};
if ($time eq "") {
    $time = 1;
}
$date = $COOKIE{'HIZUKE'};
if ($date eq "") {
    $date = "???";
}

#
# Cookie̒l𓾂
#
$time_new = $time + 1;
($sec, $min, $hour, $mday, $mon, $year) = localtime();
$date_new = sprintf("%04d/%02d/%02d %02d:%02d:%02d",
	$year + 1900, $mon + 1, $mday, $hour, $min, $sec);

#
# y[W\B
#
$setcook1 = &setCookie("KAISUU", $time_new);
$setcook2 = &setCookie("HIZUKE", $date_new);
print <<END_OF_DATA;
Content-type: text/html
$setcook1
$setcook2

<html>
<head>
<title>Cookie Test</title>
</head>
<body>
<h2>CookiẽeXg</h2>
<hr>
<h3>ǂݍCookie̒l(`O)</h3>
<div>HTTP_COOKIE = $ENV{'HTTP_COOKIE'}</div>
<hr>
<h3>ǂݍCookie̒l(`)</h3>
<div>܂ł̖K񐔁F$time</div>
<div>O̖KF$date</div>
<hr>
<h3>Cookie̒l</h3>
<div>$setcook1</div>
<div>$setcook2</div>
<hr>
<p>ĕ\Ă݂ĂBxuEUIĂAlۑĂ܂B</p>
</body>
</html>
END_OF_DATA

#
# Cookie̒lǂݏo
#
sub getCookie {
    local($xx, $name, $value);
    foreach $xx (split(/; */, $ENV{'HTTP_COOKIE'})) {
        ($name, $value) = split(/=/, $xx);
        $value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;
        $COOKIE{$name} = $value;
    }
}

#
# Cookieɒlނ߂Set-Cookie:wb_𐶐
#
sub setCookie {
    local($tmp, $val);
    $val = $_[1];
    $val =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
    $tmp = "Set-Cookie: ";
    $tmp .= "$_[0]=$val; ";
    $tmp .= "expires=Tue, 1-Jan-2030 00:00:00 GMT;";
    return($tmp);
}

#
# Cookie폜邽߂Set-Cookie:wb_𐶐
#
sub clearCookie {
    $tmp = "Set-Cookie: ";
    $tmp .= "$_[0]=xx; ";
    $tmp .= " expires=Tue, 1-Jan-1980 00:00:00 GMT;";
    return($tmp);
}

