5 # Author: Ulli Horlacher <framstag@rus.uni-stuttgart.de>
7 # Copyright: Perl Artistic
10 use strict qw'vars subs';
16 use Time::HiRes qw'time';
17 # use Smart::Comments;
18 use constant k => 2**10;
19 use constant M => 2**20;
21 our ($SH,$windoof,$sigpipe,$useragent);
23 our $version = 20150615;
26 my $server = 'fex.rus.uni-stuttgart.de';
33 my $proxy_prefix = '';
37 my $timeout = 30; # server timeout
39 $version ||= mtime($0);
43 usage: $0 [-n] [-s server] [-P proxy] #MB
44 options: -n do not store on server
45 -s use alternative F*EX server:port
46 -P use proxy server:port
50 if ($Config{osname} =~ /^mswin/i) {
51 $windoof = $Config{osname};
52 $useragent = sprintf("fbm-$version (%s %s)",
53 $Config{osname},$Config{archname});
56 $_ = `(lsb_release -d||uname -a)2>/dev/null`||'';
59 $useragent = "fbm-$version ($_)";
66 my @_ARGV = @ARGV; # save arguments
74 getopts('hvnP:s:') or die $usage;
82 if ($opt_P =~ /^[\w.-]+:\d+/) {
85 die "$0: proxy must be: SERVER:PORT\n";
89 $mb = shift or die $usage;
90 $mb =~ /^\d+$/ or die $usage;
92 # $port = $1 if $server =~ s/:(\d+)//;
95 ($server,$port) = split /:/,$opt_s;
96 $port = 80 unless $port;
98 $server =~ s{http://}{};
102 if ($port == 80) { $proxy_prefix = "http://$server" }
103 else { $proxy_prefix = "http://$server:$port" }
106 print "Testing $server:\n";
112 comment => $opt_n ? 'NOSTORE' : 'NOMAIL',
117 if (not @r or not grep /\w/,@r) {
118 die "$0: no response from server\n";
121 if (($r) = grep /ERROR:/,@r) {
124 die "$0: server error: $r\n";
127 if (($r) = grep /^Location: http/,@r) {
128 $r =~ s:.*(/fop/\w+/.+$):$1:;
131 download("/ddd/$mb");
139 my ($boundary,$filename,$filesize,$length);
141 my ($t,$bt,$t0,$t1,$t2,$tt);
146 @hh = (); # HTTP header
147 @hb = (); # HTTP body
150 serverconnect($server,$port);
152 $boundary = randstring(48);
153 $P{command} = 'CHECKRECIPIENT';
155 # HTTP POST variables
156 @pv = qw'from to id command';
157 foreach my $v (@pv) {
160 push @hb,"--$boundary";
161 push @hb,"Content-Disposition: form-data; name=\"$name\"";
166 push @hb,"--$boundary--";
168 $length = length(join('',@hb)) + scalar(@hb)*2 + $mb*M;
171 push @hh,"POST $proxy_prefix/fup HTTP/1.1";
172 push @hh,"Host: $server:$port";
173 push @hh,"User-Agent: $useragent";
174 push @hh,"Content-Length: $length";
175 push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
176 push @hh,"Connection: close";
180 printf "--> $_\n" foreach (@hh,@hb);
183 nvtsend(@hh,@hb) or die "$0: server has closed the connection\n";
187 print "<-- $_\n" if $opt_v;
192 unless (@r and $r[0] =~ / 204 /) {
195 die "$0: server error: $_\n";
198 @hh = (); # HTTP header
199 @hb = (); # HTTP body
201 $filename = 'test_'.int(time*1000);
203 serverconnect($server,$port);
205 # HTTP POST variables
206 @pv = qw'from to id keep autodelete comment filesize';
207 foreach my $v (@pv) {
210 push @hb,"--$boundary";
211 push @hb,"Content-Disposition: form-data; name=\"$name\"";
218 push @hb,"--$boundary";
219 push @hb,"Content-Disposition: form-data; name=\"FILE\"; filename=\"$filename\"";
220 push @hb,"Content-Type: application/octet-stream";
223 push @hb,"--$boundary--";
225 $length = length(join('',@hb)) + scalar(@hb)*2 + $mb*M;
227 $hb[-2] = '(file content)';
230 push @hh,"POST $proxy_prefix/fup HTTP/1.1";
231 push @hh,"Host: $server:$port";
232 push @hh,"User-Agent: $useragent";
233 push @hh,"Content-Length: $length";
234 push @hh,"Content-Type: multipart/form-data; boundary=$boundary";
235 push @hh,"Connection: close";
239 printf "--> $_\n" foreach (@hh,@hb);
244 nvtsend(@hh,@hb) or die "$0: server has closed the connection\n";
246 $t0 = $t2 = int(time);
252 print {$SH} $buf or die "$0: server has closed the connection\n";
256 if (-t STDOUT and $t2-$t1>1) {
257 # smaller block size is better on slow links
258 if ($t1 and $bs>4096 and $bt/($t2-$t0)<65536) {
263 printf STDERR "upload: %s MB of %d MB, %d kB/s \r",
268 printf STDERR "upload: %s kB of %d MB, %d kB/s \r",
276 last if $bt >= $mb*M;
280 print {$SH} "\r\n--$boundary--\r\n";
284 print "<-- $_\n" if $opt_v;
285 last if @r and $r[0] =~ / 204 / and /^$/ or /<\/html>/i;
290 printf STDERR "upload: %d MB in %d s, %d kB/s \n",
291 int($bt/M),$tt,int($bt/k/$tt);
302 my @rc = ('A'..'Z','a'..'z',0..9 );
306 for (1..$n) { $rs .= $rc[int(rand($rn))] };
312 my ($server,$port) = @_;
313 my $connect = "CONNECT $server:$port HTTP/1.1";
317 tcpconnect(split(':',$proxy));
319 printf "--> %s\n",$connect if $opt_v;
320 nvtsend($connect,"");
323 printf "<-- $_"if $opt_v;
324 unless (/^HTTP.1.. 200/) {
325 die "$0: proxy error : $_";
327 eval "use IO::Socket::SSL";
328 die "$0: cannot load IO::Socket::SSL\n" if $@;
329 $SH = IO::Socket::SSL->start_SSL($SH);
332 tcpconnect($server,$port);
337 # set up tcp/ip connection
339 my ($server,$port) = @_;
347 eval "use IO::Socket::SSL";
348 die "$0: cannot load IO::Socket::SSL\n" if $@;
349 $SH = IO::Socket::SSL->new(
355 $SH = IO::Socket::INET->new(
365 die "$0: cannot connect $server:$port - $@\n";
368 print "TCPCONNECT to $server:$port\n" if $opt_v;
373 local $SIG{PIPE} = sub { $sigpipe = "@_" };
377 die "$0: internal error: no active network handle\n" unless $SH;
378 die "$0: remote host has closed the link\n" unless $SH->connected;
380 foreach my $line (@_) {
381 print {$SH} $line,"\r\n";
393 my @d = localtime((stat shift)[9]);
394 return sprintf('%d%02d%02d',$d[5]+1900,$d[4]+1,$d[3]);
401 my ($t0,$t1,$t2,$tt,$kBs,$b,$bt,$tb,$B,$buf);
405 serverconnect($server,$port);
408 "GET $proxy_prefix$fop HTTP/1.1",
409 "User-Agent: $useragent",
410 "Host: $server:$port",
414 die "$0: no response from fex server $server\n" unless $_;
417 if (/^HTTP\/[\d.]+ 2/) {
418 warn "<-- $_" if $opt_v;
421 print "<-- $_" if $opt_v;
423 if (/^Content-length:\s*(\d+)/i) {
428 s/HTTP\/[\d.]+ \d+ //;
429 die "$0: bad server reply: $_";
432 $t0 = $t1 = $t2 = int(time);
434 while ($B < $length and $b = read $SH,$buf,$bs) {
439 if (int($t2) > $t1) {
440 $kBs = int($bt/k/($t2-$t1));
441 $kBs = int($tb/k/($t2-$t0)) if $kBs < 10;
444 # smaller block size is better on slow links
445 $bs = 4096 if $bs>4096 and $tb/($t2-$t0)<65536;
446 printf STDERR "download: %d MB in %d s, %d kB/s \r",
447 int($tb/M),$t2-$t0,$kBs;
453 $kBs = int($tb/k/($tt||1));
454 printf STDERR "download: %d MB in %d s, %d kB/s \n",
463 foreach $head (@head) {
464 print "--> $head\n" if $opt_v;
465 print {$SH} $head,"\r\n";
467 print "-->\n" if $opt_v;