You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Sumit Shah <ss...@gce2000.com> on 2006/11/09 01:35:18 UTC

Mod_perl and HTTP IO issue

 
All,

I have a perl handler that invokes a servlet (HTTP IO) running on a
remote server. The servlet returns a single word response (INVALID or
VALID). The handler is supposed to redirect the URL to some other
website. For testing I redirect it to Google. 
The handler uses plain Socket implementation.  What I am noticing is
that the handler does invoke the servlet but does read the response
back. It seems like the connection gets closed before the handler gets
to read the response. 

But, if I run the handler as a standalone Perl script it works as
expected. Below is the handler code. It is under Apache 1.3.x and
Mod_perl 1.

I would appreciate any suggestions that you may have. 

Thanks
Sumit

########################################################################
######
package Apache::Proxy;

use strict;
use mod_perl 1.17_01;
use Apache::Constants qw(REDIRECT); 
use Apache::Constants qw(OK); 
use Socket;
$Apache::Proxy::VERSION = "1.00";

sub handler {

	my $r = shift;	
	
	my $url =
"http://38.118.10.36/SSOWeb/servlet/SsoProxyServlet?ssotoken=0ee1978f-72
9f-4554-a4d2-5a7df7db7243";
	$url=~m/http\:\/\/([^\:^\/]*)(?:\:(\d+))?\/(.*)/;
	my $host = $1;
	my $port = $2;
	$port = 80 unless($port);
	my $file = '/'.$3;
	
	my $proto = getprotobyname('tcp');
	socket(SOCK, PF_INET, SOCK_STREAM, $proto);
	
	my $sin = sockaddr_in($port, inet_aton($host));
	
	connect(SOCK, $sin) || die "Connect failed: $!\n";
	
	my $old_fh = select(SOCK);
	$|=1;
	select($old_fh);
	
	print SOCK "GET $file HTTP/1.0\n";
	print SOCK "Accept: */*\n";
	print SOCK "User-Agent: webamp\n\n";
	
	my $header = <SOCK>;
	my $content; 
	my $out;
	
	#This part of the code reads the RESPONSE HEADER
	while($content = &sockread())
	{
	      $out .= $content;   
	      last if ($content =~ /^\015?\012?$/);
	}
	
	my $result='';

	#READ THE RESPONSE BODY
	while (defined($content = <SOCK>)) {
		$result = $result . $content;
	}        
	
	#Check the status	
	if ($result =~ m/INVALID/i){	
		my $url1 = 'http://www.google.com';
		$r->content_type('text/html');
		$r->headers_out->set('Location' => $url1);
		$r->status(Apache::Constants::REDIRECT);
        	return Apache::Constants::REDIRECT;
        }
	return;
}

#####################################################
# Attempt to read a line.
sub sockread () {
  my $line;
  
  eval {
      
  	local$SIG{ALRM} = sub { die 'timeout!' };
	$line = <SOCK>;
	
  };
  if ($@ and $@ !~ /timeout!/) {warn("during socket read: $@\n")}
  return $line;
} #end