3 # Perl Routines to Manipulate CGI input
5 # Copyright (c) 1995 Steven E. Brenner
6 # Permission granted to use and modify this library so long as the
7 # copyright above is maintained, modifications are documented, and
8 # credit is given for any use of the library.
10 # Thanks are due to many people for reporting bugs and suggestions
11 # especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen,
12 # Andrew Dalke, Mark-Jason Dominus, Dave Dittrich, Jason Mathews
14 # For more information, see:
15 # http://www.bio.cam.ac.uk/web/form.html
16 # http://www.seas.upenn.edu/~mengwong/forms/
18 # Minimalist http form and script (http://www.bio.cam.ac.uk/web/minimal.cgi):
20 # require "cgi-lib.pl";
21 # if (&ReadParse(*input)) {
22 # print &PrintHeader, &PrintVariables(%input);
24 # print &PrintHeader,'<form><input type="submit"> Data: <input name="myfield">';
28 # Reads in GET or POST data, converts it to unescaped text,
29 # creates key/value pairs in %in, using '\0' to separate multiple
32 # Returns TRUE if there was input, FALSE if there was no input
33 # UNDEF may be used in the future to indicate some failure.
35 # Now that cgi scripts can be put in the normal file space, it is useful
36 # to combine both the form and the script in one place. If no parameters
37 # are given (i.e., ReadParse returns FALSE), then a form could be output.
39 # If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
40 # information is stored there, rather than in $in, @in, and %in.
43 local (*in) = @_ if @_;
44 local ($i, $key, $val);
48 $in = $ENV{'QUERY_STRING'};
50 read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
53 @in = split(/[&;]/,$in);
55 foreach $i (0 .. $#in) {
56 # Convert plus's to spaces
59 # Split into key and value.
60 ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
62 # Convert %XX from hex numbers to alphanumeric
63 $key =~ s/%(..)/pack("c",hex($1))/ge;
64 $val =~ s/%(..)/pack("c",hex($1))/ge;
66 # Associate key and value
67 $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
77 # Returns the magic line which tells WWW that we're an HTML document
80 return "Content-type: text/html\n\n";
85 # Returns the <head> of a document and the beginning of the body
86 # with the title and a body <h1> header as specified by the parameter
103 # Returns the </body>, </html> codes for the bottom of every HTML page
107 return "</body>\n</html>\n";
112 # Return true if this cgi call was using the GET request, false otherwise
115 return ($ENV{'REQUEST_METHOD'} eq "GET");
120 # Return true if this cgi call was using the POST request, false otherwise
123 return ($ENV{'REQUEST_METHOD'} eq "POST");
128 # Returns a URL to the script
132 $port = ":" . $ENV{'SERVER_PORT'} if $ENV{'SERVER_PORT'} != 80;
133 return 'http://' . $ENV{'SERVER_NAME'} . $port . $ENV{'SCRIPT_NAME'};
138 # Prints out an error message which which containes appropriate headers,
141 # If no parameters, gives a generic error message
142 # Otherwise, the first parameter will be the title and the rest will
143 # be given as different paragraphs of the body
151 @msg = ("Error: script $name encountered fatal error");
155 print "<html><head><title>$msg[0]</title></head>\n";
156 print "<body><h1>$msg[0]</h1>\n";
157 foreach $i (1 .. $#msg) {
158 print "<p>$msg[$i]</p>\n";
160 print "</body></html>\n";
165 # Identical to CgiError, but also quits with the passed error message.
175 # Nicely formats variables in an associative array passed as a parameter
176 # And returns the HTML string.
179 local ($old, $out, $output);
181 $output .= "\n<dl compact>\n";
182 foreach $key (sort keys(%in)) {
183 foreach (split("\0", $in{$key})) {
184 ($out = $_) =~ s/\n/<br>\n/g;
185 $output .= "<dt><b>$key</b>\n <dd><i>$out</i><br>\n";
188 $output .= "</dl>\n";
194 # PrintVariablesShort
195 # Now obsolete; just calls PrintVariables
197 sub PrintVariablesShort {
198 return &PrintVariables(@_);