PROXY  WHOIS  RQUOTE  TEXTS  SOFT  FOREX  BBOARD
 Music  Philosophy  Code  Literature  Russian

= ROOT|Technical|Proxy_Docs|_Perl_code|httpproxy-1.5.3.pl =

page 6 of 8



	  # if server connection is dead, let's assume no more data from server
	  last if (!defined $st{$sck}{ch}) or (!defined $st{$st{$sck}{ch}}); # 
	  # prepare select to read server
	  $sl_write->remove($sck); 
	  $sl_read->add($st{$sck}{ch});
	  # reset these var
	  $st{$sck}{resp} = ""; 
	  $st{$sck}{resp_len} = 0;
	  next WRITE_FHS;
	}
	$byte_written = syswrite $sck, $st{$sck}{resp}, $st{$sck}{resp_len}, $st{$sck}{resp_offset};
	if (defined $byte_written ) {
	  debug "Proxy->Client ($byte_written bytes)",$sck;
	  $st{$sck}{resp_len} -= $byte_written;
	  $st{$sck}{resp_offset} += $byte_written;
	  if ($byte_written == 0) { #client closes its connection
	    debug "Client closed the connection",$sck;
	    clientclose $sck;
	    next WRITE_FHS;
	  }
	}
	elsif ($! == EAGAIN()) { #client overloaded, return later to finish sending request
	  debug "Client overloaded, skipping... (non-blocking)",$sck;
	  next WRITE_FHS;
	}
	elsif ($! == EPIPE()) { #client connection has disappeared!
	  debug "Client connection abruptly closed",$sck;
	  clientclose $sck;
	  next WRITE_FHS;
	}
	else { # something else happens
	  debug "Client connection error",$sck;
	  clientclose $sck;
	  next WRITE_FHS;
	}
      }
      # Client writing finished.
      $sl_write->remove($sck); # nothing to write
      if ($st{$sck}{persist}) {
	debug "Persistent on, waiting more request from Client",$sck;
	delete $st{$sck}; #remove all request/response dependent variables
	# ..but keep this var
	$st{$sck}{tipe} = 1; 
	$sl_read->add($sck);
	$st{$sck}{peerhost} = join "", $sck->peerhost(), ":", $sck->peerport();
	# reset session var to default
	$st{$sck}{mark} = 1; 
	$st{$sck}{persist} = 1; 
      }
      else {
	debug "Pesistent is off",$sck;      
	clientclose $sck;
      }
    }
  }
}

sub quickresp ($$$) { # generate response message for some errors
  my ($code, $body, $socket) = @_;
  my $nowis = localtime(time);
  debug "Error: $code $body", $socket;
  $sl_read->remove($socket); $sl_write->remove($socket); # remove both just to make sure
  $socket = $st{$socket}{ch} if $st{$socket}{tipe} == 2; # if server use this subroutine
  $st{$socket}{resp} = <<STOP;
HTTP/1.1 $code $reason{$code}\r
Content-type: text/html\r
Connection: close\r
\r
<html>
  <head>
    <title>$code $reason{$code}</title>
  </head>
  <body>
  <h1>$code $reason{$code}</h1>
  <h3>$body</h3>
  <hr>
  <p>Message generated at $nowis</p>
  <p><b>proxymultiplexer12</b> by Dody Suria Wijaya - <a href=mailto:>dody\@neuk.net</a></p>
  </body>
</html>
STOP
  $st{$socket}{resp_len} = length $st{$socket}{resp};
  $sl_write->add($socket);
  return 1;
}  

sub debug($$;$) { # argumens are message text, socket, and indents (optional)
  return unless $opt_d;
  my $message = shift;
  my $socket = shift;
  my $indent = shift;
  $indent||=10;
  $socket = $st{$socket}{ch} if $st{$socket}{tipe} == 2; # if this subroutine argument socket is a server socket

  if (defined $debug{$socket}) {
    $message = " "x$indent.$message;
  }
  else {    
    my $line = "=" x length $message;
    $message = "$line\n$message\n$line";
  }
=6=

1|2|3|4|5| < PREV = PAGE 6 = NEXT > |7|8

UP TO ROOT | UP TO DIR | TO FIRST PAGE

Google
 


E-mail Facebook Google Digg del.icio.us BlinkList Fark Furl Ma.gnolia Netscape NewsVine Reddit Slashdot Spurl StumbleUpon Technorati YahooMyWeb LiveJournal Blogmarks TwitThis Live News2.ru BobrDobr.ru Memori.ru MoeMesto.ru

0.00644088 wallclock secs ( 0.01 usr + 0.00 sys = 0.01 CPU)