#!/usr/bin/perl
# shebang only for color coding, just ignore it m'kay?
use strict;
use vars qw(%get %post %fields %cookie %INTERNAL);

$INTERNAL{getsub} = sub {
    my %get;
    if ($ENV{QUERY_STRING} ne ''){
	for (split /&/, $ENV{QUERY_STRING}) {
	    my @keyval = split /=/;
	    DecodeURI(@keyval);
	    $get{$keyval[0]} = $keyval[1];
	}
    }
    return \%get;
};

$INTERNAL{postsub} = sub {
    my %post;
    $INTERNAL{post} = <STDIN>;
    if (defined($INTERNAL{post}) && $INTERNAL{post} ne '' &&
	($ENV{CONTENT_TYPE} eq '' || $ENV{CONTENT_TYPE} eq 'application/x-www-form-urlencoded')){
        for (split /&/, $INTERNAL{post}) {
	    my @keyval = split /=/;
	    DecodeURI(@keyval);
	    $post{$keyval[0]} = $keyval[1];
	}
    }
    return \%post;
};

$INTERNAL{fieldssub} = sub {
    $get{PLPdummy}, $post{PLPdummy}; # Trigger creation
    return {%get, %post}
};

tie %get, 'PLP::Delay', 'main::get', $INTERNAL{getsub};
tie %post, 'PLP::Delay', 'main::post', $INTERNAL{postsub};
tie %fields, 'PLP::Delay', 'main::fields', $INTERNAL{fieldssub};

#%fields = (%get, %post);

if (defined($ENV{HTTP_COOKIE}) && $ENV{HTTP_COOKIE} ne ''){
    for (split /; ?/, $ENV{HTTP_COOKIE}) {
	my @keyval = split /=/;
	$cookie{$keyval[0]} ||= $keyval[1];
    }
}

1;
