<%perl>
eval {
	# Generate the original output
	my $original_output = File::Temp::tempfile;
	Contentment->capture_streams(\*STDIN, $original_output, sub {
		$m->call_next
	});

	# Transform that output according to the postprocessors
	seek $original_output, 0, 0;
	my $final_output = File::Temp::tempfile;
	Contentment->capture_streams($original_output, $final_output, sub {
		Contentment->call_hooks('request/postprocess')
	});

	seek $final_output, 0, 0;
	while (<$final_output>) {
		$m->print($_);
	}
};

$ERROR = $@;
</%perl>
<%init>
use Contentment;
use Contentment::Context;
use Contentment::Session;
use File::Temp;

Contentment->call_hooks('request/initialize');

my $ERROR;

$Contentment::context = $context = Contentment::Context->new({
	url            => $url,
	m              => $m, 
	r              => $r,
	last_processed => \@__processed_uuid__,
});

my $q = $m->cgi_object;
$url = $q->url;
$full_base = $q->url(-base => 1) . $base;

Contentment->call_hooks('request/preprocess');
</%init>
<%perl>
Contentment->call_hooks('request/finish');

if ($ERROR) {
	$log->debug("An exception was raised: $ERROR");
	if ($m->aborted($ERROR)) {
		$log->info("Rethrowing an abort.");
		die $ERROR; # Aborts should be rethrown so Mason gets them
	} else {
		$log->error("Treating error as 404: $ERROR");
		$m->clear_buffer;
</%perl>
File not found.
<%perl>
		$log->error("Returning '404 File not found'.");
		$m->abort(404);
	}
} else {
	$log->debug("No exception was raised.");
}
</%perl>
<%filter>
s{^\s+}{}; s{\s+$}{};
</%filter>
<%flags>
inherit => undef
</%flags>
<%args>
@__processed_uuid__ => ()
</%args>
