1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
package Plack::App::Proxy::Backend::LWP;
use strict;
use parent 'Plack::App::Proxy::Backend';
use LWP::UserAgent;
sub call {
my $self = shift;
my ($env) = @_;
return sub {
my $respond = shift;
my $req = HTTP::Request->new(
$self->method => $self->url,
HTTP::Headers->new(%{ $self->headers }),
$self->content
);
my $ua = LWP::UserAgent->new(%{ $self->options || {} });
my $writer;
$ua->add_handler(
response_header => sub {
my ($res) = @_;
$env->{'plack.proxy.last_protocol'} = '1.1'; # meh
$env->{'plack.proxy.last_status'} = $res->code;
$env->{'plack.proxy.last_reason'} = $res->message;
$env->{'plack.proxy.last_url'} = $self->url;
$writer = $respond->([
$res->code,
[$self->response_headers->($res->headers)],
]);
},
);
$ua->add_handler(
response_data => sub {
my (undef, undef, undef, $data) = @_;
$writer->write($data);
return 1;
},
);
$ua->add_handler(
response_done => sub {
$writer->close if $writer;
},
);
my $res = $ua->simple_request($req);
return if $writer;
$respond->([
$res->code,
[$self->response_headers->($res->headers)],
[$res->content],
]);
};
}
1;
__END__
=head1 NAME
Plack::App::Proxy::Backend::LWP - backend which uses LWP::UserAgent
=head1 SYNOPSIS
my $app = Plack::App::Proxy->new(backend => 'LWP')->to_app;
=head1 DESCRIPTION
This backend uses L<LWP::UserAgent> to make HTTP requests.
=head1 AUTHOR
Lee Aylward
Masahiro Honma
Tatsuhiko Miyagawa
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|