I found this code from Mojolicious::Plugin::Proxy works like a charm:

$self->client->async->get($url, sub {
    my ($self, $tx) = @_;
            
    if (my $res=$tx->success) {
      $self->tx->res($res);
      $self->rendered;
    }else {
      my ($msg,$error) = $tx->error;
      $self->tx->res->headers->add('X-Remote-Status',$error.': '.$msg); 
      $self->render(
        status => 500,
        text => 'Failed to fetch data from backend'
      );
    }
  })->start;

On Friday, February 3, 2017 at 1:05:52 PM UTC+1, rasta wrote:
>
> Hi!
>
> I'm using mojolicious 6.11. I have a proxy environment like this:
>
> apache -> hypnotoad -> tomcat
>
> and the proxy code looks like this:
>
> sub proxy {
>
> ...
>
>   if (Mojo::IOLoop->is_running) {
>     $self->render_later;
>     $self->ua->get(
>       $url,
>       sub {
>         my ($c, $tx) = @_;
>         _proxy_tx($self, $tx);
>       }
>     );
>   }else {
>     my $tx = $self->ua->get($url);
>     _proxy_tx($self, $tx);
>   }
> }
>
> sub _proxy_tx {
>   my ($c, $tx) = @_;
>   if (my $res = $tx->success) {
>     $c->tx->res($res);
>     $c->rendered;
>   }
>   else {
>     my $error = $tx->error;
>     $c->tx->res->headers->add('X-Remote-Status', $error->{code} . ': ' . 
> $error->{message});
>     $c->render(status => 500, text => 'Failed to fetch data: 
> '.$c->app->dumper($tx->error));
>   }
> }
>
> Am I doing this right at all? I thought it's going to stream the stuff 
> through but that's not happening, mojo writes the data down to it's 
> mojo.tmp file and it seems that nothing comes out unless it fetched the 
> whole file from tomcat. The problem is it works for smaller files but when 
> I'm proxying 2.7GB file after the mojo.tmp file reaches ~ 2.5GB I'll get 
>
> The proxy server received an invalid response from an upstream server.
> The proxy server could not handle the request *GET 
> /api/object/o:292171/diss/Content/download 
> <https://services.phaidra.univie.ac.at/api/object/o:292171/diss/Content/download>*
> .
>
> Reason: *Error reading from remote server*
>
> from Apache. In tomcat log it seems the file was sent OK (the bytesize 
> matches and the response is 200). Mojo will log
> "Nothing has been rendered, expecting delayed response"
> before it starts proxying but nothing afterwards.
>
> Any idea what could be wrong here or how to do this better? Otherwise I'd 
> have to send a redirect but I'd like to avoid that.
>
> Thanks!
> Rasta
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to