rewrite url in modperl
am 01.02.2010 14:10:17 von moli
Hello,
I have this modperl handler:
use Apache2::RequestRec ();
use Apache2::Connection ();
use Apache2::RequestUtil ();
use Apache2::ServerUtil ();
use Apache2::Log ();
use Apache2::Request ();
use Apache2::Const -compile => qw(OK FORBIDDEN);
sub handler {
my $r = shift;
my $q = Apache2::Request->new($r);
my $s = Apache2::ServerUtil->server;
# get customer's ip
my $ip = $r->connection->remote_ip;
if ( is_valid_ip($ip) ){
return Apache2::Const::OK;
} else {
$s->log_error("$ip FORBIDDEN");
return Apache2::Const::FORBIDDEN;
}
}
which verify the client's IP and return OK if ip is valid, otherwise
return FORBIDDEN.
Now I have to rewrite the requested url to another one, for example,
when customer requests:
http://example.com/path/12345_YWJjZGVmZw.mp4
the handler will remove the string after "_" (includes the "_"
itself), so the url will become:
http://example.com/path/12345.mp4
(because only 12345.mp4 is a real file on the filesystem,
12345_YWJjZGVmZw.mp4 is just virtual.)
How to implement that?
Thanks for your kind helps.
Re: rewrite url in modperl
am 01.02.2010 14:38:55 von torsten.foertsch
On Monday 01 February 2010 14:10:17 moli@normalperson.e4ward.com wrote:
> I have this modperl handler:
>
> use Apache2::RequestRec ();
> use Apache2::Connection ();
> use Apache2::RequestUtil ();
> use Apache2::ServerUtil ();
> use Apache2::Log ();
> use Apache2::Request ();
>
> use Apache2::Const -compile => qw(OK FORBIDDEN);
>
> sub handler {
>
> my $r = shift;
> my $q = Apache2::Request->new($r);
> my $s = Apache2::ServerUtil->server;
>
> # get customer's ip
> my $ip = $r->connection->remote_ip;
>
> if ( is_valid_ip($ip) ){
> return Apache2::Const::OK;
>
> } else {
> $s->log_error("$ip FORBIDDEN");
> return Apache2::Const::FORBIDDEN;
> }
> }
>
>
> which verify the client's IP and return OK if ip is valid, otherwise
> return FORBIDDEN.
>
> Now I have to rewrite the requested url to another one, for example,
>
> when customer requests:
> http://example.com/path/12345_YWJjZGVmZw.mp4
>
> the handler will remove the string after "_" (includes the "_"
> itself), so the url will become:
>
> http://example.com/path/12345.mp4
>
> (because only 12345.mp4 is a real file on the filesystem,
> 12345_YWJjZGVmZw.mp4 is just virtual.)
>
> How to implement that?
>
I think you know you can return Apache2::Const::REDIRECT or
Apache2::Const::HTTP_SEE_OTHER along with a location header and have the
browser do the redirect. But I think that is not what you want, right?
If you don't want the browser to see the actual location of the mp4 file then
you need $r->internal_redirect:
use Apache2::SubRequest ();
....
if ( is_valid_ip($ip) ){
$r->internal_redirect('/path/12345.mp4');
return Apache2::Const::OK;
Torsten
Re: rewrite url in modperl
am 01.02.2010 15:03:53 von moli
2010/2/1 Torsten Förtsch :
> If you don't want the browser to see the actual location of the mp4 file =
then
> you need $r->internal_redirect:
>
> use Apache2::SubRequest ();
> ...
> Â Â if ( is_valid_ip($ip) ){
> Â Â Â Â $r->internal_redirect('/path/12345.mp4');
> Â Â Â Â return Apache2::Const::OK;
>
Thank you Torsten, that's really what I expected.
Re: rewrite url in modperl
am 01.02.2010 15:13:53 von torsten.foertsch
On Monday 01 February 2010 15:03:53 moli@normalperson.e4ward.com wrote:
> 2010/2/1 Torsten Förtsch :
> > If you don't want the browser to see the actual location of the mp4 file
> > then you need $r->internal_redirect:
> >
> > use Apache2::SubRequest ();
> > ...
> > if ( is_valid_ip($ip) ){
> > $r->internal_redirect('/path/12345.mp4');
> > return Apache2::Const::OK;
>=20
> Thank you Torsten, that's really what I expected.
>=20
Keep in mind that /path/12345.mp4 is still a valid URI and hence can be=20
requested by the browser. But you can check if the request is the result of=
an=20
internal redirect by checking $r->prev and deny it unless set.
Torsten
Re: rewrite url in modperl
am 01.02.2010 15:19:34 von moli
2010/2/1 Torsten Förtsch :
> On Monday 01 February 2010 15:03:53 moli@normalperson.e4ward.com wrote:
>> 2010/2/1 Torsten Förtsch :
>> > If you don't want the browser to see the actual location of the mp4 fi=
le
>> > then you need $r->internal_redirect:
>> >
>> > use Apache2::SubRequest ();
>> > ...
>> > Â Â if ( is_valid_ip($ip) ){
>> > Â Â Â Â $r->internal_redirect('/path/12345.mp4');
>> > Â Â Â Â return Apache2::Const::OK;
>>
>> Thank you Torsten, that's really what I expected.
>>
> Keep in mind that /path/12345.mp4 is still a valid URI and hence can be
> requested by the browser. But you can check if the request is the result =
of an
> internal redirect by checking $r->prev and deny it unless set.
>
That's a cool idea.
But how to set it? I never used $r->prev in my before experience.
Thanks again.
Re: rewrite url in modperl
am 01.02.2010 16:07:47 von torsten.foertsch
On Monday 01 February 2010 15:19:34 moli@normalperson.e4ward.com wrote:
> > Keep in mind that /path/12345.mp4 is still a valid URI and hence can be
> > requested by the browser. But you can check if the request is the result
> > of an internal redirect by checking $r->prev and deny it unless set.
>
> That's a cool idea.
> But how to set it? I never used $r->prev in my before experience.
>
you don't have to. apache does it for you.
When a request comes in $r->main and $r->prev are false. If it issues a
subrequest $subrequest->main keeps $r so that the subrequest can modify the
main one. Similar, if the main request is replaced by an internal_redirect()
$redirectrequest->prev points to the main request in case you need something
from there.
Torsten