| 1 | # Base class for all socket types | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright 2004, Danga Interactive, Inc. | ||||||||||||||||||||||||||||
| 4 | # Copyright 2005-2007, Six Apart, Ltd. | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package Perlbal::Socket; | ||||||||||||||||||||||||||||
| 7 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 22 | ▶ | no warnings qw(deprecated); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 22 | ▶ | use Perlbal::HTTPHeaders; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 22 | ▶ | use Sys::Syscall; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 22 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 22 | ▶ | use Danga::Socket 1.44; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 22 | ▶ | use base 'Danga::Socket'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 22 | ▶ | use fields ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 22 | 'headers_string', # headers as they're being read | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 'req_headers', # the final Perlbal::HTTPHeaders object inbound | ||||||||||||||||||||||||||||
| 23 | 'res_headers', # response headers outbound (Perlbal::HTTPHeaders object) | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 'create_time', # creation time | ||||||||||||||||||||||||||||
| 26 | 'alive_time', # last time noted alive | ||||||||||||||||||||||||||||
| 27 | 'state', # general purpose state; used by descendants. | ||||||||||||||||||||||||||||
| 28 | 'do_die', # if on, die and do no further requests | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 'read_buf', # arrayref of scalarref read from client | ||||||||||||||||||||||||||||
| 31 | 'read_ahead', # bytes sitting in read_buf | ||||||||||||||||||||||||||||
| 32 | 'read_size', # total bytes read from client, ever | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 'ditch_leading_rn', # if true, the next header parsing will ignore a leading \r\n | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 'observed_ip_string', # if defined, contains the observed IP string of the peer | ||||||||||||||||||||||||||||
| 37 | # we're serving. this is intended for hoding the value of | ||||||||||||||||||||||||||||
| 38 | # the X-Forwarded-For and using it to govern ACLs. | ||||||||||||||||||||||||||||
| 39 | 22 | ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 22 | ▶ | use constant MAX_HTTP_HEADER_LENGTH => 102400; # 100k, arbitrary | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 22 | ▶ | use constant TRACK_OBJECTS => 0; # see @created_objects below | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 22 | if (TRACK_OBJECTS) { | |||||||||||||||||||||||||||
| 45 | 22 | ▶ | use Scalar::Util qw(weaken isweak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # kick-off one cleanup | ||||||||||||||||||||||||||||
| 49 | 22 | _do_cleanup(); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 22 | our %state_changes = (); # { "objref" => [ state, state, state, ... ] } | |||||||||||||||||||||||||||
| 52 | 22 | our $last_callbacks = 0; # time last ran callbacks | |||||||||||||||||||||||||||
| 53 | 22 | our $callbacks = []; # [ [ time, subref ], [ time, subref ], ... ] | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # this one deserves its own section. we keep track of every Perlbal::Socket object | ||||||||||||||||||||||||||||
| 56 | # created if the TRACK_OBJECTS constant is on. we use weakened references, though, | ||||||||||||||||||||||||||||
| 57 | # so this list will hopefully contain mostly undefs. users can ask for this list if | ||||||||||||||||||||||||||||
| 58 | # they want to work with it via the get_created_objects_ref function. | ||||||||||||||||||||||||||||
| 59 | 22 | our @created_objects; # ( $ref, $ref, $ref ... ) | |||||||||||||||||||||||||||
| 60 | 22 | our $last_co_cleanup = 0; # clean the list every few seconds | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub get_statechange_ref { | ||||||||||||||||||||||||||
|
Subroutine
get_statechange_ref: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 0 | return \%state_changes; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | sub get_created_objects_ref { | ||||||||||||||||||||||||||
|
Subroutine
get_created_objects_ref: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | return \@created_objects; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub write_debuggy { | ||||||||||||||||||||||||||
|
Subroutine
write_debuggy: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | my $cref = $_[0]; | |||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $content = ref $cref eq "SCALAR" ? $$cref : $cref; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $clen = defined $content ? length($content) : "undef"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $content = substr($content, 0, 17) . "..." if defined $content && $clen > 30; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $content and $clen > 30
MC/DC0%0/2
defined $content and $clen > 30
defined $content
$clen > 30
Truth table0%0/3
A and B
A= defined $contentB= $clen > 30
| |||||||||||||||||||||||||||||
| 77 | 0 | my ($pkg, $filename, $line) = caller; | |||||||||||||||||||||||||||
| 78 | 0 | print "write($self, <$clen>\"$content\") from ($pkg, $filename, $line)\n" if Perlbal::DEBUG >= 4; | |||||||||||||||||||||||||||
| 79 | 0 | $self->SUPER::write(@_); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 22 | if (Perlbal::DEBUG >= 4) { | |||||||||||||||||||||||||||
| 83 | 22 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 84 | *write = \&write_debuggy; | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 161 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | 161 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 89 | 161 | ▶ | $self = fields::new( $self ) unless ref $self; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 161 | Perlbal::objctor($self); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 161 | $self->SUPER::new( @_ ); | |||||||||||||||||||||||||||
| 94 | 161 | $self->{headers_string} = ''; | |||||||||||||||||||||||||||
| 95 | 161 | $self->{state} = undef; | |||||||||||||||||||||||||||
| 96 | 161 | $self->{do_die} = 0; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 161 | $self->{read_buf} = []; # arrayref of scalar refs of bufs read from client | |||||||||||||||||||||||||||
| 99 | 161 | $self->{read_ahead} = 0; # bytes sitting in read_buf | |||||||||||||||||||||||||||
| 100 | 161 | $self->{read_size} = 0; # total bytes read from client | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 161 | my $now = time; | |||||||||||||||||||||||||||
| 103 | 161 | $self->{alive_time} = $self->{create_time} = $now; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # now put this item in the list of created objects | ||||||||||||||||||||||||||||
| 106 | 161 | if (TRACK_OBJECTS) { | |||||||||||||||||||||||||||
| 107 | # clean the created objects list if necessary | ||||||||||||||||||||||||||||
| 108 | if ($last_co_cleanup < $now - 5) { | ||||||||||||||||||||||||||||
| 109 | # remove out undefs, because those are natural byproducts of weakening | ||||||||||||||||||||||||||||
| 110 | # references | ||||||||||||||||||||||||||||
| 111 | @created_objects = grep { $_ } @created_objects; | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # however, the grep turned our weak references back into strong ones, so | ||||||||||||||||||||||||||||
| 114 | # we have to re-weaken them | ||||||||||||||||||||||||||||
| 115 | weaken($_) foreach @created_objects; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # we've cleaned up at this point | ||||||||||||||||||||||||||||
| 118 | $last_co_cleanup = $now; | ||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # now add this one to our cleaned list and weaken it | ||||||||||||||||||||||||||||
| 122 | push @created_objects, $self; | ||||||||||||||||||||||||||||
| 123 | weaken($created_objects[-1]); | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 161 | return $self; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # We need to maintain a cache of socket classes and what cleanup | ||||||||||||||||||||||||||||
| 130 | # handler (if any) we perform on them. This is because classes based | ||||||||||||||||||||||||||||
| 131 | # on Perlbal::Socket get one method, and Perlbal::SocketSSL gets a | ||||||||||||||||||||||||||||
| 132 | # different handler. Caching this information is done rather than a | ||||||||||||||||||||||||||||
| 133 | # static list because you can make new client classes in Perlbal. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | # If perl cached ->isa($class) call results we can make this shorter. | ||||||||||||||||||||||||||||
| 136 | 22 | my %class_isa_cache; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # A list of socket classes that we are interested in, listed in | ||||||||||||||||||||||||||||
| 139 | # the order which they should be probed for. | ||||||||||||||||||||||||||||
| 140 | my %socket_class_handlers = ( | ||||||||||||||||||||||||||||
| 141 | 39 | ▶ | 'Perlbal::Socket' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 142 | 39 | my Perlbal::Socket $v = shift; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 39 | ▶ | my $max_age = eval { $v->max_idle_time } || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
eval {
do {
$v->max_idle_time
}
} || 0
MC/DC100%1/1
eval {
do {
$v->max_idle_time
}
} || 0
eval {
do {
$v->max_idle_time
}
}
Truth table100%2/2
A
A= eval {
do {
$v->max_idle_time
}
}
| |||||||||||||||||||||||||||||
| 145 | 39 | ▶ | return unless $max_age; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # We're inside the class where ->{alive_time} is defined, safe to use. | ||||||||||||||||||||||||||||
| 148 | $v->close("perlbal_timeout") | ||||||||||||||||||||||||||||
| 149 | 5 | ▶ | if $v->{alive_time} < $Perlbal::tick_time - $max_age; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | }, | ||||||||||||||||||||||||||||
| 151 | 22 | ); | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 22 | ▶ | sub set_socket_idle_handler { | ||||||||||||||||||||||||||
|
Subroutine
set_socket_idle_handler: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 22 | my $class = shift; | |||||||||||||||||||||||||||
| 155 | 22 | my $handler_class = shift; | |||||||||||||||||||||||||||
| 156 | 22 | my $handler = shift; | |||||||||||||||||||||||||||
| 157 | 22 | $socket_class_handlers{$handler_class} = $handler; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # FIXME: this doesn't scale in theory, but it might use less CPU in | ||||||||||||||||||||||||||||
| 161 | # practice than using the Heap:: modules and manipulating the | ||||||||||||||||||||||||||||
| 162 | # expirations all the time, thus doing things properly | ||||||||||||||||||||||||||||
| 163 | # algorithmically. and this is definitely less work, so it's worth | ||||||||||||||||||||||||||||
| 164 | # a try. | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 29 | ▶ | sub _do_cleanup { | ||||||||||||||||||||||||||
|
Subroutine
_do_cleanup: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 167 | 29 | my $sf = Perlbal::Socket->get_sock_ref; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 29 | ▶ | SOCKET: while (my $k = each %$sf) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 46 | my $sock = $sf->{$k}; | |||||||||||||||||||||||||||
| 171 | 46 | my $sock_class = ref $sf->{$k}; | |||||||||||||||||||||||||||
| 172 | 46 | ▶ | if (exists $class_isa_cache{$sock_class}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 39 | my $handler = $class_isa_cache{$sock_class}; | |||||||||||||||||||||||||||
| 174 | 39 | ▶ | next unless defined $handler; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 39 | $handler->($sock); | |||||||||||||||||||||||||||
| 176 | 39 | next SOCKET; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # No entry in the cache, find out what handler we should assign. | ||||||||||||||||||||||||||||
| 180 | 7 | my $handler; | |||||||||||||||||||||||||||
| 181 | 7 | foreach my $check_class (keys %socket_class_handlers) { | |||||||||||||||||||||||||||
| 182 | 14 | ▶ | next unless $sock->isa($check_class); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 7 | $handler = $socket_class_handlers{$check_class}; | |||||||||||||||||||||||||||
| 184 | 7 | last; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | # Outside the loop, so that we assign undef if none of the loop passes find anything. | ||||||||||||||||||||||||||||
| 187 | 7 | $class_isa_cache{$sock_class} = $handler; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 29 | Danga::Socket->AddTimer(5, \&_do_cleanup); | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # CLASS METHOD: given a delay (in seconds) and a subref, this will call | ||||||||||||||||||||||||||||
| 194 | # that subref in AT LEAST delay seconds. if the subref returns 0, the | ||||||||||||||||||||||||||||
| 195 | # callback is discarded, but if it returns a positive number, the callback | ||||||||||||||||||||||||||||
| 196 | # is pushed onto the callback stack to be called again in at least that | ||||||||||||||||||||||||||||
| 197 | # many seconds. | ||||||||||||||||||||||||||||
| 198 | 25 | ▶ | sub register_callback { | ||||||||||||||||||||||||||
|
Subroutine
register_callback: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 199 | # adds a new callback to our list | ||||||||||||||||||||||||||||
| 200 | 25 | my ($delay, $subref) = @_; | |||||||||||||||||||||||||||
| 201 | 25 | push @$callbacks, [ time + $delay, $subref ]; | |||||||||||||||||||||||||||
| 202 | 25 | return 1; | |||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | # CLASS METHOD: runs through the list of registered callbacks and executes | ||||||||||||||||||||||||||||
| 206 | # any that need to be executed | ||||||||||||||||||||||||||||
| 207 | # FIXME: this doesn't scale. need a heap. | ||||||||||||||||||||||||||||
| 208 | 847 | ▶ | sub run_callbacks { | ||||||||||||||||||||||||||
|
Subroutine
run_callbacks: called CC=6 SCAR=18.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 209 | 847 | my $now = time; | |||||||||||||||||||||||||||
| 210 | 847 | ▶ | return if $last_callbacks == $now; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 211 | 65 | $last_callbacks = $now; | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 65 | my @destlist = (); | |||||||||||||||||||||||||||
| 214 | 65 | foreach my $ref (@$callbacks) { | |||||||||||||||||||||||||||
| 215 | # if their time is <= now... | ||||||||||||||||||||||||||||
| 216 | 59 | ▶ | if ($ref->[0] <= $now) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | # find out if they want to run again... | ||||||||||||||||||||||||||||
| 218 | 1 | my $rv = $ref->[1]->(); | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | # and if they do, push onto list... | ||||||||||||||||||||||||||||
| 221 | 1 | ▶ | push @destlist, [ $rv + $now, $ref->[1] ] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $rv and $rv > 0
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
| |||||||||||||||||||||||||||||
| 222 | if defined $rv && $rv > 0; | ||||||||||||||||||||||||||||
| 223 | } else { | ||||||||||||||||||||||||||||
| 224 | # not time for this one, just shove it | ||||||||||||||||||||||||||||
| 225 | 58 | push @destlist, $ref; | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 65 | $callbacks = \@destlist; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # CLASS METHOD: | ||||||||||||||||||||||||||||
| 232 | # default is for sockets to never time out. classes | ||||||||||||||||||||||||||||
| 233 | # can override. | ||||||||||||||||||||||||||||
| 234 | 34 | ▶ | sub max_idle_time { 0; } | ||||||||||||||||||||||||||
|
Subroutine
max_idle_time: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | # Socket: specific to HTTP socket types (only here and not in | ||||||||||||||||||||||||||||
| 237 | # ClientHTTPBase because ClientManage wants it too) | ||||||||||||||||||||||||||||
| 238 | 271 | ▶ | sub read_request_headers { read_headers($_[0], 0); } | ||||||||||||||||||||||||||
|
Subroutine
read_request_headers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 239 | 164 | ▶ | sub read_response_headers { read_headers($_[0], 1); } | ||||||||||||||||||||||||||
|
Subroutine
read_response_headers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 240 | 435 | ▶ | sub read_headers { | ||||||||||||||||||||||||||
|
Subroutine
read_headers: called CC=13 SCAR=25.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 241 | 435 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 242 | 435 | my $is_res = shift; | |||||||||||||||||||||||||||
| 243 | 435 | print "Perlbal::Socket::read_headers($self) is_res=$is_res\n" if Perlbal::DEBUG >= 2; | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 435 | my $sock = $self->{sock}; | |||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 435 | my $to_read = MAX_HTTP_HEADER_LENGTH - length($self->{headers_string}); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 435 | my $bref = $self->read($to_read); | |||||||||||||||||||||||||||
| 250 | 435 | ▶ | unless (defined $bref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | # client disconnected | ||||||||||||||||||||||||||||
| 252 | 9 | print " client disconnected\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 253 | 9 | return $self->close('remote_closure'); | |||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 426 | $self->{headers_string} .= $$bref; | |||||||||||||||||||||||||||
| 257 | 426 | my $idx = index($self->{headers_string}, "\r\n\r\n"); | |||||||||||||||||||||||||||
| 258 | 426 | my $delim_len = 4; | |||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | # can't find the header delimiter? check for LFLF header delimiter. | ||||||||||||||||||||||||||||
| 261 | 426 | ▶ | if ($idx == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | 34 | $idx = index($self->{headers_string}, "\n\n"); | |||||||||||||||||||||||||||
| 263 | 34 | $delim_len = 2; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | # still can't find the header delimiter? | ||||||||||||||||||||||||||||
| 266 | 426 | ▶ | if ($idx == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | # usually we get the headers all in one packet (one event), so | ||||||||||||||||||||||||||||
| 269 | # if we get in here, that means it's more than likely the | ||||||||||||||||||||||||||||
| 270 | # extra \r\n and if we clean it now (throw it away), then we | ||||||||||||||||||||||||||||
| 271 | # can avoid a regexp later on. | ||||||||||||||||||||||||||||
| 272 | 34 | ▶ | if ($self->{ditch_leading_rn} && $self->{headers_string} eq "\r\n") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"ditch_leading_rn"} and $self->{"headers_string"} eq "\r\n"
MC/DC0%0/2
$self->{"ditch_leading_rn"} and $self->{"headers_string"} eq "\r\n"
$self->{"ditch_leading_rn"}
$self->{"headers_string"} eq "\r\n"
Truth table33%1/3
A and B
A= $self->{"ditch_leading_rn"}B= $self->{"headers_string"} eq "\r\n"
| |||||||||||||||||||||||||||||
| 273 | 34 | print " throwing away leading \\r\\n\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 274 | 34 | $self->{ditch_leading_rn} = 0; | |||||||||||||||||||||||||||
| 275 | 34 | $self->{headers_string} = ""; | |||||||||||||||||||||||||||
| 276 | 34 | return 0; | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | 0 | print " can't find end of headers\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 280 | $self->close('long_headers') | ||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if length($self->{headers_string}) >= MAX_HTTP_HEADER_LENGTH; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 0 | return 0; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 392 | my $hstr = substr($self->{headers_string}, 0, $idx); | |||||||||||||||||||||||||||
| 286 | 392 | print " pre-parsed headers: [$hstr]\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | 392 | my $extra = substr($self->{headers_string}, $idx+$delim_len); | |||||||||||||||||||||||||||
| 289 | 392 | ▶ | if (my $len = length($extra)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 192 | print " pushing back $len bytes after header\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 291 | 192 | $self->push_back_read(\$extra); | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | # some browsers send an extra \r\n after their POST bodies that isn't | ||||||||||||||||||||||||||||
| 295 | # in their content-length. a base class can tell us when they're | ||||||||||||||||||||||||||||
| 296 | # on their 2nd+ request after a POST and tell us to be ready for that | ||||||||||||||||||||||||||||
| 297 | # condition, and we'll clean it up | ||||||||||||||||||||||||||||
| 298 | 392 | ▶ | $hstr =~ s/^\r\n// if $self->{ditch_leading_rn}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 392 | ▶ | unless (($is_res ? $self->{res_headers} : $self->{req_headers}) = | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 301 | Perlbal::HTTPHeaders->new(\$hstr, $is_res)) { | ||||||||||||||||||||||||||||
| 302 | # bogus headers? close connection. | ||||||||||||||||||||||||||||
| 303 | 1 | print " bogus headers\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 304 | 1 | return $self->close("parse_header_failure"); | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 391 | print " got valid headers\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 391 | ▶ | $Perlbal::reqs++ unless $is_res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 391 | $self->{ditch_leading_rn} = 0; | |||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 391 | ▶ | return $is_res ? $self->{res_headers} : $self->{req_headers}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | ### METHOD: drain_read_buf_to( $destination ) | ||||||||||||||||||||||||||||
| 316 | ### Write read-buffered data (if any) from the receiving object to the | ||||||||||||||||||||||||||||
| 317 | ### I<destination> object. | ||||||||||||||||||||||||||||
| 318 | 115 | ▶ | sub drain_read_buf_to { | ||||||||||||||||||||||||||
|
Subroutine
drain_read_buf_to: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 319 | 115 | my ($self, $dest) = @_; | |||||||||||||||||||||||||||
| 320 | 115 | ▶ | return unless $self->{read_ahead}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | 33 | ▶ | while (my $bref = shift @{$self->{read_buf}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 38 | print "draining readbuf from $self to $dest: [$$bref]\n" if Perlbal::DEBUG >= 3; | |||||||||||||||||||||||||||
| 324 | 38 | $dest->write($bref); | |||||||||||||||||||||||||||
| 325 | 38 | $self->{read_ahead} -= length($$bref); | |||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | ### METHOD: die_gracefully() | ||||||||||||||||||||||||||||
| 330 | ### By default, if we're in persist_wait state, close. Else, ignore. Children | ||||||||||||||||||||||||||||
| 331 | ### can override if they want to do some other processing. | ||||||||||||||||||||||||||||
| 332 | 0 | ▶ | sub die_gracefully { | ||||||||||||||||||||||||||
|
Subroutine
die_gracefully: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 333 | 0 | my Perlbal::Socket $self = $_[0]; | |||||||||||||||||||||||||||
| 334 | 0 | ▶ | if (defined $self->state && $self->state eq 'persist_wait') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->state and $self->state eq "persist_wait"
MC/DC0%0/2
defined $self->state and $self->state eq "persist_wait"
defined $self->state
$self->state eq "persist_wait"
Truth table0%0/3
A and B
A= defined $self->stateB= $self->state eq "persist_wait"
| |||||||||||||||||||||||||||||
| 335 | 0 | $self->close('graceful_shutdown'); | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | 0 | $self->{do_die} = 1; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | ### METHOD: write() | ||||||||||||||||||||||||||||
| 341 | ### Overridden from Danga::Socket to update our alive time on successful writes | ||||||||||||||||||||||||||||
| 342 | ### Stops sockets from being closed on long-running write operations | ||||||||||||||||||||||||||||
| 343 | 1294 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 344 | 1294 | my $self = shift; | |||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | 1294 | my $ret; | |||||||||||||||||||||||||||
| 347 | 1294 | ▶ | if ($ret = $self->SUPER::write(@_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | # Mark this socket alive so we don't time out | ||||||||||||||||||||||||||||
| 349 | 1294 | $self->{alive_time} = $Perlbal::tick_time; | |||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | 1294 | return $ret; | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | ### METHOD: close() | ||||||||||||||||||||||||||||
| 356 | ### Set our state when we get closed. | ||||||||||||||||||||||||||||
| 357 | 91 | ▶ | sub close { | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 358 | 91 | my Perlbal::Socket $self = $_[0]; | |||||||||||||||||||||||||||
| 359 | 91 | $self->state('closed'); | |||||||||||||||||||||||||||
| 360 | 91 | return $self->SUPER::close($_[1]); | |||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | ### METHOD: state() | ||||||||||||||||||||||||||||
| 364 | ### If you pass a parameter, sets the state, else returns it. | ||||||||||||||||||||||||||||
| 365 | 1942 | ▶ | sub state { | ||||||||||||||||||||||||||
|
Subroutine
state: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 366 | 1942 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 367 | 1942 | ▶ | return $self->{state} unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 1935 | push @{$state_changes{"$self"} ||= []}, $_[0] if Perlbal::TRACK_STATES; | |||||||||||||||||||||||||||
| 370 | 1935 | return $self->{state} = $_[0]; | |||||||||||||||||||||||||||
| 371 | } | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | 0 | ▶ | sub observed_ip_string { | ||||||||||||||||||||||||||
|
Subroutine
observed_ip_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 374 | 0 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | return $self->{observed_ip_string} = $_[0]; | |||||||||||||||||||||||||||
| 378 | } else { | ||||||||||||||||||||||||||||
| 379 | 0 | return $self->{observed_ip_string}; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | } | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | 0 | ▶ | sub as_string_html { | ||||||||||||||||||||||||||
|
Subroutine
as_string_html: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 384 | 0 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 385 | 0 | return $self->SUPER::as_string; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | 221 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 389 | 221 | my Perlbal::Socket $self = shift; | |||||||||||||||||||||||||||
| 390 | 221 | delete $state_changes{"$self"} if Perlbal::TRACK_STATES; | |||||||||||||||||||||||||||
| 391 | 221 | Perlbal::objdtor($self); | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | # package function (not a method). returns bytes sent, or -1 on error. | ||||||||||||||||||||||||||||
| 395 | 22 | our $sf_defined = Sys::Syscall::sendfile_defined; | |||||||||||||||||||||||||||
| 396 | 22 | our $max_sf_readwrite = 128 * 1024; | |||||||||||||||||||||||||||
| 397 | 74 | ▶ | sub sendfile { | ||||||||||||||||||||||||||
|
Subroutine
sendfile: called CC=7 SCAR=36.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 398 | 74 | my ($sfd, $fd, $bytes) = @_; | |||||||||||||||||||||||||||
| 399 | 74 | ▶ | return Sys::Syscall::sendfile($sfd, $fd, $bytes) if $sf_defined; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | # no support for sendfile. ghetto version: read and write. | ||||||||||||||||||||||||||||
| 402 | 0 | my $buf; | |||||||||||||||||||||||||||
| 403 | 0 | ▶ | $bytes = $max_sf_readwrite if $bytes > $max_sf_readwrite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 0 | my $rv = POSIX::read($fd, $buf, $bytes); | |||||||||||||||||||||||||||
| 406 | 0 | ▶ | return -1 unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | ▶ | return -1 unless $rv == $bytes; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | 0 | my $wv = POSIX::write($sfd, $buf, $rv); | |||||||||||||||||||||||||||
| 410 | 0 | ▶ | return -1 unless defined $wv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | 0 | ▶ | if (my $over_read = $rv - $wv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 0 | POSIX::lseek($fd, -$over_read, &POSIX::SEEK_CUR); | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | return $wv; | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | 22 | 1; | |||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | # Local Variables: | ||||||||||||||||||||||||||||
| 423 | # mode: perl | ||||||||||||||||||||||||||||
| 424 | # c-basic-indent: 4 | ||||||||||||||||||||||||||||
| 425 | # indent-tabs-mode: nil | ||||||||||||||||||||||||||||
| 426 | # End: | ||||||||||||||||||||||||||||