#!/usr/bin/perl -Tw

##
# Modified from
#   Figure 27.36 from _Internet & World Wide Web: How To Program_
#   by H.M. Deitel, P.J. Deitel, and T.R. Nieto
#   Published by Prentice Hall, (c) 2000 by Prentice-Hall, Inc.
##
# Fig. 27.36: read_cookies.pl
# Program to read cookies from the client's computer

use CGI qw/:standard/;
use CGI::Carp 'fatalsToBrowser';
use diagnostics;
use strict;
$| = 1;

print header;
print << 'EOHEAD'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
 <title>Dietel et al. Fig. 27.36</title>
 <!--meta name="ROBOTS" content="NOINDEX"  /-->
 <!--meta name="ROBOTS" content="NOFOLLOW" /-->
 <link rel="stylesheet" type="text/css" 
       href="/~jamie/course/CS/3172/CS3172.css" />
 <style type="text/css">
    body  {font-family: arial,sans-serif}
    table {border:medium ridge;border-collapse:separate;
           border-spacing:0;}
    td.name {color:black;background-color:#aaaaff}
    td.name {color:black;background-color:#aaaaaa}
 </style>
</head>
<body>
   <h1>
     <a href="http://www.cs.dal.ca/~jamie/course/CS/3172/"
        >Web-centric Computing</a>
   </h1>

   <h2>
     <a href="http://www.cs.dal.ca/~jamie/course/CS/3172/examples/list.html"
        >Some Example Code</a>
   </h2>

   <h3>Cookie Colour Example</h3>

 <p><strong>
  The folowing data was saved in a cookie on your computer by this server:
 </strong><br /><br /></p>
EOHEAD
;
my %cookie = readCookies();

 print " <table>\n";
 print " <tr><th>Name</th><th>Value</th></tr>\n";

 my $cookie_name;
 foreach $cookie_name (keys %cookie) 
 {
     print "  <tr>\n";
     print "   <td class='name' >$cookie_name</td>\n";
     print "   <td class='value'>$cookie{$cookie_name}</td>\n";
     print "  </tr>\n";
 }
 print "  </table>\n";

print << 'EOTAIL'
 <p>[<a 
href='http://www.cs.dal.ca/~jamie/course/CS/3172/examples/perl/cookies/cookie-colour.html'
type="text/html"
      >return to the form</a>&nbsp;| <a
href='http://www.cs.dal.ca/~jamie/course/CS/3172/examples/perl/cookies/cookie-colour-read.pl.source'
type="text/html"
      >view the source</a>]</p>

 <hr title="end of document proper" />

 <h6 title="document identification information"
    ><a name="docID"
        ><kbd>http://www.cs.dal.ca/~jamie/cgi-bin/3172/cookie/cookie-colour-css.cgi</kbd></a>
 </h6>
   <dl>
     <dt>Version:</dt> <dd>25 October 2001</dd>
     <dt>Course</dt>   <dd><a title="course homepage"
                              href="/~jamie/course/CS/3172/"
                              >CS 3172</a> (Web-centric Computing)</dd>
     <dt>Prof:</dt>    <dd><a title="prof's homepage"
                              href="/~jamie/"
                              >J. Blustein</a> <kbd>&lt;jamie@cs.dal.ca&gt;</kbd></dd>
   </dl>

   <p>
     <a href="http://validator.w3.org/check/referer"><img
         src="/~jamie/image/vxhtml10.gif"
         alt="Valid XHTML 1.0!" height="31" width="88" /></a>

     Modified from Figure 27.36 of <cite>Internet &amp; World Wide Web: How
     To Program</cite> by H.M. Deitel, P.J. Deitel, and T.R. Nieto
     Published by Prentice Hall, &copy;2000 by Prentice-Hall, Inc.
   </p>
  </body>
</html>
EOTAIL
;

sub readCookies
{
   my @cookie_values;
   my %cookies = (); # initialize to empty list

   @cookie_values = ($ENV{'HTTP_COOKIE'})
       ?split(/; /, $ENV{'HTTP_COOKIE'})
       :();

   foreach (@cookie_values)
   {
      my ($cookie_name, $cookie_value) = split ( /=/, $_ );
      $cookies{$cookie_name} = $cookie_value;
   } 
   
   return %cookies; 
}

