#
# generic cgi include, simple
#
# Note on enviroments, if you want to get a full listing of what your
# server is provided, place the 'test' script in your cgi directory
# and call it (it will accept both POST and GET).
#
# Copyright Brandon Gillespie, June 1995
#
# Permission to use, copy, modify and distribute this software and its
# documentation is hereby granted, provided that this copyright notice
# appear in all versions and documentation.  No representations are
# made about the suitability of this software for any purpose.  This
# software is provided "as is" without express or implied warranty.

package CGI;

%fields = ();

## decode stolen from wwwlib
sub decode {
    local($line) = @_;

    $line =~ s/\+/ /g;
    $line =~ s/%([\da-f][\da-f])/pack("C",hex($1))/gei;
    return $line;
}

sub break_query {
    &break_encoded($ENV{QUERY_STRING});
}

sub break_encoded {
    @list = split(/\&/, "@_");
    for (@list) {
       ($name, $value) = split(/=/);
       if ($value) { $value = &decode($value); }
       $fields{$name} = $value;
    }
}

sub init {
    if (!$ENV{GATEWAY_INTERFACE}) {
        print STDERR "This must be executed in a CGI environment, aborting.\n";
        exit(1);
    }

    if ($ENV{REQUEST_METHOD} eq "POST" && $ENV{CONTENT_LENGTH}) {
        read(STDIN, $ENV{QUERY_STRING}, $ENV{CONTENT_LENGTH});
    }
}

1;
