Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^5: Download big file in chunks with plack response

by Thenothing (Acolyte)
on Apr 24, 2021 at 00:49 UTC ( #11131677=note: print w/replies, xml ) Need Help??

in reply to Re^4: Download big file in chunks with plack response
in thread Download big file in chunks with plack response

First thank you for your participation, The code I posted is all I have, the server is HTTP::Server::PSGI, the memory I have is 1.5gb, the file is a iso of 2.30 GB, the code is running in virtual machine VMware Workstation
  • Comment on Re^5: Download big file in chunks with plack response

Replies are listed 'Best First'.
Re^6: Download big file in chunks with plack response
by kikuchiyo (Hermit) on Apr 24, 2021 at 14:03 UTC

    I can't reproduce.

    I've created a 2 GB file with dd, hardcoded its name in my script in Re: Download big file in chunks with plack response, executed ulimit -v 1000000 to limit available memory, then in the same shell ran the server as plackup From a different terminal I've then ran curl http://localhost:5000 -o /tmp/thing.

    Predictably, the server died with "Out of memory". Then, I've commented out the slurp line, as I've suggested earlier. Predictably, the server proceeded to serve the file and didn't die.

    What are you actually doing, and what is your exact error message?

    (I had to add a line \&return_psgi; for it to work with plackup, and I hardcoded the file name because I'm not aware of a way for a PSGI a handler to get a file name as an argument, so I suspect there is something else in the picture that you're not telling us.)

      Hello there kikuchiyo
      A big thank you for you testing, now the code is working

      The problem was, I was using plackup -L Shotgun to run the psgi app, and the error I was getting was.

      Magic number checking on storable string failed at /usr/lib/x86_64-linux-gnu/perl/5.22/ line 420, at /usr/local/share/perl/5.22.1/Plack/Loader/ line 35. Sometimes I get the error memory

      But when I change to just plackup, everything works
      here is the final code:

      use strict; use warnings; ####use Plack::Response; # doesn't seem to support streaming response use Path::Tiny; use File::MimeInfo; my $app = sub { my $env = shift; my $path_file = 'win.iso'; my $content_type = mimetype($path_file); $content_type = 'application/octet-stream' if not defined $content +_type; require File::Spec::Unix; my $basename = $path_file; $basename =~ s|\\|/|g; $basename = ( File::Spec::Unix->splitpath($basename) )[2]; $basename =~ s|[^\w\.-]+|_|g; return sub { my $responder = shift; my $writer = $responder->( [ 200, [ 'Content-Type' => $content_type, 'Content-Disposition' => qq[attachment; filename="$ +basename"], ] ] ); open (FILE, "< $path_file") or die "can't open $path_file: $!" +; binmode FILE; local $/ = \102400; while (<FILE>) { $writer->write($_); } $writer->close(); close FILE; } }
      again thank you so much for your time and your code

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11131677]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2023-12-11 02:03 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.