File Coverage

blib/lib/Plack/App/Proxy/Backend/HTTP/Tiny.pm
Criterion Covered Total %
statement 17 41 41.4
branch 0 10 0.0
condition n/a
subroutine 6 8 75.0
pod 1 1 100.0
total 24 60 40.0


line stmt bran cond sub pod time code
1             package Plack::App::Proxy::Backend::HTTP::Tiny;
2              
3             =head1 NAME
4              
5             Plack::App::Proxy::HTTP::Tiny - backend for Plack::App::Proxy
6              
7             =head1 SYNOPSIS
8              
9             =for markdown ```perl
10              
11             # In app.psgi
12             use Plack::Builder;
13             use Plack::App::Proxy::Anonymous;
14              
15             builder {
16             enable "Proxy::Requests";
17             Plack::App::Proxy->new(backend => 'HTTP::Tiny')->to_app;
18             };
19              
20             =for markdown ```
21              
22             =head1 DESCRIPTION
23              
24             This backend uses L to make HTTP requests.
25              
26             L is a wrapper for L which is
27             Pure-Perl only and doesn't require any architecture specific files.
28              
29             It is possible to bundle it e.g. by L.
30              
31             =for readme stop
32              
33             =cut
34              
35 1     1   56544 use 5.006;
  1         3  
36              
37 1     1   5 use strict;
  1         2  
  1         17  
38 1     1   3 use warnings;
  1         2  
  1         46  
39              
40             our $VERSION = '0.0201';
41              
42 1     1   395 use parent qw(Plack::App::Proxy::Backend);
  1         234  
  1         16  
43              
44 1     1   12228 use HTTP::Headers;
  1         5414  
  1         31  
45              
46 1     1   360 use HTTP::Tiny::PreserveHostHeader;
  1         37195  
  1         283  
47              
48             sub call {
49 0     0 1   my ($self, $env) = @_;
50              
51             return sub {
52 0     0     my ($respond) = @_;
53              
54             my $http = HTTP::Tiny::PreserveHostHeader->new(
55             max_redirect => 0,
56 0 0         %{ $self->options || {} }
  0            
57             );
58              
59 0           my $writer;
60              
61             my $response = $http->request(
62             $self->method => $self->url,
63             { headers => $self->headers,
64             content => $self->content,
65             data_callback => sub {
66 0           my ($data, $res) = @_;
67              
68 0 0         return if $res->{status} =~ /^59\d+/;
69              
70 0 0         if (not $writer) {
71 0           $env->{'plack.proxy.last_protocol'} = '1.1'; # meh
72 0           $env->{'plack.proxy.last_status'} = $res->{status};
73 0           $env->{'plack.proxy.last_reason'} = $res->{reason};
74 0           $env->{'plack.proxy.last_url'} = $self->url;
75              
76             $writer = $respond->(
77             [ $res->{status},
78 0           [$self->response_headers->(HTTP::Headers->new(%{ $res->{headers} }))],
  0            
79             ]
80             );
81             }
82              
83 0           $writer->write($data);
84             },
85             }
86 0           );
87              
88 0 0         if ($writer) {
89 0           $writer->close;
90 0           return;
91             }
92              
93 0 0         if ($response->{status} =~ /^59\d/) {
94 0           return $respond->([502, ['Content-Type' => 'text/html'], ["Gateway error: $response->{content}"]]);
95             }
96              
97             return $respond->(
98             [ $response->{status},
99 0           [$self->response_headers->(HTTP::Headers->new(%{ $response->{headers} }))],
100 0           [$response->{content}],
101             ]
102             );
103 0           };
104             }
105              
106             1;
107              
108             =for readme continue
109              
110             =head1 SEE ALSO
111              
112             L, L, L,
113             L.
114              
115             =head1 BUGS
116              
117             This module might be incompatible with further versions of
118             L module.
119              
120             If you find the bug or want to implement new features, please report it at
121             L
122              
123             The code repository is available at
124             L
125              
126             =head1 AUTHOR
127              
128             Piotr Roszatycki
129              
130             =head1 LICENSE
131              
132             Copyright (c) 2014-2016, 2023 Piotr Roszatycki .
133              
134             This is free software; you can redistribute it and/or modify it under
135             the same terms as perl itself.
136              
137             See L