Apache-Voodoo

 view release on metacpan or  search on metacpan

lib/Apache/Voodoo/Debug/Handler.pm  view on Meta::CPAN


	$self->{json} = JSON::DWIW->new({bad_char_policy => 'convert', pretty => 1});;

	return $self;
}

sub handler {
	my $self = shift;
	my $r    = shift;

	$self->{mp}->set_request($r);

	# holds all vars associated with this page processing request
	my $uri = $self->{mp}->uri();
	$uri =~ s/^$self->{debug_root}//;
	$uri =~ s/^\///;

	if (defined($self->{static_files}->{$uri})) {
		# request for one of the static files.

		my $file = File::Spec->catfile($self->{template_dir},$uri);
		my $mtime = (stat($file))[9];

		# Handle "if not modified since" requests.
		$r->update_mtime($mtime);
		$r->set_last_modified;
		$r->meets_conditions;
		my $rc = $self->{mp}->if_modified_since($mtime);
		return $rc unless $rc == $self->{mp}->ok;

		# set the content type
		$self->{mp}->content_type($self->{static_files}->{$uri});

		# tell apache to send the underlying file
		$r->sendfile($file);

		return $self->{mp}->ok;
	}
	elsif (defined($self->{handlers}->{$uri})) {
		# request for an operation

		my $method = $self->{handlers}->{$uri};

		# parse the params
		my $params = $self->{mp}->parse_params(1);
		unless (ref($params)) {
			# something went boom
			return $self->display_host_error($params);
		}

		# connect to the debugging database
		my $dbh = DBI->connect_cached(@{$self->{constants}->debug_dbd()});
		unless ($dbh) {
			return $self->display_host_error("Can't connect to debugging database: ".DBI->errstr);
		}

		my $return;
		eval {
			$return = $self->$method($dbh,$params);
		};
		use Data::Dumper;
		warn Dumper $@;
		if ($@) {
			return $self->display_host_error("$@");
		}

		if (ref($return) eq "HASH") {
			$self->{mp}->content_type("application/json");
			$self->{mp}->print($self->{json}->to_json($return));
		}
		else {
			$self->{mp}->content_type("text/plain");
			$self->{mp}->print($return);
		}

		$self->{mp}->flush();

		return $self->{mp}->ok;
	}

	# not a request we handle
	return $self->{mp}->declined;
}

sub display_host_error {
	my $self  = shift;
	my $error = shift;

	$self->{'mp'}->content_type("text/html");
	$self->{'mp'}->print("<h2>The following error was encountered while processing this request:</h2>");
	$self->{'mp'}->print("<pre>$error</pre>");
	$self->{'mp'}->flush();

	return $self->{mp}->ok;
}

sub json_data {
	my $self = shift;
	my $type = shift;
	my $data = shift;

	if (ref($data)) {
		$data = $self->{json}->to_json($data);
	}
	elsif ($data !~ /^\s*[\[\{\"]/) {
		$data = '"'.$data.'"';
	}

	return '{"key":"'.$type.'","value":'.$data.'}';
}

sub json_error {
	my $self   = shift;
	my $errors = shift;

	my $return = {
		'success' => 'false',
		'errors'  => []
	};

	if (ref($errors) eq "HASH") {



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