Tuesday, August 15, 2006

an example of PerlMapToStorageHandler

PerlMapToStorageHandler 是个新的东西。在 2.0.2 才从 PerlTransHandler 里剥离出来,做了一些性能方面的提升。
大致的用途就是改变 uri 所对应的文件。

比如我们的 DocumentRoot 是 E:/Fayland, 然后 http://localhost/test.txt 本该访问的是 E:/Fayland/test.txt 但是我们想根据某一条件或者什么乱七八糟的事让这个 url 实际上对应的文件是个 E:/Fayland/Perl/fayland.txt 就可以用 MapToStorage 里的 filename 这函数。最最简单的例子(没有任何判断条件):
package Apache2My::FileMapping;

use strict;
use warnings;
use Apache2::RequestRec ();
use Apache2::Const -compile => qw(OK DECLINED);

sub handler {
my $r = shift;

my $uri = $r->uri;

if ($uri =~ /test\.txt/) {
$r->filename('E:\Fayland\Perl\fayland.txt');
return Apache2::Const::DECLINED;
}

return Apache2::Const::DECLINED;
}

1;
Download it
这样的代码写后,即使 E:/Fayland/test.txt 是存在的,但是这 url 实际上出来的内容还是 Perl/fayland.txt 里的内容。

conf 里的:
<Perl>
use lib 'E:/Fayland/';
</Perl>
PerlMapToStorageHandler Apache2My::FileMapping

至于有什么用途,仁者见仁,智者见智了。总是有点用处的。

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home