Jelajahi Sumber

Delete Trog::FileHandler, which nothing has loaded since tPSGI took over static files

Static files moved to TPSGI::serve, which carries its own copy of this
code, and TCMS stopped calling Trog::FileHandler then.  The module stayed
in the tree, and the critic annotations that it collected were on code
that no request reaches.  $CHUNK_SEP and $CHUNK_SIZE in Trog::Vars had no
other reader, and their POD described an upload that they were never used
for, so they go too.  The TODO in the blob renderer now names the code
that streams files.

Searched for: Trog::FileHandler, CHUNK_SEP, CHUNK_SIZE across bin, lib, t
and www.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
troglodyne-bot 6 hari lalu
induk
melakukan
cf2e97462b
3 mengubah file dengan 1 tambahan dan 162 penghapusan
  1. 0 148
      lib/Trog/FileHandler.pm
  2. 1 1
      lib/Trog/Renderer/blob.pm
  3. 0 13
      lib/Trog/Vars.pm

+ 0 - 148
lib/Trog/FileHandler.pm

@@ -1,148 +0,0 @@
-package Trog::FileHandler;
-
-use 5.040;
-use re '/aasx';
-use warnings FATAL => 'all';
-
-use POSIX qw{strftime};
-use IO::Compress::Gzip;
-use Time::HiRes qw{tv_interval};
-
-use Trog::Log qw{:all};
-use Trog::Vars;
-use Trog::Utils;
-
-my sub range ( $fullpath, $fh, $ranges, $sz, %headers ) {
-
-    # Set mode
-    my $primary_ct   = "Content-Type: $headers{'Content-type'}";
-    my $is_multipart = scalar(@$ranges) > 1;
-    if ($is_multipart) {
-        $headers{'Content-type'} = "multipart/byteranges; boundary=$Trog::Vars::CHUNK_SEP";
-    }
-    my $code = 206;
-
-    my $fc = '';
-
-    # Calculate the content-length up-front.  We have to fix unspecified lengths first, and reject bad requests.
-    foreach my $range (@$ranges) {
-        $range->[1] //= $sz - 1;
-        INFO("GET 416 31 $fullpath");
-        return [ 416, [%headers], ["Requested range not satisfiable"] ] if $range->[0] > $sz || $range->[0] < 0 || $range->[1] < 0 || $range->[0] > $range->[1];
-    }
-    $headers{'Content-Length'} = List::Util::sum( map { $_->[1] + 1 - $_->[0] } @$ranges );
-
-    #XXX Add the entity header lengths to the value - should hash-ify this to DRY
-    if ($is_multipart) {
-        foreach my $range (@$ranges) {
-            $headers{'Content-Length'} += length("$fc--$Trog::Vars::CHUNK_SEP\n$primary_ct\nContent-Range: bytes $range->[0]-$range->[1]/$sz\n\n");
-            $fc = "\n";
-        }
-        $headers{'Content-Length'} += length("\n--$Trog::Vars::CHUNK_SEP\--\n");
-        $fc = '';
-    }
-
-    #TODO log these requests with their length correctly
-    return sub {
-        my $responder = shift;
-        my $writer;
-
-        foreach my $range (@$ranges) {
-            $headers{'Content-Range'} = "bytes $range->[0]-$range->[1]/$sz" unless $is_multipart;
-            $writer //= $responder->( [ $code, [%headers] ] );
-            $writer->write("$fc--$Trog::Vars::CHUNK_SEP\n$primary_ct\nContent-Range: bytes $range->[0]-$range->[1]/$sz\n\n") if $is_multipart;
-            $fc = "\n";
-
-            my $len = List::Util::min( $sz, $range->[1] + 1 ) - $range->[0];
-
-            $fh->seek( $range->[0], 0 );
-            while ($len) {
-                $fh->read( my $buf, List::Util::min( $len, $Trog::Vars::CHUNK_SIZE ) );
-                $writer->write($buf);
-
-                # Adjust for amount written
-                $len = List::Util::max( $len - $Trog::Vars::CHUNK_SIZE, 0 );
-            }
-        }
-        $fh->close();
-        $writer->write("\n--$Trog::Vars::CHUNK_SEP\--\n") if $is_multipart;
-        $writer->close;
-    };
-}
-
-=head2 serve
-
-Serve a file, with options to stream and cache the output.
-
-=cut
-
-sub serve ( $fullpath, $path, $start, $streaming, $ranges, $last_fetch = 0, $deflate = 0 ) {
-    my $ft = Trog::Utils::mime_type($path);
-    $ft ||= $Trog::Vars::content_types{text};
-
-    my $ct      = 'Content-type';
-    my @headers = ( $ct => $ft );
-
-    #TODO use static Cache-Control for everything but JS/CSS?
-    push( @headers, 'Cache-control' => $Trog::Vars::cache_control{revalidate} );
-
-    push( @headers, 'Accept-Ranges' => 'bytes' );
-
-    my $mt         = ( stat($path) )[9];
-    my $sz         = ( stat(_) )[7];
-    my @gm         = gmtime($mt);
-    my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
-    my $code       = $mt > $last_fetch ? 200 : 304;
-
-    push( @headers, "Last-Modified" => $now_string );
-    push( @headers, 'Vary'          => 'Accept-Encoding' );
-
-    if ( open( my $fh, '<', $path ) ) {    ## no critic (InputOutput::RequireBriefOpen) -- streamed to the client, which closes it
-        return range( $fullpath, $fh, $ranges, $sz, @headers ) if @$ranges && $streaming;
-
-        # Transfer-encoding: chunked
-        return sub {
-            my $responder = shift;
-            push( @headers, 'Content-Length' => $sz );
-            my $writer = $responder->( [ $code, \@headers ] );
-            while ( $fh->read( my $buf, $Trog::Vars::CHUNK_SIZE ) ) {
-                $writer->write($buf);
-            }
-            close($fh);    ## no critic (RequireCheckedSyscalls) -- read to the end, with nothing to lose
-            $writer->close;
-          }
-          if $streaming && $sz > $Trog::Vars::CHUNK_SIZE;
-
-        #Return data in the event the caller does not support deflate
-        if ( !$deflate ) {
-            push( @headers, "Content-Length" => $sz );
-            INFO("GET 200 $sz $fullpath");
-
-            # Append server-timing headers
-            my $tot = tv_interval($start) * 1000;
-            push( @headers, 'Server-Timing' => "file;dur=$tot" );
-
-            return [ $code, \@headers, $fh ];
-        }
-
-        #Compress everything less than 1MB
-        push( @headers, "Content-Encoding" => "gzip" );
-        my $dfh;
-        IO::Compress::Gzip::gzip( $fh => \$dfh );
-        print $IO::Compress::Gzip::GzipError if $IO::Compress::Gzip::GzipError;
-        push( @headers, "Content-Length" => length($dfh) );
-
-        INFO( "GET 200 " . length($dfh) . " $fullpath" );
-
-        # Append server-timing headers
-        my $tot = tv_interval($start) * 1000;
-        push( @headers, 'Server-Timing' => "file;dur=$tot" );
-
-        return [ $code, \@headers, [$dfh] ];
-    }
-
-    INFO("GET 403 23 $fullpath");
-    return [ 403, [ $ct => $Trog::Vars::content_types{text} ], ["STAY OUT YOU RED MENACE"] ];
-}
-
-1;

+ 1 - 1
lib/Trog/Renderer/blob.pm

@@ -10,7 +10,7 @@ Render blobs, such as files stored in a DB.
 
 =cut
 
-# TODO use the streaming code from Trog::FileHandler, etc.
+# TODO stream large blobs, as TPSGI::serve streams files.
 sub render (%options) {
     my $code    = delete $options{code};
     my $headers = delete $options{headers};

+ 0 - 13
lib/Trog/Vars.pm

@@ -15,15 +15,6 @@ them directly rather than paying for a function call on every request.
 
 =over 4
 
-=item $CHUNK_SEP
-
-Separator used when a file upload has to be reassembled from chunks.
-Chosen to be something no browser will ever send us by accident.
-
-=item $CHUNK_SIZE
-
-Size of said chunks, in bytes.  1MB.
-
 =item %content_types
 
 Maps the short names used in templates and route definitions (html, json, ...)
@@ -45,10 +36,6 @@ immutable once published.
 
 =cut
 
-#1MB chunks
-our $CHUNK_SEP  = 'tCMSep666YOLO42069';
-our $CHUNK_SIZE = 1024000;
-
 our %content_types = (
     text  => "text/plain",
     html  => "text/html",