File: HTTP.pm

package info (click to toggle)
libplack-app-proxy-perl 0.29-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 256 kB
  • sloc: perl: 1,897; makefile: 2
file content (105 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (3)
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
package Plack::App::Proxy::Backend::AnyEvent::HTTP;

use strict;
use parent 'Plack::App::Proxy::Backend';
use AnyEvent::HTTP;

sub call {
    my $self = shift;
    my ($env) = @_;

    return sub {
        my $respond = shift;
        my $cv = AE::cv;
        my $writer;
        AnyEvent::HTTP::http_request(
            $self->method => $self->url,
            headers => $self->headers,
            body => $self->content,
            recurse => 0,  # want not to treat any redirections
            persistent => 0,
            %{ $self->options || {} },
            on_header => sub {
                my $headers = shift;

                if ($headers->{Status} !~ /^59\d+/) {
                    $env->{'plack.proxy.last_protocol'} = $headers->{HTTPVersion};
                    $env->{'plack.proxy.last_status'}   = $headers->{Status};
                    $env->{'plack.proxy.last_reason'}   = $headers->{Reason};
                    $env->{'plack.proxy.last_url'}      = $headers->{URL};

                    my $http_headers = HTTP::Headers->new(
                      map { $_ => $headers->{$_} } grep {! /^[A-Z]/} keys %$headers
                    );

                    my $cookies = $http_headers->header( 'Set-Cookie' );
                    if ( $cookies ) {
                        my @cookies = split /,(?=\S+)/, $cookies;
                        $http_headers->header( Set_Cookie => \@cookies );
                    }

                    $writer = $respond->([
                        $headers->{Status},
                        [$self->response_headers->($http_headers)],
                    ]);
                }
                return 1;
            },
            on_body => sub {
              $writer->write($_[0]);
              return 1;
            },
            sub {
                my (undef, $headers) = @_;

                if (!$writer and $headers->{Status} =~ /^59\d/) {
                    $respond->([502, ["Content-Type","text/html"], ["Gateway error: $headers->{Reason}"]]);
                }

                $writer->close if $writer;
                $cv->send;

                # http_request may not release $cb with perl 5.8.8
                # and AE::HTTP 1.44. So free $env manually.
                undef $env;

                # Free the reference manually for perl 5.8.x
                # to avoid nested closure memory leaks.
                undef $respond;
            }
        );
        $cv->recv unless $env->{"psgi.nonblocking"};
    }
}

1;

__END__

=head1 NAME

Plack::App::Proxy::Backend::AnyEvent::HTTP - backend which uses AnyEvent::HTTP

=head1 SYNOPSIS

  my $app = Plack::App::Proxy->new(backend => 'AnyEvent::HTTP')->to_app;

=head1 DESCRIPTION

This backend uses L<AnyEvent::HTTP> to make HTTP requests. This is the default
backend used when no backend is specified in the constructor.

=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