Apache-FilteringProxy

 view release on metacpan or  search on metacpan

lib/Apache/FilteringProxy.pm  view on Meta::CPAN

	} else {
		$r->warn("no server name: local hostname: $local_hostname, local servername: $local_servername");
		# could print out some pretty HTML message here
		return FORBIDDEN;
	}

	# this will be used when only the resource name is specified with the
	# resource subdomain.  We will default redirect the user to the url
	# specified for the resource.
	if ($resource_id and !$remote_servername) {
		if (defined($Apache::FilteringProxy::url{$resource_id}) and 
		    ($Apache::FilteringProxy::url{$resource_id} ne ""))
		{
			my $url = $Apache::FilteringProxy::url{$resource_id};

			if ($url =~ 
			m@
				(http:\/\/)					# http://
				([A-Za-z0-9\.\-]+)	 		# hostname
				(:([0-9]{1,5}))? 			# :port
				(\/.*|$)					# the path
			@ix) {
				my $http_string = $1;
				my $hostname = $2;
				my $port = $4 || "80";
				my $path = $5 || "";

				$r->warn("DEBUG: redirecting user to '$http_string$hostname.port$port.$resource_id.$local_servername$path'") unless ($Apache::FilteringProxy::logging < 2);
				$r->header_out("Location" => "$http_string$hostname.port$port.$resource_id.$local_servername$path");
				return REDIRECT;
			} else {
				$r->warn("DEBUG: no valid url (specified: '$url') to redirect user to for resource '$resource_id'") unless ($Apache::FilteringProxy::logging < 2);
				# we could do something like print some nice HTML error here
				return FORBIDDEN;
			}
		} else {
			$r->warn("DEBUG: no url to redirect user to for resource '$resource_id'") unless ($Apache::FilteringProxy::logging < 2);
			# we could do something like print some nice HTML error here
			return FORBIDDEN;
		}
	}

	# in admin mode, we want to rewrite every host we encounter
	if ($mode eq "admin") {
		# let's add the remote server to our list of hosts/domains we want to
		# configure for proxying
		$r->warn("ADMIN: adding hostname for resource") unless ($Apache::FilteringProxy::logging < 2);

		use DBI;
		my $dbh = DBI->connect("dbi:$Apache::FilteringProxy::db_driver:dbname=$Apache::FilteringProxy::db_database;host=$Apache::FilteringProxy::db_hostname;port=$Apache::FilteringProxy::db_hostport",$Apache::FilteringProxy::db_username,$Apache::FilteringP...

		# get all current hosts in admin to make sure we dont add the
		# hostname a second time.  The admin tool clears old entries
		# out before starting, so we know all entries in the db are valid
		my $sth = $dbh->prepare("SELECT hostname from admin;");
		$sth->execute();

		# make list of hosts
		my @hostname_list;
		my $hostname;
		$sth->bind_columns(\$hostname);
		while ($sth->fetch()) {
			push(@hostname_list, $hostname);
		}
		if (!grep(/^$remote_servername$/,@hostname_list)) {
			my $sth = $dbh->prepare("INSERT INTO admin (id, hostname) VALUES (nextval('admin_id_seq'), '$remote_servername');");
			$sth->execute();
		}
		$sth->finish();
		$dbh->disconnect();
	}

	# grab the uri that was requested and prepare it to be used to request
	# the document on the remote server
	my $uri = $r->parsed_uri;
	my $path = $uri->path();
	my $unparsed = $uri->unparse();
	my $query = $uri->query || "";
	if ($query) {
		$path .= "?$query";
	} elsif ($unparsed =~ /\?$/) {
		$path .= "?";
	}

	# no remote servername and no resource was caught above, so we redirect
	# user to the default_url page
	if (!$remote_servername) {
		$r->warn("DEBUG: no servername, redirecting to default url '$Apache::FilteringProxy::default_url'") unless ($Apache::FilteringProxy::logging < 2);
		$r->header_out("Location" => $Apache::FilteringProxy::default_url);
		return REDIRECT;
	}

	# in admin mode we want all hosts, not just configured ones to be proxied
	if ($mode ne "admin") {
		# used to make sure all kinds of other domains can't be directly accessed
		# through this script unless they are in the list of configured host or
		# domains
		my $found = 0;
		if (!grep(/^$remote_servername$/, @{$Apache::FilteringProxy::proxy_host_include_list{$resource_id}})) {
			foreach (@{$Apache::FilteringProxy::proxy_domain_include_list{$resource_id}}) {
				$r->warn("DEBUG: testing '$remote_servername' against domain include '$_'") unless ($Apache::FilteringProxy::logging < 2);
				if ($remote_servername =~ m/([A-Za-z0-9\.\-]+\.)*$_$/i) {
					$found = 1;
					last;
				}
			}

			# we didn't find the host in the host include list or domain
			# include list for this resource
			if (!$found) {
				$r->warn("REDIRECTING: a document on an unconfigured host ('$remote_servername') for resource ('$resource_id') has been requested") unless ($Apache::FilteringProxy::logging < 1);
				if (defined($Apache::FilteringProxy::proxy_host_include_list{$resource_id})) {
					$r->warn("REDIRECTING: included hosts for this resource are: ".join(',',@{$Apache::FilteringProxy::proxy_host_include_list{$resource_id}})) unless ($Apache::FilteringProxy::logging < 2);
				}
				if (defined($Apache::FilteringProxy::proxy_domain_include_list{$resource_id})) {
					$r->warn("REDIRECTING: included domains for this resource are: ".join(',',@{$Apache::FilteringProxy::proxy_domain_include_list{$resource_id}})) unless ($Apache::FilteringProxy::logging < 2);
				}
				if (defined($Apache::FilteringProxy::proxy_host_exclude_list{$resource_id})) {
					$r->warn("REDIRECTING: excluded hosts for this resource are: ".join(',',@{$Apache::FilteringProxy::proxy_host_exclude_list{$resource_id}})) unless ($Apache::FilteringProxy::logging < 2);
				}
				if (defined($Apache::FilteringProxy::proxy_domain_exclude_list{$resource_id})) {



( run in 2.921 seconds using v1.01-cache-2.11-cpan-2398b32b56e )