Impersonate/Connection.pm

filter: Statementstmt 82.7% 316 / 382 Branchbran 70.2% 163 / 232 Conditioncond 58.9% 89 / 151 MC/DCmcdc 30.3% 27 / 89 Subroutinesub 80.5% 29 / 36 Podpod 0.0% 0 / 2 totaltotal 69.9% 624 / 892 CC 173 SCAR 33.9
CC
173
Cov
74%
_readable
76.4
_flush
47.5
__ANON__
38.5
CRAP 29.8
↑ Summary Impersonate/HTTP.pm »
1 package Proxy::Impersonate::Connection;
2 5 ▶use v5.10; use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Errno qw(EAGAIN EWOULDBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Net::SSLeay ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Proxy::Impersonate::HTTP qw(parse_request_head body_length coherent_headers response_head strip_hop_headers);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # Per-WebKit-connection state machine. States:
9 # plain -- reading plaintext (initial: HTTP proxy request or CONNECT)
10 # handshake -- Net::SSLeay server handshake in progress (post-CONNECT)
11 # tls -- reading decrypted requests over the terminated tunnel
12 # One request is in flight at a time (keep-alive, not pipelining); the upstream
13 # is driven by the shared Curl::Impersonate::Multi (wired to EV by the daemon).
14  
15 5 my $HIWAT = 262144; # pause upstream when this many bytes are queued to WebKit
16 5 my $IDLE = 120; # close a client that makes no read/write progress for this long
17 5 my $MAXHEAD = 65536; # reject a request head larger than this (also caps a slow-drip head)
18  
19 50 ▶sub new {
Subroutine new: called CC=7 SCAR=21.8
Pod undocumented
20 50 my ($class, %o) = @_;
21 return bless {
22 sock => $o{fd},
23 cert => $o{cert}, # Proxy::Impersonate::Cert
24 multi => $o{multi}, # Curl::Impersonate::Multi (shared)
25 multi_state => $o{multi_state} // {},
26 override => $o{override} // {},# headers forced on every upstream request
27 chrome => $o{chrome} // 0, # is the impersonate target a Chrome family (Accept synthesis)
28 high_entropy => $o{high_entropy} // {}, # high-entropy client hints (Accept-CH-gated)
29 hints => $o{hints} // {}, # shared: host -> { hint-name => 1 }
30 make_handle => $o{make_handle}, # sub { Curl::Impersonate->new(...) }
31 on_request => $o{on_request}, # optional interception hook -- see _forward
32 on_response => $o{on_response}, # optional response hook -- see _forward
33 27 ▶ on_close => $o{on_close} // sub {},
Subroutine __ANON__: called CC=1 SCAR=0.0
34 50 ▶ state => 'plain',
Condition61%8/13
$o{"multi_state"} // {}
l!l
050
$o{"override"} // {}
l!l
149
$o{"chrome"} // 0
l!l
050
$o{"high_entropy"} // {}
l!l
050
$o{"hints"} // {}
l!l
050
$o{"on_close"} // sub { }
l!l&&r!l&&!r
23270
MC/DC0%0/1
$o{"multi_state"} // {}
$o{"multi_state"}
MC/DC100%1/1
$o{"override"} // {}
$o{"override"}
MC/DC0%0/1
$o{"chrome"} // 0
$o{"chrome"}
MC/DC0%0/1
$o{"high_entropy"} // {}
$o{"high_entropy"}
MC/DC0%0/1
$o{"hints"} // {}
$o{"hints"}
MC/DC0%0/2
$o{"on_close"} // sub { }
$o{"on_close"} sub { }
Truth table50%1/2
A
A= $o{"multi_state"}
Aresult
11
00
Truth table100%2/2
A
A= $o{"override"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"chrome"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"high_entropy"}
Aresult
11
00
Truth table50%1/2
A
A= $o{"hints"}
Aresult
11
00
Truth table66%2/3
A // B
A= $o{"on_close"}B= sub { }
ABresult
1X1
011
000
35 rbuf => '',
36 wbuf => '',
37 }, $class;
38 }
39  
40 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my ($self) = @_;
42 0 $self->{sock}->blocking(0);
43 0 $self->_arm_read;
44 # Reclaim a client that stalls -- a partial head, a wedged handshake, or a peer
45 # that stops reading a paused download -- which would otherwise keep this
46 # Connection pinned in the daemon's {conns} with its read watcher armed forever.
47 # A repeating timer reset on I/O progress (_touch); fires only when truly idle.
48 0 ▶ $self->{idle} = EV::timer($IDLE, $IDLE, sub { $self->_close });
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 0 return $self;
50 }
51  
52 # --- EV watcher management (one read + one write watcher, armed as needed) ---
53 0 ▶sub _arm_read { my $s = shift; $s->{io_r} ||= EV::io(fileno($s->{sock}), EV::READ, sub { $s->_readable }) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _arm_read: not called CC=2 SCAR=17.9
Condition0%0/3
$s->{"io_r"} ||= &EV::io(fileno $s->{"sock"}, 1, sub { $s->_readable; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$s->{"io_r"} ||= &EV::io(fileno $s->{"sock"}, 1, sub { $s->_readable; } )
$s->{"io_r"} &EV::io(fileno $s->{"sock"}, 1, sub { $s->_readable; } )
Truth table0%0/3
A ||= B
A= $s->{"io_r"}B= &EV::io(fileno $s->{"sock"}, 1, sub { $s->_readable; } )
ABresult
1X1
011
000
54 0 ▶sub _arm_write { my $s = shift; $s->{io_w} ||= EV::io(fileno($s->{sock}), EV::WRITE, sub { $s->_writable }) }
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine _arm_write: called CC=2 SCAR=7.6
Condition66%2/3
$s->{"io_w"} ||= &EV::io(fileno $s->{"sock"}, 2, sub { $s->_writable; } )
l!l&&r!l&&!r
7810
MC/DC0%0/2
$s->{"io_w"} ||= &EV::io(fileno $s->{"sock"}, 2, sub { $s->_writable; } )
$s->{"io_w"} &EV::io(fileno $s->{"sock"}, 2, sub { $s->_writable; } )
Truth table66%2/3
A ||= B
A= $s->{"io_w"}B= &EV::io(fileno $s->{"sock"}, 2, sub { $s->_writable; } )
ABresult
1X1
011
000
55 50 ▶sub _disarm_write { $_[0]{io_w} = undef }
Subroutine _disarm_write: called CC=1 SCAR=0.0
56 2 ▶sub _touch { $_[0]{idle}->again if $_[0]{idle} } # reset the idle timeout on I/O progress
Subroutine _touch: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"idle"} 0 2 2
57  
58 # --- readable: handshake, or decrypt/read into rbuf, then process ---
59 2 ▶sub _readable {
Subroutine _readable: called CC=17 SCAR=43.4
60 2 my ($self) = @_;
61 2 ▶ return if $self->{_dead} || $self->{_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} or $self->{"_closing"} 0 2 2
Condition33%1/3
$self->{"_dead"} or $self->{"_closing"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$self->{"_dead"} or $self->{"_closing"}
$self->{"_dead"} $self->{"_closing"}
Truth table33%1/3
A or B
A= $self->{"_dead"}B= $self->{"_closing"}
ABresult
1X1
011
000
62 2 $self->_touch;
63 # Bytes arriving during the deferred-TLS window (the 200 is still flushing, so
64 # the tunnel is not up) mean the client sent before reading the 200 -- non-
65 # compliant. Fast-close rather than read them into rbuf, where set_fd's later
66 # handshake could never see them (they would stall to the idle timeout). This
67 # mirrors the coalesced-ClientHello guard on the immediate CONNECT path.
68 2 ▶ return $self->_close if $self->{_tls_pending};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_tls_pending"} 1 1 2
69 1 ▶ return $self->_do_handshake if $self->{state} eq 'handshake';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} eq "handshake" 0 1 1
70 1 ▶ if ($self->{ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl'}) { } 1 0 1
71 1 while (1) {
72 1 my ($got, $rv) = Net::SSLeay::read($self->{ssl}, 32768);
73 0 ▶ if (defined $rv && $rv > 0) { $self->{rbuf} .= $got; next } # drain pending
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rv and $rv > 0) 0 1 1
Condition33%1/3
defined $rv and $rv > 0
!ll&&!rl&&r
010
MC/DC0%0/2
defined $rv and $rv > 0
defined $rv $rv > 0
Truth table33%1/3
A and B
A= defined $rvB= $rv > 0
ABresult
0X0
100
111
74 1 ▶ my $e = Net::SSLeay::get_error($self->{ssl}, defined $rv ? $rv : -1);
Branch: 1 branch
BranchTrueFalseTotal
defined $rv ? : 1 0 1
75 1 ▶ last if $e == Net::SSLeay::ERROR_WANT_READ();
Branch: 1 branch
BranchTrueFalseTotal
if $e == Net::SSLeay::ERROR_WANT_READ() 1 0 1
76 0 ▶ if ($e == Net::SSLeay::ERROR_WANT_WRITE()) { $self->_arm_write; last }
Branch: 1 branch
BranchTrueFalseTotal
if ($e == Net::SSLeay::ERROR_WANT_WRITE()) 0 0 0
77 0 return $self->_close; # ZERO_RETURN (EOF) or error
78 }
79 } else {
80 0 my $n = sysread($self->{sock}, my $data, 32768);
81 0 ▶ if (!defined $n) { return if $! == EAGAIN || $! == EWOULDBLOCK; return $self->_close }
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $n) 0 0 0
if $! == 11 or $! == 11 0 0 0
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
82 0 ▶ return $self->_close if $n == 0; # EOF
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
83 0 $self->{rbuf} .= $data;
84 }
85 1 $self->_process;
86 # a prior SSL_write that wanted-read retries now that we've fed the SSL layer
87 1 ▶ $self->_flush if !$self->{_dead} && length $self->{wbuf};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_dead"} and length $self->{"wbuf"} 0 1 1
Condition33%1/3
not $self->{"_dead"} and length $self->{"wbuf"}
!ll&&!rl&&r
010
MC/DC0%0/2
not $self->{"_dead"} and length $self->{"wbuf"}
not $self->{"_dead"} length $self->{"wbuf"}
Truth table33%1/3
not(A) and B
A= not $self->{"_dead"}B= length $self->{"wbuf"}
ABresult
0X0
100
111
88 }
89  
90 # --- parse + dispatch whole requests from rbuf ---
91 72 ▶sub _process {
Subroutine _process: called CC=28 SCAR=34.7
92 72 my ($self) = @_;
93 72 ▶ while (!$self->{_dead} && !$self->{_closing}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"_dead"} || $self->{"_closing"}) 72 0 72
Condition33%1/3
$self->{'_dead'} || $self->{'_closing'}
l!l&&r!l&&!r
0072
MC/DC0%0/2
$self->{'_dead'} || $self->{'_closing'}
$self->{'_dead'} $self->{'_closing'}
Truth table33%1/3
A || B
A= $self->{'_dead'}B= $self->{'_closing'}
ABresult
1X1
011
000
94 72 ▶ return if $self->{inflight} || $self->{close_after_flush};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inflight"} or $self->{"close_after_flush"} 0 72 72
Condition33%1/3
$self->{"inflight"} or $self->{"close_after_flush"}
l!l&&r!l&&!r
0072
MC/DC0%0/2
$self->{"inflight"} or $self->{"close_after_flush"}
$self->{"inflight"} $self->{"close_after_flush"}
Truth table33%1/3
A or B
A= $self->{"inflight"}B= $self->{"close_after_flush"}
ABresult
1X1
011
000
95 72 my $end = index($self->{rbuf}, "\r\n\r\n");
96 72 ▶ if ($end < 0) { # head incomplete: keep reading,
Branch: 1 branch
BranchTrueFalseTotal
if ($end < 0) 22 50 72
97 22 ▶ return $self->_close if length($self->{rbuf}) > $MAXHEAD; # unless it is a runaway
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"rbuf"} > $MAXHEAD 0 22 22
98 22 return; # (a never-terminating / slow-drip head)
99 }
100 50 ▶ return $self->_close if $end + 4 > $MAXHEAD;
Branch: 1 branch
BranchTrueFalseTotal
if $end + 4 > $MAXHEAD 1 49 50
101 49 my $head = substr($self->{rbuf}, 0, $end + 4);
102 49 my $r = parse_request_head($head);
103 49 ▶ return $self->_reject(400, 'malformed request head') unless $r;
Branch: 1 branch
BranchTrueFalseTotal
unless $r 2 47 49
104 47 my ($mode, $len) = body_length($r->{headers});
105 47 ▶ return $self->_reject(400, 'invalid request framing') if $mode eq 'invalid';
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "invalid" 0 47 47
106 47 ▶ return $self->_reject(411, 'transfer-encoded request bodies are not supported')
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "chunked" 1 46 47
107 if $mode eq 'chunked';
108 46 ▶ if ($self->{state} eq 'plain' && $r->{method} eq 'CONNECT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "plain" and $r->{"method"} eq "CONNECT") 2 44 46
Condition100%3/3
$self->{"state"} eq "plain" and $r->{"method"} eq "CONNECT"
!ll&&!rl&&r
18262
MC/DC100%2/2
$self->{"state"} eq "plain" and $r->{"method"} eq "CONNECT"
$self->{"state"} eq "plain" $r->{"method"} eq "CONNECT"
Truth table100%3/3
A and B
A= $self->{"state"} eq "plain"B= $r->{"method"} eq "CONNECT"
ABresult
0X0
100
111
109 return $self->_reject(400, 'invalid CONNECT authority')
110 2 ▶ unless $r->{target} =~ /\A(?:\[[^\]]+\]|[^:\/?#@]+):\d+\z/ && !$len;
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"target"} =~ m(\A(?:\[[^\]]+\]|[^:/?#@]+):\d+\z) and not $len 0 2 2
Condition33%1/3
$r->{"target"} =~ m(\A(?:\[[^\]]+\]|[^:/?#@]+):\d+\z) and not $len
!ll&&!rl&&r
002
MC/DC0%0/2
$r->{"target"} =~ m(\A(?:\[[^\]]+\]|[^:/?#@]+):\d+\z) and not $len
$r->{"target"} =~ m(\A(?:\[[^\]]+\]|[^:/?#@]+):\d+\z) not $len
Truth table33%1/3
A and not(B)
A= $r->{"target"} =~ m(\A(?:\[[^\]]+\]|[^:/?#@]+):\d+\z)B= not $len
ABresult
0X0
100
111
111 2 substr($self->{rbuf}, 0, $end + 4, ''); # consume head
112 # A compliant client waits for our 200 before starting TLS, so nothing
113 # follows the CONNECT head yet. If a client optimistically COALESCED its
114 # ClientHello into this segment, those bytes are now stranded in rbuf --
115 # _begin_tls binds OpenSSL to the socket (set_fd), so a buffered
116 # ClientHello is invisible to the handshake and it would stall until the
117 # idle timeout. Fail fast and honestly rather than a 120s hang.
118 2 ▶ return $self->_close if length $self->{rbuf};
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"rbuf"} 1 1 2
119 1 $self->{connect_host} = $r->{target}; # full authority host:port (curl strips :443)
120 1 $self->{state} = 'tls';
121 1 $self->_write_raw("HTTP/1.1 200 Connection established\r\n\r\n");
122 # Begin TLS only once the 200 -- plus any pre-TLS plaintext still queued
123 # from a prior pipelined response to a slow client -- has flushed.
124 # Otherwise _begin_tls flips state to handshake, _writable routes to the
125 # handshake (not _flush), and the queued 200 is stranded. _flush resumes
126 # this via _tls_pending when wbuf drains to empty.
127 0 ▶ if (length $self->{wbuf}) { $self->{_tls_pending} = 1 }
Branch: 2 branches
BranchTrueFalseTotal
if (length $self->{'wbuf'}) { } 0 1 1
elsif ($self->{'cert'}) { } 0 1 1
128 0 elsif ($self->{cert}) { $self->_begin_tls } # (test passes cert=>undef)
129 1 return;
130 }
131 44 ▶ my $need = $end + 4 + ($mode eq 'length' ? $len : 0);
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'length' ? : 4 40 44
132 44 ▶ if ($mode eq 'length' && length($self->{rbuf}) < $need) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode eq "length" and length $self->{"rbuf"} < $need) 2 42 44
Condition100%3/3
$mode eq "length" and length $self->{"rbuf"} < $need
!ll&&!rl&&r
4022
MC/DC100%2/2
$mode eq "length" and length $self->{"rbuf"} < $need
$mode eq "length" length $self->{"rbuf"} < $need
Truth table100%3/3
A and B
A= $mode eq "length"B= length $self->{"rbuf"} < $need
ABresult
0X0
100
111
133 2 ▶ if (!$self->{sent_continue}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"sent_continue"}) 1 1 2
134 1 my @expect = map { lc $_->[1] } grep { lc($_->[0]) eq 'expect' } @{$r->{headers}};
135 1 ▶ if (@expect) {
Branch: 1 branch
BranchTrueFalseTotal
if (@expect) 1 0 1
136 return $self->_reject(417, 'unsupported expectation')
137 1 ▶ if grep { $_ ne '100-continue' } @expect;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ ne "100-continue";} @expect 0 1 1
138 1 $self->{sent_continue} = 1;
139 1 $self->_write_client("HTTP/1.1 100 Continue\r\n\r\n");
140 }
141 }
142 2 return;
143 }
144 42 delete $self->{sent_continue};
145 42 substr($self->{rbuf}, 0, $end + 4, ''); # consume head
146 42 ▶ my $body = ($mode eq 'length' && $len) ? substr($self->{rbuf}, 0, $len, '') : '';
Branch: 1 branch
BranchTrueFalseTotal
$mode eq 'length' && $len ? : 2 40 42
Condition66%2/3
$mode eq "length" and $len
!ll&&!rl&&r
4002
MC/DC50%1/2
$mode eq "length" and $len
$mode eq "length" $len
Truth table66%2/3
A and B
A= $mode eq "length"B= $len
ABresult
0X0
100
111
147 42 $r->{body} = $body;
148 42 my $connection = lc join ',', $head =~ /\r\nconnection:[ \t]*([^\r\n]*)/ig;
149 $self->{request_close} = $connection =~ /(?:^|,)\s*close\s*(?:,|$)/
150 42 ▶ || ($r->{version} eq 'HTTP/1.0' && $connection !~ /(?:^|,)\s*keep-alive\s*(?:,|$)/);
Condition83%5/6
$connection =~ /(?:^|,)\s*close\s*(?:,|$)/ || $r->{'version'} eq 'HTTP/1.0' && !($connection =~ /(?:^|,)\s*keep-alive\s*(?:,|$)/)
l!l&&r!l&&!r
2139
$r->{"version"} eq "HTTP/1.0" and not $connection =~ /(?:^|,)\s*keep-alive\s*(?:,|$)/
!ll&&!rl&&r
3901
MC/DC66%2/3
$connection =~ /(?:^|,)\s*close\s*(?:,|$)/ || $r->{'version'} eq 'HTTP/1.0' && !($connection =~ /(?:^|,)\s*keep-alive\s*(?:,|$)/)
$connection =~ /(?:^|,)\s*close\s*(?:,|$)/ $r->{"version"} eq "HTTP/1.0" not $connection =~ /(?:^|,)\s*keep-alive\s*(?:,|$)/
Truth table75%3/4
A || B and not(C)
A= $connection =~ /(?:^|,)\s*close\s*(?:,|$)/B= $r->{"version"} eq "HTTP/1.0"C= not $connection =~ /(?:^|,)\s*keep-alive\s*(?:,|$)/
ABCresult
1XX1
0111
00X0
0100
151 42 $self->_forward($r, absolute => ($self->{state} eq 'plain'));
152 42 return; # inflight set; resume on on_done
153 }
154 }
155  
156 3 ▶sub _reject {
Subroutine _reject: called CC=1 SCAR=0.0
157 3 my ($self, $status, $msg) = @_;
158 3 $self->{rbuf} = '';
159 3 $self->{_closing} = $self->{close_after_flush} = $self->{response_complete} = 1;
160 3 $self->_write_client(response_head($status,
161 { 'content-type' => 'text/plain', 'content-length' => length($msg),
162 connection => 'close' }) . $msg);
163 3 $self->_maybe_close_after_flush;
164 }
165  
166 52 ▶sub _bodyless { $_[0] eq 'HEAD' || $_[1] < 200 || $_[1] == 204 || $_[1] == 205 || $_[1] == 304 }
Subroutine _bodyless: called CC=5 SCAR=16.1
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "HEAD" or $_[1] < 200 or $_[1] == 204 or $_[1] == 205 or $_[1] == 304 13 39 52
Condition88%8/9
$_[0] eq "HEAD" or $_[1] < 200 or $_[1] == 204 or $_[1] == 205
l!l&&r!l&&!r
10339
$_[0] eq "HEAD" or $_[1] < 200 or $_[1] == 204
l!l&&r!l&&!r
5542
$_[0] eq "HEAD" or $_[1] < 200
l!l&&r!l&&!r
5047
MC/DC75%3/4
$_[0] eq "HEAD" or $_[1] < 200 or $_[1] == 204 or $_[1] == 205
$_[0] eq "HEAD" $_[1] < 200 $_[1] == 204 $_[1] == 205
Truth table80%4/5
A or B or C or D
A= $_[0] eq "HEAD"B= $_[1] < 200C= $_[1] == 204D= $_[1] == 205
ABCDresult
1XXX1
01XX1
001X1
00011
00000
167  
168 # --- forward one request upstream and stream the response back ---
169 42 ▶sub _forward {
Subroutine _forward: called CC=32 SCAR=36.4
170 42 my ($self, $r, %opt) = @_;
171 42 ▶ my $url = $opt{absolute} ? $r->{target} : "https://$self->{connect_host}$r->{target}";
Branch: 1 branch
BranchTrueFalseTotal
$opt{'absolute'} ? : 24 18 42
172 42 my $fwd = coherent_headers($r->{headers}, $self->{chrome});
173 # forced identity headers (UA + low-entropy client hints) over curl's defaults
174 42 $fwd = { %$fwd, %{ $self->{override} } };
175 # high-entropy client hints only for a host that has sent Accept-CH (as Chrome
176 # does). Derive the bare host for that key: prefer the CONNECT authority (tls),
177 # else the absolute-request URL's authority (plain). Handle a bracketed IPv6
178 # literal ([::1]:443 -> ::1), not just host:port -- else distinct IPv6 origins
179 # collide under a truncated key.
180 42 my $auth = $self->{connect_host};
181 42 ▶ ($auth) = $url =~ m{^https?://([^/]+)} if !defined $auth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $auth 24 18 42
182 42 ▶ $auth //= '';
Condition50%1/2
$auth //= ""
l!l
420
MC/DC0%0/1
$auth //= ""
$auth
Truth table50%1/2
A
A= $auth
Aresult
11
00
183 42 my $host;
184 0 ▶ if ($auth =~ /^\[([^\]]+)\]/) { $host = $1 } # [ipv6](:port)?
Branch: 2 branches
BranchTrueFalseTotal
if ($auth =~ /^\[([^\]]+)\]/) { } 0 42 42
elsif ($auth =~ /^([^:]+)/) { } 42 0 42
185 42 elsif ($auth =~ /^([^:]+)/) { $host = $1 } # host(:port)?
186 42 $self->{_req_host} = $host;
187 42 ▶ if ($host && (my $want = $self->{hints}{$host})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host and my $want = $self->{"hints"}{$host}) 0 42 42
Condition33%1/3
$host and my $want = $self->{"hints"}{$host}
!ll&&!rl&&r
0420
MC/DC0%0/2
$host and my $want = $self->{"hints"}{$host}
$host my $want = $self->{"hints"}{$host}
Truth table33%1/3
A and B
A= $hostB= my $want = $self->{"hints"}{$host}
ABresult
0X0
100
111
188 $fwd->{$_} = $self->{high_entropy}{$_}
189 0 for grep { exists $self->{high_entropy}{$_} } keys %$want;
190 }
191 # Interception point. Everything the request will be is now assembled and
192 # still plaintext -- after TLS termination, before anything goes upstream --
193 # so a hook here sees EVERY request the browser makes (navigations,
194 # subresources, XHR, fetch), which no WebKit-side API does, and can rewrite
195 # it, answer it locally, or refuse it.
196 42 ▶ if (my $cb = $self->{on_request}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_request"}) 14 28 42
197 my $req = { method => $r->{method}, url => $url, headers => $fwd,
198 14 body => $r->{body}, host => $host };
199 14 my $res = eval { $cb->($req) };
200 14 ▶ return if $self->{_dead} || $self->{_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} or $self->{"_closing"} 0 14 14
Condition33%1/3
$self->{"_dead"} or $self->{"_closing"}
l!l&&r!l&&!r
0014
MC/DC0%0/2
$self->{"_dead"} or $self->{"_closing"}
$self->{"_dead"} $self->{"_closing"}
Truth table33%1/3
A or B
A= $self->{"_dead"}B= $self->{"_closing"}
ABresult
1X1
011
000
201 14 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 13 14
202 # Fail CLOSED, like the sibling on_policy gate in EV::WebKit: this
203 # hook is used to BLOCK requests, so a handler that dies must not
204 # quietly let the request through -- that is precisely the traffic
205 # the caller was trying to stop.
206 1 warn "Proxy::Impersonate: on_request died (refusing the request): $@";
207 1 $res = { status => 502, body => 'on_request handler died' };
208 }
209 else {
210 # Honour in-place rewrites of any field.
211 13 ▶ ($url, $fwd) = ($req->{url}, $req->{headers} // {});
Condition50%1/2
$req->{"headers"} // {}
l!l
130
MC/DC0%0/1
$req->{"headers"} // {}
$req->{"headers"}
Truth table50%1/2
A
A= $req->{"headers"}
Aresult
11
00
212 13 $r->{method} = $req->{method};
213 13 $r->{body} = $req->{body};
214 }
215 14 ▶ if (ref $res eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "HASH") 11 3 14
216 # A synthetic response: answer from here and never touch the
217 # network. Content-Length is computed rather than trusted, so a
218 # handler cannot desynchronise the connection by disagreeing with
219 # its own body.
220 11 ▶ my $body = defined $res->{body} ? $res->{body} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $res->{'body'} ? : 11 0 11
221 11 ▶ utf8::encode($body) if utf8::is_utf8($body);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($body) 1 10 11
222 11 ▶ my %h = map { lc($_) => $res->{headers}{$_} } keys %{ $res->{headers} // {} };
Condition100%2/2
$res->{"headers"} // {}
l!l
65
MC/DC100%1/1
$res->{"headers"} // {}
$res->{"headers"}
Truth table100%2/2
A
A= $res->{"headers"}
Aresult
11
00
223 11 strip_hop_headers(\%h);
224 11 delete $h{'content-length'};
225 11 ▶ $h{'content-type'} //= 'text/plain';
Condition100%2/2
$h{"content-type"} //= "text/plain"
l!l
110
MC/DC100%1/1
$h{"content-type"} //= "text/plain"
$h{"content-type"}
Truth table100%2/2
A
A= $h{"content-type"}
Aresult
11
00
226 11 $h{'content-length'} = length $body;
227 11 ▶ my $status = $res->{status} || 200;
Condition100%2/2
$res->{'status'} || 200
l!l
92
MC/DC100%1/1
$res->{'status'} || 200
$res->{'status'}
Truth table100%2/2
A
A= $res->{'status'}
Aresult
11
00
228 11 ▶ delete $h{'content-length'} if $status == 204 || $status == 304 || $status < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 204 or $status == 304 or $status < 200 2 9 11
Condition83%5/6
$status == 204 or $status == 304 or $status < 200
l!l&&r!l&&!r
209
$status == 204 or $status == 304
l!l&&r!l&&!r
119
MC/DC66%2/3
$status == 204 or $status == 304 or $status < 200
$status == 204 $status == 304 $status < 200
Truth table75%3/4
A or B or C
A= $status == 204B= $status == 304C= $status < 200
ABCresult
1XX1
01X1
0011
0000
229 11 ▶ $h{'content-length'} = 0 if $status == 205;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 205 1 10 11
230 11 ▶ $body = '' if _bodyless($r->{method}, $status);
Branch: 1 branch
BranchTrueFalseTotal
if _bodyless($r->{"method"}, $status) 4 7 11
231 11 ▶ if ($self->{request_close}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"request_close"}) 1 10 11
232 1 $h{connection} = 'close';
233 1 $self->{close_after_flush} = 1;
234 }
235 11 $self->{wrote_headers} = 1;
236 11 $self->{response_complete} = 1;
237 11 $self->_write_client(response_head($status, \%h) . $body);
238 11 ▶ return $self->_maybe_close_after_flush if $self->{close_after_flush};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_after_flush"} 1 10 11
239 # Keep-alive is safe here precisely because the length is exact.
240 10 $self->_reset_for_next;
241 10 $self->_process;
242 10 return;
243 }
244 3 ▶ return $self->_close if defined $res && !ref $res && $res eq 'abort';
Branch: 1 branch
BranchTrueFalseTotal
if defined $res and not ref $res and $res eq "abort" 1 2 3
Condition66%4/6
defined $res and not ref $res and $res eq "abort"
!ll&&!rl&&r
201
defined $res and not ref $res
!ll&&!rl&&r
201
MC/DC33%1/3
defined $res and not ref $res and $res eq "abort"
defined $res not ref $res $res eq "abort"
Truth table50%2/4
A and not(B) and C
A= defined $resB= not ref $resC= $res eq "abort"
ABCresult
0XX0
10X0
1100
1111
245 }
246 30 $self->{wrote_headers} = 0;
247 30 $self->{response_complete} = 0;
248 30 $self->{close_after_flush} = 0;
249 30 $self->{chunked} = 0;
250 30 my $suppress_body = 0;
251 # Guarded: add_streaming validates what it is handed and croaks on anything
252 # it will not send. Escaping here would unwind out of an EV watcher and take
253 # the daemon with it, so answer the client instead.
254 30 my $added = eval {
255 30 my $h = $self->{inflight} = $self->{make_handle}->();
256 $self->{multi}->add_streaming($h,
257 { method => $r->{method}, url => $url, headers => $fwd, body => $r->{body} },
258 {
259 21 ▶ on_headers => sub {
Subroutine __ANON__: called CC=32 SCAR=36.5
260 21 my ($status, $hdrs) = @_;
261 21 ▶ return if $self->{_dead} || $self->{_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} or $self->{"_closing"} 0 21 21
Condition33%1/3
$self->{"_dead"} or $self->{"_closing"}
l!l&&r!l&&!r
0021
MC/DC0%0/2
$self->{"_dead"} or $self->{"_closing"}
$self->{"_dead"} $self->{"_closing"}
Truth table33%1/3
A or B
A= $self->{"_dead"}B= $self->{"_closing"}
ABresult
1X1
011
000
262 21 $self->{wrote_headers} = 1;
263 # learn which high-entropy hints this host wants (Accept-CH), so
264 # later requests to it carry them -- matching Chrome's behavior
265 21 ▶ if (defined $self->{_req_host} && (my $ach = $hdrs->{'accept-ch'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_req_host"} and my $ach = $hdrs->{"accept-ch"}) 0 21 21
Condition33%1/3
defined $self->{"_req_host"} and my $ach = $hdrs->{"accept-ch"}
!ll&&!rl&&r
0210
MC/DC0%0/2
defined $self->{"_req_host"} and my $ach = $hdrs->{"accept-ch"}
defined $self->{"_req_host"} my $ach = $hdrs->{"accept-ch"}
Truth table33%1/3
A and B
A= defined $self->{"_req_host"}B= my $ach = $hdrs->{"accept-ch"}
ABresult
0X0
100
111
266 # bound memory on a long-running proxy: the per-host map has no
267 # TTL, so clear it if it grows large (hints are cheaply re-learned)
268 0 ▶ %{ $self->{hints} } = () if keys %{ $self->{hints} } > 4096;
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$$self{"hints"};} > 4096 0 0 0
269 0 ▶ my $h = ref $ach eq 'ARRAY' ? join(',', @$ach) : $ach;
Branch: 1 branch
BranchTrueFalseTotal
ref $ach eq 'ARRAY' ? : 0 0 0
270 0 for my $req (split /\s*,\s*/, lc $h) {
271 $self->{hints}{$self->{_req_host}}{$req} = 1
272 0 ▶ if exists $self->{high_entropy}{$req};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"high_entropy"}{$req} 0 0 0
273 }
274 }
275 21 my %h = %$hdrs;
276 21 strip_hop_headers(\%h);
277 # The handle is built with decode => 1, so a compressed body
278 # arrives already decompressed while the origin's own
279 # Content-Encoding and Content-Length still describe the bytes
280 # BEFORE decoding. Forwarding either would have the client
281 # inflate an already-inflated body, or cut it at the compressed
282 # length. Both headers go, and the length that replaces them is
283 # chunked framing -- WebKit cannot decode zstd, which real Chrome
284 # advertises, so passing the encoding through renders the page as
285 # binary. HEAD/304 must also drop encoded metadata, or a cache
286 # update would label its already-decoded body as compressed.
287 21 my $bodyless = _bodyless($r->{method}, $status);
288 21 ▶ if (defined $h{'content-encoding'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $h{'content-encoding'}) { } 5 16 21
elsif (not $bodyless || exists $h{'content-length'}) { } 1 15 16
289 5 delete @h{qw(content-encoding content-length)};
290 5 ▶ if (!$bodyless) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bodyless) 3 2 5
291 3 ▶ if ($r->{version} eq 'HTTP/1.0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'version'} eq 'HTTP/1.0') { } 1 2 3
292 1 $h{connection} = 'close';
293 1 $self->{close_after_flush} = 1;
294 }
295 else {
296 2 $h{'transfer-encoding'} = 'chunked';
297 2 $self->{chunked} = 1;
298 }
299 }
300 }
301 ▶ elsif (!$bodyless && !exists $h{'content-length'}) {
Condition100%3/3
$bodyless || exists $h{'content-length'}
l!l&&r!l&&!r
5101
MC/DC100%2/2
$bodyless || exists $h{'content-length'}
$bodyless exists $h{'content-length'}
Truth table100%3/3
A || B
A= $bodylessB= exists $h{'content-length'}
ABresult
1X1
011
000
302 # length unknown and nothing to re-frame: delimit by closing
303 1 $h{connection} = 'close';
304 1 $self->{close_after_flush} = 1;
305 }
306 # Response interception, the counterpart to on_request. Status and
307 # headers may be rewritten -- stripping Content-Security-Policy or
308 # X-Frame-Options is the usual reason -- but the FRAMING is ours,
309 # not the handler's: content-length and the hop-by-hop set are
310 # snapshotted here and forced back afterwards, because a handler
311 # that edits them does not desynchronise its own connection, it
312 # desynchronises the browser's.
313 21 ▶ if (my $cb = $self->{on_response}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_response"}) 10 11 21
314 10 my $framing_len = $h{'content-length'};
315 10 my $framing_conn = $h{connection};
316 10 my $framing_te = $h{'transfer-encoding'};
317 my $res = { status => $status,
318 18 ▶ headers => { map { $_ => ref $h{$_} eq 'ARRAY' ? [@{$h{$_}}] : $h{$_} } keys %h },
Branch: 1 branch
BranchTrueFalseTotal
ref $h{$_} eq 'ARRAY' ? : 1 17 18
319 10 url => $url, method => $r->{method}, host => $host };
320 10 ▶ if (eval { $cb->($res); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { &$cb($res); 1 } }) { } 8 2 10
321 8 ▶ $status = $res->{status} if defined $res->{status};
Branch: 1 branch
BranchTrueFalseTotal
if defined $res->{"status"} 8 0 8
322 19 %h = map { lc($_) => $res->{headers}{$_} } keys %{ $res->{headers} }
323 8 ▶ if ref $res->{headers} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $res->{"headers"} eq "HASH" 8 0 8
324 }
325 else {
326 # Fail OPEN here, unlike on_request. This hook observes a
327 # response that has already been fetched; refusing it would
328 # break a page over a bug in an observer, and there is no
329 # security decision left to fail closed on -- the request
330 # was already made.
331 2 warn "Proxy::Impersonate: on_response died (passing the response through): $@";
332 }
333 10 ▶ return if $self->{_dead} || $self->{_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} or $self->{"_closing"} 1 9 10
Condition66%2/3
$self->{"_dead"} or $self->{"_closing"}
l!l&&r!l&&!r
019
MC/DC50%1/2
$self->{"_dead"} or $self->{"_closing"}
$self->{"_dead"} $self->{"_closing"}
Truth table66%2/3
A or B
A= $self->{"_dead"}B= $self->{"_closing"}
ABresult
1X1
011
000
334 9 strip_hop_headers(\%h);
335 defined $framing_len ? ($h{'content-length'} = $framing_len)
336 9 ▶ : delete $h{'content-length'};
Branch: 1 branch
BranchTrueFalseTotal
defined $framing_len ? : 8 1 9
337 defined $framing_conn ? ($h{connection} = $framing_conn)
338 9 ▶ : delete $h{connection};
Branch: 1 branch
BranchTrueFalseTotal
defined $framing_conn ? : 1 8 9
339 defined $framing_te ? ($h{'transfer-encoding'} = $framing_te)
340 9 ▶ : delete $h{'transfer-encoding'};
Branch: 1 branch
BranchTrueFalseTotal
defined $framing_te ? : 0 9 9
341 }
342 20 my $final_bodyless = _bodyless($r->{method}, $status);
343 20 ▶ $suppress_body = $final_bodyless || $bodyless;
Condition100%3/3
$final_bodyless || $bodyless
l!l&&r!l&&!r
7112
MC/DC100%2/2
$final_bodyless || $bodyless
$final_bodyless $bodyless
Truth table100%3/3
A || B
A= $final_bodylessB= $bodyless
ABresult
1X1
011
000
344 20 ▶ if ($suppress_body) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suppress_body) 8 12 20
345 8 $self->{chunked} = $self->{close_after_flush} = 0;
346 8 delete @h{qw(transfer-encoding connection)};
347 8 ▶ if (!$final_bodyless) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($final_bodyless) 1 7 8
348 1 delete $h{'content-encoding'};
349 1 $h{'content-length'} = 0;
350 }
351 8 ▶ delete $h{'content-length'} if $status == 204 || $status < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 204 or $status < 200 2 6 8
Condition66%2/3
$status == 204 or $status < 200
l!l&&r!l&&!r
206
MC/DC50%1/2
$status == 204 or $status < 200
$status == 204 $status < 200
Truth table66%2/3
A or B
A= $status == 204B= $status < 200
ABresult
1X1
011
000
352 8 ▶ $h{'content-length'} = 0 if $status == 205;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 205 1 7 8
353 8 ▶ delete $h{'content-length'} if $status == 304;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 304 2 6 8
354 }
355 20 ▶ if ($self->{request_close}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"request_close"}) 2 18 20
356 2 $h{connection} = 'close';
357 2 $self->{close_after_flush} = 1;
358 }
359 20 $self->_write_client(response_head($status, \%h));
360 },
361 12 ▶ on_body => sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
362 12 my ($chunk) = @_;
363 12 ▶ return 2 if $self->{_write_dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_write_dead"} 1 11 12
364 11 ▶ return 0 if $suppress_body;
Branch: 1 branch
BranchTrueFalseTotal
if $suppress_body 6 5 11
365 # Pause BEFORE consuming when already backed up: libcurl treats a
366 # CURL_WRITEFUNC_PAUSE return as "this chunk was NOT consumed" and
367 # re-delivers it on resume. Appending it here AND pausing would then
368 # write it twice -- corrupting (duplicating a 16 KiB block in) any
369 # response larger than HIWAT. Resume only fires once wbuf has drained
370 # to empty, so the replayed chunk is appended exactly once.
371 0 ▶ if (length($self->{wbuf}) >= $HIWAT) { $self->{paused} = 1; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"wbuf"} >= $HIWAT) 0 5 5
372 # Framed here, not in on_headers: a paused chunk is re-delivered
373 # on resume, and the pause above returns before consuming it, so
374 # each chunk is wrapped exactly once.
375 $chunk = sprintf("%X\r\n", length $chunk) . $chunk . "\r\n"
376 5 ▶ if $self->{chunked} && length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"chunked"} and length $chunk 2 3 5
Condition66%2/3
$self->{"chunked"} and length $chunk
!ll&&!rl&&r
302
MC/DC50%1/2
$self->{"chunked"} and length $chunk
$self->{"chunked"} length $chunk
Truth table66%2/3
A and B
A= $self->{"chunked"}B= length $chunk
ABresult
0X0
100
111
377 5 $self->_write_client($chunk);
378 5 ▶ return 2 if $self->{_write_dead}; # write just failed -> abort
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_write_dead"} 0 5 5
379 5 return 0;
380 },
381 14 ▶ on_done => sub {
Subroutine __ANON__: called CC=10 SCAR=26.2
382 14 my ($err) = @_;
383 14 delete $self->{inflight};
384 14 ▶ return $self->_close if $self->{_write_dead} || $self->{_dead} || $self->{_closing};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_write_dead"} or $self->{"_dead"} or $self->{"_closing"} 0 14 14
Condition33%2/6
$self->{"_write_dead"} or $self->{"_dead"} or $self->{"_closing"}
l!l&&r!l&&!r
0014
$self->{"_write_dead"} or $self->{"_dead"}
l!l&&r!l&&!r
0014
MC/DC0%0/3
$self->{"_write_dead"} or $self->{"_dead"} or $self->{"_closing"}
$self->{"_write_dead"} $self->{"_dead"} $self->{"_closing"}
Truth table25%1/4
A or B or C
A= $self->{"_write_dead"}B= $self->{"_dead"}C= $self->{"_closing"}
ABCresult
1XX1
01X1
0011
0000
385 14 ▶ if ($err && !$self->{wrote_headers}) { # failed before any byte
Branch: 1 branch
BranchTrueFalseTotal
if ($err and not $self->{"wrote_headers"}) 0 14 14
Condition66%2/3
$err and not $self->{"wrote_headers"}
!ll&&!rl&&r
1310
MC/DC0%0/2
$err and not $self->{"wrote_headers"}
$err not $self->{"wrote_headers"}
Truth table66%2/3
A and not(B)
A= $errB= not $self->{"wrote_headers"}
ABresult
0X0
100
111
386 0 my $msg = "upstream error: $err";
387 0 $self->_write_client(response_head(502,
388 { 'content-type' => 'text/plain', 'content-length' => length($msg),
389 'connection' => 'close' }) . $msg);
390 0 $self->{close_after_flush} = 1;
391 0 $self->{response_complete} = 1;
392 0 return $self->_maybe_close_after_flush;
393 }
394 14 ▶ return $self->_close if $err; # failed mid-stream -> abort
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 13 14
395 # Close the chunked body. Only on success: a truncated response
396 # must NOT be terminated, or the client accepts a short body as
397 # complete -- the abort above is what tells it something broke.
398 13 ▶ if ($self->{chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunked"}) 1 12 13
399 1 $self->_write_client("0\r\n\r\n");
400 1 ▶ return $self->_close if $self->{_write_dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_write_dead"} 0 1 1
401 }
402 13 $self->{response_complete} = 1;
403 13 ▶ if ($self->{close_after_flush}) { $self->_maybe_close_after_flush }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'close_after_flush'}) { } 2 11 13
404 11 else { $self->_reset_for_next; $self->_process } # keep-alive
405 },
406 29 }); 1 };
407 30 ▶ if (!$added) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $added) 2 28 30
408 2 ▶ my $why = $@ || 'upstream refused the request';
Condition50%1/2
$@ || 'upstream refused the request'
l!l
20
MC/DC0%0/1
$@ || 'upstream refused the request'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
409 2 $why =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*\z//;
410 2 warn "Proxy::Impersonate: could not forward the request: $why\n";
411 2 delete $self->{inflight};
412 2 my $msg = 'could not forward the request';
413 2 $self->{wrote_headers} = 1;
414 2 $self->{response_complete} = 1;
415 2 $self->_write_client(response_head(502,
416 { 'content-type' => 'text/plain', 'content-length' => length($msg),
417 'connection' => 'close' }) . $msg);
418 2 $self->{close_after_flush} = 1;
419 2 return $self->_maybe_close_after_flush;
420 }
421 }
422  
423 21 ▶sub _reset_for_next {
Subroutine _reset_for_next: called CC=1 SCAR=0.0
424 21 my ($self) = @_;
425 21 delete @{$self}{qw(wrote_headers response_complete close_after_flush _write_dead chunked request_close)};
426 }
427  
428 17 ▶sub _maybe_close_after_flush {
Subroutine _maybe_close_after_flush: called CC=5 SCAR=16.7
429 17 my ($self) = @_;
430 $self->_close if $self->{close_after_flush} && $self->{response_complete}
431 17 ▶ && !length($self->{wbuf}) && !$self->{inflight};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_after_flush"} and $self->{"response_complete"} and not length $self->{"wbuf"} and not $self->{"inflight"} 12 5 17
Condition66%6/9
$self->{"close_after_flush"} and $self->{"response_complete"} and not length $self->{"wbuf"} and not $self->{"inflight"}
!ll&&!rl&&r
5012
$self->{"close_after_flush"} and $self->{"response_complete"} and not length $self->{"wbuf"}
!ll&&!rl&&r
5012
$self->{"close_after_flush"} and $self->{"response_complete"}
!ll&&!rl&&r
0512
MC/DC25%1/4
$self->{"close_after_flush"} and $self->{"response_complete"} and not length $self->{"wbuf"} and not $self->{"inflight"}
$self->{"close_after_flush"} $self->{"response_complete"} not length $self->{"wbuf"} not $self->{"inflight"}
Truth table40%2/5
A and B and not(C) and not(D)
A= $self->{"close_after_flush"}B= $self->{"response_complete"}C= not length $self->{"wbuf"}D= not $self->{"inflight"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
432 }
433  
434 # --- Net::SSLeay server handshake, EV-driven ---
435 2 ▶sub _begin_tls {
Subroutine _begin_tls: called CC=1 SCAR=0.0
436 2 my ($self) = @_;
437 2 my $ctx = $self->{ctx} = Net::SSLeay::CTX_new();
438 2 Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL);
439 # After SSL_write returns WANT_WRITE, an upstream on_body appends to wbuf before
440 # the retry, which can realloc (move) the Perl PV. Without this mode OpenSSL
441 # rejects a moved retry buffer with "bad write retry" (fatal) and the HTTPS
442 # response is silently truncated -- exactly the backpressure path HIWAT builds.
443 # 0x2 == SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER (fixed OpenSSL value; the pending
444 # prefix bytes are never mutated, only appended after, so the mode is safe).
445 2 Net::SSLeay::CTX_set_mode($ctx, 0x2);
446 2 $self->{cert}->apply_to_ctx($ctx);
447 2 my $ssl = $self->{ssl} = Net::SSLeay::new($ctx);
448 2 Net::SSLeay::set_fd($ssl, fileno($self->{sock}));
449 2 $self->{state} = 'handshake';
450 2 $self->_do_handshake;
451 }
452  
453 4 ▶sub _do_handshake {
Subroutine _do_handshake: called CC=4 SCAR=14.8
454 4 my ($self) = @_;
455 4 my $rc = Net::SSLeay::accept($self->{ssl});
456 # On success, drain via _readable (SSL_read loop), not _process: a TLS 1.3
457 # client's Finished + first request can coalesce into one segment, leaving the
458 # request buffered inside OpenSSL where a bare _process (which only reads rbuf)
459 # would never see it -> hang.
460 4 ▶ if ($rc > 0) { $self->{state} = 'tls'; $self->_disarm_write; return $self->_readable }
Branch: 1 branch
BranchTrueFalseTotal
if ($rc > 0) 1 3 4
461 3 my $e = Net::SSLeay::get_error($self->{ssl}, $rc);
462 3 ▶ if ($e == Net::SSLeay::ERROR_WANT_READ()) { $self->_disarm_write } # read watcher already armed
Branch: 2 branches
BranchTrueFalseTotal
if ($e == Net::SSLeay::ERROR_WANT_READ()) { } 3 0 3
elsif ($e == Net::SSLeay::ERROR_WANT_WRITE()) { } 0 0 0
463 0 elsif ($e == Net::SSLeay::ERROR_WANT_WRITE()) { $self->_arm_write }
464 0 else { $self->_close }
465 }
466  
467 # --- writable: continue a handshake, or flush the client write buffer ---
468 0 ▶sub _writable {
Subroutine _writable: not called CC=2 SCAR=17.9
469 0 my ($self) = @_;
470 0 ▶ return $self->_do_handshake if $self->{state} eq 'handshake';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"state"} eq "handshake" 0 0 0
471 0 $self->_flush;
472 }
473  
474 1 ▶sub _write_raw { my ($self, $d) = @_; $self->{wbuf} .= $d; $self->_flush } # pre-TLS plaintext
Subroutine _write_raw: called CC=1 SCAR=0.0
475 45 ▶sub _write_client { my ($self, $d) = @_; $self->{wbuf} .= $d; $self->_flush }
Subroutine _write_client: called CC=1 SCAR=0.0
476  
477 # A fatal write to the client: never _close synchronously from here, because
478 # _flush can run inside an upstream on_body/on_headers callback and _close would
479 # reentrantly remove() the in-flight handle. Mark the write dead so on_body
480 # aborts the transfer (curl -> on_done -> safe _close); close directly only when
481 # no forward is in flight.
482 0 ▶sub _fail_write {
Subroutine _fail_write: not called CC=3 SCAR=24.8
483 0 my ($self) = @_;
484 0 $self->{_write_dead} = 1;
485 0 $self->_disarm_write; # nothing more to write; don't spin
486 0 ▶ if ($self->{inflight}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inflight"}) 0 0 0
487 # wake a paused upstream so on_body sees _write_dead and aborts promptly
488 # (instead of waiting for the request timeout)
489 0 ▶ if ($self->{paused}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"paused"}) 0 0 0
490 0 $self->{paused} = 0;
491 0 local $self->{multi_state}{busy} = 1;
492 0 eval { $self->{multi}->resume($self->{inflight}) };
493 }
494 0 return;
495 }
496 0 $self->_close;
497 }
498  
499 125 ▶sub _flush {
Subroutine _flush: called CC=22 SCAR=38.6
500 125 my ($self) = @_;
501 0 ▶ if (!length $self->{wbuf}) { $self->_disarm_write; return } # nothing to write; drop the watcher
Branch: 1 branch
BranchTrueFalseTotal
unless (length $self->{"wbuf"}) 0 125 125
502 125 my $n;
503 125 ▶ if ($self->{ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl'}) { } 80 45 125
504 # A WANT retry must keep its original byte count even if wbuf grew.
505 80 my $pending = $self->{ssl_write_len};
506 $n = defined $pending
507 ? Net::SSLeay::write_partial($self->{ssl}, 0, $pending, $self->{wbuf})
508 80 ▶ : Net::SSLeay::write($self->{ssl}, $self->{wbuf});
Branch: 1 branch
BranchTrueFalseTotal
defined $pending ? : 78 2 80
509 80 ▶ if (!defined $n || $n <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $n or $n <= 0) 78 2 80
Condition66%2/3
not defined $n or $n <= 0
l!l&&r!l&&!r
0782
MC/DC50%1/2
not defined $n or $n <= 0
not defined $n $n <= 0
Truth table66%2/3
not(A) or B
A= not defined $nB= $n <= 0
ABresult
1X1
011
000
510 78 ▶ my $e = Net::SSLeay::get_error($self->{ssl}, defined $n ? $n : -1);
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 78 0 78
511 78 ▶ if ($e == Net::SSLeay::ERROR_WANT_WRITE() || $e == Net::SSLeay::ERROR_WANT_READ()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e == Net::SSLeay::ERROR_WANT_WRITE() or $e == Net::SSLeay::ERROR_WANT_READ()) 78 0 78
Condition33%1/3
$e == Net::SSLeay::ERROR_WANT_WRITE() or $e == Net::SSLeay::ERROR_WANT_READ()
l!l&&r!l&&!r
7800
MC/DC0%0/2
$e == Net::SSLeay::ERROR_WANT_WRITE() or $e == Net::SSLeay::ERROR_WANT_READ()
$e == Net::SSLeay::ERROR_WANT_WRITE() $e == Net::SSLeay::ERROR_WANT_READ()
Truth table33%1/3
A or B
A= $e == Net::SSLeay::ERROR_WANT_WRITE()B= $e == Net::SSLeay::ERROR_WANT_READ()
ABresult
1X1
011
000
512 78 ▶ $self->{ssl_write_len} //= length $self->{wbuf};
Condition66%2/3
$self->{"ssl_write_len"} //= length $self->{"wbuf"}
l!l&&r!l&&!r
7620
MC/DC0%0/2
$self->{"ssl_write_len"} //= length $self->{"wbuf"}
$self->{"ssl_write_len"} length $self->{"wbuf"}
Truth table66%2/3
A //= B
A= $self->{"ssl_write_len"}B= length $self->{"wbuf"}
ABresult
1X1
011
000
513 78 ▶ return $self->_arm_write if $e == Net::SSLeay::ERROR_WANT_WRITE();
Branch: 1 branch
BranchTrueFalseTotal
if $e == Net::SSLeay::ERROR_WANT_WRITE() 78 0 78
514 0 $self->_disarm_write; return;
515 }
516 0 return $self->_fail_write;
517 }
518 2 ▶ delete $self->{ssl_write_len} if defined $pending;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pending 2 0 2
519 } else {
520 45 $n = syswrite($self->{sock}, $self->{wbuf});
521 0 ▶ if (!defined $n) { return $self->_arm_write if $! == EAGAIN || $! == EWOULDBLOCK; return $self->_fail_write }
Branch: 2 branches
BranchTrueFalseTotal
unless (defined $n) 0 45 45
if $! == 11 or $! == 11 0 0 0
Condition0%0/3
$! == 11 or $! == 11
l!l&&r!l&&!r
000
MC/DC0%0/2
$! == 11 or $! == 11
$! == 11 $! == 11
Truth table0%0/3
A or B
A= $! == 11B= $! == 11
ABresult
1X1
011
000
522 }
523 47 substr($self->{wbuf}, 0, $n, '');
524 47 ▶ $self->{idle}->again if $self->{idle}; # wrote bytes -> progress
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"idle"} 0 47 47
525 47 ▶ if (length $self->{wbuf}) { return $self->_arm_write } # partial; finish later
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"wbuf"}) 1 46 47
526 46 $self->_disarm_write;
527 # pre-TLS plaintext (the CONNECT 200, possibly behind a prior response tail) has
528 # now fully flushed -> it is safe to start the handshake (deferred from CONNECT)
529 46 ▶ if (delete $self->{_tls_pending} && $self->{cert}) { return $self->_begin_tls }
Branch: 1 branch
BranchTrueFalseTotal
if (delete $self->{"_tls_pending"} and $self->{"cert"}) 1 45 46
Condition66%2/3
delete $self->{"_tls_pending"} and $self->{"cert"}
!ll&&!rl&&r
4501
MC/DC50%1/2
delete $self->{"_tls_pending"} and $self->{"cert"}
delete $self->{"_tls_pending"} $self->{"cert"}
Truth table66%2/3
A and B
A= delete $self->{"_tls_pending"}B= $self->{"cert"}
ABresult
0X0
100
111
530 45 ▶ if ($self->{paused}) { # room again -> resume upstream
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"paused"}) 0 45 45
531 0 $self->{paused} = 0;
532 0 local $self->{multi_state}{busy} = 1;
533 0 ▶ $self->{multi}->resume($self->{inflight}) if $self->{inflight};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inflight"} 0 0 0
534 }
535 45 ▶ $self->_maybe_close_after_flush if $self->{close_after_flush};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"close_after_flush"} 9 36 45
536 }
537  
538 41 ▶sub _close {
Subroutine _close: called CC=9 SCAR=22.0
539 41 my ($self) = @_;
540 41 ▶ return if $self->{_dead};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_dead"} 4 37 41
541 37 $self->{_write_dead} = 1;
542 37 ▶ if ($self->{multi_state}{busy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"multi_state"}{"busy"}) 4 33 37
543 4 $self->{_closing} = 1;
544 4 ▶ $self->{closer} ||= EV::timer(0, 0, sub { delete $self->{closer}; $self->_close });
Subroutine __ANON__: called CC=1 SCAR=0.4
Condition33%1/3
$self->{"closer"} ||= &EV::timer(0, 0, sub { delete $self->{"closer"}; $self->_close; } )
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->{"closer"} ||= &EV::timer(0, 0, sub { delete $self->{"closer"}; $self->_close; } )
$self->{"closer"} &EV::timer(0, 0, sub { delete $self->{"closer"}; $self->_close; } )
Truth table33%1/3
A ||= B
A= $self->{"closer"}B= &EV::timer(0, 0, sub { delete $self->{"closer"}; $self->_close; } )
ABresult
1X1
011
000
545 4 return;
546 }
547 33 $self->{_dead} = 1;
548 33 $self->{io_r} = undef; $self->{io_w} = undef; $self->{idle} = undef; $self->{closer} = undef;
549 33 ▶ if ($self->{inflight}) { eval { $self->{multi}->remove($self->{inflight}) }; delete $self->{inflight} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"inflight"}) 2 31 33
550 33 ▶ if ($self->{ssl}) { Net::SSLeay::free($self->{ssl}); undef $self->{ssl} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ssl"}) 2 31 33
551 33 ▶ if ($self->{ctx}) { Net::SSLeay::CTX_free($self->{ctx}); undef $self->{ctx} }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ctx"}) 2 31 33
552 33 ▶ close $self->{sock} if $self->{sock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sock"} 33 0 33
553 33 my $on_close = delete $self->{on_close};
554 33 delete @{$self}{qw(make_handle on_request on_response)};
555 33 ▶ $on_close->($self) if $on_close;
Branch: 1 branch
BranchTrueFalseTotal
if $on_close 33 0 33
556 }
557  
558 5 1;
559  
560 __END__
↑ Summary Impersonate/HTTP.pm »