Apache-FilteringProxy

 view release on metacpan or  search on metacpan

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

# PerlHandler function that:
#   1) checks for requests that are unproxied
#   2) reads in the configuration file, if it has been modified since last read
#   3) pushes a new content handler that will handle the entire request
#
sub handler {
	# get our request and log object
	my $r = shift;

	# If this is an already proxied request, decline the request.
	if ($r->proxyreq) {
		$r->warn("DEBUG: determining if this is already a proxied request...yes");
		return DECLINED;
	} else {
		$r->warn("DEBUG: determining if this is already a proxied request...no");
	}

	# get the path to the configuration file
	my $config_file;
	if (!($config_file = $r->dir_config("FilteringProxyConfig"))) {
		$r->warn("FilteringProxyConfig not set");
		return DECLINED;
	}

	# get the path to the configuration file
	if (!($mode = $r->dir_config("FilteringProxyMode"))) {
		$mode = "normal";
		$r->warn("FilteringProxyMode not set, defaulting to normal mode");
	} else {
		$mode = lc($mode);
		$r->warn("FilteringProxy mode set to '$mode'");
	}

	# FilteringProxyServername is only for admin mode to override the local servername
	if (($mode eq "admin") or ($mode eq "mirror")) {
		if (!($local_servername = $r->dir_config("FilteringProxyServername"))) {
			my $s = $r->server;
			$local_servername = $s->server_hostname();
			$r->warn("FilteringProxyServername not set, defaulting to '$local_servername'");
		} else {
			$r->warn("FilteringProxy local_servername set to '$local_servername'");
		}
	} else {
		my $s = $r->server;
		$local_servername = $s->server_hostname();
	}

	# get the path to the configuration file
	if (!($resource_domain = $r->dir_config("FilteringProxyResourceDomain"))) {
		$resource_domain = $local_servername;
		$r->warn("FilteringProxyResourceDomain not set, defaulting to servername '$local_servername'");
	} else {
		$resource_domain = lc($resource_domain);
		$r->warn("FilteringProxyResourceDomain set to '$resource_domain'");
	}

	### XML CONFIGURATION
	# get the modification time of the configuration file
	# dev,ino,mode,nlink,uid,gid,rdev,size,atime,mtime,ctime,blksize,blocks
	my @stat;
	unless ((-r $config_file) and (@stat = stat($config_file))) {
		$r->warn("could not stat '$config_file' ( FilteringProxyConfig )");
	} else {
		$r->warn("DEBUG: entering XML configuration");

		my $mtime = $stat[9];

		# only update our cached configuration if the config file has been modififed
		if (!defined($Apache::FilteringProxy::config_modification) or 
		   ($mtime > $Apache::FilteringProxy::config_modification)) 
		{
			$r->warn("DEBUG: updating XML configuration");

			# get our XML from the config file
			open(CONFIG, "<$config_file") || $r->warn("couldn't open configuration file '$config_file'");
			undef $/;
			my $xml_source = <CONFIG>;
			$/ = "\n";
			close(CONFIG);

			# create parser object and parse configuration from our string
			my $config = new XML::EasyOBJ(-type => 'string', -param => $xml_source);
			#my $config = my $doc = new XML::EasyOBJ(-type => 'file', -param => $config_file);

			# we have just modified our file, so let's set our modification
			# date to the new mod time so we don't keep causing ourself to
			# reread the config
			@stat = stat($config_file);
			$mtime = $stat[9];

			# log the configuration file modification stats 
			if (defined($Apache::FilteringProxy::config_modification)) {
				$r->warn("config: modification time: current=$mtime,last=$Apache::FilteringProxy::config_modification");
			} else {
				$r->warn("config: modification time: current=$mtime,last=none");
			}

			$Apache::FilteringProxy::config_modification = $mtime;

			my @resources = $config->resource;
			my @filter_types = $config->getElement("filter-type")->type();
			my @strip_headers = $config->getElement("strip-headers")->name();
			my @strip_cookies = $config->getElement("strip-cookies")->name();
			my @type_translations = $config->getElement("type-translations")->item();
			my @translations = $config->translations()->item();
			my $default_url = $config->getElement("default-url")->getAttr("value");
			my $proxy_url = $config->getElement("proxy-url")->getAttr("value");
			my $logging = $config->logging()->getAttr("value");

			# see if we have logging enabled
			#
			# logging levels
			# 	0 - critical
			#	1 - verbose
			#	2 - debug with headers
			#	3 - debug with headers & source
			if ($logging) {
				$Apache::FilteringProxy::logging = $logging;
				$r->warn("config: logging level set to '".$logging."'");
			} else {
				$Apache::FilteringProxy::logging = 0;
			}

			# get admin database configuration
			$Apache::FilteringProxy::db_hostname = $config->getElement("admin-database")->hostname->getString() || "localhost";
			$Apache::FilteringProxy::db_hostport = $config->getElement("admin-database")->hostport->getString() || "5432";
			$Apache::FilteringProxy::db_username = $config->getElement("admin-database")->username->getString() || "user";
			$Apache::FilteringProxy::db_password = $config->getElement("admin-database")->password->getString() || "pass";
			$Apache::FilteringProxy::db_database = $config->getElement("admin-database")->database->getString() || "default";
			$Apache::FilteringProxy::db_driver   = $config->getElement("admin-database")->getElement("dbi-dbd")->getString() || "Pg";
			$r->warn("config: admin db hostname: " . $Apache::FilteringProxy::db_hostname) unless ($Apache::FilteringProxy::logging < 1);
			$r->warn("config: admin db hostport: " . $Apache::FilteringProxy::db_hostport) unless ($Apache::FilteringProxy::logging < 1);
			$r->warn("config: admin db username: " . $Apache::FilteringProxy::db_username) unless ($Apache::FilteringProxy::logging < 1);
			$r->warn("config: admin db password: " . (($Apache::FilteringProxy::db_password) ? "*not empty*" : "*empty*")) unless ($Apache::FilteringProxy::logging < 1);
			$r->warn("config: admin db database: " . $Apache::FilteringProxy::db_database) unless ($Apache::FilteringProxy::logging < 1);
			$r->warn("config: admin db driver: " . $Apache::FilteringProxy::db_driver) unless ($Apache::FilteringProxy::logging < 1);

			# the proxy that will be used in all requests made by LWP to
			# retrieve content from a remote server
			if ($proxy_url) {
				$Apache::FilteringProxy::proxy_url = $proxy_url;
				$r->warn("config: proxy url: $proxy_url");
			} else {
				$Apache::FilteringProxy::proxy_url = "";
			}

			# the url that a user will be sent to when they try to access
			# an unconfigured resource - set in the configuration file



( run in 1.841 second using v1.01-cache-2.11-cpan-39bf76dae61 )