From 459dd0062bfc3ff82ee10a01e7dbb34ae003ce21 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 7 Jan 2011 16:43:29 -0600 Subject: factor out scheme handling into separate backends docs still need updating --- lib/Plack/Client/Backend/http.pm | 28 +++++++++++++++++++++++ lib/Plack/Client/Backend/psgi_local.pm | 41 ++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 lib/Plack/Client/Backend/http.pm create mode 100644 lib/Plack/Client/Backend/psgi_local.pm (limited to 'lib/Plack/Client') diff --git a/lib/Plack/Client/Backend/http.pm b/lib/Plack/Client/Backend/http.pm new file mode 100644 index 0000000..9982cd1 --- /dev/null +++ b/lib/Plack/Client/Backend/http.pm @@ -0,0 +1,28 @@ +package Plack::Client::Backend::http; +use strict; +use warnings; + +use Plack::App::Proxy; + +sub new { + my $class = shift; + my %params = @_; + + bless { + proxy => Plack::App::Proxy->new->to_app, + }, $class; +} + +sub proxy { shift->{proxy} } + +sub app_from_req { + my $self = shift; + my ($req) = @_; + + my $uri = $req->uri->clone; + $uri->path('/'); + $req->env->{'plack.proxy.remote'} = $uri->as_string; + return $self->proxy; +} + +1; diff --git a/lib/Plack/Client/Backend/psgi_local.pm b/lib/Plack/Client/Backend/psgi_local.pm new file mode 100644 index 0000000..212479e --- /dev/null +++ b/lib/Plack/Client/Backend/psgi_local.pm @@ -0,0 +1,41 @@ +package Plack::Client::Backend::psgi_local; +use strict; +use warnings; + +use Plack::Middleware::ContentLength; + +sub new { + my $class = shift; + my %params = @_; + + die 'apps must be a hashref' + if exists($params{apps}) && ref($params{apps}) ne 'HASH'; + + bless { + apps => $params{apps}, + }, $class; +} + +sub apps { shift->{apps} } + +sub app_for { + my $self = shift; + my ($for) = @_; + return $self->apps->{$for}; +} + +sub app_from_req { + my $self = shift; + my ($req) = @_; + + my $app_name = $req->env->{'plack.client.authority'}; + if (!defined $app_name) { + $app_name = $req->uri->authority; + $app_name =~ s/(.*):.*/$1/; # in case a port was added at some point + } + my $app = $self->app_for($app_name); + die "Unknown app: $app_name" unless $app; + return Plack::Middleware::ContentLength->wrap($app); +} + +1; -- cgit v1.2.3-54-g00ecf