FileHandler.pm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package Trog::FileHandler;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use POSIX qw{strftime};
  7. use Mojo::File;
  8. use Plack::MIME;
  9. use IO::Compress::Gzip;
  10. use Time::HiRes qw{tv_interval};
  11. use Trog::Log qw{:all};
  12. use Trog::Vars;
  13. #TODO consider integrating libfile
  14. #Stuff that isn't in upstream finders
  15. my %extra_types = (
  16. '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  17. );
  18. =head2 serve
  19. Serve a file, with options to stream and cache the output.
  20. =cut
  21. sub serve ( $fullpath, $path, $start, $streaming, $ranges, $last_fetch = 0, $deflate = 0 ) {
  22. my $mf = Mojo::File->new($path);
  23. my $ext = '.' . $mf->extname();
  24. my $ft;
  25. if ($ext) {
  26. $ft = Plack::MIME->mime_type($ext) if $ext;
  27. $ft ||= $extra_types{$ext} if exists $extra_types{$ext};
  28. }
  29. $ft ||= $Trog::Vars::content_types{text};
  30. my $ct = 'Content-type';
  31. my @headers = ( $ct => $ft );
  32. #TODO use static Cache-Control for everything but JS/CSS?
  33. push( @headers, 'Cache-control' => $Trog::Vars::cache_control{revalidate} );
  34. push( @headers, 'Accept-Ranges' => 'bytes' );
  35. my $mt = ( stat($path) )[9];
  36. my $sz = ( stat(_) )[7];
  37. my @gm = gmtime($mt);
  38. my $now_string = strftime( "%a, %d %b %Y %H:%M:%S GMT", @gm );
  39. my $code = $mt > $last_fetch ? 200 : 304;
  40. push( @headers, "Last-Modified" => $now_string );
  41. push( @headers, 'Vary' => 'Accept-Encoding' );
  42. if ( open( my $fh, '<', $path ) ) {
  43. return _range( $fullpath, $fh, $ranges, $sz, @headers ) if @$ranges && $streaming;
  44. # Transfer-encoding: chunked
  45. return sub {
  46. my $responder = shift;
  47. push( @headers, 'Content-Length' => $sz );
  48. my $writer = $responder->( [ $code, \@headers ] );
  49. while ( $fh->read( my $buf, $Trog::Vars::CHUNK_SIZE ) ) {
  50. $writer->write($buf);
  51. }
  52. close $fh;
  53. $writer->close;
  54. }
  55. if $streaming && $sz > $Trog::Vars::CHUNK_SIZE;
  56. #Return data in the event the caller does not support deflate
  57. if ( !$deflate ) {
  58. push( @headers, "Content-Length" => $sz );
  59. # Append server-timing headers
  60. my $tot = tv_interval($start) * 1000;
  61. push( @headers, 'Server-Timing' => "file;dur=$tot" );
  62. return [ $code, \@headers, $fh ];
  63. }
  64. #Compress everything less than 1MB
  65. push( @headers, "Content-Encoding" => "gzip" );
  66. my $dfh;
  67. IO::Compress::Gzip::gzip( $fh => \$dfh );
  68. print $IO::Compress::Gzip::GzipError if $IO::Compress::Gzip::GzipError;
  69. push( @headers, "Content-Length" => length($dfh) );
  70. INFO("GET 200 $fullpath");
  71. # Append server-timing headers
  72. my $tot = tv_interval($start) * 1000;
  73. push( @headers, 'Server-Timing' => "file;dur=$tot" );
  74. return [ $code, \@headers, [$dfh] ];
  75. }
  76. INFO("GET 403 $fullpath");
  77. return [ 403, [ $ct => $Trog::Vars::content_types{text} ], ["STAY OUT YOU RED MENACE"] ];
  78. }
  79. sub _range ( $fullpath, $fh, $ranges, $sz, %headers ) {
  80. # Set mode
  81. my $primary_ct = "Content-Type: $headers{'Content-type'}";
  82. my $is_multipart = scalar(@$ranges) > 1;
  83. if ($is_multipart) {
  84. $headers{'Content-type'} = "multipart/byteranges; boundary=$Trog::Vars::CHUNK_SEP";
  85. }
  86. my $code = 206;
  87. my $fc = '';
  88. # Calculate the content-length up-front. We have to fix unspecified lengths first, and reject bad requests.
  89. foreach my $range (@$ranges) {
  90. $range->[1] //= $sz - 1;
  91. INFO("GET 416 $fullpath");
  92. return [ 416, [%headers], ["Requested range not satisfiable"] ] if $range->[0] > $sz || $range->[0] < 0 || $range->[1] < 0 || $range->[0] > $range->[1];
  93. }
  94. $headers{'Content-Length'} = List::Util::sum( map { my $arr = $_; $arr->[1] + 1, -$arr->[0] } @$ranges );
  95. #XXX Add the entity header lengths to the value - should hash-ify this to DRY
  96. if ($is_multipart) {
  97. foreach my $range (@$ranges) {
  98. $headers{'Content-Length'} += length("$fc--$Trog::Vars::CHUNK_SEP\n$primary_ct\nContent-Range: bytes $range->[0]-$range->[1]/$sz\n\n");
  99. $fc = "\n";
  100. }
  101. $headers{'Content-Length'} += length("\n--$Trog::Vars::CHUNK_SEP\--\n");
  102. $fc = '';
  103. }
  104. return sub {
  105. my $responder = shift;
  106. my $writer;
  107. foreach my $range (@$ranges) {
  108. $headers{'Content-Range'} = "bytes $range->[0]-$range->[1]/$sz" unless $is_multipart;
  109. $writer //= $responder->( [ $code, [%headers] ] );
  110. $writer->write("$fc--$Trog::Vars::CHUNK_SEP\n$primary_ct\nContent-Range: bytes $range->[0]-$range->[1]/$sz\n\n") if $is_multipart;
  111. $fc = "\n";
  112. my $len = List::Util::min( $sz, $range->[1] + 1 ) - $range->[0];
  113. $fh->seek( $range->[0], 0 );
  114. while ($len) {
  115. $fh->read( my $buf, List::Util::min( $len, $Trog::Vars::CHUNK_SIZE ) );
  116. $writer->write($buf);
  117. # Adjust for amount written
  118. $len = List::Util::max( $len - $Trog::Vars::CHUNK_SIZE, 0 );
  119. }
  120. }
  121. $fh->close();
  122. $writer->write("\n--$Trog::Vars::CHUNK_SEP\--\n") if $is_multipart;
  123. $writer->close;
  124. };
  125. }
  126. 1;