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, $cl, $rd);
48 $in = $ENV{'QUERY_STRING'};
50 $cl = $ENV{'CONTENT_LENGTH'};
51 $rd= read(STDIN,$in,$cl);
52 $rd == $cl or CgiDie("unable to read POST data ($cl, $rd, $!)");
55 @in = split(/[&;]/,$in);
57 foreach $i (0 .. $#in) {
58 # Convert plus's to spaces
61 # Split into key and value.
62 ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
64 # Convert %XX from hex numbers to alphanumeric
65 $key =~ s/%(..)/pack("c",hex($1))/ge;
66 $val =~ s/%(..)/pack("c",hex($1))/ge;
68 # Associate key and value
69 $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
79 # Returns the magic line which tells WWW that we're an HTML document
82 return "Content-type: text/html\n\n";
87 # Returns the <head> of a document and the beginning of the body
88 # with the title and a body <h1> header as specified by the parameter
105 # Returns the </body>, </html> codes for the bottom of every HTML page
109 return "</body>\n</html>\n";
114 # Return true if this cgi call was using the GET request, false otherwise
117 return ($ENV{'REQUEST_METHOD'} eq "GET");
122 # Return true if this cgi call was using the POST request, false otherwise
125 return ($ENV{'REQUEST_METHOD'} eq "POST");
130 # Returns a URL to the script
134 $port = ":" . $ENV{'SERVER_PORT'} if $ENV{'SERVER_PORT'} != 80;
135 return 'http://' . $ENV{'SERVER_NAME'} . $port . $ENV{'SCRIPT_NAME'};
140 # Prints out an error message which which containes appropriate headers,
143 # If no parameters, gives a generic error message
144 # Otherwise, the first parameter will be the title and the rest will
145 # be given as different paragraphs of the body
153 @msg = ("Error: script $name encountered fatal error");
157 print "<html><head><title>$msg[0]</title></head>\n";
158 print "<body><h1>$msg[0]</h1>\n";
159 foreach $i (1 .. $#msg) {
160 print "<p>$msg[$i]</p>\n";
162 print "</body></html>\n";
167 # Identical to CgiError, but also quits with the passed error message.
177 # Nicely formats variables in an associative array passed as a parameter
178 # And returns the HTML string.
181 local ($old, $out, $output);
183 $output .= "\n<dl compact>\n";
184 foreach $key (sort keys(%in)) {
185 foreach (split("\0", $in{$key})) {
186 ($out = $_) =~ s/\n/<br>\n/g;
187 $output .= "<dt><b>$key</b>\n <dd><i>$out</i><br>\n";
190 $output .= "</dl>\n";
196 # PrintVariablesShort
197 # Now obsolete; just calls PrintVariables
199 sub PrintVariablesShort {
200 return &PrintVariables(@_);