| 1 | package Arriba::Connection::HTTP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Data::Dump qw(dump); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use HTTP::Status qw(status_message); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use IO::Socket qw(:crlf); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Plack::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use base 'Arriba::Connection'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 15 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 16 | 1 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | if ($self->{client}->NS_proto eq 'TCP') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | setsockopt($self->{client}, IPPROTO_TCP, TCP_NODELAY, 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | or die $!; | ||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | $self->{_inputbuf} = ''; | |||||||||||||||||||||||||||
| 24 | 1 | $self->{_current_req} = undef; | |||||||||||||||||||||||||||
| 25 | 1 | $self->{_keepalive} = 1; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | return $self; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | sub read_request { | ||||||||||||||||||||||||||
|
Subroutine
read_request: called CC=19 SCAR=53.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 1 | my $req; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | if ($req = $self->{_current_req}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 36 | # Partially processed request | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $get_chunk = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($self->{_inputbuf}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | my $chunk = delete $self->{_inputbuf}; | |||||||||||||||||||||||||||
| 40 | 0 | return ($chunk, length $chunk); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | my $read = sysread $self->{client}, my($chunk), $self->{chunk_size}; | |||||||||||||||||||||||||||
| 43 | 0 | return ($chunk, $read); | |||||||||||||||||||||||||||
| 44 | 0 | }; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | my $chunked = do { | |||||||||||||||||||||||||||
| 47 | 2 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | 0 | lc delete $req->{env}->{HTTP_TRANSFER_ENCODING} eq 'chunked' | |||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 0 | ▶ | if ((my $cl = $req->{content_length}) >= 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 52 | 0 | $req->{body_stream} = Stream::Buffered->new($req->{content_length}); | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | while ($cl > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | my($chunk, $read) = $get_chunk->(); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if (!defined $read || $read == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $read or $read == 0
MC/DC0%0/2
not defined $read or $read == 0
not defined $read
$read == 0
Truth table0%0/3
not(A) or B
A= not defined $readB= $read == 0
| |||||||||||||||||||||||||||||
| 57 | 0 | die "Read error: $!\n"; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | $cl -= $read; | |||||||||||||||||||||||||||
| 61 | 0 | $req->{body_stream}->print($chunk); | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | elsif ($chunked) { | ||||||||||||||||||||||||||||
| 65 | 0 | $req->{body_stream} = Stream::Buffered->new; | |||||||||||||||||||||||||||
| 66 | 0 | my $chunk_buffer = ''; | |||||||||||||||||||||||||||
| 67 | 0 | my $length; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | DECHUNK: | ||||||||||||||||||||||||||||
| 70 | 0 | while (1) { | |||||||||||||||||||||||||||
| 71 | 0 | my($chunk, $read) = $get_chunk->(); | |||||||||||||||||||||||||||
| 72 | 0 | $chunk_buffer .= $chunk; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | while ($chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | my $trailer = $1; | |||||||||||||||||||||||||||
| 76 | 0 | my $chunk_len = hex $2; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($chunk_len == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 79 | 0 | last DECHUNK; | |||||||||||||||||||||||||||
| 80 | } elsif (length $chunk_buffer < $chunk_len + 2) { | ||||||||||||||||||||||||||||
| 81 | 0 | $chunk_buffer = $trailer . $chunk_buffer; | |||||||||||||||||||||||||||
| 82 | 0 | last; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 0 | $req->{body_stream}->print(substr($chunk_buffer, 0, | |||||||||||||||||||||||||||
| 86 | $chunk_len, '')); | ||||||||||||||||||||||||||||
| 87 | 0 | $chunk_buffer =~ s/^\015\012//; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $length += $chunk_len; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | last unless $read && $read > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$read and $read > 0
MC/DC0%0/2
$read and $read > 0
$read
$read > 0
Truth table0%0/3
A and B
A= $readB= $read > 0
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $req->{content_length} = $length; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $req->{complete} = 1; | |||||||||||||||||||||||||||
| 99 | 0 | $self->{_current_req} = undef; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | elsif ($self->{_keepalive}) { | ||||||||||||||||||||||||||||
| 102 | # New request | ||||||||||||||||||||||||||||
| 103 | 1 | $req = Arriba::Request->new($self); | |||||||||||||||||||||||||||
| 104 | 1 | ▶ | $req->{scheme} = $self->{ssl} ? 'https' : 'http'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 1 | while (1) { | |||||||||||||||||||||||||||
| 107 | last if defined $self->{_inputbuf} && | ||||||||||||||||||||||||||||
| 108 | 1 | ▶ | $self->{_inputbuf} =~ /$CRLF$CRLF/s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $self->{"_inputbuf"} and $self->{"_inputbuf"} =~ /$Arriba::Connection::HTTP::CRLF$Arriba::Connection::HTTP::CRLF/s
MC/DC0%0/2
defined $self->{"_inputbuf"} and $self->{"_inputbuf"} =~ /$Arriba::Connection::HTTP::CRLF$Arriba::Connection::HTTP::CRLF/s
defined $self->{"_inputbuf"}
$self->{"_inputbuf"} =~ /$Arriba::Connection::HTTP::CRLF$Arriba::Connection::HTTP::CRLF/s
Truth table33%1/3
A and B
A= defined $self->{"_inputbuf"}B= $self->{"_inputbuf"} =~ /$Arriba::Connection::HTTP::CRLF$Arriba::Connection::HTTP::CRLF/s
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 1 | my $read = sysread $self->{client}, my $buf, $self->{chunk_size}; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 1 | ▶ | if (!defined $read || $read == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $read or $read == 0
MC/DC0%0/2
not defined $read or $read == 0
not defined $read
$read == 0
Truth table33%1/3
not(A) or B
A= not defined $readB= $read == 0
| |||||||||||||||||||||||||||||
| 113 | 1 | die "Read error: $!\n"; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | $self->{_inputbuf} .= $buf; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | (my $headers, $self->{_inputbuf}) = | ||||||||||||||||||||||||||||
| 120 | 0 | split /$CRLF$CRLF/, $self->{_inputbuf}, 2; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Add back two CRLFs, HTTP::Parser's parse_http_requests expects that | ||||||||||||||||||||||||||||
| 123 | 0 | $req->{headers} = $headers . $CRLF . $CRLF; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($req->{headers} =~ /^content-length:\s*(\d+)\015?$/im) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | $req->{content_length} = $1; | |||||||||||||||||||||||||||
| 127 | 0 | $self->{_current_req} = $req; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | # No "Content-length" header, we have the whole request | ||||||||||||||||||||||||||||
| 131 | 0 | $req->{complete} = 1; | |||||||||||||||||||||||||||
| 132 | 0 | $self->{_current_req} = undef; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | return $req; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub write_response { | ||||||||||||||||||||||||||
|
Subroutine
write_response: not called CC=12 SCAR=50.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 140 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 141 | 0 | my $req = shift; | |||||||||||||||||||||||||||
| 142 | 0 | my $res = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $proto = $req->{env}->{SERVER_PROTOCOL}; | |||||||||||||||||||||||||||
| 145 | 0 | my $status = $res->[0]; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | my %headers; | |||||||||||||||||||||||||||
| 148 | my $chunked; | ||||||||||||||||||||||||||||
| 149 | 0 | my @header_lines = ("$proto $status " . status_message($status)); | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $res_headers = $res->[1]; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | for (my $i = 0; $i < @$res_headers; $i += 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | next if $res_headers->[$i] eq 'Connection'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | 0 | push @header_lines, $res_headers->[$i] . ": " . $res_headers->[$i+1]; | |||||||||||||||||||||||||||
| 156 | 0 | $headers{lc $res_headers->[$i]} = $res_headers->[$i+1]; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($proto eq 'HTTP/1.1') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | if (!exists $headers{'content-length'}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 161 | 0 | ▶ | if ($status !~ /^1\d\d|[23]04$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | push @header_lines, 'Transfer-Encoding: chunked'; | |||||||||||||||||||||||||||
| 163 | 0 | $chunked = 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | elsif (my $te = $headers{'transfer-encoding'}) { | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if ($te eq 'chunked') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 0 | $chunked = 1; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | else { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | if (!exists $headers{'transfer-encoding'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $self->{_keepalive} = 0; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | if ($self->{_keepalive}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 0 | push @header_lines, 'Connection: keep-alive'; | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | else { | ||||||||||||||||||||||||||||
| 182 | 0 | push @header_lines, 'Connection: close'; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | syswrite $self->{client}, join($CRLF, @header_lines, '') . $CRLF; | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 0 | ▶ | if (defined $res->[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | Plack::Util::foreach($res->[2], sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 189 | 0 | my $buffer = $_[0]; | |||||||||||||||||||||||||||
| 190 | 0 | my ($len, $offset); | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ($chunked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $len = length $buffer; | |||||||||||||||||||||||||||
| 194 | 0 | ▶ | return unless $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | 0 | $buffer = sprintf( "%x", $len ) . $CRLF . $buffer . $CRLF; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 0 | $len = length $buffer; | |||||||||||||||||||||||||||
| 199 | 0 | $offset = 0; | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | while ($len) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 0 | my $written = syswrite $self->{client}, $buffer, $len, $offset; | |||||||||||||||||||||||||||
| 202 | # TODO: Handle errors maybe? | ||||||||||||||||||||||||||||
| 203 | 0 | $len -= $written; | |||||||||||||||||||||||||||
| 204 | 0 | $offset += $written; | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | 0 | }); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 0 | ▶ | syswrite $self->{client}, "0$CRLF$CRLF" if $chunked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | else { | ||||||||||||||||||||||||||||
| 211 | # TODO: Above loop also needed here | ||||||||||||||||||||||||||||
| 212 | return Plack::Util::inline_object | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | write => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 214 | 0 | my $buf = $_[0]; | |||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($chunked) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 0 | my $len = length $buf; | |||||||||||||||||||||||||||
| 217 | 0 | ▶ | return unless $len; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | 0 | $buf = sprintf( "%x", $len ) . $CRLF . $buf . $CRLF; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | 0 | syswrite $self->{client}, $buf; | |||||||||||||||||||||||||||
| 221 | }, | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | close => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | syswrite $self->{client}, "0$CRLF$CRLF" if $chunked; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 0 | }; | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | 2 | 1; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||