#!/usr/bin/perl
#
#  PROGRAM:     setcookie.pl
#
#  PURPOSE:     Changes the JSESSION cookie value
#

#------------------------------#
#  1. Create a new CGI object  #
#------------------------------#

use CGI;
use CGI::Cookie;

$query = new CGI;
$server = $query->url_param('server');

# Watch for bad requests

if ($server !~ /[a-z]{5}-app-(b2c|bpm|cms|esb)-[0-9]{3}\.[a-z0-9]{2}\.(cw|ch\.itr|spc|mhk)\.lan/)
{
    print $query->header;
    print $query->h3($server);
    print $query->h3('Not allowed');
    print $query->end_html;
    exit;
}

# Get the path
my $path = (split '/', $ENV{'REQUEST_URI'})[1];

# Create the cookie
$cookie = $query->cookie(-name=>'JSESSIONID',
                           -value       => '0000000000000000000000000000.' . $server,
                           -expires     => '+60s',
                           -path        =>  $path,
                           -secure      =>  1,
                           -httponly    =>  1
                        );







#--------------------------------------------------------------#
#  2. Create the HTTP header and print the doctype statement.  #
#     Set the cookie.                                          #
#--------------------------------------------------------------#

print $query->header(-cookie=>$cookie);


#----------------------------------------------------#
#  3. Start the HTML doc, and give the page a title  #
#----------------------------------------------------#

print $query->start_html('JSESSIONID');


#----------------------------------------------------------------------#
#  4. Output something for users.                                      #
#----------------------------------------------------------------------#

print $query->h3('Session wurde gesetzt.');


#-------------------------#
#  5. End the HTML page.  #
#-------------------------#

print $query->end_html;

