http://www.perlmonks.org?node_id=1032881


in reply to Re: Dancer as a proxy
in thread Dancer as a proxy

streaming support depends on the webserver as much as the framework , and a grep http://search.cpan.org/grep?cpanid=YANICK&release=Dancer-1.3113&string=streaming&i=1&n=1&C=3 shows Dancer higher than 1.3072 supports streaming

Streaming data using dancer, Re^3: Resume downloads, Re^2: file download with connection problems, http://search.cpan.org/perldoc/Dancer#send_file

This was really hard to write as the documentation has holes on this, there are no tests on this in test suite, and Dancer hides the errors from these callbacks, so you have to eval { ...; 1 } or warn $@;

get '/stream_example' => sub { return send_file( \'fake contents to be replaced in override', streaming => 1, callbacks => { override => sub { eval { my ( $respond, $response ) = @_; require LWP; my $ua = LWP::UserAgent->new; my $writer ; $ua->get( 'http://www.example.com', ':content_cb' => sub { my ( $data, $response, $protocol ) = @_; if(not $writer ){ my $h = $response->headers ; my $r = [ $response->code, [ map { $_ => $h->{$_}, } 'last-modified' , 'date' , 'content-length' , +'etag' , 'content-type' ], ]; $writer = $respond->( $r ); } $writer->write( $data ); }, ## end of :content_cb ); ## end of $ua->get } or warn "\n\n OH NO\n$@\n\n"; return; }, ## end of override }, ); ## end of send_file }; ## end of get '/stream_example'

Replies are listed 'Best First'.
Re^3: Dancer as a proxy
by gsiems (Deacon) on May 14, 2013 at 21:56 UTC

    Thank you Anonymous Monk! I did need to change it a bit to make it work as I was having issues getting the headers information and things appear to working rather nicely now.

    For future reference, and in case others find it useful, the modified send_file is:

    return send_file( \'fake contents to be replaced in override', streaming => 1, callbacks => { override => sub { eval { my ( $respond, $response ) = @_; require LWP; my $ua = LWP::UserAgent->new; my $writer ; $ua->get( $new_route, ':content_cb' => sub { my ( $data, $response, $protocol ) = @_; if(not $writer ){ my $h = $response->headers ; my %m = map {$_ => $h->header($_)} ('Content-Length', 'Content-Type', 'Last-Modified' +); $writer = $respond->( [ $response->code, [ %m ] ] ); } $writer->write( $data ); }, ## end of :content_cb ); ## end of $ua->get } or Dancer::Logger::error("stream-file: bad eval $@"); return; }, ## end of override }, ); ## end of send_file