Apache-HTTunnel

 view release on metacpan or  search on metacpan

Client/httunnel  view on Meta::CPAN

							send_to($client, $buf) ;
						}
						else {
							write_to($client, $buf) ;
						}
					}
					else {
						$hc->print($buf) ;
					}
				}
			}
		} ;
		if ($@){
			my $e = $@ ;
			kill 9, $pid if defined($pid) ;
			eval { $hc->close() ; } ;
			slog('err', "$@\n") if $@ ;
			sdie("$e\n") ;
		}
	}
}


###############################################################################


sub get_conf {
	my $path = shift ;

	my @files = () ;
	if (-d $path){
		$path =~ s/\/+$// ;
		# open dir and get all .conf files.
		opendir(CONFD, $path) or die("Can't open configuration directory '$path': $!\n") ;
		push @files, map {{name => "$path/$_", lines => []}} grep {$_ =~ /\.conf$/} readdir CONFD ;
		die("No configuration files (*.conf) found in '$path'") unless scalar(@files) ;
	}
	else{
		push @files, {name => $path, lines => []} ;
	}

	foreach my $f (@files){
		slog('notice', "Processing configuration file '$f->{name}'") ;
		open(CONF, "<$f->{name}") or die("Can't open configuration file '$f->{name}': $!\n") ;
		push @{$f->{lines}}, <CONF> ;
		close(CONF) ;
	}

	# Process configuration
	my %defaults = (
		local_addr => 'localhost',
		local_port => undef,
		protocol => 'tcp',
		remote_port => undef,
		remote_host => undef,

		url => undef,
		http_protocol => 'HTTP/1.1',
		http_keep_alive => 1,
		http_username => '',
		http_password => '',
		http_proxy => '',
		http_proxy_username => '',
		http_proxy_password => '',
		read_length => 131072,
		read_timeout => 15,
		verbose => $DEBUG,
	) ;
	
	foreach my $f (@files){
		my $cnt = 0 ;
		my $section = $f->{name} ;
		my $config = {$section => {%defaults}} ;
		foreach my $l (@{$f->{lines}}){
			chomp($l) ;
			$l =~ s/^\s+// ;
			$l =~ s/\s+$// ;
			$cnt++ ;

			if (($l eq '')||($l =~ /^[;#]/)){
				next ;
			}
			elsif ($l =~ /^\[(.+?)\]$/){
				$section = "$f->{name}:[$1]" ;
				$config->{$section} = {%{$config->{$f->{name}}}} ;
			}
			elsif (($l =~ /^(\w+)\s*=\s*(.*?)$/)&&(exists $config->{$section}->{$1})){
				$config->{$section}->{$1} = $2 ;
			}
			else{
				die("Invalid configuration directive at '$f->{name}' line $cnt") ;
			}
		}
		$f->{config} = $config ;
	}

	# Validation
	my $config = {} ;
	foreach my $f (@files){
		my $nb_sections = 0 ;
		foreach my $s (keys %{$f->{config}}){
			if ($s ne $f->{name}){
				$nb_sections++ ;
				foreach my $k (keys %{$f->{config}->{$s}}){
					die("Configuration parameter '$k' is required in section '$s'\n") 
						unless defined($f->{config}->{$s}->{$k}) ;
				}
				$config->{$s} = $f->{config}->{$s} ;
				$config->{$s}->{__file__} = $f->{name} ;
				$config->{$s}->{__section__} = $s ;
			}
		}
		die("No sections declared in configuration file '$f->{name}'\n") unless $nb_sections ;
	}

	sdebug(Dumper($config)) ;

	return $config ;
}


sub start_listeners {
	my $config = shift ;

Client/httunnel  view on Meta::CPAN

	my $maybe_binary = shift ;

	if (($DEBUG)||($SYSLOG)){
		my @lines = split(/\n/, $msg) ;
		foreach my $l (@lines){
			if ($SYSLOG){
				# We do not send possible binary debug info to syslog since it may screw up syslog.
				Sys::Syslog::syslog($level, "$indent$l") unless $maybe_binary ;
			}
			elsif ($DEBUG){
				if ((! $maybe_binary)||($DEBUG > 1)){
					print STDERR "[$$][$level] $indent$l\n" ;
				}
			}
		}
	}
}


sub sdie ($){
	my $msg = shift ;

	$DEBUG = 1 ;
	slog('err', $msg) ;
	exit(1) ;
}



###############################################################################



package My::HTTunnel::Client ;
BEGIN { @My::HTTunnel::Client::ISA = qw(HTTunnel::Client) ; }


use strict ;


sub new {
	my $class = shift ;
	my $cfg = shift ;

	my $this = $class->SUPER::new($cfg->{url}, @_) ;
	$this->{__PACKAGE__}->{cfg} = $cfg ;
	bless($this, $class) ;

	return $this ;
}


sub get_basic_credentials {
	my $this = shift ;
	my $realm = shift ;
	my $uri = shift ;
	my $proxy = shift ;

	my $cfg = $this->{__PACKAGE__}->{cfg} ;
	if ($proxy){
		return ($cfg->{http_proxy_username}, $cfg->{http_proxy_password}) ;
	}

	return ($cfg->{http_username}, $cfg->{http_password}) ;
}


sub request_callback {
	my $this = shift ;
	my $req = shift ;

	$req->protocol($this->{__PACKAGE__}->{cfg}->{http_protocol}) ;
	main::sdebug("HTTP Request:") ;
	main::sdebug(join(' ', $req->method(), $req->uri(), $req->protocol()), '  ') ;
	main::sdebug($req->headers()->as_string(), '  ') ;
	main::sdebug($req->content(), '  ', 1) ;
}


sub response_callback {
	my $this = shift ;
	my $resp = shift ;

	main::sdebug("HTTP Response:") ;
	main::sdebug(join(' ', $resp->protocol(), $resp->code(), $resp->message()), '  ') ;
	main::sdebug($resp->headers()->as_string(), '  ') ;
	main::sdebug($resp->content(), '  ', 1) ;
}



( run in 1.312 second using v1.01-cache-2.11-cpan-c966e8aa7e8 )