http://qs1969.pair.com?node_id=1097361

hardburn has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to write out a filehandle using Dancer2. The data may come from a live video stream, so the filehandle must be streamed in chunks, not returned all at once.

The documention for Dancer2::Manual has this to say about send_file():

"You can use around to get all the content (whether a filehandle if it's a regular file or a full string if it's a scalar ref) and decide what to do with it"

This and the example code suggests to me that I can pass the filehandle as the first argument to send_file(), and then the around callback will get the filehandle as its second argument. Here's an attempt at that:

return send_file( $in_fh, streaming => 1, content_type => $mime_type, callbacks => { around => sub { my ($writer, $content) = @_; my $buf = ''; while( read( $content, $buf, VID_READ_LENGTH ) ) { $writer->write( $buf ); } } }, );

This results in the error:

Device::WebIO::Dancer:5371] error @2014-08-13 18:01:28> Route exceptio +n: isa check for "path_info" failed: GLOB(0x222a5c8) is not a string! + at (eval 197) line 27.

OK, so the first argument can't be a filehandle. Maybe I can pass a dummy string as a scalar ref (which is documented to work) and then have the callback use $in_fh directly as a closure:

return send_file( \'foo', streaming => 1, content_type => $mime_type, callbacks => { around => sub { my ($writer, $content) = @_; my $buf = ''; while( read( $in_fh, $buf, VID_READ_LENGTH ) ) { $writer->write( $buf ); } } }, );

But the output of this is "foo", as if the callback is not being used at all.

OK, let's try passing a dummy file and see if the callback is called:

return send_file( '/dev/zero', streaming => 1, system_path => 1, content_type => $mime_type, callbacks => { around => sub { my ($writer, $content) = @_; my $buf = ''; while( read( $in_fh, $buf, VID_READ_LENGTH ) ) { $writer->write( $buf ); } } }, );

Even with system_path and /dev/zero existing on my system, this gives an HTTP 404 error.

Is there any way to coerce Dancer2 into streaming the filehandle? It feels like this shouldn't require callbacks at all, just:

return send_file( '$in_fh, streaming => 1, content_type => $mime_type, );

"There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Replies are listed 'Best First'.
Re: Write a Filehandle w/Dancer2
by hardburn (Abbot) on Aug 14, 2014 at 01:04 UTC

    Annnnndddddd . . . it's not actually implemented in Dancer2 yet. Stepping through with the debugger, into Dancer2::Core::App::send_file() finds:

    571==> ( ref($path) eq 'SCALAR' ) 572 and return $$path;

    Which explains why it just returns when a scalarref is passed. There's also nowhere in the code that actually handles the callback. Plus, there's a comment at the end of:

    595 # TODO Streaming support

    So, that's that, then.


    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Hi, I just saw that to in Dancer2::Core::App

      There's also nowhere in the code that actually handles the callback.

      Sure there is, send_file creates a Dancer2::Handler::File , which does that

      Regarding streaming the https://metacpan.org/pod/Dancer2::Manual#send_file says Send file supports streaming possibility using PSGI streaming. The server should support it but normal streaming is supported on most, if not all.

      So , give it real pathname (not a scalar reference), and try override

        Tried this, but it gives a 404 error:

        return send_file( '/dev/null', streaming => 1, system_path => 1, content_type => $mime_type, callbacks => { override => sub { my ($respond, $response) = @_; my $writer = $respond->([ 200, {} ]); my $buf = ''; while( read( $in_fh, $buf, VID_READ_LENGTH ) ) { $writer->write( $buf ); } } }, );

        Debugger still shows that the callback isn't called.


        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Re: Write a Filehandle w/Dancer2
by Anonymous Monk on Aug 13, 2014 at 23:41 UTC
    Probably similar to Dancer-1.x, see Re^2: Dancer as a proxy, it says
    get '/stream_example' => sub { return send_file( \'fake contents to be replaced in override', streaming => 1, callbacks => { override => sub {...

      Tried the below off of the Dancer2 docs, but it returns 'foo':

      return send_file( \'foo', streaming => 1, content_type => $mime_type, callbacks => { override => sub { my ($respond, $response) = @_; my $writer = $respond->([ 200, {} ]); my $buf = ''; while( read( $in_fh, $buf, VID_READ_LENGTH ) ) { $writer->write( $buf ); } } }, );

      Running in the debugger shows that the callback never gets called.


      "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

        ... Running in the debugger shows that the callback never gets called.

        Well :) time to UTSL good buddy