summaryrefslogtreecommitdiffstats
path: root/lib/Plack/Client/Backend/http.pm
blob: c5c0a7f83749217d655987aaff8ed475deded9b0 (plain) (blame)
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
package Plack::Client::Backend::http;
use strict;
use warnings;
# ABSTRACT: backend for handling HTTP requests

use Plack::App::Proxy;

use base 'Plack::Client::Backend';

=head1 SYNOPSIS

  Plack::Client->new(
      'http' => {},
  );

  Plack::Client->new(
      'http' => Plack::Client::Backend::http->new,
  );

=head1 DESCRIPTION

This backend implements HTTP requests. The current implementation uses
L<Plack::App::Proxy> to make the request.

=cut

=method new

Constructor. Takes two optional arguments:

=over 4

=item proxy_args

Hashref of arguments to pass to the L<Plack::App::Proxy> constructor.

=item proxy

L<Plack::App::Proxy> object to use for requests.

=back

=cut

sub new {
    my $class = shift;
    my %args = @_;

    $args{proxy} ||= Plack::App::Proxy->new(
        exists $args{proxy_args} ? $args{proxy_args} : ()
    );

    my $self = $class->SUPER::new(@_);

    $self->{proxy} = $args{proxy}->to_app;

    return $self;
}

sub _proxy { shift->{proxy} }

=method app_from_request

Takes a L<Plack::Request> object, and returns an app which will retrieve the
HTTP resource.

=cut

sub app_from_request {
    my $self = shift;
    my ($req) = @_;

    my $uri = $req->uri->clone;
    $uri->path('/');
    $uri->query(undef);
    $req->env->{'plack.proxy.remote'} = $uri->as_string;
    return $self->_proxy;
}

1;