blib/lib/Plack/Middleware/MockProxyFrontend.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 118 CC 10 SCAR 34.5
CC
10
Cov
0%
call
72.0
new
12.0
BEGIN
2.0
CRAP 31.5
1 use 5.014; use warnings;
2  
3 package Plack::Middleware::MockProxyFrontend;
4  
5 our $VERSION = '0.005';
6  
7 BEGIN { require Plack::Middleware; our @ISA = 'Plack::Middleware' }
8 use Plack::Util::Accessor qw( host_acceptor http_server _ssl_context );
9 use URI::Split ();
10 use Plack::Util ();
11 use IO::Socket::SSL ();
12  
13 sub new {
14 my $class = shift;
15 my $self = $class->SUPER::new( @_ );
16  
17 $self->_ssl_context( IO::Socket::SSL::SSL_Context->new(
18 ( map { /^SSL_/ ? ( $_, $self->{ $_ } ) : () } keys %$self ),
19 SSL_server => 1,
20 ) );
21  
22 $self->http_server( do {
23 require HTTP::Server::PSGI;
24 HTTP::Server::PSGI->new;
25 } ) unless $self->http_server;
26  
27 $self;
28 }
29  
30 sub call {
31 my $self = shift;
32 my $env = shift;
33  
34 my ( $scheme, $auth, $path, $query, $client_fh );
35  
36 if ( 'CONNECT' eq $env->{'REQUEST_METHOD'} ) {
37 $client_fh = $env->{'psgix.io'}
38 or return [ 405, [], ['CONNECT is not supported'] ];
39 $auth = $env->{'REQUEST_URI'};
40 $scheme = 'https';
41 }
42 else {
43 ( $scheme, $auth, $path, $query ) = URI::Split::uri_split $env->{'REQUEST_URI'};
44 return [ 400, [], ['Not a proxy request'] ] if not $scheme;
45 return [ 400, [], ['Non-HTTP(S) requests are unsupported'] ] if $scheme !~ /\Ahttps?\z/i;
46 }
47  
48 my ( $host, $port ) = ( lc $auth ) =~ m{^(?:.+\@)?(.+?)(?::(\d+))?$};
49 $port //= 'https' eq lc $scheme ? 443 : 80;
50  
51 my $acceptor = $self->host_acceptor;
52 return [ 403, [], ['Refused by MockProxyFrontend'] ]
53 if $acceptor and not grep $acceptor->( $host ), $host;
54  
55 $client_fh
56 ? sub {
57 # lie to the client that we have connected to the destination
58 my $writer = shift->( [ 200, [] ] );
59  
60 # client starts SSL handshake only after hearing that the connection succeeded
61 my $conn = IO::Socket::SSL->new_from_fd(
62 fileno $client_fh,
63 SSL_server => 1,
64 SSL_reuse_ctx => $self->_ssl_context,
65 );
66  
67 # the client thinks it is establishing an SSL connection with the destination
68 # if this fails, that failure is already communicated at the SSL layer
69 # the proxy server only sees opaque traffic, it has no idea what happened
70 # all it knows is that the connection has ended, so the request is simply over
71 $writer->close, return if not $conn;
72  
73 # now act as the destination
74 $self->http_server->handle_connection( {
75 'psgi.url_scheme' => $scheme,
76 SERVER_NAME => $host,
77 SERVER_PORT => $port,
78 SCRIPT_NAME => '',
79 'psgix.io' => $conn,
80 # pass-through
81 REMOTE_ADDR => $env->{'REMOTE_ADDR'},
82 REMOTE_PORT => $env->{'REMOTE_PORT'},
83 'psgi.errors' => $env->{'psgi.errors'},
84 'psgi.version' => $env->{'psgi.version'},
85 # constants
86 'psgi.run_once' => Plack::Util::TRUE,
87 'psgi.multithread' => Plack::Util::FALSE,
88 'psgi.multiprocess' => Plack::Util::FALSE,
89 'psgi.streaming' => Plack::Util::TRUE,
90 'psgi.nonblocking' => Plack::Util::FALSE,
91 'psgix.input.buffered' => Plack::Util::TRUE,
92 }, $conn, $self->app );
93  
94 $conn->close;
95 $writer->close;
96 }
97 : $self->app->( {
98 %$env,
99 'psgi.url_scheme' => $scheme,
100 HTTP_HOST => $host,
101 SERVER_PORT => $port,
102 REQUEST_URI => ( join '?', $path, $query // () ),
103 PATH_INFO => $path =~ s!%([0-9]{2})!chr hex $1!rge,
104 } );
105 }
106  
107 1;
108  
109 __END__