Apache-ASP
view release on metacpan or search on metacpan
lib/Apache/ASP/Response.pm view on Meta::CPAN
shift @headers;
}
while(@headers) {
my $out = shift @headers;
next unless $out; # skip the blank that comes after the last newline
if($out =~ /^[^\s]+\: /) { # we are a header
unless(defined $self->{header_buffer}) {
$self->{header_buffer} .= '';
}
$self->{header_buffer} .= "$out\n";
} else {
unshift(@headers, $out);
last;
}
}
# take remaining non-headers & set the data to them joined back up
my $data_left = join("\n", @headers);
$dataref = \$data_left;
}
$dataref;
}
sub Null {};
sub TrapInclude {
my($self, $file) = (shift, shift);
my $out = "";
local $self->{out} = local $self->{BinaryRef} = \$out;
local $self->{Ended} = 0;
local *Apache::ASP::Response::Flush = *Null;
$self->Include($file, @_);
\$out;
}
sub Include {
my $self = shift;
my $file = shift;
my $asp = $self->{asp};
my($cache, $cache_key, $cache_expires, $cache_clear);
if(ref($file) && ref($file) eq 'HASH') {
my $data = $file;
$file = $data->{File}
|| $asp->Error("no File key passed to Include(), keys ".join(',', keys %$file));
$asp->{dbg} && $asp->Debug("file $file from HASH ref in Include()");
if($data->{Cache}) {
$cache = 1;
$cache_expires = $data->{'Expires'};
$cache_clear = $data->{'Clear'};
my $file_data = '';
if(ref($file)) {
$file_data = 'INCLUDE SCALAR REF '.$$file;
} else {
my $real_file = $asp->SearchDirs($file);
$file_data = 'INCLUDE FILE '.(stat($real_file))[9].' //\\ :: '.$real_file.' //\\ :: '.$file;
}
if($data->{Key}) {
$cache_key = $file_data .' //\\ :: '.DumperX($data->{Key});
$asp->{dbg} && $asp->Debug("include cache key length ".length($cache_key)." with extra Key data");
} else {
$asp->{dbg} && $asp->Debug("include cache key length ".length($file_data));
$cache_key = $file_data;
}
$cache_key .= ' //\\ COMPILE CHECKSUM :: '.$asp->{compile_checksum};
$cache_key .= ' //\\ ARGS :: '.DumperX(@_);
if(! $cache_clear) {
my $rv = $asp->Cache('Response', \$cache_key, undef, $data->{Expires}, $data->{LastModified});
if($rv) {
if(! eval { ($rv->{RV} && $rv->{OUT}) }) {
$asp->{dbg} && $self->Debug("cache item invalid: $@");
} else {
$asp->{dbg} && $asp->Debug("found include $file output in cache");
$self->WriteRef($rv->{OUT});
my $rv_data = $rv->{RV};
return wantarray ? @$rv_data : $rv_data->[0];
}
}
}
}
}
my $_CODE = $asp->CompileInclude($file);
unless(defined $_CODE) {
die("error including $file, not compiled: $@");
}
$asp->{last_compile_include_data} = $_CODE;
my $eval = $_CODE->{code};
# exit early for cached static file
if(ref $eval eq 'SCALAR') {
$asp->{dbg} && $asp->Debug("static file data cached, not compiled, length: ".length($$eval));
$self->WriteRef($eval);
return;
}
$asp->{dbg} && $asp->Debug("executing $eval");
my @rc;
if($cache) {
my $out = "";
{
local $self->{out} = local $self->{BinaryRef} = \$out;
local $self->{Ended} = 0;
local *Apache::ASP::Response::Flush = *Null;
@rc = eval { &$eval(@_) };
$asp->{dbg} && $asp->Debug("caching $file output expires: ".($cache_expires || ''));
$asp->Cache('Response', \$cache_key, { RV => [ @rc ], OUT => \$out }, $cache_expires);
}
$self->WriteRef(\$out);
} else {
@rc = eval { &$eval(@_) };
}
if($@) {
my $code = $_CODE;
( run in 1.233 second using v1.01-cache-2.11-cpan-39bf76dae61 )