Mail Index


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [ApacheGallery] help with chached files needed!



On Fri, Jun 18, 2004 at 03:07:46PM +0400, Evgeny Stepanov wrote:
> 7. A::G is smart enough to take thumbnail from GalleryCacheDir and
> sendfile() (for Apache2) and this cycles for every thumbnail on the
> page.  my $subr = $r->lookup_file($file);
>    if (MP2) {
>      $r->sendfile($file);
>    }
> 8. then A::G returns decline to apache so it will handle non-existing
> file itself - here the confuse begins
> this is funny, cause file has already been sent by this time, but
> server gets an error.
That does seem to be the crux of the "File does not exist" problem in
apache2 and I feel silly for not noticing it. Good work.


> >> I think making ApacheGallery only change the file path
> >> in a PerlTransHandler instead of at the PerlResponseHandler stage
> >> fixes the problem, but don't have a fix as a tidy patch (though I have
> >> tested this theory).
> i think this isn't PerlTransHandler or PerlResponseHandler issue.
> PerlResponseHandler is ok anyway.
In the long run I think it would be better to split Apache Gallery
into a PerlTransHandler and a PerlResponseHandler. The
PerlTransHandler will translate requests for image files (original and
.cache) to their physical path to be handled by the default apache
response handler. The AG PerlResponseHandler only serves HTML.
Depending on if perforance is a problem it may be worth cutting out
the PerlTransHandler by putting the cache images in a location
reachable by Apache's traslation and creating the images in a 404
handler.

I've attached to this message a patch against Gallery -r263 which does
what I described above. When using this patch you need to replace the
normal PerlHandler (Apache 1.3) or PerlResponseHandler (Apache 2) line
with
PerlTransHandler       Apache::Gallery


> APJ> Afaics, we use sendfile to ship the file under Apache2, so OK would
> APJ> probably be the correct return code.
> could anyone test this OK with apache 1.3. server?
I tried the following
  if ($r->uri =~ m/\.cache\//i) {
  ...
    if (MP2) {
      $r->sendfile($file);
      return Apache::OK;
    }
    else {
      $r->path_info('');
      $r->filename($file);
      return Apache::Constants::DECLINED;
    }
  }
which seemed to work quite well for me.


Both the diff and the fix above seemed to work for me with the following
setups but it wouldn't surprise me if there are bugs. Before anyone
asks, don't use the fix and the diff at the same time.

Apache/2.0.47 (Debian GNU/Linux) mod_perl/1.99_12 Perl/v5.8.3
Apache/1.3.29 (Debian GNU/Linux) mod_perl/1.29


-- 
28 70 20 71 2C 65 29 61 9C B1 36 3D D4 69 CE 62 4A 22 8B 0E DC 3E
mailto:tdbrown@xxxxxxxx
http://thecap.org/
58a59,127
> 	my $r = shift or Apache->request();
> 
> 	# Let Apache serve icons without us modifying the request
> 	if ($r->uri =~ m/^\/icons/i) {
> 		return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED;
> 	}
> 
> 	# Lookup the file in the cache and scale the image if the cached
> 	# image does not exist. Sets the request filename so Apache can
> 	# directly serve the file.
> 	if ($r->uri =~ m/\.cache\//i) {
> 
> 		my $filename = $r->document_root().$r->uri();
> 		$filename =~ s/\.cache//;
> 
> 		$filename =~ m/\/(\d+)x(\d+)\-/;
> 		my $image_width = $1;
> 		my $image_height = $2;
> 
> 		$filename =~ s/\/(\d+)x(\d+)\-//;
> 
> 		my ($width, $height, $type) = imgsize($filename);
> 
> 		my $imageinfo = get_imageinfo($r, $filename, $type, $width, $height);
> 	
> 		# Could probably do with some error checking here
> 		my $cached = scale_picture($r, $filename, $image_width, $image_height, $imageinfo);
> 
> 		my $file = cache_dir($r, 0);
> 		$file =~ s/\.cache//;
> 
> 		$r->path_info('');
> 		$r->filename($file);
> 
> 		return MP2 ? Apache::OK : Apache::Constants::OK;
> 	}
> 
> 	# If requesting the original file and the GalleryAllowOriginal is 1
> 	# let Apache handle the request in the default manner. Note that
> 	# using the arg orig=1 when requesting a directory will make the original
> 	# directory handler, not Gallery, generate the response.
> 	if ($r->args() eq 'orig') {
> 		if ($r->dir_config('GalleryAllowOriginal') ? 1 : 0) {
> 			return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED;
> 		} else {
> 			return MP2 ? Apache::FORBIDDEN : Apache::Constants::FORBIDDEN;
> 		}
> 	}
> 
> 	# Else let Apache do its normal translation (the results of which will most
> 	# likely be ignored) and queue a response handler to generate HTML.
> 	if (MP2) {
> 		$r->handler('modperl');
> 		$r->set_handlers(PerlResponseHandler => \&response_handler);
> 		return Apache::DECLINED;
> 	} else {
> 		# Apache/1.3.29 with mod_perl/1.29 seems to get stuck in an inf loop when I 
> 		# push a response handler for subrequests so only queue the handler on the
> 		# initial request.
> 		if( $r->is_initial_req() ) {
> 			$r->handler('perl-script');
> 			$r->push_handlers(PerlHandler => \&response_handler);
> 			return Apache::Constants::DECLINED;
> 		}
> 	}
> 
> }
> 
> sub response_handler {
110,150d178
< 	# Let Apache serve icons without us modifying the request
< 	if ($r->uri =~ m/^\/icons/i) {
< 		return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED;
< 	}
< 	# Lookup the file in the cache and scale the image if the cached
< 	# image does not exist
< 	if ($r->uri =~ m/\.cache\//i) {
< 
< 		my $filename = $r->document_root().$r->uri();
< 		$filename =~ s/\.cache//;
< 
< 		$filename =~ m/\/(\d+)x(\d+)\-/;
< 		my $image_width = $1;
< 		my $image_height = $2;
< 
< 		$filename =~ s/\/(\d+)x(\d+)\-//;
< 
< 		my ($width, $height, $type) = imgsize($filename);
< 
< 		my $imageinfo = get_imageinfo($r, $filename, $type, $width, $height);
< 	
< 		my $cached = scale_picture($r, $filename, $image_width, $image_height, $imageinfo);
< 
< 		my $file = cache_dir($r, 0);
< 		$file =~ s/\.cache//;
< 
< 		my $subr = $r->lookup_file($file);
< 		$r->content_type($subr->content_type());
< 
< 		if (MP2) {
< 			$r->sendfile($file);
< 		}
< 		else {
< 			$r->path_info('');
< 			$r->filename($file);
< 		}
< 		
< 		return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED;
< 
< 	}
< 
420,429d447
< 		# original size
< 		if (defined($ENV{QUERY_STRING}) && $ENV{QUERY_STRING} eq 'orig') {
< 			if ($r->dir_config('GalleryAllowOriginal') ? 1 : 0) {
< 				$r->filename($filename);
< 				return MP2 ? Apache::DECLINED : Apache::Constants::DECLINED;
< 			} else {
< 				return MP2 ? Apache::FORBIDDEN : Apache::Constants::FORBIDDEN;
< 			}
< 		}
<