Thread CGI mit sleep() (15 answers)
Opened by kimmy at 2011-05-16 09:58

topeg
 2011-05-20 06:09
#148870 #148870
User since
2006-07-10
2611 articles
BenutzerIn

user image
Ich habe das Teststript für den Upload gerade gefunden:

more (25.6kb):
upload.pl
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/perl
use strict;
use warnings;
use CGI;
use Fcntl qw(:flock);
use Digest::MD5;
use CGI::Carp qw(fatalsToBrowser);

my $tbase='tmp/';
my $tname='%s.tmp';
my $wait=1;
my $interval=2;

my $fname;
my $size=0;
my $ttt=time();
my $cgi=CGI->new(sub{
    $size+=$_[2];
    $fname=sprintf($tname,Digest::MD5::md5_hex($ENV{QUERY_STRING})) unless($fname);
    if(time()-$ttt>$interval)
    {
      $ttt=time();
      if(open(my $fh, '>', $tbase.$fname))
      {
        flock($fh, LOCK_EX);
        print $fh $size;
        flock($fh, LOCK_UN);
        close($fh);
      }
    }
    select(undef,undef,undef,0.3);
  });

my $action=$cgi->param('action') || '';
my $id=$cgi->param('id') || int(rand(1000000)).time();
$fname=sprintf($tname,Digest::MD5::md5_hex("id=$id")) unless($fname);
my $sname=$cgi->script_name();
print $cgi->header();

if($action eq 'empty')
{
  print <<EOT;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><title>LEER</title></head><body>LEER</body></html>
EOT
}
elsif($action eq 'start')
{
  print <<EOT;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
   <title>UPLOAD</title>
 </head>
 <body>
   <form method="post" action="$sname?id=$id" enctype="multipart/form-data" onSubmit="parent.start(); return 1">
     Datei:
     <input type="hidden" name="action" value="run">
     <input type="file" name="datei" size="40" maxlength="100000" />
     <input type="submit" name="Submit" value="Senden" />
    </form>
  </body>
</html>
EOT
}
elsif($action eq 'show')
{
  my $size=0;
  if(open(my $fh, '<', $tbase.$fname))
  {
    flock($fh, LOCK_EX);
    $size=<$fh>;
    flock($fh, LOCK_UN);
    close($fh);
  }
  print <<EOT;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 <head>
   <title>FORTSCHRITT</title>
   <meta http-equiv="Refresh" content="$interval; url=$sname?id=$id&action=show" />
 </head>
 <body>
   <p>SIZE:$size</p>
 </body>
</html>
EOT
}
elsif($action eq 'run')
{
  my $ufh=$cgi->param('datei');

  #if($ufh && open(my $fh, '>', 'test.out'))
  #{
  #  binmde($fh);
  #  my $buffer;
  #  while(read($ufh,$buffer,1024))
  #  { print $fh $buffer; }
  #}

  unlink($tbase.$fname);

  print <<EOT;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head><title>UPLOAD</title></head>
  <body onload="parent.finish()">
    <p>UPLOAD von $ufh Abgeschlossen</p>
    <a href="$sname" target="_top" >Eine weitere Datei hochladen</a>
  </body>
</html>
EOT
}
else
{
  $wait*=1000;
  print <<EOT;
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
  <head>
    <title>UPLOAD</title>
  </head>
  <script type="text/javascript">
  function start(){ window.setTimeout("run()", $wait); }

  function run()
  {
    document.getElementById('main').rows='*,100%';
    window.show.location.href='$sname?id=$id&action=show';
  }

  function finish()
  {
    document.getElementById('main').rows='100%,*';
    window.show.location.href='$sname?action=empty';
  }
  </script>
  <frameset rows="100%,*" border="0" id="main">
    <frame src="$sname?id=$id&action=start" scrolling="no" border="0">
    <frame src="$sname?action=empty"        id="show" name="show" scrolling="no" border="0">
    <noframes>
      <body></body>
    </noframes>
  </frameset>
</html>
EOT
}

Ich habe es damals mit Frames und etwas Javascript gemacht. Das hatte ich falsch in Erinnerung. Den Dateinamen für die temporäre Datei erzeuge ich mit MD5 aus dem QueryString.

Wenn ich die Gelegenheit dazu finde werde ich meinen vorher genannten Ansatz mal testen.

View full thread CGI mit sleep()