summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2010-12-30 11:44:29 -0600
committerJesse Luehrs <doy@tozt.net>2010-12-30 11:44:29 -0600
commitbb0bfecd8ecf0e46a56a054c9509d8d52904ba8d (patch)
tree29a330805e466b034d1b9336f0ee87280760ad68 /lib
parent7d926665df71283ed8dfef33d5b5faf6851782dd (diff)
downloadplack-client-bb0bfecd8ecf0e46a56a054c9509d8d52904ba8d.tar.gz
plack-client-bb0bfecd8ecf0e46a56a054c9509d8d52904ba8d.zip
also allow Plack::Request objects and env hashes for ->request
Diffstat (limited to 'lib')
-rw-r--r--lib/Plack/Client.pm27
1 files changed, 23 insertions, 4 deletions
diff --git a/lib/Plack/Client.pm b/lib/Plack/Client.pm
index 60ae800..91c7f46 100644
--- a/lib/Plack/Client.pm
+++ b/lib/Plack/Client.pm
@@ -31,22 +31,33 @@ sub app_for {
sub request {
my $self = shift;
- my $req = blessed($_[0]) && $_[0]->isa('HTTP::Request')
+ my $req = blessed($_[0]) && ($_[0]->isa('HTTP::Request')
+ || $_[0]->isa('Plack::Request'))
? $_[0]
- : HTTP::Request->new(@_);
+ : ref($_[0]) eq 'HASH'
+ ? Plack::Request->new(@_)
+ : HTTP::Request->new(@_);
+ # both Plack::Request and HTTP::Request have a ->uri method
my $scheme = $req->uri->scheme;
my $res;
if ($scheme eq 'psgi') {
my ($app_key, $path) = $self->_parse_request($req->uri->opaque);
+
+ # to_psgi doesn't like non-http uris
+ $req->uri($path);
+ my $env = $req->isa('HTTP::Request') ? $req->to_psgi : $req->env;
+
my $app = $self->app_for($app_key);
- $req->uri($path); # ?
die 'XXX' unless $app;
- my $psgi_res = $app->($req->to_psgi);
+ my $psgi_res = $app->($env);
die 'XXX' unless ref($psgi_res) eq 'ARRAY';
$res = Plack::Response->new(@$psgi_res);
}
elsif ($scheme eq 'http' || $scheme eq 'https') {
+ $req = $self->_req_from_psgi($req)
+ if $req->isa('Plack::Request');
+
my $http_res = $self->ua->simple_request($req); # or just ->request?
$res = Plack::Response->new(
map { $http_res->$_ } qw(code headers content)
@@ -59,6 +70,14 @@ sub request {
return $res;
}
+sub _req_from_psgi {
+ my $self = shift;
+ my ($req) = @_;
+ return HTTP::Request->new(
+ map { $req->$_ } qw(method uri headers raw_body)
+ );
+}
+
sub _parse_request {
my $self = shift;
my ($req) = @_;