You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by André Warnier <aw...@ice-sa.com> on 2012/01/13 16:06:49 UTC

Obtaining the Apache Content-type for a file

Hello.

re: mod_perl 2.x, httpd 2.x

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 ?

I have searched the online documentation, like 
(http://perl.apache.org/docs/2.0/api/APR/Finfo.html#C_filetype_),
but I am not quite sure that what I found so far is what I am looking for.

Thanks
AW

Re: Obtaining the Apache Content-type for a file

Posted by André Warnier <aw...@ice-sa.com>.
André Warnier wrote:
> 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.
> 
Aaaah, maybe I'm being too complicated here.
If I do

my $subr = $r->lookup_file($my_path);
my $mime = $subr->content_type();

would be what I need.  I'll try that.


Re: Obtaining the Apache Content-type for a file

Posted by André Warnier <aw...@ice-sa.com>.
Thanks. I just got the same illumination while you were writing..
:-)

Joe Schaefer wrote:
>>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 <aw...@ice-sa.com>
>> To: mod_perl list <mo...@perl.apache.org>
>> 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.
>>
> 


Re: Obtaining the Apache Content-type for a file

Posted by Joe Schaefer <jo...@yahoo.com>.
>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 <aw...@ice-sa.com>
> To: mod_perl list <mo...@perl.apache.org>
> 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.
> 

Re: Obtaining the Apache Content-type for a file

Posted by André Warnier <aw...@ice-sa.com>.
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.

Re: Obtaining the Apache Content-type for a file

Posted by André Warnier <aw...@ice-sa.com>.
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.

  but


Re: Obtaining the Apache Content-type for a file

Posted by David Booth <da...@dbooth.org>.
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.


-- 
David Booth, Ph.D.
http://dbooth.org/

Opinions expressed herein are those of the author and do not necessarily
reflect those of his employer.


Re: Obtaining the Apache Content-type for a file

Posted by David Booth <da...@dbooth.org>.
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.

Thanks!

-- 
David Booth, Ph.D.
http://dbooth.org/

Opinions expressed herein are those of the author and do not necessarily
reflect those of his employer.