Remark: Normally I comment (almost) every code line, and I did it as well here, but I couldn't find out how it's done without creating a mess in the posting. However, if someone would like to have the commented code I'd gladly send it to her/him.#!/usr/bin/perl -w require 5.004; use strict; my (@Query,$CGIPath,$TmpAbs,$TmpVrt,$HtmOut,$HtmTmp); my ($HtmName,$Status,$ForkVal,$STime,$Elapsed,$Ticks,$Prg,$QI); $Prg = "demo.cgi"; @Query = split(/&/,$ENV{'QUERY_STRING'}); $CGIPath = $ENV{'SCRIPT_NAME'}; while (chop($CGIPath) ne "/") { } $TmpAbs = "/www/WEBSERVER/htdocs/tmp"; $TmpVrt = "/tmp"; $HtmName = "demo.html"; $HtmOut = sprintf("%s/%s",$TmpAbs,$HtmName); $HtmTmp = sprintf("%s/%s.tmp",$TmpAbs,$HtmName); for ($QI=0;$QI<=$#Query;++$QI) { if ($Query[$QI] =~ /^Time=/) { ($STime) = ($Query[$QI] =~ /Time=(.*? +)$/); } elsif ($Query[$QI] =~ /^Tick=/) { ($Ticks) = ($Query[$QI] =~ /Tick=( +.*?)$/); } elsif ($Query[$QI] =~ /^STAT=/) { ($Status) = ($Query[$QI] =~ /STAT= +(.*?)$/); } } if (!defined($Status)) { $Status = 0; } #---- If "Start Page" is selected ---------- if ($Status == 0) { &StartPage; } #---- If "Processing" is selected ---------- elsif ($Status == 1) { if (!open(HTMFILE,">$HtmTmp")) { &ErrorMsg(1,$HtmTmp); } $ForkVal = fork(); if (!defined($ForkVal)) { &ErrorMsg(0,""); } if ($ForkVal == 0) { $Elapsed = "00:00:00"; &WaitingPage(sprintf("%s/%s?STAT=2&Time=%s",$CGIPath,$Prg,&CurrTim +e)); close(STDOUT); close(STDERR); close(STDIN); exit(0); } else { print HTMFILE &ProcessPage; close(HTMFILE); system("mv $HtmTmp $HtmOut"); } } #---- If "Waiting Page" is selected ---------- elsif ($Status == 2) { $Elapsed = &ElapsedTime; if (-s $HtmOut) { &WaitingPage(sprintf("%s/%s",$TmpVrt,$HtmName)); } else { &WaitingPage(sprintf("%s/%s?STAT=2&Time=%s",$CGIPath,$Prg,$ST +ime)); } exit(0); } sub ErrorMsg { my ($Stat,$Mess) = @_; print "Content-type: text/html \n\n"; print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN +\">\n\n<html>\n"; print "<head>\n <title>DEMO Error Message</title>\n</head>\n\n"; print "<body>\n<center>\n\n"; print "<span style=\"font-size:22pt; color:Green\">DEMO ERROR MESSAG +E</span><br>\n<br>\n"; if ($Stat == 0) { print "!!! System error - could NOT fork process ! +!!<br>\n"; } elsif ($Stat == 1) { print "!!! System error - could NOT write \"HTM +L\" file '$Mess'!!!<br>\n"; } print "</center>\n</body>\n</html>\n"; exit(0); } sub StartPage { print "Content-type: text/html \n\n"; print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN +\">\n<html>\n"; print "<head>\n <title>DEMO START PAGE</title>\n</head>\n\n"; print "<body>\n<center>\n<br>\n"; print "<span style=\"font-size:22pt; color:Green\">DEMO START PAGE</ +span><br>\n<br>\n"; print "<form action=\"$CGIPath/demo.cgi\" method=\"get\">\n"; print "<input type=\"hidden\" name=\"STAT\" value=\"1\">\n"; print "<b>TYPE A "TICK NUMBER":</b><br>\n"; print "<input type=\"text\" name=\"Tick\" size=\"10\"><br><br>\n"; print "<input type=\"submit\" value=\"PROCESS\"> <input type=\"reset +\" value=\"RESET FORM\">\n"; print "</form>\n</center>\n</body>\n</html>\n"; } sub ProcessPage { my $Html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitiona +l//EN\">\n<html>\n"; $Html .= "<head>\n <title>DEMO PROCESS PAGE</title>\n"; $Html .= " <meta HTTP-EQUIV=\"Expires\" CONTENT=\"NOW\">\n</head>\n +\n"; $Html .= "<body>\n<center>\n<br>\n"; $Html .= "<span style=\"font-size:22pt; color:Green\">DEMO PROCESS P +AGE</span><br>\n<br>\n"; $Html .= "<span style=\"font-size:22pt; color:Violet\">...THAT'S THE + TEST...</span><br>\n<br>\n"; $Html .= "...<a href=\"$CGIPath/demo.cgi\">go back to the DEMO start + page</a>...<br>\n"; $Html .= "</center>\n</body>\n</html>\n"; sleep($Ticks); $Html; } sub WaitingPage { my ($Url) = @_; print "Content-type: text/html \n\n"; print "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN +\">\n<html>\n"; print "<head>\n <title>...DEMO is running...</title>\n"; print " <META HTTP-EQUIV=\"refresh\" content=\"2; url=$Url\">\n</he +ad>\n\n"; print "<body>\n<center>\n<br>\n"; print "<span style=\"font-size:22pt; color:Red\">...DEMO is running. +..</span><br>\n"; print "<br>\n<span style=\"font-size:22pt\">Don't go BACK!</span><br +>\n<br>\n"; print "<span style=\"font-size:20pt; color:Blue\">Elapsed Time: $Ela +psed</span><br>\n"; print "</center>\n</body>\n</html>\n"; } sub CurrTime { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime +(time); my $LocTime = "$mday $mon $year $wday $yday $isdst"; $LocTime = sprintf("%02d:%02d:%02d",$hour,$min,$sec); $LocTime; } sub ElapsedTime { my ($Sec,$Min,$Hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime +(time); my $CTime = "$mday $mon $year $wday $yday $isdst"; $CTime = ((($Hour * 60) + $Min) * 60) + $Sec; ($Hour,$Min,$Sec) = split(/:/,$STime); my $ITime = ((($Hour * 60) + $Min) * 60) + $Sec; if ($CTime < $ITime) { $CTime += 86400; } my $ETime = $CTime - $ITime; $Sec = $ETime % 60; $ETime = ($ETime - $Sec) / 60; $Min = $ETime % 60; $Hour = ($ETime - $Min) / 60; $ETime = sprintf("%02d:%02d:%02d",$Hour,$Min,$Sec); $ETime; }
In reply to PERL CGI - waiting page is hanging by chanklaus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |