Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Dancer as a proxy

by Corion (Pope)
on May 09, 2013 at 19:54 UTC ( #1032832=note: print w/replies, xml ) Need Help??


in reply to Dancer as a proxy

Take a look at PSGI and what it says about streaming responses. I don't know if Dancer properly supports streaming, but only because I haven't looked at it closely enough.

Replies are listed 'Best First'.
Re^2: Dancer as a proxy
by Anonymous Monk on May 10, 2013 at 03:54 UTC

    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'

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2021-06-16 01:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (74 votes). Check out past polls.

    Notices?