@@ -32,13 +32,18 @@ sub main {
3232 $output = " Top of $tod to you $name ! It is very nice to meet you :)" ;
3333 }
3434
35- # first print the cookie
36- my $cookie ;
37- if ($name ) {
38- $cookie = bake_cookie($name );
35+ # prepare the cookie
36+ my $cookie = ' ' ;
37+ if ($cgi -> param(' reset' )) {
38+ $cookie = drop_cookie();
39+ $output = " Goodbye $name :)" ;
3940 }
41+ else {
42+ $cookie = ($name ? bake_cookie($name ) : ' ' );
43+ }
44+
4045 # then the rest
41- print_out( $cookie , $output , $footer ) ;
46+ printf get_template(), $cookie , $output , $footer ;
4247
4348 return ;
4449}
@@ -50,37 +55,39 @@ sub fetch_from_cookie {
5055
5156sub bake_cookie {
5257 my ($name ) = @_ ;
53- return " Set-Cookie: name=$name \n " ;
58+ return " Set-Cookie: name=$name ; HttpOnly " ;
5459}
5560
56- sub print_out {
57- my ($cookie , $output , $footer ) = @_ ;
58-
59- $cookie ||= ' ' ;
61+ sub drop_cookie {
62+ my ($name ) = @_ ;
63+ return bake_cookie( " deleted " ) . " ; MaxAge=0; expires=Thu, 01 Jan 1970 00:00:00 GMT " ;
64+ }
6065
61- print << "EOF" ;
66+ sub get_template {
67+ my $template = << "EOF" ;
6268HTTP/1.x 200 OK
6369Content-Type: text/html; charset=UTF-8;
64- $cookie
70+ %s
6571
6672<html>
6773 <head>
6874 <title>Created by form.pl</title>
6975 <link rel="stylesheet" type="text/css" href="/t/css/form.css">
7076 </head>
7177 <body>
72- <h2>$output </h2>
78+ <h2>%s </h2>
7379 <p class="content">
7480 <img src="/t/images/hello.png" alt="hello"></img></br>
75- <a href="/t/form.html">Back to the form</a>
76- <a href="/t/cgi/form.pl">Show with no input params</a>
81+ <a href="/t/form.html">Back to the form</a><br/>
82+ <a href="/t/cgi/form.pl">Show with no input params</a><br/>
83+ <a href="/t/cgi/form.pl?reset=1">Drop cookie</a>
7784 </p>
78- <p class="footer">$footer </p>
85+ <p class="footer">%s </p>
7986 </body>
8087</html>
8188EOF
8289
83- return ;
90+ return $template ;
8491}
8592
8693main();
0 commit comments