Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

mod_perl, getting filename or mime type

by pingo (Hermit)
on Nov 15, 2006 at 09:18 UTC ( #584127=perlquestion: print w/replies, xml ) Need Help??

pingo has asked for the wisdom of the Perl Monks concerning the following question:

I have a pair of mod_perl (Apache 2.2.3) modules that attach to the Authen/Authz request phases, and they work just fine. Problem is, I would like to be able to decline handling of certain content (images, for instance). Getting the mime type seems to be impossible, since that is done in the Type phase, if I understand things correctly. Getting the filename works, except when the url refers to a directory (ends in a slash), in which case $r->filename() just returns the path to the directory on disk. (Which is kind of strange, by the way, I would have expected it to return the filename too, based on DirectoryIndex.)

I guess I could just assume that anything that ends in a slash should be protected, but I'm not entirely happy about such a solution. Any help or ideas would be appreciated.

Replies are listed 'Best First'.
Re: mod_perl, getting filename or mime type
by Joost (Canon) on Nov 15, 2006 at 10:07 UTC
    Strictly speaking you won't know the type until it is send; before that any code might still change the response. In any case you don't want to run a bunch of code just to figure out if whether you want to allow the user access.

    So, in other words, if you can't figure out the content-type from the url, I would suggest you use a more structured approach and put non-authorization-required content under a seperate path: "http://example.com/public/" and "http://example.com/auth/" for instance, and then you can easily match the uri in the auth phase, or even configure apache to just run the auth handlers for the right content.

Re: mod_perl, getting filename or mime type
by derby (Abbot) on Nov 15, 2006 at 13:21 UTC

    I think the reasonable approach is to look at just the uri:

    sub handler { my $r = shift; my $log = $r->log; # The real content type is not available # until the ContentHandler my ($content_type) = $r->uri() =~ /\.(\w+)$/; my %ignore = map {$_ => 1} qw(gif jpg jpeg png css); if ($ignore{lc($content_type)}) { $log->debug( "Allowing access to ($content_type)." ); return DECLINED; } ...

    -derby
      Thanks. I think I'll go for a combined derby/Joost approach (use a different vhost for the content that I have control over myself, look at uri's for the content I don't have control over).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://584127]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2020-12-05 11:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you use taint mode?





    Results (63 votes). Check out past polls.

    Notices?