correct static file serving and implement progress bar
authorDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Jul 2009 17:07:48 +0000 (17:07 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Thu, 30 Jul 2009 17:07:48 +0000 (17:07 +0000)
lib/PXElator/httpd.pm

index 331569a..fd451b7 100644 (file)
@@ -30,23 +30,35 @@ use html;
 sub static {
        my ($client,$path) = @_;
 
-       $path = "tftp/$path";
+       my $full = "$server::base_dir/tftp/$path";
 
-       if ( ! -e $path ||  -d $path ) {
-               print $client "HTTP/1.0 404 $path not found\r\n";
-               return;
-       }
+       return if ! -f $full;
 
-       my $type = 'text/plain';
+       my $type = 'application/octet-stream';
        $type = 'text/html' if $path =~ m{\.htm};
        $type = 'application/javascript' if $path =~ m{\.js};
+       $type = 'text/plain' if $path =~ m{\.txt};
+
+       my $size = -s $full || return;
+
+       print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: $size\r\nConnection: close\r\n\r\n";
 
-       print $client "HTTP/1.0 200 OK\r\nContent-Type: $type\r\nContent-Length: ", -s $path,"\r\n\r\n";
-       open(my $html, $path);
-       while(<$html>) {
-               print $client $_;
+       open(my $fh, $full);
+       print "static $path $type $size\n";
+
+       my $block = 8192;
+       my $buff;
+       my $pos = 0;
+
+       while( my $len = read $fh, $buff, $block ) {
+               print $client $buff;
+               $pos += $len;
+               printf "%s %d/%d %.2f%%\r", $path, $pos, $size, $pos * 100 / $size;
        }
-       close($html);
+       close($fh);
+       close($client);
+
+       print "$path $pos == $size OK\n";
 
        return $path;
 }