📄 ch06l04.txt
字号:
Listing 6.4 Report errors in the gateway's execution.############################################################################## TESTCGI.PL #### This is a script which sets up a test environment for the CGI script #### to be executed and then traps the common errors. The PATH is set to #### the minimal set by most systems, for example. All error messages are #### trapped and made available to the user. Thus, he does not have to #### wonder why for error cases. #### This code is in the public domain for people to do whatever they wish #### to with it. But, maintain this copyright notice and don't say you #### wrote it. This work is distributed in the hope that its useful. But, #### the author is not liable for any any incurred damages, directly or #### indirectly due to use orinability to use this software. ##############################################################################$tmpdir = "/tmp/"; # Directory under which the error file will be created.require "cgi-parse.pl";sub Usage { print "Usage: testcgi [-f filename containing input] -m METHOD scriptname\n"; print " where METHOD is GET/POST\n"; exit (0);} %cgi_input = ();&CGIGetInput(*cgi_input);&PrintHeader;$script = $cgi_input{'TestCgi-ScriptName'};$method = $cgi_input{'TestCgi-Method'};delete ($cgi_input {'TestCgi-ScriptName'});delete ($cgi_input {'TestCgi-Method'});$inp = "";foreach $elem (keys %cgi_input) { $cgi_input{$elem} = $cgi_input{$elem}; $cgi_input{$elem} =~ s# #+#g; $cgi_input{$elem} =~ s#([^+A-Za-z0-9])#sprintf("%%%02x",ord($1))#ge; $cgi_input{$elem} =~ s#%3d#=#g; $inp .= "$elem=$cgi_input{$elem}&";}# Encode the input in the form used by HTTP.# Turn off the include path. The script must use its own @INC and environment.@INC=();$ENV{'PATH'} = "/bin:/usr/bin/:/etc:";#Set the request method.$error_file = $tmpdir.$^T;$ENV{'REQUEST_METHOD'} = $method;if ($method eq "GET") { $ENV{'QUERY_STRING'} = $inp; open (OUTPUT,"$script 2\>/tmp/errors |") || die "unable to pipe script $! \n";}elsif ($method eq "POST") { $ENV{'CONTENT_LENGTH'} = length($inp); open (OUTPUT,"echo \"$inp\" | $script 2>$error_file |") || die "unable to pipe script $! \n";}else { print "Unknown method: $method\n"; exit (3);}print "<HTML><BODY>\n";$_ = <OUTPUT>;if (!/^Content-type: / && !/^Location: /) { if (-s $error_file) { open (ERRF, "< $error_file") || die "testcgi.cgi - Unable to open error file $!\n";print "<HTML><BODY>\n"; @errors = <ERRF>; print "<H3>Script $script has an execution error !!!</H3>\n"; print "@errors \n"; unlink ($error_file); exit (4); } print "<H3>Script $script has an error !!!</H3>\n"; print "It does not output the Content-type/Location header.\n"; exit (3);}$format = m#^Content-type:[ \t]*text/html#;$_ = <OUTPUT>;if (!/^$/) { print "Your second line must be a blank\n"; exit (3);}print "<H3>Script $script Seems OK</H3> \n";print "<P ALIGN=Justify> Here is its output \n";print "<PRE>\n" if (!$format) ;while (<OUTPUT>) { print;} print "</PRE>" if (!$format);print "</BODY></HTML>";exit (0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -