Re: Obtaining the Apache Content-type for a file

Joe Schaefer <[email protected]>
Newsgroups gmane.comp.apache.mod-perl
Message-ID <[email protected]>
From the ASF CMS codebase:


    my $subr         = $r->lookup_file($file);
    my $content_type = $subr->content_type || "";


an undefined content-type will eventually defer to 
the default content-type if you've set that in your httpd config.


----- Original Message -----
> From: André Warnier <[email protected]>
> To: mod_perl list <[email protected]>
> Cc: 
> Sent: Friday, January 13, 2012 2:17 PM
> Subject: Re: Obtaining the Apache Content-type for a file
> 
> André Warnier wrote:
>>  David Booth wrote:
>>>  On Fri, 2012-01-13 at 12:09 -0500, David Booth wrote:
>>>>  On Fri, 2012-01-13 at 16:06 +0100, André Warnier wrote:
>>>>>  I have a PerlResponseHandler which processes some kind of 
> "logical document-id" provided in a request, locates the corresponding 
> "real document" on the filesystem, and returns it to the client via 
> sendfile().
>>>>>  At the moment, this handler uses its own custom logic to 
> determine the MIME type of the document and return it to the client as a 
> Content-type HTTP header.
>>>>> 
>>>>>  My question is : instead of this custom logic, does there exist 
> a way, via mod_perl, to obtain this target file's MIME-type from Apache, 
> using Apache's own logic (mod_mime, AddType etc..) for that ?
>>>>  This isn't exactly what you asked for, but if you don't 
> need to server
>>>>  anything else along with it, then perhaps you could use
>>>>  internal_redirect
>>>> 
> http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html#C_internal_redirect_ 
> 
>>>>  and let Apache set the Content-Type for you. If you do find the 
> direct answer to your question, please post it, as
>>>>  I'm interested in this question also.
>>> 
>>>  P.S. I just noticed lookup_file:
>>> 
> http://perl.apache.org/docs/2.0/api/Apache2/SubRequest.html#C_lookup_file_ 
>>>  I haven't tried it, but it sounds like it *might* do what you want.
>>> 
>>  Thanks.  I will have a look at both.  I don't think I an use the 
> internal_redirect in my case,
>>  lookup_file sounds interesting.  I didn't think of looking there.
>> 
> 
> I had a look, and it looks a bit like a circular argument..
> 
> I can do $r->lookup_file($my_path), but
> 
> "lookup_file" is a method of Apache2::SubRequest (and returns an 
> Apache2::SubRequest object).  Apache2::SubRequest is a subclass of 
> Apache2::RequestRec.
> Apache2::RequestRec has a "finfo" method, which returns an APR::Finfo 
> object.
> The APR::Finfo object has wealth of information (see below), unfortunately 
> apparently not what I'm after (the MIME type of $mypath, as resolved by 
> mod_mime e.g.).
> 
> $device = $finfo->device;     # (stat $file)[0]
>   $inode  = $finfo->inode;      # (stat $file)[1]
>   # stat returns an octal number while protection is hex
>   $prot   = $finfo->protection; # (stat $file)[2]
>   $nlink  = $finfo->nlink;      # (stat $file)[3]
>   $gid    = $finfo->group;      # (stat $file)[4]
>   $uid    = $finfo->user;       # (stat $file)[5]
>   $size   = $finfo->size;       # (stat $file)[7]
>   $atime  = $finfo->atime;      # (stat $file)[8]
>   $mtime  = $finfo->mtime;      # (stat $file)[9]
>   $ctime  = $finfo->ctime;      # (stat $file)[10]
>   $csize = $finfo->csize; # consumed size: not portable!
>   $filetype = $finfo->filetype; # file/dir/socket/etc
>   $fname = $finfo->fname;
>   $name  = $finfo->name;  # in filesystem case:
> 
> Any other idea anyone ? We're now two to be interested in the answer.
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.