You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Mark Stosberg <ma...@summersault.com> on 2007/02/27 17:20:05 UTC

Using or working around File::MMagic under mod_perl

Hello,

Recently I ran into a bug with File::MMagic where it returns
inconsistent results under mod_perl.

I'm interested in getting this bug fixed, or using an alternative module
that works.

What's interesting is that the same version of File::MMagic works
correctly on a different machine. I suspect that some other module
loaded has changed a global variable that File::MMagic depends on.

Someone has a bug report and patch to that effect here:
http://rt.cpan.org/Public/Bug/Display.html?id=11121

I recall having tried the patch there and didn't work for me. Maybe I
missed something. ( I didn't take good notes during that experiment... )

A README for it also states "Built-in magic entry does not work
correctly under mod_perl" Using an explicit magic file is recommended
instead. Can anyone spot the specific modperl  bug? I couldn't.

File::MMagic is used by Data::FormValidator's "file_format" constraint
for file uploads, so some other people may be using indirectly under
mod_perl as well.

Also, there are now some competing modules published, in part just to
work around this bug. I'm interested in anyone is using these
successfully under modperl and recommends them. One that seemed a bit
promising is File::Type:

http://search.cpan.org/dist/File-Type/lib/File/Type.pm

Adam Kennedy objected that this design used excessive system resources:
http://cpanratings.perl.org/dist/File-Type

Personally, I like the idea of fixing the mod_perl bug in File::MMagic
if we can.

Could it simply be a matter of fixing the use of the DATA handle, as
Stats suggest here?

http://mail-archives.apache.org/mod_mbox/perl-modperl/200403.mbox/%3c405F2D19.5090200@stason.org%3e


     Mark


Re: Using or working around File::MMagic under mod_perl

Posted by Robert Landrum <rl...@aol.net>.
Mark Stosberg wrote:
> Hello,
> 
> Recently I ran into a bug with File::MMagic where it returns
> inconsistent results under mod_perl.
> 
> Could it simply be a matter of fixing the use of the DATA handle, as
> Stats suggest here?
> 
> http://mail-archives.apache.org/mod_mbox/perl-modperl/200403.mbox/%3c405F2D19.5090200@stason.org%3e
> 

Yep.  It's definitly the problem.  The work around is to pass a magic
definition file into MMagic when the object is created. Easier said than 
done.

Maybe the best solution is move the DATA section to a variable.  It's
not as elegant as reading the data from a DATA section, but it's
mod_perl compliant.

Hmm...  You might try adding this to your startup.pl (or whatever is 
per-loading perl modules).

# a bit of a hack
use vars qw($magicObject);
BEGIN {
   if($ENV{MOD_PERL}) {
     # load from <DATA>
     $magicObject = File::MMagic->new();
     # save it globally
     {
       no strict;
       # save original method
       *{"File::MMagic::original_new"} = *{"File::MMagic::new"}{CODE};
       # replace original method
       *{"File::MMagic::new"} = sub {
         return $magicObject;
       };
     } # end no strict
   }
}


It's untested. And it probably leaks memory, too.  :(

Rob

Re: Using or working around File::MMagic under mod_perl

Posted by Perrin Harkins <ph...@gmail.com>.
Hi Mark,

> What's interesting is that the same version of File::MMagic works
> correctly on a different machine. I suspect that some other module
> loaded has changed a global variable that File::MMagic depends on.
>
> Someone has a bug report and patch to that effect here:
> http://rt.cpan.org/Public/Bug/Display.html?id=11121

Hmm, if someone is changing $/ without localizing it in a persistent
interpreter like mod_perl or FastCGI, it's pretty much guaranteed to
break something.  I don't think File::MMagic should be expected to
defend against that kind of insanity.

> File::MMagic is used by Data::FormValidator's "file_format" constraint
> for file uploads, so some other people may be using indirectly under
> mod_perl as well.

Under mod_perl, it is possible to get this from apache without using
File::MMagic, by using a subrequest.  There's some more discussion
here:
http://modperl.com/book/chapters/ch8.html#Reimplementing_I_mod_mime_in_Pe

> Could it simply be a matter of fixing the use of the DATA handle, as
> Stats suggest here?

Yes, very likely.

- Perrin