You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by "J. Peng" <pe...@gmail.com> on 2008/02/28 05:17:50 UTC
need to write a fitler based on request header
Hello members,
I need to write an input filter based on the request headers.
If request includes a "Accept-Encoding: gzip, deflate" header, I
should redirect the request to /pathA.
If request doesn't include that header, I should redirect the request to /pathB.
(pathA and pathB are web document dirs on web server.)
How to do it? thanks.
Re: need to write a fitler based on request header
Posted by Torsten Foertsch <to...@gmx.net>.
On Thu 28 Feb 2008, J. Peng wrote:
> Also I checked apache's official document for mod_rewrite:
> http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Well, Apache 2.2 can, see
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
Other things you should be aware of:
...
4. %{HTTP:header}, where header can be any HTTP MIME-header name, can always
be used to obtain the value of a header sent in the HTTP request.
Example: %{HTTP:Proxy-Connection} is the value of the HTTP header
``Proxy-Connection:''.
...
I wasn't aware that this is a feature only of 2.2+.
Torsten
Re: need to write a fitler based on request header
Posted by "J. Peng" <pe...@gmail.com>.
On Thu, Feb 28, 2008 at 8:30 PM, Torsten Foertsch
<to...@gmx.net> wrote:
> On Thu 28 Feb 2008, J. Peng wrote:
>
> > currently I write it with PerlAccessHandler, it also works. is it
> > right with this handler?
>
> Do you want to send a redirect to the browser (HTTP code 3xx)?
I use apache's inner redirect rather than the 3xx external redirection.
> If yes then it
> can be done in an access handler as well. If you simply want to send the
> document in /pathA or /pathB then I think you'd prefer something *before* the
> core map_to_storage handler, that means a PerlTransHandler or a
> PerlMapToStorageHandler since otherwise you'd need to fill out the finfo
> field by yourself, see
>
> http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_finfo_
>
>
thanks for the info.
> > no, mod_rewrite can't rewrite requests based on Accept-Encoding header.
>
> yes, something like this:
>
> RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
> RewriteCond %{HTTP:Accept-Encoding} deflate
> RewriteRule ^(.*) /pathA/$1 [PT,L]
>
> RewriteRule ^(.*) /pathB/$1 [PT,L]
>
> or as an external redirect:
>
> RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
> RewriteCond %{HTTP:Accept-Encoding} deflate
> RewriteRule ^(.*) /pathA/$1 [R,L]
>
> RewriteRule ^(.*) /pathB/$1 [R,L]
>
> Why do you think this wouldn't work?
>
I'll try it. thanks so much torsten.
Re: need to write a fitler based on request header
Posted by "J. Peng" <pe...@gmail.com>.
Hello Torsten,
I have tested your rewrite syntax like below:
<Location /unzip>
RewriteEngine On
RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
RewriteCond %{HTTP:Accept-Encoding} deflate
RewriteRule ^/unzip/(.*) /gziped/$1 [PT,L]
</Location>
Sorry it can't work.
Also I checked apache's official document for mod_rewrite:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
It says the rewriting conditions on http headers include only:
HTTP headers:
--------------------------
HTTP_USER_AGENT
HTTP_REFERER
HTTP_COOKIE
HTTP_FORWARDED
HTTP_HOST
HTTP_PROXY_CONNECTION
HTTP_ACCEPT
So as I've said, you can't rewrite the request based on
Accept-Encoding header.Is it?
Thanks.
On Thu, Feb 28, 2008 at 8:30 PM, Torsten Foertsch
<to...@gmx.net> wrote:
> On Thu 28 Feb 2008, J. Peng wrote:
>
> > no, mod_rewrite can't rewrite requests based on Accept-Encoding header.
>
> yes, something like this:
>
> RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
> RewriteCond %{HTTP:Accept-Encoding} deflate
> RewriteRule ^(.*) /pathA/$1 [PT,L]
>
> RewriteRule ^(.*) /pathB/$1 [PT,L]
>
> or as an external redirect:
>
> RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
> RewriteCond %{HTTP:Accept-Encoding} deflate
> RewriteRule ^(.*) /pathA/$1 [R,L]
>
> RewriteRule ^(.*) /pathB/$1 [R,L]
>
> Why do you think this wouldn't work?
>
Re: need to write a fitler based on request header
Posted by Torsten Foertsch <to...@gmx.net>.
On Thu 28 Feb 2008, J. Peng wrote:
> currently I write it with PerlAccessHandler, it also works. is it
> right with this handler?
Do you want to send a redirect to the browser (HTTP code 3xx)? If yes then it
can be done in an access handler as well. If you simply want to send the
document in /pathA or /pathB then I think you'd prefer something *before* the
core map_to_storage handler, that means a PerlTransHandler or a
PerlMapToStorageHandler since otherwise you'd need to fill out the finfo
field by yourself, see
http://perl.apache.org/docs/2.0/api/Apache2/RequestRec.html#C_finfo_
> no, mod_rewrite can't rewrite requests based on Accept-Encoding header.
yes, something like this:
RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
RewriteCond %{HTTP:Accept-Encoding} deflate
RewriteRule ^(.*) /pathA/$1 [PT,L]
RewriteRule ^(.*) /pathB/$1 [PT,L]
or as an external redirect:
RewriteCond %{HTTP:Accept-Encoding} gzip [OR]
RewriteCond %{HTTP:Accept-Encoding} deflate
RewriteRule ^(.*) /pathA/$1 [R,L]
RewriteRule ^(.*) /pathB/$1 [R,L]
Why do you think this wouldn't work?
> On Thu, Feb 28, 2008 at 7:26 PM, Torsten Foertsch
>
> <to...@gmx.net> wrote:
> > On Thu 28 Feb 2008, J. Peng wrote:
> > > I need to write an input filter based on the request headers.
> > > If request includes a "Accept-Encoding: gzip, deflate" header, I
> > > should redirect the request to /pathA.
> > > If request doesn't include that header, I should redirect the request
> > to > /pathB. (pathA and pathB are web document dirs on web server.)
> >
> > I think you don't need an input filter. What you need is a
> > PerlTransHandler or a PerlMapToStorageHandler that checks
> > $r->headers_in->{'Accept-Encoding'} and sets $r->filename and/or $r->uri
> > accordingly.
> >
> > This can even be done with mod_rewrite.
Torsten
--
A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?
Re: need to write a fitler based on request header
Posted by "J. Peng" <pe...@gmail.com>.
thanks torsten.
currently I write it with PerlAccessHandler, it also works. is it
right with this handler?
no, mod_rewrite can't rewrite requests based on Accept-Encoding header.
On Thu, Feb 28, 2008 at 7:26 PM, Torsten Foertsch
<to...@gmx.net> wrote:
> On Thu 28 Feb 2008, J. Peng wrote:
> > I need to write an input filter based on the request headers.
> > If request includes a "Accept-Encoding: gzip, deflate" header, I
> > should redirect the request to /pathA.
> > If request doesn't include that header, I should redirect the request to
> > /pathB. (pathA and pathB are web document dirs on web server.)
>
> I think you don't need an input filter. What you need is a PerlTransHandler or
> a PerlMapToStorageHandler that checks $r->headers_in->{'Accept-Encoding'} and
> sets $r->filename and/or $r->uri accordingly.
>
> This can even be done with mod_rewrite.
>
> Torsten
>
Re: need to write a fitler based on request header
Posted by Torsten Foertsch <to...@gmx.net>.
On Thu 28 Feb 2008, J. Peng wrote:
> I need to write an input filter based on the request headers.
> If request includes a "Accept-Encoding: gzip, deflate" header, I
> should redirect the request to /pathA.
> If request doesn't include that header, I should redirect the request to
> /pathB. (pathA and pathB are web document dirs on web server.)
I think you don't need an input filter. What you need is a PerlTransHandler or
a PerlMapToStorageHandler that checks $r->headers_in->{'Accept-Encoding'} and
sets $r->filename and/or $r->uri accordingly.
This can even be done with mod_rewrite.
Torsten