Skip to content

Instantly share code, notes, and snippets.

@hiratara
Forked from miyagawa/gist:291417
Created February 1, 2010 04:31

Revisions

  1. hiratara renamed this gist Feb 1, 2010. 1 changed file with 12 additions and 2 deletions.
    14 changes: 12 additions & 2 deletions gistfile1.txt → gistfile1.PL
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,11 @@ sub call {
    $env->{HTTP_X_PROXY_CAPABILITIES} = 'reproxy-file';

    return sub {
    my $orig_respond = shift;;
    my $orig_respond = shift;

    my $cv = AE::cv;
    my $orig_nonblocking = $env->{"psgi.nonblocking"};
    $env->{"psgi.nonblocking"} = 1;

    my $respond; $respond = sub {
    my $res = shift;
    @@ -23,12 +27,18 @@ sub call {
    write => sub { warn @_ },
    close => sub { };
    } else {
    return $orig_respond->( $res );
    my $orig_writer = $orig_respond->( $res );
    return Plack::Util::inline_object
    write => sub { $orig_writer->write( @_ ) },
    close => sub { $cv->send; };
    }
    };

    my $cb = $self->app->($env);
    $cb->($respond);

    $cv->recv unless $orig_nonblocking;
    $env->{"psgi.nonblocking"} = $orig_nonblocking;
    };
    }

  2. hiratara renamed this gist Feb 1, 2010. 1 changed file with 21 additions and 21 deletions.
    42 changes: 21 additions & 21 deletions gistfile1.perl → gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -3,34 +3,33 @@ package Plack::Middleware::Proxy::Reproxy;
    use parent 'Plack::Middleware';

    use Plack::Util;
    use Try::Tiny;

    sub call {
    my($self, $env) = @_;

    $env->{HTTP_X_PROXY_CAPABILITIES} = 'reproxy-file';

    my $cv = AE::cv;
    my $respond; $respond = sub {
    my $res = shift;
    if (my $url = Plack::Util::header_get($res->[1], 'X-Reproxy-URL')) {
    $env->{'plack.proxy.url'} = $url;
    $env->{REQUEST_METHOD} = 'GET';
    my $cb = $self->app->($env);
    $cb->($respond);
    } else {
    $cv->send($res);
    }

    return Plack::Util::inline_object
    write => sub { warn @_ },
    close => sub { };
    return sub {
    my $orig_respond = shift;;

    my $respond; $respond = sub {
    my $res = shift;
    if (my $url = Plack::Util::header_get($res->[1], 'X-Reproxy-URL')) {
    $env->{'plack.proxy.url'} = $url;
    $env->{REQUEST_METHOD} = 'GET';
    my $cb = $self->app->($env);
    $cb->($respond);
    return Plack::Util::inline_object
    write => sub { warn @_ },
    close => sub { };
    } else {
    return $orig_respond->( $res );
    }
    };

    my $cb = $self->app->($env);
    $cb->($respond);
    };

    my $cb = $self->app->($env);
    $cb->($respond);

    return $cv;
    }

    1;
    @@ -64,3 +63,4 @@ =head1 SEE ALSO
    L<Plack::App::Proxy>

    =cut

  3. @miyagawa miyagawa created this gist Feb 1, 2010.
    66 changes: 66 additions & 0 deletions gistfile1.perl
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    package Plack::Middleware::Proxy::Reproxy;
    use strict;
    use parent 'Plack::Middleware';

    use Plack::Util;
    use Try::Tiny;

    sub call {
    my($self, $env) = @_;

    $env->{HTTP_X_PROXY_CAPABILITIES} = 'reproxy-file';

    my $cv = AE::cv;
    my $respond; $respond = sub {
    my $res = shift;
    if (my $url = Plack::Util::header_get($res->[1], 'X-Reproxy-URL')) {
    $env->{'plack.proxy.url'} = $url;
    $env->{REQUEST_METHOD} = 'GET';
    my $cb = $self->app->($env);
    $cb->($respond);
    } else {
    $cv->send($res);
    }

    return Plack::Util::inline_object
    write => sub { warn @_ },
    close => sub { };
    };

    my $cb = $self->app->($env);
    $cb->($respond);

    return $cv;
    }

    1;

    __END__
    =head1 NAME
    Plack::Middleware::Proxy::Reproxy - Adds reproxy behavior to Plack::App::Proxy
    =head1 SYNOPSIS
    use Plack::Builder;
    use Plack::App::Proxy;
    builder {
    enable "Proxy::Reproxy";
    Plack::App::Proxy->new(host => "http://10.0.1.2:8080/")->to_app;
    };
    =head1 DESCRIPTION
    Plack::Middleware::Proxy::Reproxy adds reproxy capability to Plack::App::Proxy.
    =head1 AUTHOR
    Tatsuhiko Miyagawa
    =head1 SEE ALSO
    L<Plack::App::Proxy>
    =cut