/usr/share/perl5/Plack/App/Proxy.pm is in libplack-app-proxy-perl 0.29-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | package Plack::App::Proxy;
use strict;
use 5.008_001;
use parent 'Plack::Component';
use Plack::Util::Accessor qw/remote preserve_host_header backend options/;
use Plack::Request;
use Plack::Util;
use HTTP::Headers;
our $VERSION = '0.29';
sub prepare_app {
my $self = shift;
$self->backend($ENV{PLACK_PROXY_BACKEND} || 'AnyEvent::HTTP') unless defined $self->backend;
}
# hop-by-hop headers (see also RFC2616)
my @hop_by_hop = qw(
Connection Keep-Alive Proxy-Authenticate Proxy-Authorization
TE Trailer Transfer-Encoding Upgrade
);
sub filter_headers {
my $self = shift;
my ( $headers ) = @_;
# Save from known hop-by-hop deletion.
my @connection_tokens = $headers->header('Connection');
# Remove hop-by-hop headers.
$headers->remove_header( $_ ) for @hop_by_hop;
# Connection header's tokens are also hop-by-hop.
for my $token ( @connection_tokens ){
$headers->remove_header( $_ ) for split /\s*,\s*/, $token;
}
}
sub build_url_from_env {
my($self, $env) = @_;
return $env->{'plack.proxy.url'}
if exists $env->{'plack.proxy.url'};
my $url = $env->{'plack.proxy.remote'} || $self->remote
or return;
# avoid double slashes
$url =~ s!/$!! unless $env->{SCRIPT_NAME} && $env->{SCRIPT_NAME} =~ m!/$!;
$url .= $env->{PATH_INFO} || '';
$url .= '?' . $env->{QUERY_STRING} if defined $env->{QUERY_STRING} && length $env->{QUERY_STRING} > 0;
return $url;
}
sub build_headers_from_env {
my($self, $env, $req) = @_;
my $headers = $req->headers->clone;
$headers->header("X-Forwarded-For" => $env->{REMOTE_ADDR});
$headers->remove_header("Host") unless $self->preserve_host_header;
$self->filter_headers( $headers );
+{ map {$_ => scalar $headers->header($_) } $headers->header_field_names };
}
sub call {
my ($self, $env) = @_;
unless ($env->{'psgi.streaming'}) {
die "Plack::App::Proxy only runs with the server with psgi.streaming support";
}
my $url = $self->build_url_from_env($env)
or return [502, ["Content-Type","text/html"], ["Can't determine proxy remote URL"]];
my $req = Plack::Request->new($env);
my $headers = $self->build_headers_from_env($env, $req);
my $method = $env->{REQUEST_METHOD};
my $content = $req->content;
my $backend_class = Plack::Util::load_class(
$self->backend, 'Plack::App::Proxy::Backend'
);
return $backend_class->new(
url => $url,
req => $req,
headers => $headers,
method => $method,
content => $content,
options => $self->options,
response_headers => sub { $self->response_headers(@_) },
)->call($env);
}
sub response_headers {
my ($self, $headers) = @_;
$self->filter_headers( $headers );
# Remove PSGI forbidden headers
$headers->remove_header('Status');
my @headers;
$headers->scan( sub { push @headers, @_ } );
return @headers;
}
1;
__END__
=head1 NAME
Plack::App::Proxy - proxy requests
=head1 SYNOPSIS
use Plack::Builder;
# proxy all requests for /static to 127.0.0.1:80
builder {
mount "/static" => Plack::App::Proxy->new(remote => "http://127.0.0.1:80")->to_app;
};
# Call from other app
my $proxy = Plack::App::Proxy->new->to_app;
my $app = sub {
my $env = shift;
...
$env->{'plack.proxy.url'} = $url;
$proxy->($env);
};
=head1 DESCRIPTION
Plack::App::Proxy is a middleware-aware proxy application for Plack.
=head1 OPTIONS
=over 4
=item remote
Plack::App::Proxy->new(remote => 'http://perl.org')->to_app;
Specifies the base remote URL to proxy requests to.
builder {
mount "/example",
Plack::App::Proxy->new(remote => 'http://example.com/app/foo')->to_app;
};
This proxies incoming requests for C</example/bar> proxied to
C<http://example.com/app/foo/bar>.
=item preserve_host_header
Preserves the original Host header, which is useful when you do
reverse proxying to the internal hosts.
=item backend
The HTTP backend to use. This dist comes with C<LWP> and C<AnyEvent::HTTP>
backends. C<AnyEvent::HTTP> is the default if no backend is specified.
=item options
The options for the HTTP backend instance.
=back
=head1 MIDDLEWARE CONFIGURATIONS
This application is just like a normal PSGI application and is
middleware aware, which means you can modify proxy requests (and
responses) using Plack middleware stack.
It also supports the following special environment variables:
=over 4
=item plack.proxy.url
Overrides the proxy request URL.
=item plack.proxy.remote
Overrides the base URL path to proxy to.
=back
For example, the following builder code allows you to proxy all GET
requests for .png paths to the lolcat image (yes, a silly example) but
proxies to the internal host otherwise.
my $mw = sub {
my $app = shift;
sub {
my $env = shift;
if ($env->{REQUEST_METHOD} eq 'GET' && $env->{PATH_INFO} =~ /\.png$/) {
$env->{'plack.proxy.url'} = 'http://lolcat.example.com/lol.png';
}
$app->($env);
};
};
use Plack::Builder;
builder {
enable $mw;
Plack::App::Proxy->new(remote => 'http://10.0.0.1:8080')->to_app;
};
=head1 AUTHOR
Lee Aylward
Masahiro Honma
Tatsuhiko Miyagawa
Jesse Luehrs
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
L<Plack::Builder>
=cut
|