Streaming in any form was the main problem that I had w/Dancer 1. While moving to Dancer 2 was partially influenced by that, it was also because Dancer 2 has nicer routing and other features.

The current code looks like:

sub proxy_request { my $self; $self = shift if ( ( _whoami() )[1] ne (caller)[1] ); my ( $proxy_route, $file_name ) = @_; unless ($proxy_route) { Tranquillus::Util->return_error('BAD_QUERY'); } my $cb = sub { my $respond = $Dancer2::Core::Route::RESPONDER; require LWP; my $ua = LWP::UserAgent->new; my $writer; my %m; $ua->get( $proxy_route, ':content_cb' => sub { my ( $data, $response, $protocol ) = @_; if ( not $writer ) { my $h = $response->headers; my @ary = ( 'Cache-Control', 'Content-Length', 'Content- +Type', 'Last-Modified', 'Content-Disposition' ); foreach my $key (@ary) { if ( $h->header($key) ) { $m{$key} = $h->header($key); } } unless ( exists $m{'Content-Length'} ) { # RFC 7230 # http://tools.ietf.org/html/rfc7230#section-4 $m{'Transfer-Encoding'} = 'chunked'; } if ($file_name) { $m{'Content-Disposition'} ||= 'attachment; fil +ename="' . $file_name . '"'; } $writer = $respond->( [ $response->code, [%m] ] ); } # Ensure that we have a valid writer... if ($writer) { $writer->write($data); } }, ); # Cleanup. # Ensure that we have a valid writer... if ($writer) { if ( exists $m{'Transfer-Encoding'} ) { $writer->write(undef); $writer->write("\r\n"); } $writer->close; } }; my $response = Dancer2::Core::Response::Delayed->new( # error_cb => sub { $weak_self->logger_engine->log( + warning => @_ ) }, cb => $cb, request => $Dancer2::Core::Route::REQUEST, response => $Dancer2::Core::Route::RESPONSE, ); return $response; }

... and can be found at: https://github.com/gsiems/tranquillus/blob/master/lib/Tranquillus/Proxy.pm

Update: fixed the github link.


In reply to Re^3: EOF problem with Dancer streaming proxy by gsiems
in thread EOF problem with Dancer streaming proxy by dsheroh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.